From 437f9ce4ff952633d7f756a7800ebdd885b6fbf6 Mon Sep 17 00:00:00 2001 From: ZTL-UwU Date: Sat, 15 Jun 2024 13:24:22 +0800 Subject: [PATCH] feat: add toast config Signed-off-by: ZTL-UwU --- app.config.ts | 1 + app.vue | 8 ++--- components.json | 2 +- components/content/CodeCopy.vue | 11 +++---- components/ui/toast/Toast.vue | 38 ++++++++++++------------ components/ui/toast/ToastAction.vue | 26 ++++++++-------- components/ui/toast/ToastClose.vue | 29 +++++++++--------- components/ui/toast/ToastDescription.vue | 26 ++++++++-------- components/ui/toast/ToastProvider.vue | 12 ++++---- components/ui/toast/ToastTitle.vue | 26 ++++++++-------- components/ui/toast/ToastViewport.vue | 22 +++++++------- components/ui/toast/Toaster.vue | 16 +++++----- components/ui/toast/index.ts | 36 +++++++++++----------- components/ui/toast/use-toast.ts | 31 +++++++++---------- composables/useConfig.ts | 2 ++ package.json | 2 +- 16 files changed, 143 insertions(+), 145 deletions(-) diff --git a/app.config.ts b/app.config.ts index 99d2054a..f1ea759f 100644 --- a/app.config.ts +++ b/app.config.ts @@ -67,6 +67,7 @@ export default defineAppConfig({ main: { breadCrumb: true, showTitle: true, + codeCopyToast: true, codeIcon: { 'package.json': 'vscode-icons:file-type-node', 'tsconfig.json': 'vscode-icons:file-type-tsconfig', diff --git a/app.vue b/app.vue index c71937a3..e826fd8b 100644 --- a/app.vue +++ b/app.vue @@ -1,9 +1,7 @@ - + + diff --git a/components/ui/toast/ToastAction.vue b/components/ui/toast/ToastAction.vue index 28ca8c8e..766f607c 100644 --- a/components/ui/toast/ToastAction.vue +++ b/components/ui/toast/ToastAction.vue @@ -1,19 +1,19 @@ + + - - diff --git a/components/ui/toast/ToastClose.vue b/components/ui/toast/ToastClose.vue index ab685a51..9b77481d 100644 --- a/components/ui/toast/ToastClose.vue +++ b/components/ui/toast/ToastClose.vue @@ -1,22 +1,21 @@ + + - - diff --git a/components/ui/toast/ToastDescription.vue b/components/ui/toast/ToastDescription.vue index 58520378..5c62482d 100644 --- a/components/ui/toast/ToastDescription.vue +++ b/components/ui/toast/ToastDescription.vue @@ -1,19 +1,19 @@ + + - - diff --git a/components/ui/toast/ToastProvider.vue b/components/ui/toast/ToastProvider.vue index 340cbd83..f9bcd387 100644 --- a/components/ui/toast/ToastProvider.vue +++ b/components/ui/toast/ToastProvider.vue @@ -1,11 +1,11 @@ - - + + diff --git a/components/ui/toast/ToastTitle.vue b/components/ui/toast/ToastTitle.vue index 0a1dffbe..1e9bd4d0 100644 --- a/components/ui/toast/ToastTitle.vue +++ b/components/ui/toast/ToastTitle.vue @@ -1,19 +1,19 @@ + + - - diff --git a/components/ui/toast/ToastViewport.vue b/components/ui/toast/ToastViewport.vue index 57deec81..56f1cccf 100644 --- a/components/ui/toast/ToastViewport.vue +++ b/components/ui/toast/ToastViewport.vue @@ -1,17 +1,17 @@ + + - - diff --git a/components/ui/toast/Toaster.vue b/components/ui/toast/Toaster.vue index f86ce40b..2efe5622 100644 --- a/components/ui/toast/Toaster.vue +++ b/components/ui/toast/Toaster.vue @@ -1,11 +1,3 @@ - - + + diff --git a/components/ui/toast/index.ts b/components/ui/toast/index.ts index a1949157..233d3f20 100644 --- a/components/ui/toast/index.ts +++ b/components/ui/toast/index.ts @@ -1,20 +1,20 @@ -import type { ToastRootProps } from 'radix-vue' -import type { HTMLAttributes } from 'vue' +import type { ToastRootProps } from 'radix-vue'; +import type { HTMLAttributes } from 'vue'; -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' -export { toast, useToast } from './use-toast' +import { type VariantProps, cva } from 'class-variance-authority'; -import { type VariantProps, cva } from 'class-variance-authority' +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'; +export { toast, useToast } from './use-toast'; 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-[--radix-toast-swipe-end-x] data-[swipe=move]:translate-x-[--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', + 'group pointer-events-auto relative flex w-full items-center justify-between space-x-2 overflow-hidden rounded-md border p-4 pr-6 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: { @@ -27,12 +27,12 @@ export const toastVariants = cva( variant: 'default', }, }, -) +); -type ToastVariants = VariantProps +type ToastVariants = VariantProps; export interface ToastProps extends ToastRootProps { - class?: HTMLAttributes['class'] - variant?: ToastVariants['variant'] - onOpenChange?: ((value: boolean) => void) | undefined + class?: HTMLAttributes['class']; + variant?: ToastVariants['variant']; + onOpenChange?: ((value: boolean) => void) | undefined; } diff --git a/components/ui/toast/use-toast.ts b/components/ui/toast/use-toast.ts index 9ad9eea9..6d610f13 100644 --- a/components/ui/toast/use-toast.ts +++ b/components/ui/toast/use-toast.ts @@ -2,8 +2,8 @@ import { computed, ref } from 'vue'; import type { Component, VNode } from 'vue'; import type { ToastProps } from '.'; -const TOAST_LIMIT = 5; -const TOAST_REMOVE_DELAY = 3000; +const TOAST_LIMIT = 1; +const TOAST_REMOVE_DELAY = 1000000; export type StringOrVNode = | string @@ -57,7 +57,7 @@ interface State { const toastTimeouts = new Map>(); -function addToRemoveQueue(toastId: string, duration: number) { +function addToRemoveQueue(toastId: string) { if (toastTimeouts.has(toastId)) return; @@ -67,7 +67,7 @@ function addToRemoveQueue(toastId: string, duration: number) { type: actionTypes.REMOVE_TOAST, toastId, }); - }, duration); + }, TOAST_REMOVE_DELAY); toastTimeouts.set(toastId, timeout); } @@ -92,13 +92,10 @@ function dispatch(action: Action) { const { toastId } = action; if (toastId) { - const toast = state.value.toasts.find(t => t.id === toastId); - if (toast) { - addToRemoveQueue(toastId, toast.duration ?? TOAST_REMOVE_DELAY); - } + addToRemoveQueue(toastId); } else { state.value.toasts.forEach((toast) => { - addToRemoveQueue(toast.id, toast.duration ?? TOAST_REMOVE_DELAY); + addToRemoveQueue(toast.id); }); } @@ -114,7 +111,11 @@ function dispatch(action: Action) { } case actionTypes.REMOVE_TOAST: - state.value.toasts = state.value.toasts.filter(t => t.id !== action.toastId); + if (action.toastId === undefined) + state.value.toasts = []; + else + state.value.toasts = state.value.toasts.filter(t => t.id !== action.toastId); + break; } } @@ -127,12 +128,12 @@ function useToast() { }; } -type Toast = Omit & { duration?: number }; +type Toast = Omit; function toast(props: Toast) { const id = genId(); - const update = (props: Partial) => + const update = (props: ToasterToast) => dispatch({ type: actionTypes.UPDATE_TOAST, toast: { ...props, id }, @@ -153,10 +154,6 @@ function toast(props: Toast) { }, }); - setTimeout(() => { - dismiss(); - }, props.duration ?? TOAST_REMOVE_DELAY); - return { id, dismiss, @@ -164,4 +161,4 @@ function toast(props: Toast) { }; } -export { state, useToast, toast }; +export { toast, useToast }; diff --git a/composables/useConfig.ts b/composables/useConfig.ts index 9ffba569..46c7111b 100644 --- a/composables/useConfig.ts +++ b/composables/useConfig.ts @@ -38,6 +38,7 @@ const defaultConfig: { main: { breadCrumb: boolean; showTitle: boolean; + codeCopyToast: boolean; codeIcon: { [key: string]: string; }; @@ -89,6 +90,7 @@ const defaultConfig: { main: { breadCrumb: true, showTitle: true, + codeCopyToast: true, codeIcon: { 'package.json': 'vscode-icons:file-type-node', 'tsconfig.json': 'vscode-icons:file-type-tsconfig', diff --git a/package.json b/package.json index e27c025a..3cc93ebe 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "shadcn-docs-nuxt", "type": "module", - "version": "0.2.0", + "version": "0.2.1", "author": "Tony Zhang ", "license": "MIT", "homepage": "https://shadcn-docs-nuxt.vercel.app/",