-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[WEB-4692]chore: added toast to propel package #7640
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9e2c652
* chore: migrated toast to base ui toast
vamsikrishnamathala b5df4ef
fix: lint errors
vamsikrishnamathala e77d0f0
fix: export path
vamsikrishnamathala c5e0f7b
fix: lint errors
vamsikrishnamathala f57c221
chore: imporoved props handling
vamsikrishnamathala 9ec39a3
Merge branch 'preview' into chore-toast_migration_propel
vamsikrishnamathala 4845338
fix: lint errors
vamsikrishnamathala 60196c7
Merge branch 'preview' of github.com:makeplane/plane into chore-toast…
vamsikrishnamathala ebab023
chore: updated import paths
vamsikrishnamathala c6ad874
chore: replaced clsx with cn
vamsikrishnamathala 1f95933
* chore: updated tsdown config
vamsikrishnamathala 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 hidden or 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 hidden or 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 hidden or 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,35 @@ | ||
| import * as React from "react"; | ||
|
|
||
| interface ICircularBarSpinner extends React.SVGAttributes<SVGElement> { | ||
| height?: string; | ||
| width?: string; | ||
| className?: string | undefined; | ||
| } | ||
|
|
||
| export const CircularBarSpinner: React.FC<ICircularBarSpinner> = ({ | ||
| height = "16px", | ||
| width = "16px", | ||
| className = "", | ||
| }) => ( | ||
| <div role="status"> | ||
| <svg xmlns="http://www.w3.org/2000/svg" width={width} height={height} viewBox="0 0 24 24" className={className}> | ||
| <g> | ||
| <rect width={2} height={5} x={11} y={1} fill="currentColor" opacity={0.14} /> | ||
| <rect width={2} height={5} x={11} y={1} fill="currentColor" opacity={0.29} transform="rotate(30 12 12)" /> | ||
| <rect width={2} height={5} x={11} y={1} fill="currentColor" opacity={0.43} transform="rotate(60 12 12)" /> | ||
| <rect width={2} height={5} x={11} y={1} fill="currentColor" opacity={0.57} transform="rotate(90 12 12)" /> | ||
| <rect width={2} height={5} x={11} y={1} fill="currentColor" opacity={0.71} transform="rotate(120 12 12)" /> | ||
| <rect width={2} height={5} x={11} y={1} fill="currentColor" opacity={0.86} transform="rotate(150 12 12)" /> | ||
| <rect width={2} height={5} x={11} y={1} fill="currentColor" transform="rotate(180 12 12)" /> | ||
| <animateTransform | ||
| attributeName="transform" | ||
| calcMode="discrete" | ||
| dur="0.75s" | ||
| repeatCount="indefinite" | ||
| type="rotate" | ||
| values="0 12 12;30 12 12;60 12 12;90 12 12;120 12 12;150 12 12;180 12 12;210 12 12;240 12 12;270 12 12;300 12 12;330 12 12;360 12 12" | ||
| /> | ||
| </g> | ||
| </svg> | ||
| </div> | ||
| ); |
This file contains hidden or 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,33 @@ | ||
| import * as React from "react"; | ||
| // helpers | ||
| import clsx from "clsx"; | ||
|
|
||
| export interface ISpinner extends React.SVGAttributes<SVGElement> { | ||
| height?: string; | ||
| width?: string; | ||
| className?: string | undefined; | ||
| } | ||
|
|
||
| export const Spinner: React.FC<ISpinner> = ({ height = "32px", width = "32px", className = "" }) => ( | ||
| <div role="status"> | ||
| <svg | ||
| aria-hidden="true" | ||
| height={height} | ||
| width={width} | ||
| className={clsx("animate-spin text-custom-text-200", className)} | ||
| viewBox="0 0 100 101" | ||
| fill="none" | ||
| xmlns="http://www.w3.org/2000/svg" | ||
| > | ||
| <path | ||
| d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" | ||
| fill="currentColor" | ||
| /> | ||
| <path | ||
| className="fill-custom-primary-100" | ||
| d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" | ||
| /> | ||
| </svg> | ||
| <span className="sr-only">Loading...</span> | ||
| </div> | ||
| ); |
This file contains hidden or 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,2 @@ | ||
| export * from "./circular-spinner"; | ||
| export * from "./circular-bar-spinner"; |
This file contains hidden or 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 hidden or 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,232 @@ | ||
| import * as React from "react"; | ||
| import { Toast as BaseToast } from "@base-ui-components/react/toast"; | ||
| import { AlertTriangle, CheckCircle2, X, XCircle } from "lucide-react"; | ||
| // spinner | ||
| import { CircularBarSpinner } from "../spinners/circular-bar-spinner"; | ||
| import { cn } from "../utils/classname"; | ||
|
|
||
| export enum TOAST_TYPE { | ||
| SUCCESS = "success", | ||
| ERROR = "error", | ||
| INFO = "info", | ||
| WARNING = "warning", | ||
| LOADING = "loading", | ||
| } | ||
|
|
||
| type SetToastProps = | ||
| | { | ||
| type: TOAST_TYPE.LOADING; | ||
| title?: string; | ||
| } | ||
| | { | ||
| id?: string | number; | ||
| type: Exclude<TOAST_TYPE, TOAST_TYPE.LOADING>; | ||
| title: string; | ||
| message?: string; | ||
| actionItems?: React.ReactNode; | ||
| }; | ||
|
|
||
| type PromiseToastCallback<ToastData> = (data: ToastData) => string; | ||
| type ActionItemsPromiseToastCallback<ToastData> = (data: ToastData) => React.ReactNode; | ||
|
|
||
| type PromiseToastData<ToastData> = { | ||
| title: string; | ||
| message?: PromiseToastCallback<ToastData>; | ||
| actionItems?: ActionItemsPromiseToastCallback<ToastData>; | ||
| }; | ||
|
|
||
| type PromiseToastOptions<ToastData> = { | ||
| loading?: string; | ||
| success: PromiseToastData<ToastData>; | ||
| error: PromiseToastData<ToastData>; | ||
| }; | ||
|
|
||
| export type ToastProps = { | ||
| theme: "light" | "dark" | "system"; | ||
| }; | ||
|
|
||
| const toastManager = BaseToast.createToastManager(); | ||
|
|
||
| export const Toast = (props: ToastProps) => ( | ||
| <BaseToast.Provider toastManager={toastManager}> | ||
| <BaseToast.Portal> | ||
| <BaseToast.Viewport data-theme={props.theme}> | ||
| <ToastList /> | ||
| </BaseToast.Viewport> | ||
| </BaseToast.Portal> | ||
| </BaseToast.Provider> | ||
| ); | ||
|
|
||
| const TOAST_DATA = { | ||
| [TOAST_TYPE.SUCCESS]: { | ||
| icon: <CheckCircle2 width={24} height={24} strokeWidth={1.5} className="text-toast-text-success" />, | ||
| textColorClassName: "text-toast-text-success", | ||
| backgroundColorClassName: "bg-toast-background-success", | ||
| borderColorClassName: "border-toast-border-success", | ||
| }, | ||
| [TOAST_TYPE.ERROR]: { | ||
| icon: <XCircle width={24} height={24} strokeWidth={1.5} className="text-toast-text-error" />, | ||
| textColorClassName: "text-toast-text-error", | ||
| backgroundColorClassName: "bg-toast-background-error", | ||
| borderColorClassName: "border-toast-border-error", | ||
| }, | ||
| [TOAST_TYPE.WARNING]: { | ||
| icon: <AlertTriangle width={24} height={24} strokeWidth={1.5} className="text-toast-text-warning" />, | ||
| textColorClassName: "text-toast-text-warning", | ||
| backgroundColorClassName: "bg-toast-background-warning", | ||
| borderColorClassName: "border-toast-border-warning", | ||
| }, | ||
| [TOAST_TYPE.INFO]: { | ||
| icon: <></>, | ||
| textColorClassName: "text-toast-text-info", | ||
| backgroundColorClassName: "bg-toast-background-info", | ||
| borderColorClassName: "border-toast-border-info", | ||
| }, | ||
| [TOAST_TYPE.LOADING]: { | ||
| icon: <CircularBarSpinner className="text-toast-text-tertiary" />, | ||
| textColorClassName: "text-toast-text-loading", | ||
| backgroundColorClassName: "bg-toast-background-loading", | ||
| borderColorClassName: "border-toast-border-loading", | ||
| }, | ||
vamsikrishnamathala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| const ToastList = () => { | ||
| const { toasts } = BaseToast.useToastManager(); | ||
| return toasts.map((toast) => <ToastRender key={toast.id} id={toast.id} toast={toast} />); | ||
| }; | ||
|
|
||
| const ToastRender = ({ id, toast }: { id: React.Key; toast: BaseToast.Root.ToastObject }) => { | ||
| const toastData = toast.data as SetToastProps; | ||
| const type = toastData.type as TOAST_TYPE; | ||
| const data = TOAST_DATA[type]; | ||
|
|
||
| return ( | ||
| <BaseToast.Root | ||
| toast={toast} | ||
| key={id} | ||
| className={cn( | ||
| "flex items-center rounded-lg border shadow-sm p-2 w-[350px] absolute right-3 bottom-3 z-[calc(1000-var(--toast-index))] [transform:translateX(var(--toast-swipe-movement-x))_translateY(calc(var(--toast-swipe-movement-y)+calc(min(var(--toast-index),10)*-10px)))_scale(calc(max(0,1-(var(--toast-index)*0.1))))] transition-[opacity,transform] duration-500 ease-[cubic-bezier(0.22,1,0.36,1)] select-none after:absolute after:bottom-full after:left-0 after:h-[calc(var(--gap)+1px)] after:w-full after:content-[''] data-[ending-style]:opacity-0 data-[expanded]:[transform:translateX(var(--toast-swipe-movement-x))_translateY(calc(var(--toast-offset-y)*-1+calc(var(--toast-index)*var(--gap)*-1)+var(--toast-swipe-movement-y)))] data-[limited]:opacity-0 data-[starting-style]:[transform:translateY(150%)] data-[ending-style]:data-[swipe-direction=down]:[transform:translateY(calc(var(--toast-swipe-movement-y)+150%))] data-[expanded]:data-[ending-style]:data-[swipe-direction=down]:[transform:translateY(calc(var(--toast-swipe-movement-y)+150%))] data-[ending-style]:data-[swipe-direction=left]:[transform:translateX(calc(var(--toast-swipe-movement-x)-150%))_translateY(var(--offset-y))] data-[expanded]:data-[ending-style]:data-[swipe-direction=left]:[transform:translateX(calc(var(--toast-swipe-movement-x)-150%))_translateY(var(--offset-y))] data-[ending-style]:data-[swipe-direction=right]:[transform:translateX(calc(var(--toast-swipe-movement-x)+150%))_translateY(var(--offset-y))] data-[expanded]:data-[ending-style]:data-[swipe-direction=right]:[transform:translateX(calc(var(--toast-swipe-movement-x)+150%))_translateY(var(--offset-y))] data-[ending-style]:data-[swipe-direction=up]:[transform:translateY(calc(var(--toast-swipe-movement-y)-150%))] data-[expanded]:data-[ending-style]:data-[swipe-direction=up]:[transform:translateY(calc(var(--toast-swipe-movement-y)-150%))] data-[ending-style]:[&:not([data-limited])]:[transform:translateY(150%)]", | ||
| data.backgroundColorClassName, | ||
| data.borderColorClassName | ||
| )} | ||
| style={{ | ||
| ["--gap" as string]: "1rem", | ||
| ["--offset-y" as string]: | ||
| "calc(var(--toast-offset-y) * -1 + (var(--toast-index) * var(--gap) * -1) + var(--toast-swipe-movement-y))", | ||
| }} | ||
| onMouseDown={(e) => { | ||
| e.stopPropagation(); | ||
| e.preventDefault(); | ||
| }} | ||
| > | ||
| {toastData.type === TOAST_TYPE.LOADING ? ( | ||
| <div className="w-full h-full flex items-center justify-center px-4 py-2"> | ||
| {data.icon && <div className="flex items-center justify-center">{data.icon}</div>} | ||
| <div className={cn("w-full flex items-center gap-0.5 pr-1", data.icon ? "pl-4" : "pl-1")}> | ||
| <div className={cn("grow text-sm font-semibold", data.textColorClassName)}> | ||
| {toastData.title ?? "Loading..."} | ||
| </div> | ||
| <BaseToast.Close | ||
| className="absolute top-2 right-2.5 text-toast-text-secondary hover:text-toast-text-tertiary cursor-pointer" | ||
| aria-label="Close" | ||
| > | ||
| <X strokeWidth={1.5} width={14} height={14} /> | ||
| </BaseToast.Close> | ||
| </div> | ||
| </div> | ||
| ) : ( | ||
| <> | ||
| <BaseToast.Close className="absolute top-2 right-2.5 text-toast-text-secondary hover:text-toast-text-tertiary cursor-pointer"> | ||
| <X strokeWidth={1.5} width={14} height={14} /> | ||
| </BaseToast.Close> | ||
vamsikrishnamathala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <div className="w-full flex flex-col gap-2 p-2"> | ||
| <div className="flex items-center w-full"> | ||
| {data.icon && <div className="flex items-center justify-center">{data.icon}</div>} | ||
| <div className={cn("flex flex-col gap-0.5 pr-1", data.icon ? "pl-4" : "pl-1")}> | ||
| <BaseToast.Title className={cn("text-sm font-semibold", data.textColorClassName)}> | ||
| {toastData.title} | ||
| </BaseToast.Title> | ||
| {toastData.message && ( | ||
| <BaseToast.Description className="text-toast-text-secondary text-xs font-medium"> | ||
| {toastData.message} | ||
| </BaseToast.Description> | ||
| )} | ||
| </div> | ||
| </div> | ||
| {toastData.actionItems && <div className="flex items-center pl-[32px]">{toastData.actionItems}</div>} | ||
| </div> | ||
| </> | ||
| )} | ||
| </BaseToast.Root> | ||
| ); | ||
| }; | ||
|
|
||
| export const setToast = (props: SetToastProps) => { | ||
| let toastId: string | undefined; | ||
| if (props.type !== TOAST_TYPE.LOADING) { | ||
| toastId = toastManager.add({ | ||
| data: { | ||
| type: props.type, | ||
| title: props.title, | ||
| message: props.message, | ||
| actionItems: props.actionItems, | ||
| }, | ||
| }); | ||
| } else { | ||
| toastId = toastManager.add({ | ||
| data: { | ||
| type: props.type, | ||
| title: props.title, | ||
| }, | ||
| }); | ||
| } | ||
| return toastId; | ||
| }; | ||
vamsikrishnamathala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export const updateToast = (id: string, props: SetToastProps) => { | ||
| toastManager.update(id, { | ||
| data: | ||
| props.type === TOAST_TYPE.LOADING | ||
| ? { | ||
| type: TOAST_TYPE.LOADING, | ||
| title: props.title, | ||
| } | ||
| : { | ||
| type: props.type, | ||
| title: props.title, | ||
| message: props.message, | ||
| actionItems: props.actionItems, | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| export const setPromiseToast = <ToastData,>( | ||
| promise: Promise<ToastData>, | ||
| options: PromiseToastOptions<ToastData> | ||
| ): void => { | ||
| toastManager.promise(promise, { | ||
| loading: { | ||
| data: { | ||
| title: options.loading ?? "Loading...", | ||
| type: TOAST_TYPE.LOADING, | ||
| message: undefined, | ||
| actionItems: undefined, | ||
| }, | ||
| }, | ||
| success: (data) => ({ | ||
| data: { | ||
| type: TOAST_TYPE.SUCCESS, | ||
| title: options.success.title, | ||
| message: options.success.message?.(data), | ||
| actionItems: options.success.actionItems?.(data), | ||
| }, | ||
| }), | ||
| error: (data) => ({ | ||
| data: { | ||
| type: TOAST_TYPE.ERROR, | ||
| title: options.error.title, | ||
| message: options.error.message?.(data), | ||
| actionItems: options.error.actionItems?.(data), | ||
| }, | ||
| }), | ||
| }); | ||
| }; | ||
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.