Skip to content

Commit 49804fb

Browse files
feat: toast component (#65)
1 parent 63baf62 commit 49804fb

File tree

11 files changed

+550
-29
lines changed

11 files changed

+550
-29
lines changed

.changeset/calm-ties-brush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@zenml-io/react-component-library": minor
3+
---
4+
5+
add toast

.eslintrc.json

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
{
2-
"env": {
3-
"browser": true,
4-
"es2021": true
5-
},
6-
"extends": [
7-
"eslint:recommended",
8-
"plugin:@typescript-eslint/recommended",
9-
"plugin:react/recommended",
10-
"plugin:storybook/recommended"
11-
],
12-
"parser": "@typescript-eslint/parser",
13-
"parserOptions": {
14-
"ecmaVersion": "latest",
15-
"sourceType": "module"
16-
},
17-
"plugins": [
18-
"@typescript-eslint",
19-
"react"
20-
],
21-
"settings": {
22-
"react": {
23-
"version": "detect"
24-
}
25-
},
26-
"rules": {
27-
"react/prop-types": "off"
28-
}
2+
"env": {
3+
"browser": true,
4+
"es2021": true
5+
},
6+
"extends": [
7+
"eslint:recommended",
8+
"plugin:@typescript-eslint/recommended",
9+
"plugin:react/recommended",
10+
"plugin:storybook/recommended"
11+
],
12+
"parser": "@typescript-eslint/parser",
13+
"parserOptions": {
14+
"ecmaVersion": "latest",
15+
"sourceType": "module"
16+
},
17+
"plugins": ["@typescript-eslint", "react"],
18+
"settings": {
19+
"react": {
20+
"version": "detect"
21+
}
22+
},
23+
"rules": {
24+
"react/prop-types": "off",
25+
"no-mixed-spaces-and-tabs": "off"
26+
}
2927
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"@radix-ui/react-dropdown-menu": "^2.0.6",
9292
"@radix-ui/react-slot": "^1.0.2",
9393
"@radix-ui/react-tabs": "^1.0.4",
94+
"@radix-ui/react-toast": "^1.1.5",
9495
"@radix-ui/react-tooltip": "^1.0.7",
9596
"@tanstack/react-table": "^8.15.3",
9697
"class-variance-authority": "^0.7.0",

pnpm-lock.yaml

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Sidebar/Sidebar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ export function SidebarItemContent({
149149
? "stroke-primary-400"
150150
: "stroke-theme-text-tertiary"
151151
: isActive
152-
? "fill-primary-400"
153-
: "fill-theme-text-tertiary"
152+
? "fill-primary-400"
153+
: "fill-theme-text-tertiary"
154154
} `
155155
);
156156
return (
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { Meta } from "@storybook/react";
2+
import React from "react";
3+
import { Toaster, Toast, toast } from "./index";
4+
import { StoryObj } from "@storybook/react";
5+
import { Button } from "../Button";
6+
7+
const meta = {
8+
title: "Elements/Toast",
9+
component: Toast,
10+
argTypes: {
11+
emphasis: {
12+
description: "Emphasis of the toast",
13+
control: "select",
14+
defaultValue: "subtle",
15+
options: ["subtle", "bold"]
16+
},
17+
status: {
18+
description: "Status of the toast",
19+
control: "select",
20+
defaultValue: "default",
21+
options: ["default", "error", "success", "warning"]
22+
},
23+
rounded: {
24+
control: "boolean",
25+
defaultValue: true
26+
}
27+
},
28+
parameters: {
29+
layout: "centered"
30+
},
31+
decorators: [
32+
(Story) => (
33+
<div className="min-w-[800px] flex items-center justify-center min-h-[1000px]">
34+
<Story />
35+
</div>
36+
)
37+
],
38+
39+
tags: ["autodocs"]
40+
} satisfies Meta<typeof Toast>;
41+
42+
export default meta;
43+
44+
type Story = StoryObj<typeof meta>;
45+
46+
export const DefaultVariant: Story = {
47+
name: "Toast",
48+
argTypes: {
49+
// @ts-expect-error for some reason a wrong type is picked up there
50+
emphasis: "subtle",
51+
// @ts-expect-error for some reason a wrong type is picked up there
52+
status: "error",
53+
// @ts-expect-error for some reason a wrong type is picked up there
54+
rounded: "true"
55+
},
56+
render: ({ emphasis, status, rounded }) => (
57+
<div>
58+
<Button
59+
onClick={() => {
60+
toast({
61+
status,
62+
emphasis,
63+
description: "This is a toast message",
64+
rounded
65+
});
66+
}}
67+
>
68+
Add Toast
69+
</Button>
70+
<Toaster />
71+
</div>
72+
)
73+
};

src/components/Toast/Toast.tsx

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import * as ToastPrimitives from "@radix-ui/react-toast";
2+
import { cva, type VariantProps } from "class-variance-authority";
3+
import * as React from "react";
4+
import { cn } from "../../utilities";
5+
6+
const ToastProvider = ToastPrimitives.Provider;
7+
8+
const ToastViewport = React.forwardRef<
9+
React.ElementRef<typeof ToastPrimitives.Viewport>,
10+
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Viewport>
11+
>(({ className, ...props }, ref) => (
12+
<ToastPrimitives.Viewport
13+
ref={ref}
14+
className={cn(
15+
"fixed top-0 z-[9999] flex max-h-screen w-full flex-col-reverse gap-1 p-4 sm:right-0 sm:top-7 sm:flex-col md:max-w-[480px]",
16+
className
17+
)}
18+
{...props}
19+
/>
20+
));
21+
ToastViewport.displayName = ToastPrimitives.Viewport.displayName;
22+
23+
export const toastVariants = cva(
24+
"group pointer-events-auto pl-4 pr-6 py-3 relative flex w-full items-center justify-between overflow-hidden border 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 data-[state=open]:sm:slide-in-from-bottom-full",
25+
{
26+
variants: {
27+
rounded: {
28+
true: "rounded-md",
29+
false: ""
30+
},
31+
status: {
32+
default: "",
33+
error: "",
34+
success: "",
35+
warning: ""
36+
},
37+
emphasis: {
38+
subtle: "border",
39+
bold: ""
40+
}
41+
},
42+
compoundVariants: [
43+
{
44+
emphasis: "bold",
45+
status: "default",
46+
className: "white-close bg-theme-surface-strong text-theme-text-negative"
47+
},
48+
{
49+
emphasis: "subtle",
50+
status: "default",
51+
class: "bg-primary-25 border-primary-400 text-theme-text-primary"
52+
},
53+
{
54+
emphasis: "bold",
55+
status: "error",
56+
className: "white-close bg-error-600 text-theme-text-negative"
57+
},
58+
{
59+
emphasis: "subtle",
60+
status: "error",
61+
className: "bg-error-50 border-error-300 text-error-900"
62+
},
63+
{
64+
emphasis: "bold",
65+
status: "success",
66+
className: "white-close bg-success-700 text-theme-text-negative"
67+
},
68+
{
69+
emphasis: "subtle",
70+
status: "success",
71+
className: "bg-success-50 border-success-300 text-success-900"
72+
},
73+
{
74+
emphasis: "bold",
75+
status: "warning",
76+
className: "bg-warning-400"
77+
},
78+
{
79+
emphasis: "subtle",
80+
status: "warning",
81+
className: "bg-warning-50 border-warning-300 text-warning-900"
82+
}
83+
],
84+
defaultVariants: {
85+
status: "default"
86+
}
87+
}
88+
);
89+
90+
const Toast = React.forwardRef<
91+
React.ElementRef<typeof ToastPrimitives.Root>,
92+
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> & VariantProps<typeof toastVariants>
93+
>(({ className, status, rounded, emphasis, ...props }, ref) => {
94+
return (
95+
<ToastPrimitives.Root
96+
ref={ref}
97+
className={cn(toastVariants({ status, rounded, emphasis }), className)}
98+
{...props}
99+
/>
100+
);
101+
});
102+
Toast.displayName = ToastPrimitives.Root.displayName;
103+
104+
const ToastAction = React.forwardRef<
105+
React.ElementRef<typeof ToastPrimitives.Action>,
106+
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Action>
107+
>(({ className, ...props }, ref) => (
108+
<ToastPrimitives.Action
109+
ref={ref}
110+
className={cn(
111+
"text-sm ring-offset-background hover:bg-secondary focus:ring-ring inline-flex h-8 shrink-0 items-center justify-center rounded-md border bg-transparent px-3 font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
112+
className
113+
)}
114+
{...props}
115+
/>
116+
));
117+
ToastAction.displayName = ToastPrimitives.Action.displayName;
118+
119+
const ToastClose = React.forwardRef<
120+
React.ElementRef<typeof ToastPrimitives.Close>,
121+
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close>
122+
>(({ className, ...props }, ref) => (
123+
<ToastPrimitives.Close
124+
ref={ref}
125+
className={cn(
126+
"absolute right-2 top-2 rounded-md p-0.5 group-[.white-close]:text-theme-text-negative",
127+
className
128+
)}
129+
toast-close=""
130+
{...props}
131+
>
132+
<svg
133+
className="h-4 w-4 group-[.white-close]:fill-white"
134+
viewBox="0 0 24 24"
135+
fill="black"
136+
xmlns="http://www.w3.org/2000/svg"
137+
>
138+
<path
139+
fillRule="evenodd"
140+
clipRule="evenodd"
141+
d="M5.29289 5.29289C5.68342 4.90237 6.31658 4.90237 6.70711 5.29289L12 10.5858L17.2929 5.29289C17.6834 4.90237 18.3166 4.90237 18.7071 5.29289C19.0976 5.68342 19.0976 6.31658 18.7071 6.70711L13.4142 12L18.7071 17.2929C19.0976 17.6834 19.0976 18.3166 18.7071 18.7071C18.3166 19.0976 17.6834 19.0976 17.2929 18.7071L12 13.4142L6.70711 18.7071C6.31658 19.0976 5.68342 19.0976 5.29289 18.7071C4.90237 18.3166 4.90237 17.6834 5.29289 17.2929L10.5858 12L5.29289 6.70711C4.90237 6.31658 4.90237 5.68342 5.29289 5.29289Z"
142+
/>
143+
</svg>
144+
</ToastPrimitives.Close>
145+
));
146+
ToastClose.displayName = ToastPrimitives.Close.displayName;
147+
148+
const ToastTitle = React.forwardRef<
149+
React.ElementRef<typeof ToastPrimitives.Title>,
150+
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title>
151+
>(({ className, ...props }, ref) => (
152+
<ToastPrimitives.Title ref={ref} className={cn("text-sm font-semibold", className)} {...props} />
153+
));
154+
ToastTitle.displayName = ToastPrimitives.Title.displayName;
155+
156+
const ToastDescription = React.forwardRef<
157+
React.ElementRef<typeof ToastPrimitives.Description>,
158+
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description>
159+
>(({ className, ...props }, ref) => (
160+
<ToastPrimitives.Description
161+
ref={ref}
162+
className={cn("text-sm opacity-90", className)}
163+
{...props}
164+
/>
165+
));
166+
ToastDescription.displayName = ToastPrimitives.Description.displayName;
167+
168+
type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>;
169+
170+
type ToastActionElement = React.ReactElement<typeof ToastAction>;
171+
172+
export {
173+
Toast,
174+
ToastAction,
175+
ToastClose,
176+
ToastDescription,
177+
ToastProvider,
178+
ToastTitle,
179+
ToastViewport,
180+
type ToastActionElement,
181+
type ToastProps
182+
};

0 commit comments

Comments
 (0)