-
Notifications
You must be signed in to change notification settings - Fork 613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(admin-ui): Toast component #4249
Open
leopuleo
wants to merge
15
commits into
feat/new-admin-ui
Choose a base branch
from
leo/feat/ui-toast
base: feat/new-admin-ui
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4758bc9
chore: merge
leopuleo 08ac3e6
chore: merge
leopuleo 9d1a656
wip: various
leopuleo 48286f3
wip: toast
leopuleo 9aaf476
chore: merge conflicts
leopuleo 4c83868
wip: toast
leopuleo 57e3c34
wip: backward compatibility layer
leopuleo 4187133
chore: improve stories
leopuleo 6d716d3
chore: improve classNames
leopuleo b9e9c07
Merge branch 'feat/new-admin-ui' into leo/feat/ui-toast
leopuleo c61aeb3
chore: remove @rmwc library
leopuleo 7228839
chore: merge
leopuleo 34446dd
chore: merge
leopuleo 72f9c47
fix: various styling
leopuleo fd70e29
Merge branch 'feat/new-admin-ui' into leo/feat/ui-toast
leopuleo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
import React from "react"; | ||
import { ToastProvider, ToastViewport } from "~/Toast"; | ||
import { TooltipProvider } from "~/Tooltip"; | ||
|
||
export interface ProvidersProps { | ||
children?: React.ReactNode; | ||
} | ||
|
||
export const Providers = ({ children }: ProvidersProps) => { | ||
return <TooltipProvider>{children}</TooltipProvider>; | ||
return ( | ||
<ToastProvider> | ||
<TooltipProvider>{children}</TooltipProvider> | ||
<ToastViewport /> | ||
</ToastProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import React from "react"; | ||
import { ReactComponent as SettingsIcon } from "@material-design-icons/svg/outlined/settings.svg"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { | ||
Toast, | ||
ToastProvider, | ||
ToastViewport, | ||
ToastAction, | ||
ToastTitle, | ||
ToastDescription | ||
} from "./Toast"; | ||
import { Button } from "~/Button"; | ||
|
||
const meta: Meta<typeof Toast> = { | ||
title: "Components/Toast", | ||
component: Toast, | ||
tags: ["autodocs"], | ||
parameters: { | ||
layout: "fullscreen" | ||
}, | ||
argTypes: { | ||
// Note: after upgrading to Storybook 8.X, use `fn`from `@storybook/test` to spy on the onOpenChange argument. | ||
onOpenChange: { action: "onOpenChange" } | ||
}, | ||
decorators: [ | ||
(Story, context) => { | ||
const { args } = context; | ||
const [open, setOpen] = React.useState<boolean>(false); | ||
const timerRef = React.useRef(0); | ||
|
||
React.useEffect(() => { | ||
return () => clearTimeout(timerRef.current); | ||
}, []); | ||
|
||
return ( | ||
<ToastProvider> | ||
<div className="w-full h-64 flex justify-center items-center"> | ||
<Button | ||
text={"Display Toast"} | ||
onClick={() => { | ||
setOpen(false); | ||
window.clearTimeout(timerRef.current); | ||
timerRef.current = window.setTimeout(() => { | ||
setOpen(true); | ||
}, 100); | ||
}} | ||
/> | ||
<Story | ||
args={{ | ||
...args, | ||
open: open, | ||
onOpenChange: open => { | ||
setOpen(open); | ||
if (typeof args.onOpenChange === "function") { | ||
args.onOpenChange(open); | ||
} | ||
} | ||
}} | ||
/> | ||
<ToastViewport /> | ||
</div> | ||
</ToastProvider> | ||
); | ||
} | ||
] | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof Toast>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
title: <ToastTitle text={"New entry created"} /> | ||
} | ||
}; | ||
|
||
export const SubtleVariant: Story = { | ||
args: { | ||
...Default.args, | ||
variant: "subtle" | ||
} | ||
}; | ||
|
||
export const WithDescription: Story = { | ||
args: { | ||
...Default.args, | ||
description: <ToastDescription text={'Entry "Article One" has been successfully created'} /> | ||
} | ||
}; | ||
|
||
export const WithActions: Story = { | ||
args: { | ||
...Default.args, | ||
actions: [ | ||
<ToastAction | ||
key={"open"} | ||
text={"Open"} | ||
altText={"Open Entry"} | ||
onClick={e => console.log("open", e)} | ||
/> | ||
] | ||
} | ||
}; | ||
|
||
export const WithCustomIcon: Story = { | ||
args: { | ||
...Default.args, | ||
icon: <SettingsIcon /> | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
import * as React from "react"; | ||
import { ReactComponent as CloseIcon } from "@material-design-icons/svg/outlined/close.svg"; | ||
import { ReactComponent as NotificationsIcon } from "@material-design-icons/svg/outlined/notifications_active.svg"; | ||
import * as ToastPrimitives from "@radix-ui/react-toast"; | ||
import { makeDecoratable } from "@webiny/react-composition"; | ||
import { cva, type VariantProps } from "class-variance-authority"; | ||
|
||
import { cn } from "~/utils"; | ||
import { Heading } from "~/Heading"; | ||
import { Text } from "~/Text"; | ||
import { Button } from "~/Button"; | ||
|
||
const ToastProvider = ToastPrimitives.Provider; | ||
|
||
/** | ||
* Toast Viewport | ||
*/ | ||
const ToastViewportBase = React.forwardRef< | ||
React.ElementRef<typeof ToastPrimitives.Viewport>, | ||
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Viewport> | ||
>(({ className, ...props }, ref) => ( | ||
<ToastPrimitives.Viewport | ||
ref={ref} | ||
className={cn( | ||
"fixed top-0 right-0 bottom-auto z-[100] flex max-h-screen flex-col p-4", | ||
className | ||
)} | ||
{...props} | ||
/> | ||
)); | ||
ToastViewportBase.displayName = ToastPrimitives.Viewport.displayName; | ||
|
||
const ToastViewport = makeDecoratable("ToastViewport", ToastViewportBase); | ||
|
||
/** | ||
* Toast Root | ||
*/ | ||
const toastVariants = cva( | ||
"group pointer-events-auto relative flex w-full items-start justify-start p-md gap-sm-extra self-stretch overflow-hidden rounded-md border-sm border-neutral-dimmed shadow-lg transition-all data-[swipe=cancel]:translate-x-0 data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:transition-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[swipe=end]:animate-out data-[state=closed]:fade-out-80 data-[state=closed]:slide-out-to-right-full data-[state=open]:slide-in-from-top-full", | ||
{ | ||
variants: { | ||
variant: { | ||
default: "bg-neutral-dark text-neutral-light fill-neutral-base", | ||
subtle: "bg-white fill-neutral-xstrong" | ||
} | ||
}, | ||
defaultVariants: { | ||
variant: "default" | ||
} | ||
} | ||
); | ||
|
||
const ToastRootBase = React.forwardRef< | ||
React.ElementRef<typeof ToastPrimitives.Root>, | ||
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> & VariantProps<typeof toastVariants> | ||
>(({ className, variant, ...props }, ref) => { | ||
return ( | ||
<ToastPrimitives.Root | ||
ref={ref} | ||
className={cn(toastVariants({ variant }), className)} | ||
{...props} | ||
/> | ||
); | ||
}); | ||
ToastRootBase.displayName = ToastPrimitives.Root.displayName; | ||
|
||
const ToastRoot = makeDecoratable("ToastRoot", ToastRootBase); | ||
|
||
/** | ||
* Toast Action | ||
*/ | ||
type ToastActionProps = ToastPrimitives.ToastActionProps & { | ||
text: React.ReactNode; | ||
}; | ||
|
||
const ToastActionBase = React.forwardRef< | ||
React.ElementRef<typeof ToastPrimitives.Action>, | ||
ToastActionProps | ||
>(({ onClick, text, ...props }, ref) => ( | ||
<ToastPrimitives.ToastAction asChild {...props}> | ||
<Button ref={ref} onClick={onClick} text={text} variant={"primary"} size={"sm"} /> | ||
</ToastPrimitives.ToastAction> | ||
)); | ||
ToastActionBase.displayName = ToastPrimitives.Action.displayName; | ||
|
||
const ToastAction = makeDecoratable("ToastAction", ToastActionBase); | ||
|
||
/** | ||
* Toast Close Icon | ||
*/ | ||
const ToastCloseBase = React.forwardRef< | ||
React.ElementRef<typeof ToastPrimitives.Close>, | ||
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close> | ||
>(({ className, ...props }, ref) => ( | ||
<ToastPrimitives.Close | ||
ref={ref} | ||
className={cn("p-xs focus:outline-none", className)} | ||
aria-label="Close" | ||
{...props} | ||
> | ||
<span className="h-4 w-4" aria-hidden> | ||
<CloseIcon /> | ||
</span> | ||
</ToastPrimitives.Close> | ||
)); | ||
ToastCloseBase.displayName = ToastPrimitives.Close.displayName; | ||
|
||
const ToastClose = makeDecoratable("ToastClose", ToastCloseBase); | ||
|
||
/** | ||
* Toast Icon | ||
*/ | ||
type ToastIconProps = { | ||
icon?: React.ReactNode; | ||
}; | ||
|
||
const ToastIconBase = ({ icon = <NotificationsIcon /> }: ToastIconProps) => ( | ||
<div className={"fill-accent-default"}> | ||
<span className="h-4 w-4" aria-hidden> | ||
{icon} | ||
</span> | ||
</div> | ||
); | ||
|
||
const ToastIcon = makeDecoratable("ToastIcon", ToastIconBase); | ||
|
||
/** | ||
* Toast Title | ||
*/ | ||
type ToastTitleProps = Omit<ToastPrimitives.ToastTitleProps, "children"> & { | ||
text: React.ReactNode; | ||
}; | ||
|
||
const ToastTitleBase = React.forwardRef< | ||
React.ElementRef<typeof ToastPrimitives.Title>, | ||
ToastTitleProps | ||
>(({ text, ...props }, ref) => ( | ||
<ToastPrimitives.Title ref={ref} asChild {...props}> | ||
<Heading level={6} text={text} /> | ||
</ToastPrimitives.Title> | ||
)); | ||
ToastTitleBase.displayName = ToastPrimitives.Title.displayName; | ||
|
||
const ToastTitle = makeDecoratable("ToastTitle", ToastTitleBase); | ||
|
||
/** | ||
* Toast Description | ||
*/ | ||
type ToastDescriptionProps = Omit<ToastPrimitives.ToastDescriptionProps, "children"> & { | ||
text: React.ReactNode; | ||
}; | ||
|
||
const ToastDescriptionBase = React.forwardRef< | ||
React.ElementRef<typeof ToastPrimitives.Description>, | ||
ToastDescriptionProps | ||
>(({ text, ...props }, ref) => ( | ||
<ToastPrimitives.Description ref={ref} asChild {...props}> | ||
<Text text={text} as={"div"} size={"sm"} className={"text-neutral-dimmed"} /> | ||
</ToastPrimitives.Description> | ||
)); | ||
ToastDescriptionBase.displayName = ToastPrimitives.Description.displayName; | ||
|
||
const ToastDescription = makeDecoratable("ToastDescription", ToastDescriptionBase); | ||
|
||
/** | ||
* Toast | ||
*/ | ||
type ToastRootProps = React.ComponentPropsWithoutRef<typeof ToastRoot>; | ||
|
||
interface ToastProps extends Omit<ToastRootProps, "title" | "content" | "children"> { | ||
title: React.ReactElement<ToastTitleProps>; | ||
description?: React.ReactElement<ToastDescriptionProps>; | ||
icon?: React.ReactNode; | ||
actions?: React.ReactElement<ToastActionProps> | React.ReactElement<ToastActionProps>[]; | ||
} | ||
|
||
const ToastBase = ({ title, description, icon, actions, ...props }: ToastProps) => { | ||
return ( | ||
<ToastRoot {...props}> | ||
<ToastIcon icon={icon} /> | ||
<div className="w-64"> | ||
{title} | ||
{description && description} | ||
{actions && actions} | ||
</div> | ||
<ToastClose /> | ||
</ToastRoot> | ||
); | ||
}; | ||
|
||
const Toast = makeDecoratable("Toast", ToastBase); | ||
|
||
export { | ||
type ToastRootProps, | ||
type ToastActionProps, | ||
type ToastProps, | ||
Toast, | ||
ToastAction, | ||
ToastDescription, | ||
ToastProvider, | ||
ToastTitle, | ||
ToastViewport | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./Toast"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noticed this here, and thinking we should do the same approach I have here.
So, in this case, we'd end up prolly with
Toast.Description
. Wdyt?BTW not sure if applicable here, but I did this
withStaticProps
helper within the Avatar PR, just to make it a bit more humane when sticking extra components as static properties on an existing React component / class.