Skip to content
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
wants to merge 15 commits into
base: feat/new-admin-ui
Choose a base branch
from
1 change: 1 addition & 0 deletions packages/admin-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-switch": "^1.1.0",
"@radix-ui/react-tabs": "^1.1.0",
"@radix-ui/react-toast": "^1.2.1",
"@radix-ui/react-tooltip": "^1.1.2",
"@webiny/react-composition": "0.0.0",
"@webiny/utils": "0.0.0",
Expand Down
8 changes: 7 additions & 1 deletion packages/admin-ui/src/Providers/Providers.tsx
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>
);
};
111 changes: 111 additions & 0 deletions packages/admin-ui/src/Toast/Toast.stories.tsx
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'} />
Copy link
Member

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.

}
};

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 />
}
};
203 changes: 203 additions & 0 deletions packages/admin-ui/src/Toast/Toast.tsx
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
};
1 change: 1 addition & 0 deletions packages/admin-ui/src/Toast/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./Toast";
1 change: 1 addition & 0 deletions packages/admin-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export * from "./Slider";
export * from "./Switch";
export * from "./Tabs";
export * from "./Text";
export * from "./Toast";
export * from "./Tooltip";
7 changes: 5 additions & 2 deletions packages/app-admin/src/base/Admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import React from "react";
import { App } from "@webiny/app";
import { ThemeProvider } from "@webiny/app-theme";
import { WcpProvider } from "@webiny/app-wcp";
import { CircularProgress } from "@webiny/ui/Progress";
import { Providers as UiProviders } from "@webiny/admin-ui";
import { CircularProgress } from "@webiny/ui/Progress";
import { ApolloClientFactory, createApolloProvider } from "./providers/ApolloProvider";
import { Base } from "./Base";
import { createTelemetryProvider } from "./providers/TelemetryProvider";
import { createUiStateProvider } from "./providers/UiStateProvider";
import { createAdminUiStateProvider } from "./providers/AdminUiStateProvider";
import { SearchProvider } from "./ui/Search";
import { UserMenuProvider } from "./ui/UserMenu";
import { NavigationProvider } from "./ui/Navigation";
Expand All @@ -23,6 +24,7 @@ export const Admin = ({ children, createApolloClient }: AdminProps) => {
const ApolloProvider = createApolloProvider(createApolloClient);
const TelemetryProvider = createTelemetryProvider();
const UiStateProvider = createUiStateProvider();
const AdminUiStateProvider = createAdminUiStateProvider();
const DialogsProvider = createDialogsProvider();

return (
Expand All @@ -38,7 +40,8 @@ export const Admin = ({ children, createApolloClient }: AdminProps) => {
UserMenuProvider,
NavigationProvider,
DialogsProvider,
IconPickerConfigProvider
IconPickerConfigProvider,
AdminUiStateProvider
]}
>
<Base />
Expand Down
Loading
Loading