From d827f9065828de357a3d1313bf11a5e476e4c234 Mon Sep 17 00:00:00 2001 From: Ilya Artamonov Date: Mon, 23 Oct 2023 18:15:53 +0300 Subject: [PATCH 1/3] refactor: Toast rework started --- .../examples/SlotListenerExample.vue | 37 ++-- .../examples/FwbToastProviderExample.vue | 107 ++++++--- .../examples/FwbToastProviderExampleChild.vue | 84 -------- .../toastProvider/examples/UpdateToast.vue | 74 ++++--- src/components/FwbToast/FwbToastProvider.vue | 203 ++++-------------- .../FwbToast/composables/useToast.ts | 61 +++--- .../FwbToast/composables/useToastClasses.ts | 9 +- src/components/FwbToast/types.ts | 8 +- 8 files changed, 209 insertions(+), 374 deletions(-) delete mode 100644 docs/components/toastProvider/examples/FwbToastProviderExampleChild.vue diff --git a/docs/components/PLAYGROUND/examples/SlotListenerExample.vue b/docs/components/PLAYGROUND/examples/SlotListenerExample.vue index ee9a2d3b..479e7cd8 100644 --- a/docs/components/PLAYGROUND/examples/SlotListenerExample.vue +++ b/docs/components/PLAYGROUND/examples/SlotListenerExample.vue @@ -1,22 +1,25 @@ diff --git a/docs/components/toastProvider/examples/FwbToastProviderExample.vue b/docs/components/toastProvider/examples/FwbToastProviderExample.vue index ca71bb78..2973ca4e 100644 --- a/docs/components/toastProvider/examples/FwbToastProviderExample.vue +++ b/docs/components/toastProvider/examples/FwbToastProviderExample.vue @@ -1,40 +1,85 @@ diff --git a/docs/components/toastProvider/examples/FwbToastProviderExampleChild.vue b/docs/components/toastProvider/examples/FwbToastProviderExampleChild.vue deleted file mode 100644 index 7fa07db5..00000000 --- a/docs/components/toastProvider/examples/FwbToastProviderExampleChild.vue +++ /dev/null @@ -1,84 +0,0 @@ - - - diff --git a/docs/components/toastProvider/examples/UpdateToast.vue b/docs/components/toastProvider/examples/UpdateToast.vue index 6f7be76b..04f66db4 100644 --- a/docs/components/toastProvider/examples/UpdateToast.vue +++ b/docs/components/toastProvider/examples/UpdateToast.vue @@ -1,45 +1,43 @@ diff --git a/src/components/FwbToast/FwbToastProvider.vue b/src/components/FwbToast/FwbToastProvider.vue index 6481908f..7e1d3aff 100644 --- a/src/components/FwbToast/FwbToastProvider.vue +++ b/src/components/FwbToast/FwbToastProvider.vue @@ -1,169 +1,40 @@ - - - + diff --git a/src/components/FwbToast/composables/useToast.ts b/src/components/FwbToast/composables/useToast.ts index 0394c84b..652e3330 100644 --- a/src/components/FwbToast/composables/useToast.ts +++ b/src/components/FwbToast/composables/useToast.ts @@ -1,38 +1,39 @@ -import { inject } from 'vue' -import type { ToastItem, UseToastInjection } from '../types' -import { FLOWBITE_TOAST_INJECTION_KEY } from '../injection/config' - -export function useToast (): UseToastInjection { - const injection = inject(FLOWBITE_TOAST_INJECTION_KEY, null) - if (injection === null) console.warn('Cannot use useToast outside component. Please wrap your component with ') - - const add = (toast: ToastItem): string => { - if (!injection) { - return '' +import { ref, type Ref, type ShallowRef } from 'vue' +import { type ToastAlign, type ToastType } from '@/components/FwbToast/types' +import { nanoid } from 'nanoid' +import { useTimeoutFn } from '@vueuse/core' + +interface ToastItem { + id?: string + text: string, + time: number, + component?: ShallowRef + componentAttrs: { + type: ToastType, + alignment: ToastAlign, + closable: boolean, + divide: boolean, } - - return injection?.add(toast) - } - - const remove = (id: string): boolean => { - if (!injection) { - return false +} +const toasts:Ref = ref([]) +export function useToast () { + function addToast (item: ToastItem) { + const toastId = nanoid() + toasts.value.push({ + ...item, + id: toastId, + }) + if (item.time && item.time > 0) { + useTimeoutFn(() => removeToast(toastId), item.time) } - - return injection?.remove(id) } - - const pop = (): string => { - if (!injection) { - return '' - } - - return injection?.pop() + function removeToast (id: string) { + toasts.value = toasts.value.filter(el => el.id !== id) } return { - add, - remove, - pop, + addToast, + removeToast, + toasts, } } diff --git a/src/components/FwbToast/composables/useToastClasses.ts b/src/components/FwbToast/composables/useToastClasses.ts index 314a713a..16bbfff5 100644 --- a/src/components/FwbToast/composables/useToastClasses.ts +++ b/src/components/FwbToast/composables/useToastClasses.ts @@ -1,6 +1,7 @@ import { computed, type Ref } from 'vue' import type { ToastAlign, ToastType } from '../types' import { simplifyTailwindClasses } from '@/utils/simplifyTailwindClasses' +import { twMerge } from 'tailwind-merge' type UseToastClassesReturns = { typeClasses: Ref @@ -26,7 +27,7 @@ const wrapperAlignmentClasses: Record = { end: 'items-end', start: 'items-start', } -const defaultWrapperClasses = 'flex w-full max-w-xs p-4 text-gray-500 bg-white rounded-lg shadow dark:text-gray-400 dark:bg-gray-800' +const defaultWrapperClasses = 'flex items-center w-full max-w-xs p-4 text-gray-500 bg-white rounded-lg shadow dark:text-gray-400 dark:bg-gray-800' const defaultContentClasses = 'text-sm font-normal' @@ -36,15 +37,15 @@ export function useToastClasses (props: UseToastClassesProps): UseToastClassesRe const wrapperClasses = computed(() => { const alignmentClass = wrapperAlignmentClasses[props.alignment.value] if (props.divide.value) { - return simplifyTailwindClasses(defaultWrapperClasses, 'dark:divide-gray-700 divide-x divide-gray-200', alignmentClass) + return twMerge(defaultWrapperClasses, 'dark:divide-gray-700 divide-x divide-gray-200', alignmentClass) } - return simplifyTailwindClasses(defaultWrapperClasses, alignmentClass) + return twMerge(defaultWrapperClasses, alignmentClass) }) const contentClasses = computed(() => { if (props.type.value !== 'empty' && props.divide.value) { - return simplifyTailwindClasses(defaultContentClasses, 'pl-3') + return twMerge(defaultContentClasses, 'pl-3') } return defaultContentClasses diff --git a/src/components/FwbToast/types.ts b/src/components/FwbToast/types.ts index 872aa361..f2bf1b81 100644 --- a/src/components/FwbToast/types.ts +++ b/src/components/FwbToast/types.ts @@ -4,10 +4,10 @@ export type ToastAlign = 'start' | 'center' | 'end' export type ToastType = 'success' | 'warning' | 'danger' | 'empty' export type ToastItem = { - time: number // ms - type: ToastType - text: string - component?: DefineComponent + time?: number // ms + type?: ToastType + text?: string + component?: string componentProps?: Record } From 75da14e61f72c9f8d4f5836e6958a2855d8991ef Mon Sep 17 00:00:00 2001 From: Ilya Artamonov Date: Mon, 23 Oct 2023 18:15:53 +0300 Subject: [PATCH 2/3] feat: Updated toast --- docs/components/toast.md | 86 ++-------- .../toast/examples/FwbToastExample.vue | 80 ++++++++- .../examples/FwbToastExampleClosable.vue | 40 ++--- .../toast/examples/FwbToastExampleDivide.vue | 38 ++--- .../toast/examples/FwbToastExampleIcon.vue | 63 ------- .../examples/FwbToastExampleInteractive.vue | 77 ++++----- .../toast/examples/FwbToastExampleMessage.vue | 2 +- .../examples/FwbToastProviderExample.vue | 18 +- .../toastProvider/examples/UpdateToast.vue | 14 +- .../components/toastProvider/toastProvider.md | 4 - src/components/FwbToast/FwbToast.vue | 156 +++++++----------- .../FwbToast/composables/useToast.ts | 3 +- .../FwbToast/composables/useToastClasses.ts | 36 ++-- src/components/FwbToast/injection/config.ts | 1 - src/components/FwbToast/types.ts | 30 ++-- 15 files changed, 291 insertions(+), 357 deletions(-) delete mode 100644 docs/components/toast/examples/FwbToastExampleIcon.vue delete mode 100644 src/components/FwbToast/injection/config.ts diff --git a/docs/components/toast.md b/docs/components/toast.md index 313b171f..a09fe7f0 100644 --- a/docs/components/toast.md +++ b/docs/components/toast.md @@ -1,11 +1,11 @@ + # Vue Toast - Flowbite #### Push notifications to your users using the toast component and choose from multiple sizes, colors, styles, and positions @@ -18,21 +18,22 @@ Original reference: [https://flowbite.com/docs/components/toast/](https://flowbi The toast component can be used to enhance your website’s interactivity by pushing notifications to your visitors. You can choose from multiple styles, colors, sizes, and positions and even dismiss the component. -## Prop - type +## Colors + ```vue @@ -42,23 +43,15 @@ import { FwbToast } from 'flowbite-vue' ``` -## Prop - closable +## Dismissable + ```vue ``` -## Prop - divide +## Divide + ```vue ``` - -## Slot - icon - -You can use icon slot for rendering your own icons. Also you can change icon background color by using [FlowbiteThemable](/components/flowbiteThemable/flowbiteThemable.md) - - -```vue - - - -``` diff --git a/docs/components/toast/examples/FwbToastExample.vue b/docs/components/toast/examples/FwbToastExample.vue index bd22956a..6d8ed3d0 100644 --- a/docs/components/toast/examples/FwbToastExample.vue +++ b/docs/components/toast/examples/FwbToastExample.vue @@ -1,15 +1,87 @@