diff --git a/apps/www/.vitepress/theme/layout/MainLayout.vue b/apps/www/.vitepress/theme/layout/MainLayout.vue index a1a0015c..4c2b0a06 100644 --- a/apps/www/.vitepress/theme/layout/MainLayout.vue +++ b/apps/www/.vitepress/theme/layout/MainLayout.vue @@ -12,11 +12,11 @@ import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, Command import { Button } from '@/lib/registry/default/ui/button' import RadixIconsGithubLogo from '~icons/radix-icons/github-logo' -import TablerBrandX from '~icons/tabler/brand-x' import RadixIconsMoon from '~icons/radix-icons/moon' import RadixIconsSun from '~icons/radix-icons/sun' import { useConfigStore } from '@/stores/config' import { Dialog, DialogContent } from '@/lib/registry/default/ui/dialog' +import { Toaster } from '@/lib/registry/default/ui/toast' import File from '~icons/radix-icons/file' import Circle from '~icons/radix-icons/circle' @@ -276,5 +276,6 @@ watch(() => $route.path, (n) => { + diff --git a/apps/www/src/content/docs/components/toast.md b/apps/www/src/content/docs/components/toast.md new file mode 100644 index 00000000..fcba11d6 --- /dev/null +++ b/apps/www/src/content/docs/components/toast.md @@ -0,0 +1,93 @@ +--- +title: Toast +description: A succinct message that is displayed temporarily. +source: apps/www/src/lib/registry/default/ui/toast +primitive: https://www.radix-vue.com/components/toast.html +--- + + + + +## Installation + + + + +### Run the following command + +```bash +npx shadcn-vue@latest add toast +``` + +### Add the Toaster component + +Add the following `Toaster` component to your `App.vue` file: + +```vue title="App.vue" {2,6} + + + + + +``` + + + + +## Usage + +The `useToast` hook returns a `toast` function that you can use to display a toast. + +```tsx +import { useToast } from '@/components/ui/toast/use-toast' +``` + +```vue + + + + { + toast({ + title: 'Scheduled: Catch up', + description: 'Friday, February 10, 2023 at 5:57 PM', + }); + }" + > + Add to calander + + +``` + + + +To display multiple toasts at the same time, you can update the `TOAST_LIMIT` in `use-toast.ts`. + + + +## Examples + +### Simple + + + +### With Title + + + +### With Action + + + +### Destructive + +Use `toast({ variant: "destructive" })` to display a destructive toast. + + diff --git a/apps/www/src/lib/registry/default/example/ToastDemo.vue b/apps/www/src/lib/registry/default/example/ToastDemo.vue new file mode 100644 index 00000000..abb8cbb7 --- /dev/null +++ b/apps/www/src/lib/registry/default/example/ToastDemo.vue @@ -0,0 +1,19 @@ + + + + { + toast({ + title: 'Scheduled: Catch up', + description: 'Friday, February 10, 2023 at 5:57 PM', + }); + }" + > + Add to calander + + diff --git a/apps/www/src/lib/registry/default/example/ToastDestructive.vue b/apps/www/src/lib/registry/default/example/ToastDestructive.vue new file mode 100644 index 00000000..0cf00554 --- /dev/null +++ b/apps/www/src/lib/registry/default/example/ToastDestructive.vue @@ -0,0 +1,27 @@ + + + + { + toast({ + title: 'Uh oh! Something went wrong.', + description: 'There was a problem with your request.', + variant: 'destructive', + action: h(ToastAction, { + altText: 'Try again', + }, { + default: () => 'Try again', + }), + }); + }" + > + Show Toast + + diff --git a/apps/www/src/lib/registry/default/example/ToastSimple.vue b/apps/www/src/lib/registry/default/example/ToastSimple.vue new file mode 100644 index 00000000..2310017e --- /dev/null +++ b/apps/www/src/lib/registry/default/example/ToastSimple.vue @@ -0,0 +1,18 @@ + + + + { + toast({ + description: 'Your message has been sent.', + }); + }" + > + Show Toast + + diff --git a/apps/www/src/lib/registry/default/example/ToastWithAction.vue b/apps/www/src/lib/registry/default/example/ToastWithAction.vue new file mode 100644 index 00000000..03f7640a --- /dev/null +++ b/apps/www/src/lib/registry/default/example/ToastWithAction.vue @@ -0,0 +1,26 @@ + + + + { + toast({ + title: 'Uh oh! Something went wrong.', + description: 'There was a problem with your request.', + action: h(ToastAction, { + altText: 'Try again', + }, { + default: () => 'Try again', + }), + }); + }" + > + Show Toast + + diff --git a/apps/www/src/lib/registry/default/example/ToastWithTitle.vue b/apps/www/src/lib/registry/default/example/ToastWithTitle.vue new file mode 100644 index 00000000..800bf9e3 --- /dev/null +++ b/apps/www/src/lib/registry/default/example/ToastWithTitle.vue @@ -0,0 +1,19 @@ + + + + { + toast({ + title: 'Uh oh! Something went wrong.', + description: 'There was a problem with your request.', + }); + }" + > + Show Toast + + diff --git a/apps/www/src/lib/registry/default/ui/toast/Toast.vue b/apps/www/src/lib/registry/default/ui/toast/Toast.vue new file mode 100644 index 00000000..72747db6 --- /dev/null +++ b/apps/www/src/lib/registry/default/ui/toast/Toast.vue @@ -0,0 +1,24 @@ + + + + + + + diff --git a/apps/www/src/lib/registry/default/ui/toast/ToastAction.vue b/apps/www/src/lib/registry/default/ui/toast/ToastAction.vue new file mode 100644 index 00000000..b5ccbefe --- /dev/null +++ b/apps/www/src/lib/registry/default/ui/toast/ToastAction.vue @@ -0,0 +1,12 @@ + + + + + + + diff --git a/apps/www/src/lib/registry/default/ui/toast/ToastClose.vue b/apps/www/src/lib/registry/default/ui/toast/ToastClose.vue new file mode 100644 index 00000000..766593cf --- /dev/null +++ b/apps/www/src/lib/registry/default/ui/toast/ToastClose.vue @@ -0,0 +1,15 @@ + + + + + + + diff --git a/apps/www/src/lib/registry/default/ui/toast/ToastDescription.vue b/apps/www/src/lib/registry/default/ui/toast/ToastDescription.vue new file mode 100644 index 00000000..98630e3d --- /dev/null +++ b/apps/www/src/lib/registry/default/ui/toast/ToastDescription.vue @@ -0,0 +1,12 @@ + + + + + + + diff --git a/apps/www/src/lib/registry/default/ui/toast/ToastProvider.vue b/apps/www/src/lib/registry/default/ui/toast/ToastProvider.vue new file mode 100644 index 00000000..340cbd83 --- /dev/null +++ b/apps/www/src/lib/registry/default/ui/toast/ToastProvider.vue @@ -0,0 +1,11 @@ + + + + + + + diff --git a/apps/www/src/lib/registry/default/ui/toast/ToastTitle.vue b/apps/www/src/lib/registry/default/ui/toast/ToastTitle.vue new file mode 100644 index 00000000..b317850a --- /dev/null +++ b/apps/www/src/lib/registry/default/ui/toast/ToastTitle.vue @@ -0,0 +1,12 @@ + + + + + + + diff --git a/apps/www/src/lib/registry/default/ui/toast/ToastViewport.vue b/apps/www/src/lib/registry/default/ui/toast/ToastViewport.vue new file mode 100644 index 00000000..5f2f2b95 --- /dev/null +++ b/apps/www/src/lib/registry/default/ui/toast/ToastViewport.vue @@ -0,0 +1,10 @@ + + + + + diff --git a/apps/www/src/lib/registry/default/ui/toast/Toaster.vue b/apps/www/src/lib/registry/default/ui/toast/Toaster.vue new file mode 100644 index 00000000..d5db5dcd --- /dev/null +++ b/apps/www/src/lib/registry/default/ui/toast/Toaster.vue @@ -0,0 +1,24 @@ + + + + + + + + {{ toast.title }} + + + {{ toast.description }} + + + + + + + + diff --git a/apps/www/src/lib/registry/default/ui/toast/index.ts b/apps/www/src/lib/registry/default/ui/toast/index.ts new file mode 100644 index 00000000..17f038c7 --- /dev/null +++ b/apps/www/src/lib/registry/default/ui/toast/index.ts @@ -0,0 +1,26 @@ +export { default as Toaster } from './Toaster.vue' +export { default as Toast } from './Toast.vue' +export { default as ToastViewport } from './ToastViewport.vue' +export { default as ToastAction } from './ToastAction.vue' +export { default as ToastClose } from './ToastClose.vue' +export { default as ToastTitle } from './ToastTitle.vue' +export { default as ToastDescription } from './ToastDescription.vue' +export { default as ToastProvider } from './ToastProvider.vue' + +import { cva } from 'class-variance-authority' + +export const toastVariants = cva( + 'group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-6 pr-8 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 data-[state=open]:sm:slide-in-from-bottom-full', + { + variants: { + variant: { + default: 'border bg-background text-foreground', + destructive: + 'destructive group border-destructive bg-destructive text-destructive-foreground', + }, + }, + defaultVariants: { + variant: 'default', + }, + }, +) diff --git a/apps/www/src/lib/registry/default/ui/toast/use-toast.ts b/apps/www/src/lib/registry/default/ui/toast/use-toast.ts new file mode 100644 index 00000000..f21f6ebb --- /dev/null +++ b/apps/www/src/lib/registry/default/ui/toast/use-toast.ts @@ -0,0 +1,160 @@ +import { computed, ref } from 'vue' +import type { Component } from 'vue' +import type { ToastProps } from './Toast.vue' + +const TOAST_LIMIT = 1 +const TOAST_REMOVE_DELAY = 1000000 + +type ToasterToast = ToastProps & { + id: string + title?: string + description?: string + action?: Component +} + +const actionTypes = { + ADD_TOAST: 'ADD_TOAST', + UPDATE_TOAST: 'UPDATE_TOAST', + DISMISS_TOAST: 'DISMISS_TOAST', + REMOVE_TOAST: 'REMOVE_TOAST', +} as const + +let count = 0 + +function genId() { + count = (count + 1) % Number.MAX_VALUE + return count.toString() +} + +type ActionType = typeof actionTypes + +type Action = + | { + type: ActionType['ADD_TOAST'] + toast: ToasterToast + } + | { + type: ActionType['UPDATE_TOAST'] + toast: Partial + } + | { + type: ActionType['DISMISS_TOAST'] + toastId?: ToasterToast['id'] + } + | { + type: ActionType['REMOVE_TOAST'] + toastId?: ToasterToast['id'] + } + +interface State { + toasts: ToasterToast[] +} + +const toastTimeouts = new Map>() + +function addToRemoveQueue(toastId: string) { + if (toastTimeouts.has(toastId)) + return + + const timeout = setTimeout(() => { + toastTimeouts.delete(toastId) + dispatch({ + type: actionTypes.REMOVE_TOAST, + toastId, + }) + }, TOAST_REMOVE_DELAY) + + toastTimeouts.set(toastId, timeout) +} + +const state = ref({ + toasts: [], +}) + +function dispatch(action: Action) { + switch (action.type) { + case actionTypes.ADD_TOAST: + state.value.toasts = [action.toast, ...state.value.toasts].slice(0, TOAST_LIMIT) + break + + case actionTypes.UPDATE_TOAST: + state.value.toasts = state.value.toasts.map(t => + t.id === action.toast.id ? { ...t, ...action.toast } : t, + ) + break + + case actionTypes.DISMISS_TOAST: { + const { toastId } = action + + if (toastId) { + addToRemoveQueue(toastId) + } + else { + state.value.toasts.forEach((toast) => { + addToRemoveQueue(toast.id) + }) + } + + state.value.toasts = state.value.toasts.map(t => + t.id === toastId || toastId === undefined + ? { + ...t, + open: false, + } + : t, + ) + break + } + + case actionTypes.REMOVE_TOAST: + if (action.toastId === undefined) + state.value.toasts = [] + else + state.value.toasts = state.value.toasts.filter(t => t.id !== action.toastId) + + break + } +} + +function useToast() { + return { + toasts: computed(() => state.value.toasts), + toast, + dismiss: (toastId?: string) => dispatch({ type: actionTypes.DISMISS_TOAST, toastId }), + } +} + +type Toast = Omit + +function toast(props: Toast) { + const id = genId() + + const update = (props: ToasterToast) => + dispatch({ + type: actionTypes.UPDATE_TOAST, + toast: { ...props, id }, + }) + + const dismiss = () => dispatch({ type: actionTypes.DISMISS_TOAST, toastId: id }) + + dispatch({ + type: actionTypes.ADD_TOAST, + toast: { + ...props, + id, + 'open': true, + 'onUpdate:open': (open: boolean) => { + if (!open) + dismiss() + }, + }, + }) + + return { + id, + dismiss, + update, + } +} + +export { toast, useToast }