akar 2.0.20 → 2.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Autocomplete/AutocompleteRoot.cjs +8 -2
- package/dist/Autocomplete/AutocompleteRoot.cjs.map +1 -1
- package/dist/Autocomplete/AutocompleteRoot.js +8 -2
- package/dist/Autocomplete/AutocompleteRoot.js.map +1 -1
- package/dist/ColorSwatchPicker/ColorSwatchPickerRoot.cjs +5 -0
- package/dist/ColorSwatchPicker/ColorSwatchPickerRoot.cjs.map +1 -1
- package/dist/ColorSwatchPicker/ColorSwatchPickerRoot.js +5 -0
- package/dist/ColorSwatchPicker/ColorSwatchPickerRoot.js.map +1 -1
- package/dist/Combobox/ComboboxRoot.cjs +7 -1
- package/dist/Combobox/ComboboxRoot.cjs.map +1 -1
- package/dist/Combobox/ComboboxRoot.js +7 -1
- package/dist/Combobox/ComboboxRoot.js.map +1 -1
- package/dist/ContextMenu/ContextMenuRoot.cjs +12 -6
- package/dist/ContextMenu/ContextMenuRoot.cjs.map +1 -1
- package/dist/ContextMenu/ContextMenuRoot.js +13 -7
- package/dist/ContextMenu/ContextMenuRoot.js.map +1 -1
- package/dist/ContextMenu/ContextMenuTrigger.cjs +18 -2
- package/dist/ContextMenu/ContextMenuTrigger.cjs.map +1 -1
- package/dist/ContextMenu/ContextMenuTrigger.js +19 -3
- package/dist/ContextMenu/ContextMenuTrigger.js.map +1 -1
- package/dist/DateField/DateFieldRoot.cjs +1 -0
- package/dist/DateField/DateFieldRoot.cjs.map +1 -1
- package/dist/DateField/DateFieldRoot.js +1 -0
- package/dist/DateField/DateFieldRoot.js.map +1 -1
- package/dist/DateRangeField/DateRangeFieldRoot.cjs +1 -0
- package/dist/DateRangeField/DateRangeFieldRoot.cjs.map +1 -1
- package/dist/DateRangeField/DateRangeFieldRoot.js +1 -0
- package/dist/DateRangeField/DateRangeFieldRoot.js.map +1 -1
- package/dist/DismissableLayer/DismissableLayer.cjs +1 -1
- package/dist/DismissableLayer/DismissableLayer.cjs.map +1 -1
- package/dist/DismissableLayer/DismissableLayer.js +1 -1
- package/dist/DismissableLayer/DismissableLayer.js.map +1 -1
- package/dist/DismissableLayer/utils.cjs +8 -3
- package/dist/DismissableLayer/utils.cjs.map +1 -1
- package/dist/DismissableLayer/utils.js +8 -3
- package/dist/DismissableLayer/utils.js.map +1 -1
- package/dist/Drawer/DrawerContent.cjs +2 -1
- package/dist/Drawer/DrawerContent.cjs.map +1 -1
- package/dist/Drawer/DrawerContent.js +2 -1
- package/dist/Drawer/DrawerContent.js.map +1 -1
- package/dist/Drawer/DrawerContentImpl.cjs +10 -4
- package/dist/Drawer/DrawerContentImpl.cjs.map +1 -1
- package/dist/Drawer/DrawerContentImpl.js +11 -5
- package/dist/Drawer/DrawerContentImpl.js.map +1 -1
- package/dist/Drawer/DrawerRoot.cjs +14 -3
- package/dist/Drawer/DrawerRoot.cjs.map +1 -1
- package/dist/Drawer/DrawerRoot.js +14 -3
- package/dist/Drawer/DrawerRoot.js.map +1 -1
- package/dist/Drawer/DrawerSwipeArea.cjs +5 -2
- package/dist/Drawer/DrawerSwipeArea.cjs.map +1 -1
- package/dist/Drawer/DrawerSwipeArea.js +6 -3
- package/dist/Drawer/DrawerSwipeArea.js.map +1 -1
- package/dist/DropdownMenu/DropdownMenuTrigger.cjs +2 -1
- package/dist/DropdownMenu/DropdownMenuTrigger.cjs.map +1 -1
- package/dist/DropdownMenu/DropdownMenuTrigger.js +2 -1
- package/dist/DropdownMenu/DropdownMenuTrigger.js.map +1 -1
- package/dist/Listbox/ListboxRoot.cjs +25 -2
- package/dist/Listbox/ListboxRoot.cjs.map +1 -1
- package/dist/Listbox/ListboxRoot.js +25 -2
- package/dist/Listbox/ListboxRoot.js.map +1 -1
- package/dist/Menubar/MenubarTrigger.cjs +2 -1
- package/dist/Menubar/MenubarTrigger.cjs.map +1 -1
- package/dist/Menubar/MenubarTrigger.js +2 -1
- package/dist/Menubar/MenubarTrigger.js.map +1 -1
- package/dist/NavigationMenu/NavigationMenuTrigger.cjs +3 -2
- package/dist/NavigationMenu/NavigationMenuTrigger.cjs.map +1 -1
- package/dist/NavigationMenu/NavigationMenuTrigger.js +3 -2
- package/dist/NavigationMenu/NavigationMenuTrigger.js.map +1 -1
- package/dist/NumberField/NumberFieldRoot.cjs +20 -4
- package/dist/NumberField/NumberFieldRoot.cjs.map +1 -1
- package/dist/NumberField/NumberFieldRoot.js +20 -4
- package/dist/NumberField/NumberFieldRoot.js.map +1 -1
- package/dist/PinInput/PinInputRoot.cjs +1 -0
- package/dist/PinInput/PinInputRoot.cjs.map +1 -1
- package/dist/PinInput/PinInputRoot.js +1 -0
- package/dist/PinInput/PinInputRoot.js.map +1 -1
- package/dist/RovingFocus/RovingFocusItem.cjs +7 -2
- package/dist/RovingFocus/RovingFocusItem.cjs.map +1 -1
- package/dist/RovingFocus/RovingFocusItem.js +7 -2
- package/dist/RovingFocus/RovingFocusItem.js.map +1 -1
- package/dist/Select/SelectContent.cjs +4 -0
- package/dist/Select/SelectContent.cjs.map +1 -1
- package/dist/Select/SelectContent.js +4 -0
- package/dist/Select/SelectContent.js.map +1 -1
- package/dist/Select/SelectContentImpl.cjs +7 -2
- package/dist/Select/SelectContentImpl.cjs.map +1 -1
- package/dist/Select/SelectContentImpl.js +9 -4
- package/dist/Select/SelectContentImpl.js.map +1 -1
- package/dist/Select/SelectTrigger.cjs +24 -15
- package/dist/Select/SelectTrigger.cjs.map +1 -1
- package/dist/Select/SelectTrigger.js +25 -16
- package/dist/Select/SelectTrigger.js.map +1 -1
- package/dist/Tabs/TabsTrigger.cjs +4 -1
- package/dist/Tabs/TabsTrigger.cjs.map +1 -1
- package/dist/Tabs/TabsTrigger.js +4 -1
- package/dist/Tabs/TabsTrigger.js.map +1 -1
- package/dist/TimeField/TimeFieldRoot.cjs +1 -0
- package/dist/TimeField/TimeFieldRoot.cjs.map +1 -1
- package/dist/TimeField/TimeFieldRoot.js +1 -0
- package/dist/TimeField/TimeFieldRoot.js.map +1 -1
- package/dist/TimeRangeField/TimeRangeFieldRoot.cjs +1 -0
- package/dist/TimeRangeField/TimeRangeFieldRoot.cjs.map +1 -1
- package/dist/TimeRangeField/TimeRangeFieldRoot.js +1 -0
- package/dist/TimeRangeField/TimeRangeFieldRoot.js.map +1 -1
- package/dist/Toast/ToastAction.cjs +14 -6
- package/dist/Toast/ToastAction.cjs.map +1 -1
- package/dist/Toast/ToastAction.js +15 -7
- package/dist/Toast/ToastAction.js.map +1 -1
- package/dist/Toast/ToastArrow.cjs +53 -0
- package/dist/Toast/ToastArrow.cjs.map +1 -0
- package/dist/Toast/ToastArrow.js +47 -0
- package/dist/Toast/ToastArrow.js.map +1 -0
- package/dist/Toast/ToastDescription.cjs +5 -3
- package/dist/Toast/ToastDescription.cjs.map +1 -1
- package/dist/Toast/ToastDescription.js +6 -4
- package/dist/Toast/ToastDescription.js.map +1 -1
- package/dist/Toast/ToastPositioner.cjs +166 -0
- package/dist/Toast/ToastPositioner.cjs.map +1 -0
- package/dist/Toast/ToastPositioner.js +154 -0
- package/dist/Toast/ToastPositioner.js.map +1 -0
- package/dist/Toast/ToastProvider.cjs +74 -3
- package/dist/Toast/ToastProvider.cjs.map +1 -1
- package/dist/Toast/ToastProvider.js +75 -4
- package/dist/Toast/ToastProvider.js.map +1 -1
- package/dist/Toast/ToastRoot.cjs +38 -15
- package/dist/Toast/ToastRoot.cjs.map +1 -1
- package/dist/Toast/ToastRoot.js +39 -16
- package/dist/Toast/ToastRoot.js.map +1 -1
- package/dist/Toast/ToastRootImpl.cjs +69 -12
- package/dist/Toast/ToastRootImpl.cjs.map +1 -1
- package/dist/Toast/ToastRootImpl.js +74 -17
- package/dist/Toast/ToastRootImpl.js.map +1 -1
- package/dist/Toast/ToastTitle.cjs +5 -3
- package/dist/Toast/ToastTitle.cjs.map +1 -1
- package/dist/Toast/ToastTitle.js +6 -4
- package/dist/Toast/ToastTitle.js.map +1 -1
- package/dist/Toast/ToastViewport.cjs +40 -5
- package/dist/Toast/ToastViewport.cjs.map +1 -1
- package/dist/Toast/ToastViewport.js +41 -6
- package/dist/Toast/ToastViewport.js.map +1 -1
- package/dist/Toast/createToastManager.cjs +119 -0
- package/dist/Toast/createToastManager.cjs.map +1 -0
- package/dist/Toast/createToastManager.js +95 -0
- package/dist/Toast/createToastManager.js.map +1 -0
- package/dist/Toast/useToastManager.cjs +25 -0
- package/dist/Toast/useToastManager.cjs.map +1 -0
- package/dist/Toast/useToastManager.js +20 -0
- package/dist/Toast/useToastManager.js.map +1 -0
- package/dist/Toast/useToastStore.cjs +85 -0
- package/dist/Toast/useToastStore.cjs.map +1 -0
- package/dist/Toast/useToastStore.js +79 -0
- package/dist/Toast/useToastStore.js.map +1 -0
- package/dist/Toast/utils.cjs +2 -4
- package/dist/Toast/utils.cjs.map +1 -1
- package/dist/Toast/utils.js +2 -3
- package/dist/Toast/utils.js.map +1 -1
- package/dist/Tree/TreeRoot.cjs +9 -3
- package/dist/Tree/TreeRoot.cjs.map +1 -1
- package/dist/Tree/TreeRoot.js +9 -3
- package/dist/Tree/TreeRoot.js.map +1 -1
- package/dist/Tree/TreeVirtualizer.cjs +10 -1
- package/dist/Tree/TreeVirtualizer.cjs.map +1 -1
- package/dist/Tree/TreeVirtualizer.js +11 -2
- package/dist/Tree/TreeVirtualizer.js.map +1 -1
- package/dist/VisuallyHidden/VisuallyHidden.cjs +1 -1
- package/dist/VisuallyHidden/VisuallyHidden.cjs.map +1 -1
- package/dist/VisuallyHidden/VisuallyHidden.js +1 -1
- package/dist/VisuallyHidden/VisuallyHidden.js.map +1 -1
- package/dist/composables/useSwipeDismiss.cjs +26 -6
- package/dist/composables/useSwipeDismiss.cjs.map +1 -1
- package/dist/composables/useSwipeDismiss.js +26 -6
- package/dist/composables/useSwipeDismiss.js.map +1 -1
- package/dist/constant/components.cjs +6 -2
- package/dist/constant/components.cjs.map +1 -1
- package/dist/constant/components.js +6 -2
- package/dist/constant/components.js.map +1 -1
- package/dist/constant.d.cts +2 -2
- package/dist/constant.d.cts.map +1 -1
- package/dist/constant.d.ts +2 -2
- package/dist/constant.d.ts.map +1 -1
- package/dist/index.cjs +10 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +7 -1
- package/dist/index2.d.cts +33 -21
- package/dist/index2.d.cts.map +1 -1
- package/dist/index2.d.ts +21 -9
- package/dist/index2.d.ts.map +1 -1
- package/dist/index3.d.cts +1685 -1437
- package/dist/index3.d.cts.map +1 -1
- package/dist/index3.d.ts +1681 -1433
- package/dist/index3.d.ts.map +1 -1
- package/dist/internal.cjs +1 -0
- package/dist/internal.d.cts +3 -3
- package/dist/internal.d.cts.map +1 -1
- package/dist/internal.d.ts +3 -3
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +1 -0
- package/dist/namespaced/index.cjs +3 -1
- package/dist/namespaced/index.d.cts +3 -1
- package/dist/namespaced/index.d.mts +3 -1
- package/dist/namespaced/index.mjs +4 -2
- package/dist/shared/isHTMLElement.cjs +16 -0
- package/dist/shared/isHTMLElement.cjs.map +1 -0
- package/dist/shared/isHTMLElement.js +10 -0
- package/dist/shared/isHTMLElement.js.map +1 -0
- package/dist/shared/useForwardPropsEmits.cjs.map +1 -1
- package/dist/shared/useForwardPropsEmits.js.map +1 -1
- package/dist/shared.cjs +2 -0
- package/dist/shared.d.cts +2 -2
- package/dist/shared.d.ts +2 -2
- package/dist/shared.js +2 -1
- package/package.json +18 -18
- package/src/Autocomplete/AutocompleteRoot.vue +4 -1
- package/src/Combobox/ComboboxRoot.vue +2 -1
- package/src/ContextMenu/ContextMenuRoot.vue +8 -7
- package/src/ContextMenu/ContextMenuTrigger.vue +37 -3
- package/src/DateField/DateFieldRoot.vue +1 -0
- package/src/DatePicker/DatePickerContent.vue +1 -1
- package/src/DateRangeField/DateRangeFieldRoot.vue +1 -0
- package/src/DateRangePicker/DateRangePickerContent.vue +1 -1
- package/src/DismissableLayer/DismissableLayer.vue +4 -0
- package/src/DismissableLayer/utils.ts +20 -7
- package/src/Drawer/DrawerContent.vue +2 -2
- package/src/Drawer/DrawerContentImpl.vue +19 -8
- package/src/Drawer/DrawerRoot.vue +23 -5
- package/src/Drawer/DrawerSwipeArea.vue +10 -3
- package/src/Drawer/composables/useSwipeDismiss.ts +51 -5
- package/src/DropdownMenu/DropdownMenuTrigger.vue +2 -1
- package/src/Listbox/ListboxRoot.vue +30 -2
- package/src/Menubar/MenubarTrigger.vue +2 -1
- package/src/NavigationMenu/NavigationMenuTrigger.vue +7 -2
- package/src/NumberField/NumberFieldRoot.vue +38 -5
- package/src/PinInput/PinInputRoot.vue +1 -0
- package/src/RovingFocus/RovingFocusItem.vue +13 -2
- package/src/Select/SelectContentImpl.vue +13 -3
- package/src/Select/SelectTrigger.vue +52 -21
- package/src/Tabs/TabsTrigger.vue +4 -1
- package/src/TimeField/TimeFieldRoot.vue +1 -0
- package/src/TimeRangeField/TimeRangeFieldRoot.vue +1 -0
- package/src/Toast/ToastAction.vue +27 -14
- package/src/Toast/ToastArrow.vue +37 -0
- package/src/Toast/ToastDescription.vue +8 -2
- package/src/Toast/ToastPositioner.vue +76 -0
- package/src/Toast/ToastProvider.vue +113 -5
- package/src/Toast/ToastRoot.vue +39 -8
- package/src/Toast/ToastRootImpl.vue +67 -17
- package/src/Toast/ToastTitle.vue +8 -2
- package/src/Toast/ToastViewport.vue +41 -3
- package/src/Toast/createToastManager.ts +207 -0
- package/src/Toast/index.ts +15 -0
- package/src/Toast/useToastManager.ts +23 -0
- package/src/Toast/useToastStore.ts +90 -0
- package/src/Toast/utils.ts +2 -4
- package/src/Tree/TreeRoot.vue +6 -1
- package/src/Tree/TreeVirtualizer.vue +17 -2
- package/src/VisuallyHidden/VisuallyHidden.vue +11 -1
- package/src/internal.ts +2 -1
- package/src/shared/index.ts +1 -0
- package/src/shared/isHTMLElement.ts +9 -0
- package/src/shared/useForwardPropsEmits.ts +12 -5
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
+
import type { Ref } from 'vue';
|
|
3
|
+
import type { ToastObject } from './createToastManager';
|
|
2
4
|
import type { SwipeEvent } from './utils';
|
|
3
5
|
import type { PrimitiveProps } from '@/Primitive';
|
|
4
6
|
import { isClient } from '@vueuse/shared';
|
|
@@ -39,18 +41,23 @@ export interface ToastRootImplProps extends PrimitiveProps {
|
|
|
39
41
|
* given to `ToastProvider`.
|
|
40
42
|
*/
|
|
41
43
|
duration?: number;
|
|
44
|
+
/**
|
|
45
|
+
* A toast from `useToastManager()`. Its `open`, `duration`, `type` and `status` drive the toast,
|
|
46
|
+
* and `ToastTitle`, `ToastDescription` and `ToastAction` render its content.
|
|
47
|
+
*/
|
|
48
|
+
toast?: ToastObject;
|
|
42
49
|
}
|
|
43
50
|
|
|
44
51
|
export const [injectToastRootContext, provideToastRootContext]
|
|
45
|
-
= createContext<{ onClose: () => void }>('ToastRoot');
|
|
52
|
+
= createContext<{ onClose: () => void; toast: Ref<ToastObject | undefined> }>('ToastRoot');
|
|
46
53
|
</script>
|
|
47
54
|
|
|
48
55
|
<script setup lang="ts">
|
|
49
|
-
import {
|
|
50
|
-
import {
|
|
51
|
-
import { computed, onMounted, onUnmounted, ref, watch, watchEffect } from 'vue';
|
|
56
|
+
import { onKeyStroke, useRafFn, useResizeObserver } from '@vueuse/core';
|
|
57
|
+
import { computed, onMounted, onUnmounted, reactive, ref, shallowRef, toRef, watch, watchEffect } from 'vue';
|
|
52
58
|
import { Primitive } from '@/Primitive';
|
|
53
59
|
import ToastAnnounce from './ToastAnnounce.vue';
|
|
60
|
+
import { injectToastPositionerContext } from './ToastPositioner.vue';
|
|
54
61
|
import { injectToastProviderContext } from './ToastProvider.vue';
|
|
55
62
|
import { getAnnounceTextContent, handleAndDispatchCustomEvent, isDeltaInDirection, TOAST_SWIPE_CANCEL, TOAST_SWIPE_END, TOAST_SWIPE_MOVE, TOAST_SWIPE_START, VIEWPORT_PAUSE, VIEWPORT_RESUME } from './utils';
|
|
56
63
|
|
|
@@ -72,7 +79,7 @@ const providerContext = injectToastProviderContext();
|
|
|
72
79
|
const pointerStartRef = ref<{ x: number; y: number } | null>(null);
|
|
73
80
|
const swipeDeltaRef = ref<{ x: number; y: number } | null>(null);
|
|
74
81
|
const duration = computed(
|
|
75
|
-
() =>
|
|
82
|
+
() => typeof props.duration === 'number'
|
|
76
83
|
? props.duration
|
|
77
84
|
: providerContext.duration.value,
|
|
78
85
|
);
|
|
@@ -105,7 +112,7 @@ function startTimer(duration: number) {
|
|
|
105
112
|
function handleClose(event?: PointerEvent) {
|
|
106
113
|
const isNonPointerEvent = event?.pointerType === '';
|
|
107
114
|
|
|
108
|
-
// update to only perform focus when user focus via keyboard
|
|
115
|
+
// akar: update to only perform focus when user focus via keyboard
|
|
109
116
|
// focus viewport if focus is within toast to read the remaining toast
|
|
110
117
|
// count to SR users and ensure focus isn't lost
|
|
111
118
|
const isFocusInToast = currentElement.value?.contains(getActiveElement());
|
|
@@ -121,7 +128,39 @@ function handleClose(event?: PointerEvent) {
|
|
|
121
128
|
emits('close');
|
|
122
129
|
}
|
|
123
130
|
|
|
124
|
-
const
|
|
131
|
+
const positionerContext = injectToastPositionerContext(null);
|
|
132
|
+
|
|
133
|
+
// Read after the DOM updates, and again when a managed toast changes, so that an
|
|
134
|
+
// updated toast (e.g. a promise going from loading to success) is announced again.
|
|
135
|
+
const announceTextContent = shallowRef<Array<string> | null>(null);
|
|
136
|
+
watch([currentElement, () => props.toast?.updateKey], ([element]) => {
|
|
137
|
+
announceTextContent.value = element ? getAnnounceTextContent(element) : null;
|
|
138
|
+
}, { flush: 'post', immediate: true });
|
|
139
|
+
|
|
140
|
+
const stackEntry = reactive({ seq: 0, height: 0, open: props.open });
|
|
141
|
+
const stack = computed(() => providerContext.getToastStack(stackEntry));
|
|
142
|
+
|
|
143
|
+
function measureHeight() {
|
|
144
|
+
const element = currentElement.value;
|
|
145
|
+
if (!element) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
// Measure the natural height, even when styles collapse the toast to the frontmost height.
|
|
149
|
+
const previousHeight = element.style.height;
|
|
150
|
+
element.style.height = 'auto';
|
|
151
|
+
stackEntry.height = element.offsetHeight;
|
|
152
|
+
element.style.height = previousHeight;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
useResizeObserver(currentElement, measureHeight);
|
|
156
|
+
watch(() => props.toast?.updateKey, measureHeight, { flush: 'post' });
|
|
157
|
+
|
|
158
|
+
watch(() => props.open, (open, wasOpen) => {
|
|
159
|
+
stackEntry.open = open;
|
|
160
|
+
if (open && !wasOpen) {
|
|
161
|
+
providerContext.reopenToast(stackEntry);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
125
164
|
|
|
126
165
|
if (props.type && !['foreground', 'background'].includes(props.type)) {
|
|
127
166
|
const error = 'Invalid prop `type` supplied to `Toast`. Expected `foreground | background`.';
|
|
@@ -136,7 +175,6 @@ watchEffect((cleanupFn) => {
|
|
|
136
175
|
remainingRaf.resume();
|
|
137
176
|
emits('resume');
|
|
138
177
|
};
|
|
139
|
-
|
|
140
178
|
const handlePause = () => {
|
|
141
179
|
const elapsedTime = Date.now() - closeTimerStartTimeRef.value;
|
|
142
180
|
closeTimerRemainingTimeRef.value = closeTimerRemainingTimeRef.value - elapsedTime;
|
|
@@ -144,10 +182,8 @@ watchEffect((cleanupFn) => {
|
|
|
144
182
|
remainingRaf.pause();
|
|
145
183
|
emits('pause');
|
|
146
184
|
};
|
|
147
|
-
|
|
148
185
|
viewport.addEventListener(VIEWPORT_PAUSE, handlePause);
|
|
149
186
|
viewport.addEventListener(VIEWPORT_RESUME, handleResume);
|
|
150
|
-
|
|
151
187
|
cleanupFn(() => {
|
|
152
188
|
viewport.removeEventListener(VIEWPORT_PAUSE, handlePause);
|
|
153
189
|
viewport.removeEventListener(VIEWPORT_RESUME, handleResume);
|
|
@@ -158,7 +194,8 @@ watchEffect((cleanupFn) => {
|
|
|
158
194
|
// start timer when toast opens or duration changes.
|
|
159
195
|
// we include `open` in deps because closed !== unmounted when animating
|
|
160
196
|
// so it could reopen before being completely unmounted
|
|
161
|
-
|
|
197
|
+
const restartKey = computed(() => props.toast ? providerContext.toastStore.getRestartKey(props.toast.id) : 0);
|
|
198
|
+
watch([() => props.open, duration, restartKey], () => {
|
|
162
199
|
// Reset the timer when the toast is rerendered with the new duration
|
|
163
200
|
closeTimerRemainingTimeRef.value = duration.value;
|
|
164
201
|
|
|
@@ -175,19 +212,24 @@ onKeyStroke('Escape', (event) => {
|
|
|
175
212
|
}
|
|
176
213
|
});
|
|
177
214
|
|
|
215
|
+
let unregisterToast: (() => void) | undefined;
|
|
178
216
|
onMounted(() => {
|
|
179
217
|
providerContext.onToastAdd();
|
|
218
|
+
unregisterToast = providerContext.registerToast(stackEntry);
|
|
219
|
+
measureHeight();
|
|
180
220
|
});
|
|
181
221
|
onUnmounted(() => {
|
|
182
222
|
providerContext.onToastRemove();
|
|
223
|
+
unregisterToast?.();
|
|
183
224
|
});
|
|
184
225
|
|
|
185
|
-
provideToastRootContext({ onClose: handleClose });
|
|
226
|
+
provideToastRootContext({ onClose: handleClose, toast: toRef(props, 'toast') });
|
|
186
227
|
</script>
|
|
187
228
|
|
|
188
229
|
<template>
|
|
189
230
|
<ToastAnnounce
|
|
190
231
|
v-if="announceTextContent"
|
|
232
|
+
:key="toast?.updateKey ?? 0"
|
|
191
233
|
role="alert"
|
|
192
234
|
:aria-live="type === 'foreground' ? 'assertive' : 'polite'"
|
|
193
235
|
>
|
|
@@ -208,8 +250,9 @@ provideToastRootContext({ onClose: handleClose });
|
|
|
208
250
|
</ToastAnnounce>
|
|
209
251
|
|
|
210
252
|
<Teleport
|
|
211
|
-
v-if="providerContext.viewport.value"
|
|
212
|
-
:to="providerContext.viewport.value"
|
|
253
|
+
v-if="providerContext.viewport.value || positionerContext"
|
|
254
|
+
:to="providerContext.viewport.value ?? 'body'"
|
|
255
|
+
:disabled="!!positionerContext"
|
|
213
256
|
>
|
|
214
257
|
<CollectionItem>
|
|
215
258
|
<Primitive
|
|
@@ -220,9 +263,16 @@ provideToastRootContext({ onClose: handleClose });
|
|
|
220
263
|
:as-child="asChild"
|
|
221
264
|
:data-state="open ? 'open' : 'closed'"
|
|
222
265
|
:data-swipe-direction="providerContext.swipeDirection.value"
|
|
223
|
-
:
|
|
224
|
-
|
|
225
|
-
|
|
266
|
+
:data-status="toast?.status"
|
|
267
|
+
:data-expanded="providerContext.expanded.value ? '' : undefined"
|
|
268
|
+
:data-limited="stack.limited ? '' : undefined"
|
|
269
|
+
:inert="stack.limited || undefined"
|
|
270
|
+
:style="{
|
|
271
|
+
...(providerContext.disableSwipe.value ? undefined : { userSelect: 'none', touchAction: 'none' }),
|
|
272
|
+
'--akar-toast-index': stack.index,
|
|
273
|
+
'--akar-toast-offset-y': `${stack.offsetY}px`,
|
|
274
|
+
'--akar-toast-height': stackEntry.height ? `${stackEntry.height}px` : undefined,
|
|
275
|
+
}"
|
|
226
276
|
@pointerdown.left="(event: PointerEvent) => {
|
|
227
277
|
if (providerContext.disableSwipe.value) return;
|
|
228
278
|
|
package/src/Toast/ToastTitle.vue
CHANGED
|
@@ -7,13 +7,19 @@ export interface ToastTitleProps extends PrimitiveProps {}
|
|
|
7
7
|
|
|
8
8
|
<script setup lang="ts">
|
|
9
9
|
import { Primitive } from '@/Primitive';
|
|
10
|
+
import { injectToastRootContext } from './ToastRootImpl.vue';
|
|
10
11
|
|
|
11
12
|
const props = defineProps<ToastTitleProps>();
|
|
12
13
|
useForwardExpose();
|
|
14
|
+
const rootContext = injectToastRootContext(null);
|
|
13
15
|
</script>
|
|
14
16
|
|
|
15
17
|
<template>
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
<!-- A managed toast without a title renders nothing, unless slot content is given. -->
|
|
19
|
+
<Primitive
|
|
20
|
+
v-if="$slots.default || !rootContext?.toast.value || rootContext.toast.value.title"
|
|
21
|
+
v-bind="props"
|
|
22
|
+
>
|
|
23
|
+
<slot>{{ rootContext?.toast.value?.title }}</slot>
|
|
18
24
|
</Primitive>
|
|
19
25
|
</template>
|
|
@@ -21,9 +21,8 @@ export interface ToastViewportProps extends PrimitiveProps {
|
|
|
21
21
|
</script>
|
|
22
22
|
|
|
23
23
|
<script setup lang="ts">
|
|
24
|
-
import { isString } from '@vinicunca/perkakas';
|
|
25
24
|
import { onKeyStroke, unrefElement } from '@vueuse/core';
|
|
26
|
-
import { computed, onMounted, ref, toRefs, watchEffect } from 'vue';
|
|
25
|
+
import { computed, onMounted, ref, toRefs, watch, watchEffect } from 'vue';
|
|
27
26
|
import { DismissableLayerBranch } from '@/DismissableLayer';
|
|
28
27
|
import { focusFirst, getTabbableCandidates } from '@/FocusScope/utils';
|
|
29
28
|
import { Primitive } from '@/Primitive';
|
|
@@ -61,6 +60,24 @@ onMounted(() => {
|
|
|
61
60
|
providerContext.onViewportChange(currentElement.value);
|
|
62
61
|
});
|
|
63
62
|
|
|
63
|
+
// Collapse only once closing toasts have finished animating, so the stack does not
|
|
64
|
+
// shift under the pointer while a toast is dismissed.
|
|
65
|
+
const pendingPointerLeave = ref(false);
|
|
66
|
+
watch([pendingPointerLeave, providerContext.closingCount], ([pending, closingCount]) => {
|
|
67
|
+
if (pending && closingCount === 0) {
|
|
68
|
+
providerContext.hovering.value = false;
|
|
69
|
+
pendingPointerLeave.value = false;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
watch(hasToasts, (value) => {
|
|
74
|
+
if (!value) {
|
|
75
|
+
providerContext.hovering.value = false;
|
|
76
|
+
providerContext.focused.value = false;
|
|
77
|
+
pendingPointerLeave.value = false;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
64
81
|
watchEffect((cleanupFn) => {
|
|
65
82
|
const viewport = currentElement.value;
|
|
66
83
|
if (hasToasts.value && viewport) {
|
|
@@ -83,17 +100,28 @@ watchEffect((cleanupFn) => {
|
|
|
83
100
|
const handleFocusOutResume = (event: FocusEvent) => {
|
|
84
101
|
const isFocusMovingOutside = !viewport.contains(event.relatedTarget as HTMLElement);
|
|
85
102
|
if (isFocusMovingOutside) {
|
|
103
|
+
providerContext.focused.value = false;
|
|
86
104
|
handleResume();
|
|
87
105
|
}
|
|
88
106
|
};
|
|
89
107
|
|
|
90
108
|
const handlePointerLeaveResume = () => {
|
|
109
|
+
pendingPointerLeave.value = true;
|
|
91
110
|
const isFocusInside = viewport.contains(getActiveElement());
|
|
92
111
|
if (!isFocusInside) {
|
|
93
112
|
handleResume();
|
|
94
113
|
}
|
|
95
114
|
};
|
|
96
115
|
|
|
116
|
+
const handlePointerMoveExpand = () => {
|
|
117
|
+
pendingPointerLeave.value = false;
|
|
118
|
+
providerContext.hovering.value = true;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const handleFocusInExpand = () => {
|
|
122
|
+
providerContext.focused.value = true;
|
|
123
|
+
};
|
|
124
|
+
|
|
97
125
|
// We programmatically manage tabbing as we are unable to influence
|
|
98
126
|
// the source order with portals, this allows us to reverse the
|
|
99
127
|
// tab order so that it runs from most recent toast to least
|
|
@@ -130,8 +158,10 @@ watchEffect((cleanupFn) => {
|
|
|
130
158
|
};
|
|
131
159
|
|
|
132
160
|
viewport.addEventListener('focusin', handlePause);
|
|
161
|
+
viewport.addEventListener('focusin', handleFocusInExpand);
|
|
133
162
|
viewport.addEventListener('focusout', handleFocusOutResume);
|
|
134
163
|
viewport.addEventListener('pointermove', handlePause);
|
|
164
|
+
viewport.addEventListener('pointermove', handlePointerMoveExpand);
|
|
135
165
|
viewport.addEventListener('pointerleave', handlePointerLeaveResume);
|
|
136
166
|
viewport.addEventListener('keydown', handleKeyDown);
|
|
137
167
|
window.addEventListener('blur', handlePause);
|
|
@@ -139,8 +169,10 @@ watchEffect((cleanupFn) => {
|
|
|
139
169
|
|
|
140
170
|
cleanupFn(() => {
|
|
141
171
|
viewport.removeEventListener('focusin', handlePause);
|
|
172
|
+
viewport.removeEventListener('focusin', handleFocusInExpand);
|
|
142
173
|
viewport.removeEventListener('focusout', handleFocusOutResume);
|
|
143
174
|
viewport.removeEventListener('pointermove', handlePause);
|
|
175
|
+
viewport.removeEventListener('pointermove', handlePointerMoveExpand);
|
|
144
176
|
viewport.removeEventListener('pointerleave', handlePointerLeaveResume);
|
|
145
177
|
viewport.removeEventListener('keydown', handleKeyDown);
|
|
146
178
|
window.removeEventListener('blur', handlePause);
|
|
@@ -166,7 +198,7 @@ function getSortedTabbableCandidates({ tabbingDirection }: { tabbingDirection: '
|
|
|
166
198
|
<template>
|
|
167
199
|
<DismissableLayerBranch
|
|
168
200
|
role="region"
|
|
169
|
-
:aria-label="
|
|
201
|
+
:aria-label="typeof label === 'string' ? label.replace('{hotkey}', hotkeyMessage) : label(hotkeyMessage)"
|
|
170
202
|
tabindex="-1"
|
|
171
203
|
:style="{
|
|
172
204
|
// incase list has size when empty (e.g. padding), we remove pointer events so
|
|
@@ -194,6 +226,12 @@ function getSortedTabbableCandidates({ tabbingDirection }: { tabbingDirection: '
|
|
|
194
226
|
tabindex="-1"
|
|
195
227
|
:as="as"
|
|
196
228
|
:as-child="asChild"
|
|
229
|
+
:data-expanded="providerContext.expanded.value ? '' : undefined"
|
|
230
|
+
:style="{
|
|
231
|
+
'--akar-toast-frontmost-height': providerContext.frontmostHeight.value
|
|
232
|
+
? `${providerContext.frontmostHeight.value}px`
|
|
233
|
+
: undefined,
|
|
234
|
+
}"
|
|
197
235
|
v-bind="$attrs"
|
|
198
236
|
>
|
|
199
237
|
<slot />
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import type { ReferenceElement } from '@floating-ui/vue';
|
|
2
|
+
import type { ToastPositionerProps } from './ToastPositioner.vue';
|
|
3
|
+
import { isClient } from '@vueuse/shared';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The status of a toast. `loading` toasts are never dismissed automatically.
|
|
7
|
+
* `promise()` sets `loading`, then `success` or `error`.
|
|
8
|
+
*/
|
|
9
|
+
export type ToastStatus = 'loading' | 'success' | 'error' | (string & {});
|
|
10
|
+
|
|
11
|
+
export interface ToastActionOptions {
|
|
12
|
+
/** The text rendered inside `ToastAction` when it has no slot content. */
|
|
13
|
+
label?: string;
|
|
14
|
+
/** A short description of an alternate way to carry out the action, for screen reader users. */
|
|
15
|
+
altText: string;
|
|
16
|
+
/**
|
|
17
|
+
* Whether the action should close the toast when clicked.
|
|
18
|
+
* @defaultValue true
|
|
19
|
+
*/
|
|
20
|
+
closeOnClick?: boolean;
|
|
21
|
+
/** Called when the action is clicked. */
|
|
22
|
+
onClick?: (event: MouseEvent) => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ToastPositionerOptions extends Omit<ToastPositionerProps, 'toast' | 'anchor' | 'as' | 'asChild'> {
|
|
26
|
+
/** The element to position the toast against. */
|
|
27
|
+
anchor?: ReferenceElement | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ToastObject<Data extends object = any> {
|
|
31
|
+
/** The unique identifier for the toast. */
|
|
32
|
+
id: string;
|
|
33
|
+
/** Whether the toast is open. Closed toasts stay in the list until their exit animation ends. */
|
|
34
|
+
open: boolean;
|
|
35
|
+
/** The title of the toast. Rendered by `ToastTitle` when it has no slot content. */
|
|
36
|
+
title?: string;
|
|
37
|
+
/** The description of the toast. Rendered by `ToastDescription` when it has no slot content. */
|
|
38
|
+
description?: string;
|
|
39
|
+
/**
|
|
40
|
+
* The sensitivity of the toast for accessibility purposes. Overrides the `type` prop of `ToastRoot`.
|
|
41
|
+
* `foreground` toasts are announced immediately, `background` toasts politely.
|
|
42
|
+
*/
|
|
43
|
+
type?: 'foreground' | 'background';
|
|
44
|
+
/** The status of the toast. Exposed as `data-status` on `ToastRoot`. */
|
|
45
|
+
status?: ToastStatus;
|
|
46
|
+
/**
|
|
47
|
+
* Time in milliseconds before the toast is dismissed automatically. Overrides
|
|
48
|
+
* the `duration` of `ToastRoot` and `ToastProvider`. `0` or `Infinity` keeps it open.
|
|
49
|
+
*/
|
|
50
|
+
duration?: number;
|
|
51
|
+
/** Props for `ToastAction` inside the toast. */
|
|
52
|
+
actionProps?: ToastActionOptions;
|
|
53
|
+
/** Props for `ToastPositioner`, used to anchor the toast to an element. */
|
|
54
|
+
positionerProps?: ToastPositionerOptions;
|
|
55
|
+
/** Custom data for the toast. */
|
|
56
|
+
data?: Data;
|
|
57
|
+
/** Called when the toast is closed. */
|
|
58
|
+
onClose?: () => void;
|
|
59
|
+
/** Called when the toast is removed from the list, after its exit animation. */
|
|
60
|
+
onRemove?: () => void;
|
|
61
|
+
/** Increments whenever the toast is updated, or added again with the same `id`. */
|
|
62
|
+
updateKey: number;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface ToastAddOptions<Data extends object = any> extends Omit<ToastObject<Data>, 'id' | 'open' | 'updateKey'> {
|
|
66
|
+
/**
|
|
67
|
+
* The unique identifier for the toast. Adding a toast with an existing `id`
|
|
68
|
+
* updates it in place and restarts its dismiss timer.
|
|
69
|
+
*/
|
|
70
|
+
id?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface ToastUpdateOptions<Data extends object = any> extends Partial<Omit<ToastObject<Data>, 'id' | 'open' | 'updateKey'>> {}
|
|
74
|
+
|
|
75
|
+
type ToastPromiseState<Value, Data extends object>
|
|
76
|
+
= | string
|
|
77
|
+
| ToastUpdateOptions<Data>
|
|
78
|
+
| ((value: Value) => string | ToastUpdateOptions<Data>);
|
|
79
|
+
|
|
80
|
+
export interface ToastPromiseOptions<Value, Data extends object = any> {
|
|
81
|
+
loading: string | ToastAddOptions<Data>;
|
|
82
|
+
success: ToastPromiseState<Value, Data>;
|
|
83
|
+
error: ToastPromiseState<any, Data>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface ToastManager<Data extends object = any> {
|
|
87
|
+
/** Adds a toast and returns its `id`. */
|
|
88
|
+
add: <T extends Data = Data>(options: ToastAddOptions<T>) => string;
|
|
89
|
+
/** Closes the toast with the given `id`, or every toast when no `id` is given. */
|
|
90
|
+
close: (id?: string) => void;
|
|
91
|
+
/** Updates an open toast. */
|
|
92
|
+
update: <T extends Data = Data>(
|
|
93
|
+
id: string,
|
|
94
|
+
updates: ToastUpdateOptions<T> | ((prevToast: ToastObject<T>) => ToastUpdateOptions<T>),
|
|
95
|
+
) => void;
|
|
96
|
+
/** Shows a `loading` toast that turns into a `success` or `error` toast when the promise settles. */
|
|
97
|
+
promise: <Value, T extends Data = Data>(
|
|
98
|
+
promise: Promise<Value>,
|
|
99
|
+
options: ToastPromiseOptions<Value, T>,
|
|
100
|
+
) => Promise<Value>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
type ToastUpdater = ToastUpdateOptions | ((prevToast: ToastObject) => ToastUpdateOptions);
|
|
104
|
+
|
|
105
|
+
export type ToastManagerEvent
|
|
106
|
+
= | { action: 'add'; options: ToastAddOptions & { id: string } }
|
|
107
|
+
| { action: 'close'; id?: string }
|
|
108
|
+
| { action: 'update'; id: string; updates: ToastUpdater };
|
|
109
|
+
|
|
110
|
+
export const TOAST_MANAGER_SUBSCRIBE = Symbol('ToastManagerSubscribe');
|
|
111
|
+
|
|
112
|
+
export interface GlobalToastManager<Data extends object = any> extends ToastManager<Data> {
|
|
113
|
+
/**
|
|
114
|
+
* Used by `ToastProvider` to receive the manager's events.
|
|
115
|
+
* @internal
|
|
116
|
+
*/
|
|
117
|
+
[TOAST_MANAGER_SUBSCRIBE]: (listener: (event: ToastManagerEvent) => void) => () => void;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
let count = 0;
|
|
121
|
+
|
|
122
|
+
export function generateToastId() {
|
|
123
|
+
count = (count + 1) % Number.MAX_SAFE_INTEGER;
|
|
124
|
+
return `akar-toast-${count}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function resolvePromiseState<Value, Data extends object>(
|
|
128
|
+
state: ToastPromiseState<Value, Data>,
|
|
129
|
+
value?: Value,
|
|
130
|
+
): ToastUpdateOptions<Data> {
|
|
131
|
+
const resolved = typeof state === 'function' ? state(value as Value) : state;
|
|
132
|
+
return typeof resolved === 'string' ? { description: resolved } : resolved;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function runToastPromise<Value, Data extends object>(
|
|
136
|
+
promise: Promise<Value>,
|
|
137
|
+
options: ToastPromiseOptions<Value, Data>,
|
|
138
|
+
add: ToastManager<Data>['add'],
|
|
139
|
+
update: ToastManager<Data>['update'],
|
|
140
|
+
): Promise<Value> {
|
|
141
|
+
const loading = typeof options.loading === 'string' ? { description: options.loading } : options.loading;
|
|
142
|
+
const id = add({ ...loading, status: 'loading' });
|
|
143
|
+
|
|
144
|
+
return promise.then(
|
|
145
|
+
(value) => {
|
|
146
|
+
update(id, { ...resolvePromiseState(options.success, value), status: 'success' });
|
|
147
|
+
return value;
|
|
148
|
+
},
|
|
149
|
+
(error) => {
|
|
150
|
+
update(id, { ...resolvePromiseState(options.error, error), status: 'error' });
|
|
151
|
+
throw error;
|
|
152
|
+
},
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Creates a toast manager that can add toasts from anywhere, including outside components.
|
|
158
|
+
* Pass it to the `toastManager` prop of `ToastProvider`.
|
|
159
|
+
*/
|
|
160
|
+
export function createToastManager<Data extends object = any>(): GlobalToastManager<Data> {
|
|
161
|
+
const listeners = new Set<(event: ToastManagerEvent) => void>();
|
|
162
|
+
// Toasts added before any `ToastProvider` has mounted are kept until one subscribes.
|
|
163
|
+
// Only on the client: a module-level manager is shared between SSR requests.
|
|
164
|
+
let pending: Array<ToastManagerEvent> = [];
|
|
165
|
+
|
|
166
|
+
function emit(event: ToastManagerEvent) {
|
|
167
|
+
if (listeners.size === 0) {
|
|
168
|
+
if (isClient) {
|
|
169
|
+
pending.push(event);
|
|
170
|
+
}
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
listeners.forEach((listener) => {
|
|
174
|
+
listener(event);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const manager: ToastManager<Data> = {
|
|
179
|
+
add(options) {
|
|
180
|
+
const id = options.id ?? generateToastId();
|
|
181
|
+
emit({ action: 'add', options: { ...options, id } });
|
|
182
|
+
return id;
|
|
183
|
+
},
|
|
184
|
+
close(id) {
|
|
185
|
+
emit({ action: 'close', id });
|
|
186
|
+
},
|
|
187
|
+
update(id, updates) {
|
|
188
|
+
emit({ action: 'update', id, updates: updates as ToastUpdater });
|
|
189
|
+
},
|
|
190
|
+
promise(promise, options) {
|
|
191
|
+
return runToastPromise(promise, options, manager.add, manager.update);
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
return {
|
|
196
|
+
...manager,
|
|
197
|
+
[TOAST_MANAGER_SUBSCRIBE](listener) {
|
|
198
|
+
listeners.add(listener);
|
|
199
|
+
const queued = pending;
|
|
200
|
+
pending = [];
|
|
201
|
+
queued.forEach(listener);
|
|
202
|
+
return () => {
|
|
203
|
+
listeners.delete(listener);
|
|
204
|
+
};
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
}
|
package/src/Toast/index.ts
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createToastManager,
|
|
3
|
+
type GlobalToastManager,
|
|
4
|
+
type ToastActionOptions,
|
|
5
|
+
type ToastAddOptions,
|
|
6
|
+
type ToastManager,
|
|
7
|
+
type ToastObject,
|
|
8
|
+
type ToastPositionerOptions,
|
|
9
|
+
type ToastPromiseOptions,
|
|
10
|
+
type ToastStatus,
|
|
11
|
+
type ToastUpdateOptions,
|
|
12
|
+
} from './createToastManager';
|
|
1
13
|
export { default as ToastAction, type ToastActionProps } from './ToastAction.vue';
|
|
14
|
+
export { default as ToastArrow, type ToastArrowProps } from './ToastArrow.vue';
|
|
2
15
|
export { default as ToastClose, type ToastCloseProps } from './ToastClose.vue';
|
|
3
16
|
export { default as ToastDescription, type ToastDescriptionProps } from './ToastDescription.vue';
|
|
4
17
|
export { default as ToastPortal, type ToastPortalProps } from './ToastPortal.vue';
|
|
18
|
+
export { default as ToastPositioner, type ToastPositionerProps } from './ToastPositioner.vue';
|
|
5
19
|
export { injectToastProviderContext, default as ToastProvider, type ToastProviderProps } from './ToastProvider.vue';
|
|
6
20
|
export { default as ToastRoot, type ToastRootEmits, type ToastRootProps } from './ToastRoot.vue';
|
|
7
21
|
export { default as ToastTitle, type ToastTitleProps } from './ToastTitle.vue';
|
|
8
22
|
export { default as ToastViewport, type ToastViewportProps } from './ToastViewport.vue';
|
|
23
|
+
export { useToastManager, type UseToastManagerReturn } from './useToastManager';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Ref } from 'vue';
|
|
2
|
+
import type { ToastManager, ToastObject } from './createToastManager';
|
|
3
|
+
import { injectToastProviderContext } from './ToastProvider.vue';
|
|
4
|
+
|
|
5
|
+
export interface UseToastManagerReturn<Data extends object = any> extends ToastManager<Data> {
|
|
6
|
+
/** The toasts of the nearest `ToastProvider`, newest first. */
|
|
7
|
+
toasts: Readonly<Ref<Array<ToastObject<Data>>>>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Returns the toasts of the nearest `ToastProvider` and methods to manage them.
|
|
12
|
+
*/
|
|
13
|
+
export function useToastManager<Data extends object = any>(): UseToastManagerReturn<Data> {
|
|
14
|
+
const { toastStore } = injectToastProviderContext();
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
toasts: toastStore.toasts as Readonly<Ref<Array<ToastObject<Data>>>>,
|
|
18
|
+
add: toastStore.add,
|
|
19
|
+
close: toastStore.close,
|
|
20
|
+
update: toastStore.update,
|
|
21
|
+
promise: toastStore.promise,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { ShallowRef } from 'vue';
|
|
2
|
+
import type { ToastAddOptions, ToastManager, ToastObject, ToastUpdateOptions } from './createToastManager';
|
|
3
|
+
import { reactive, shallowRef } from 'vue';
|
|
4
|
+
import { generateToastId, runToastPromise } from './createToastManager';
|
|
5
|
+
|
|
6
|
+
export interface ToastStore extends ToastManager {
|
|
7
|
+
/** Toasts in the list, newest first. */
|
|
8
|
+
toasts: ShallowRef<Array<ToastObject>>;
|
|
9
|
+
/** Removes a closed toast from the list, after its exit animation. */
|
|
10
|
+
remove: (id: string) => void;
|
|
11
|
+
/** Increments when a toast is added again with the same `id`, to restart its dismiss timer. */
|
|
12
|
+
getRestartKey: (id: string) => number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function useToastStore(): ToastStore {
|
|
16
|
+
const toasts = shallowRef<Array<ToastObject>>([]);
|
|
17
|
+
const restartKeys = reactive(new Map<string, number>());
|
|
18
|
+
|
|
19
|
+
function find(id: string) {
|
|
20
|
+
return toasts.value.find((toast) => toast.id === id);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function replace(id: string, next: ToastObject) {
|
|
24
|
+
toasts.value = toasts.value.map((toast) => toast.id === id ? next : toast);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function remove(id: string) {
|
|
28
|
+
const toast = find(id);
|
|
29
|
+
if (!toast || toast.open) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
toasts.value = toasts.value.filter((item) => item.id !== id);
|
|
34
|
+
restartKeys.delete(id);
|
|
35
|
+
toast.onRemove?.();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function add(options: ToastAddOptions): string {
|
|
39
|
+
const id = options.id ?? generateToastId();
|
|
40
|
+
const existing = find(id);
|
|
41
|
+
|
|
42
|
+
if (existing?.open) {
|
|
43
|
+
const { id: _id, ...updates } = options;
|
|
44
|
+
replace(id, { ...existing, ...updates, updateKey: existing.updateKey + 1 });
|
|
45
|
+
restartKeys.set(id, (restartKeys.get(id) ?? 0) + 1);
|
|
46
|
+
return id;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// A closing toast that is added again starts a new lifecycle at the front of the list.
|
|
50
|
+
const rest = existing ? toasts.value.filter((toast) => toast.id !== id) : toasts.value;
|
|
51
|
+
toasts.value = [{ ...options, id, open: true, updateKey: 0 }, ...rest];
|
|
52
|
+
return id;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function close(id?: string) {
|
|
56
|
+
const closing = toasts.value.filter((toast) => toast.open && (id === undefined || toast.id === id));
|
|
57
|
+
if (!closing.length) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
toasts.value = toasts.value.map((toast) => closing.includes(toast) ? { ...toast, open: false } : toast);
|
|
62
|
+
closing.forEach((toast) => toast.onClose?.());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function update(id: string, updates: ToastUpdateOptions | ((prevToast: ToastObject) => ToastUpdateOptions)) {
|
|
66
|
+
const prevToast = find(id);
|
|
67
|
+
// Ignore updates to closing toasts, so a late promise result cannot reopen one.
|
|
68
|
+
if (!prevToast?.open) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const patch = typeof updates === 'function' ? updates(prevToast) : updates;
|
|
73
|
+
const current = find(id);
|
|
74
|
+
if (!current?.open) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
replace(id, { ...current, ...patch, id, open: true, updateKey: current.updateKey + 1 });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
toasts,
|
|
83
|
+
add,
|
|
84
|
+
close,
|
|
85
|
+
update,
|
|
86
|
+
promise: (promise, options) => runToastPromise(promise, options, add, update),
|
|
87
|
+
remove,
|
|
88
|
+
getRestartKey: (id) => restartKeys.get(id) ?? 0,
|
|
89
|
+
};
|
|
90
|
+
}
|
package/src/Toast/utils.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isHTMLElement } from '@/shared';
|
|
2
|
+
|
|
1
3
|
export const TOAST_SWIPE_START = 'toast.swipeStart';
|
|
2
4
|
export const TOAST_SWIPE_MOVE = 'toast.swipeMove';
|
|
3
5
|
export const TOAST_SWIPE_CANCEL = 'toast.swipeCancel';
|
|
@@ -49,10 +51,6 @@ export function isDeltaInDirection(delta: { x: number; y: number }, direction: S
|
|
|
49
51
|
}
|
|
50
52
|
}
|
|
51
53
|
|
|
52
|
-
export function isHTMLElement(node: any): node is HTMLElement {
|
|
53
|
-
return node.nodeType === node.ELEMENT_NODE;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
54
|
export function getAnnounceTextContent(container: HTMLElement) {
|
|
57
55
|
const textContent: Array<string> = [];
|
|
58
56
|
const childNodes = Array.from(container.childNodes);
|