@react-stately/toast 3.0.0-nightly.4582 → 3.0.0-nightly.4595
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/types.d.ts.map +1 -1
- package/dist/useToastState.main.js +25 -11
- package/dist/useToastState.main.js.map +1 -1
- package/dist/useToastState.mjs +25 -11
- package/dist/useToastState.module.js +25 -11
- package/dist/useToastState.module.js.map +1 -1
- package/package.json +2 -2
- package/src/useToastState.ts +20 -19
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAgBA;IACE,yDAAyD;IACzD,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAA;CAC3B;AAED;IACE,8FAA8F;IAC9F,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,yEAAyE;IACzE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,mGAAmG;IACnG,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,6BAA6B,CAAC,CAAE,SAAQ,YAAY;IAClD,gCAAgC;IAChC,OAAO,EAAE,CAAC,CAAC;IACX,kCAAkC;IAClC,GAAG,EAAE,MAAM,CAAC;IACZ,mDAAmD;IACnD,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,iDAAiD;IACjD,SAAS,CAAC,EAAE,UAAU,GAAG,QAAQ,GAAG,SAAS,CAAA;CAC9C;AAED,4BAA4B,CAAC;IAC3B,qCAAqC;IACrC,GAAG,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,MAAM,CAAC;IAChD;;;OAGG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,0EAA0E;IAC1E,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,gDAAgD;IAChD,QAAQ,IAAI,IAAI,CAAC;IACj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AAG,MAAM,CAAC;IAChD;;;OAGG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,0EAA0E;IAC1E,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,gDAAgD;IAChD,QAAQ,IAAI,IAAI,CAAC;IACjB,iDAAiD;IACjD,SAAS,IAAI,IAAI,CAAC;IAClB,0BAA0B;IAC1B,aAAa,EAAE,YAAY,CAAC,CAAC,EAAE,CAAA;CAChC;AAED;;;GAGG;AACH,8BAA8B,CAAC,EAAE,KAAK,GAAE,eAAoB,GAAG,WAAW,CAAC,CAAC,CAI3E;AAED;;GAEG;AACH,8BAA8B,CAAC,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAapE;AAED;;GAEG;AACH,wBAAwB,CAAC;IAKvB,oCAAoC;IACpC,aAAa,EAAE,YAAY,CAAC,CAAC,EAAE,CAAM;gBAEzB,OAAO,CAAC,EAAE,eAAe;IAKrC,mDAAmD;IACnD,SAAS,CAAC,EAAE,EAAE,MAAM,IAAI;IAKxB,qCAAqC;IACrC,GAAG,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,GAAE,YAAiB;IAgC1C;;;OAGG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM;IAUjB,0EAA0E;IAC1E,MAAM,CAAC,GAAG,EAAE,MAAM;IA+BlB,gDAAgD;IAChD,QAAQ;IAQR,iDAAiD;IACjD,SAAS;CAOV;AAED;gBAMc,QAAQ,EAAE,MAAM,IAAI,EAAE,KAAK,EAAE,MAAM;IAK/C,KAAK,CAAC,KAAK,EAAE,MAAM;IAKnB,KAAK;IAUL,MAAM;CAYP","sources":["packages/@react-stately/toast/src/packages/@react-stately/toast/src/useToastState.ts","packages/@react-stately/toast/src/packages/@react-stately/toast/src/index.ts","packages/@react-stately/toast/src/index.ts"],"sourcesContent":[null,null,"/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\nexport {useToastState, ToastQueue, useToastQueue} from './useToastState';\n\nexport type {ToastState, QueuedToast, ToastStateProps, ToastOptions} from './useToastState';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
|
@@ -69,12 +69,13 @@ class $c995945a643dde76$export$f1f8569633bbbec4 {
|
|
|
69
69
|
if ((toast.priority || 0) > (this.queue[mid].priority || 0)) high = mid;
|
|
70
70
|
else low = mid + 1;
|
|
71
71
|
}
|
|
72
|
-
|
|
73
|
-
else this.queue.unshift(toast);
|
|
72
|
+
this.queue.splice(low, 0, toast);
|
|
74
73
|
toast.animation = low < this.maxVisibleToasts ? "entering" : "queued";
|
|
75
74
|
let i = this.maxVisibleToasts;
|
|
76
75
|
while(i < this.queue.length)this.queue[i++].animation = "queued";
|
|
77
|
-
this.updateVisibleToasts(
|
|
76
|
+
this.updateVisibleToasts({
|
|
77
|
+
action: "add"
|
|
78
|
+
});
|
|
78
79
|
return toastKey;
|
|
79
80
|
}
|
|
80
81
|
/**
|
|
@@ -87,22 +88,35 @@ class $c995945a643dde76$export$f1f8569633bbbec4 {
|
|
|
87
88
|
(_this_queue_index_onClose = (_this_queue_index = this.queue[index]).onClose) === null || _this_queue_index_onClose === void 0 ? void 0 : _this_queue_index_onClose.call(_this_queue_index);
|
|
88
89
|
this.queue.splice(index, 1);
|
|
89
90
|
}
|
|
90
|
-
this.updateVisibleToasts(
|
|
91
|
+
this.updateVisibleToasts({
|
|
92
|
+
action: "close",
|
|
93
|
+
key: key
|
|
94
|
+
});
|
|
91
95
|
}
|
|
92
96
|
/** Removes a toast from the visible toasts after an exiting animation. */ remove(key) {
|
|
93
|
-
this.
|
|
94
|
-
|
|
97
|
+
this.updateVisibleToasts({
|
|
98
|
+
action: "remove",
|
|
99
|
+
key: key
|
|
100
|
+
});
|
|
95
101
|
}
|
|
96
|
-
updateVisibleToasts(
|
|
102
|
+
updateVisibleToasts(options) {
|
|
103
|
+
let { action: action, key: key } = options;
|
|
97
104
|
let toasts = this.queue.slice(0, this.maxVisibleToasts);
|
|
98
|
-
if (this.hasExitAnimation) {
|
|
105
|
+
if (action === "add" && this.hasExitAnimation) {
|
|
99
106
|
let prevToasts = this.visibleToasts.filter((t)=>!toasts.some((t2)=>t.key === t2.key)).map((t)=>({
|
|
100
107
|
...t,
|
|
101
108
|
animation: "exiting"
|
|
102
109
|
}));
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
110
|
+
this.visibleToasts = prevToasts.concat(toasts).sort((a, b)=>b.priority - a.priority);
|
|
111
|
+
} else if (action === "close" && this.hasExitAnimation) // Cause a rerender to happen for exit animation
|
|
112
|
+
this.visibleToasts = this.visibleToasts.map((t)=>{
|
|
113
|
+
if (t.key !== key) return t;
|
|
114
|
+
else return {
|
|
115
|
+
...t,
|
|
116
|
+
animation: "exiting"
|
|
117
|
+
};
|
|
118
|
+
});
|
|
119
|
+
else this.visibleToasts = toasts;
|
|
106
120
|
for (let fn of this.subscriptions)fn();
|
|
107
121
|
}
|
|
108
122
|
/** Pauses the timers for all visible toasts. */ pauseAll() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;AA2DM,SAAS,0CAAiB,QAAyB,CAAC,CAAC;IAC1D,IAAI,oBAAC,mBAAmB,qBAAG,mBAAmB,OAAM,GAAG;IACvD,IAAI,QAAQ,CAAA,GAAA,oBAAM,EAAE,IAAM,IAAI,0CAAc;8BAAC;8BAAkB;QAAgB,IAAI;QAAC;QAAkB;KAAiB;IACvH,OAAO,0CAAc;AACvB;AAKO,SAAS,0CAAiB,KAAoB;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,GAAG;YACf,IAAI,CAAC,SAAS,GAAG;YACjB,IAAI,CAAC,QAAQ;QACf,GAAG,IAAI,CAAC,SAAS;IACnB;IA/BA,YAAY,QAAoB,EAAE,KAAa,CAAE;QAC/C,IAAI,CAAC,SAAS,GAAG;QACjB,IAAI,CAAC,QAAQ,GAAG;IAClB;AA6BF","sources":["packages/@react-stately/toast/src/useToastState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {useCallback, useMemo} from 'react';\n// Shim to support React 17 and below.\nimport {useSyncExternalStore} from 'use-sync-external-store/shim/index.js';\n\nexport interface ToastStateProps {\n /** The maximum number of toasts to display at a time. */\n maxVisibleToasts?: number,\n /**\n * Whether toasts have an exit animation. If true, toasts are not\n * removed immediately but transition into an \"exiting\" state instead.\n * Once the animation is complete, call the `remove` function.\n */\n hasExitAnimation?: boolean\n}\n\nexport interface ToastOptions {\n /** Handler that is called when the toast is closed, either by the user or after a timeout. */\n onClose?: () => void,\n /** A timeout to automatically close the toast after, in milliseconds. */\n timeout?: number,\n /** The priority of the toast relative to other toasts. Larger numbers indicate higher priority. */\n priority?: number\n}\n\nexport interface QueuedToast<T> extends ToastOptions {\n /** The content of the toast. */\n content: T,\n /** A unique key for the toast. */\n key: string,\n /** A timer for the toast, if a timeout was set. */\n timer?: Timer,\n /** The current animation state for the toast. */\n animation?: 'entering' | 'queued' | 'exiting'\n}\n\nexport interface ToastState<T> {\n /** Adds a new toast to the queue. */\n add(content: T, options?: ToastOptions): string,\n /**\n * Closes a toast. If `hasExitAnimation` is true, the toast\n * transitions to an \"exiting\" state instead of being removed immediately.\n */\n close(key: string): void,\n /** Removes a toast from the visible toasts after an exiting animation. */\n remove(key: string): void,\n /** Pauses the timers for all visible toasts. */\n pauseAll(): void,\n /** Resumes the timers for all visible toasts. */\n resumeAll(): void,\n /** The visible toasts. */\n visibleToasts: QueuedToast<T>[]\n}\n\n/**\n * Provides state management for a toast queue. Toasts display brief, temporary notifications\n * of actions, errors, or other events in an application.\n */\nexport function useToastState<T>(props: ToastStateProps = {}): ToastState<T> {\n let {maxVisibleToasts = 1, hasExitAnimation = false} = props;\n let queue = useMemo(() => new ToastQueue<T>({maxVisibleToasts, hasExitAnimation}), [maxVisibleToasts, hasExitAnimation]);\n return useToastQueue(queue);\n}\n\n/**\n * Subscribes to a provided toast queue and provides methods to update it.\n */\nexport function useToastQueue<T>(queue: ToastQueue<T>): ToastState<T> {\n let subscribe = useCallback(fn => queue.subscribe(fn), [queue]);\n let getSnapshot = useCallback(() => queue.visibleToasts, [queue]);\n let visibleToasts = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n return {\n visibleToasts,\n add: (content, options) => queue.add(content, options),\n close: key => queue.close(key),\n remove: key => queue.remove(key),\n pauseAll: () => queue.pauseAll(),\n resumeAll: () => queue.resumeAll()\n };\n}\n\n/**\n * A ToastQueue is a priority queue of toasts.\n */\nexport class ToastQueue<T> {\n private queue: QueuedToast<T>[] = [];\n private subscriptions: Set<() => void> = new Set();\n private maxVisibleToasts: number;\n private hasExitAnimation: boolean;\n /** The currently visible toasts. */\n visibleToasts: QueuedToast<T>[] = [];\n\n constructor(options?: ToastStateProps) {\n this.maxVisibleToasts = options?.maxVisibleToasts ?? 1;\n this.hasExitAnimation = options?.hasExitAnimation ?? false;\n }\n\n /** Subscribes to updates to the visible toasts. */\n subscribe(fn: () => void) {\n this.subscriptions.add(fn);\n return () => this.subscriptions.delete(fn);\n }\n\n /** Adds a new toast to the queue. */\n add(content: T, options: ToastOptions = {}) {\n let toastKey = Math.random().toString(36);\n let toast: QueuedToast<T> = {\n ...options,\n content,\n key: toastKey,\n timer: options.timeout ? new Timer(() => this.close(toastKey), options.timeout) : null\n };\n\n let low = 0;\n let high = this.queue.length;\n while (low < high) {\n let mid = Math.floor((low + high) / 2);\n if ((toast.priority || 0) > (this.queue[mid].priority || 0)) {\n high = mid;\n } else {\n low = mid + 1;\n }\n }\n\n if (toast.priority) {\n this.queue.splice(low, 0, toast);\n } else {\n this.queue.unshift(toast);\n }\n\n toast.animation = low < this.maxVisibleToasts ? 'entering' : 'queued';\n let i = this.maxVisibleToasts;\n while (i < this.queue.length) {\n this.queue[i++].animation = 'queued';\n }\n\n this.updateVisibleToasts();\n return toastKey;\n }\n\n /**\n * Closes a toast. If `hasExitAnimation` is true, the toast\n * transitions to an \"exiting\" state instead of being removed immediately.\n */\n close(key: string) {\n let index = this.queue.findIndex(t => t.key === key);\n if (index >= 0) {\n this.queue[index].onClose?.();\n this.queue.splice(index, 1);\n }\n\n this.updateVisibleToasts(index);\n }\n\n /** Removes a toast from the visible toasts after an exiting animation. */\n remove(key: string) {\n this.visibleToasts = this.visibleToasts.filter(t => t.key !== key);\n this.updateVisibleToasts();\n }\n\n private updateVisibleToasts(oldIndex = -1) {\n let toasts = this.queue.slice(0, this.maxVisibleToasts);\n if (this.hasExitAnimation) {\n let prevToasts: QueuedToast<T>[] = this.visibleToasts\n .filter(t => !toasts.some(t2 => t.key === t2.key))\n .map(t => ({...t, animation: 'exiting'}));\n\n if (oldIndex !== -1) {\n toasts.splice(oldIndex, 0, prevToasts?.[0]);\n }\n\n this.visibleToasts = toasts;\n } else {\n this.visibleToasts = toasts;\n }\n\n for (let fn of this.subscriptions) {\n fn();\n }\n }\n\n /** Pauses the timers for all visible toasts. */\n pauseAll() {\n for (let toast of this.visibleToasts) {\n if (toast.timer) {\n toast.timer.pause();\n }\n }\n }\n\n /** Resumes the timers for all visible toasts. */\n resumeAll() {\n for (let toast of this.visibleToasts) {\n if (toast.timer) {\n toast.timer.resume();\n }\n }\n }\n}\n\nclass Timer {\n private timerId;\n private startTime: number;\n private remaining: number;\n private callback: () => void;\n\n constructor(callback: () => void, delay: number) {\n this.remaining = delay;\n this.callback = callback;\n }\n\n reset(delay: number) {\n this.remaining = delay;\n this.resume();\n }\n\n pause() {\n if (this.timerId == null) {\n return;\n }\n\n clearTimeout(this.timerId);\n this.timerId = null;\n this.remaining -= Date.now() - this.startTime;\n }\n\n resume() {\n if (this.remaining <= 0) {\n return;\n }\n\n this.startTime = Date.now();\n this.timerId = setTimeout(() => {\n this.timerId = null;\n this.remaining = 0;\n this.callback();\n }, this.remaining);\n }\n}\n"],"names":[],"version":3,"file":"useToastState.main.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;AA2DM,SAAS,0CAAiB,QAAyB,CAAC,CAAC;IAC1D,IAAI,oBAAC,mBAAmB,qBAAG,mBAAmB,OAAM,GAAG;IACvD,IAAI,QAAQ,CAAA,GAAA,oBAAM,EAAE,IAAM,IAAI,0CAAc;8BAAC;8BAAkB;QAAgB,IAAI;QAAC;QAAkB;KAAiB;IACvH,OAAO,0CAAc;AACvB;AAKO,SAAS,0CAAiB,KAAoB;IACnD,IAAI,YAAY,CAAA,GAAA,wBAAU,EAAE,CAAA,KAAM,MAAM,SAAS,CAAC,KAAK;QAAC;KAAM;IAC9D,IAAI,cAAc,CAAA,GAAA,wBAAU,EAAE,IAAM,MAAM,aAAa,EAAE;QAAC;KAAM;IAChE,IAAI,gBAAgB,CAAA,GAAA,2DAAmB,EAAE,WAAW,aAAa;IAEjE,OAAO;uBACL;QACA,KAAK,CAAC,SAAS,UAAY,MAAM,GAAG,CAAC,SAAS;QAC9C,OAAO,CAAA,MAAO,MAAM,KAAK,CAAC;QAC1B,QAAQ,CAAA,MAAO,MAAM,MAAM,CAAC;QAC5B,UAAU,IAAM,MAAM,QAAQ;QAC9B,WAAW,IAAM,MAAM,SAAS;IAClC;AACF;AAKO,MAAM;IAaX,iDAAiD,GACjD,UA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AM,KAAK,EACb,MAAM,KAAK,CAAC,KAAK;IAGvB;IAEA,+CAA+C,GAC/C,YAAY;QACV,KAAK,IAAI,SAAS,IAAI,CAAC,aAAa,CAClC,IAAI,MAAM,KAAK,EACb,MAAM,KAAK,CAAC,MAAM;IAGxB;IA1GA,YAAY,OAAyB,CAAE;aAP/B,QAA0B,EAAE;aAC5B,gBAAiC,IAAI;QAG7C,kCAAkC,QAClC,gBAAkC,EAAE;YAGV;QAAxB,IAAI,CAAC,gBAAgB,GAAG,CAAA,4BAAA,oBAAA,8BAAA,QAAS,gBAAgB,cAAzB,uCAAA,4BAA6B;YAC7B;QAAxB,IAAI,CAAC,gBAAgB,GAAG,CAAA,4BAAA,oBAAA,8BAAA,QAAS,gBAAgB,cAAzB,uCAAA,4BAA6B;IACvD;AAwGF;AAEA,MAAM;IAWJ,MAAM,KAAa,EAAE;QACnB,IAAI,CAAC,SAAS,GAAG;QACjB,IAAI,CAAC,MAAM;IACb;IAEA,QAAQ;QACN,IAAI,IAAI,CAAC,OAAO,IAAI,MAClB;QAGF,aAAa,IAAI,CAAC,OAAO;QACzB,IAAI,CAAC,OAAO,GAAG;QACf,IAAI,CAAC,SAAS,IAAI,KAAK,GAAG,KAAK,IAAI,CAAC,SAAS;IAC/C;IAEA,SAAS;QACP,IAAI,IAAI,CAAC,SAAS,IAAI,GACpB;QAGF,IAAI,CAAC,SAAS,GAAG,KAAK,GAAG;QACzB,IAAI,CAAC,OAAO,GAAG,WAAW;YACxB,IAAI,CAAC,OAAO,GAAG;YACf,IAAI,CAAC,SAAS,GAAG;YACjB,IAAI,CAAC,QAAQ;QACf,GAAG,IAAI,CAAC,SAAS;IACnB;IA/BA,YAAY,QAAoB,EAAE,KAAa,CAAE;QAC/C,IAAI,CAAC,SAAS,GAAG;QACjB,IAAI,CAAC,QAAQ,GAAG;IAClB;AA6BF","sources":["packages/@react-stately/toast/src/useToastState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {useCallback, useMemo} from 'react';\n// Shim to support React 17 and below.\nimport {useSyncExternalStore} from 'use-sync-external-store/shim/index.js';\n\nexport interface ToastStateProps {\n /** The maximum number of toasts to display at a time. */\n maxVisibleToasts?: number,\n /**\n * Whether toasts have an exit animation. If true, toasts are not\n * removed immediately but transition into an \"exiting\" state instead.\n * Once the animation is complete, call the `remove` function.\n */\n hasExitAnimation?: boolean\n}\n\nexport interface ToastOptions {\n /** Handler that is called when the toast is closed, either by the user or after a timeout. */\n onClose?: () => void,\n /** A timeout to automatically close the toast after, in milliseconds. */\n timeout?: number,\n /** The priority of the toast relative to other toasts. Larger numbers indicate higher priority. */\n priority?: number\n}\n\nexport interface QueuedToast<T> extends ToastOptions {\n /** The content of the toast. */\n content: T,\n /** A unique key for the toast. */\n key: string,\n /** A timer for the toast, if a timeout was set. */\n timer?: Timer,\n /** The current animation state for the toast. */\n animation?: 'entering' | 'queued' | 'exiting'\n}\n\nexport interface ToastState<T> {\n /** Adds a new toast to the queue. */\n add(content: T, options?: ToastOptions): string,\n /**\n * Closes a toast. If `hasExitAnimation` is true, the toast\n * transitions to an \"exiting\" state instead of being removed immediately.\n */\n close(key: string): void,\n /** Removes a toast from the visible toasts after an exiting animation. */\n remove(key: string): void,\n /** Pauses the timers for all visible toasts. */\n pauseAll(): void,\n /** Resumes the timers for all visible toasts. */\n resumeAll(): void,\n /** The visible toasts. */\n visibleToasts: QueuedToast<T>[]\n}\n\n/**\n * Provides state management for a toast queue. Toasts display brief, temporary notifications\n * of actions, errors, or other events in an application.\n */\nexport function useToastState<T>(props: ToastStateProps = {}): ToastState<T> {\n let {maxVisibleToasts = 1, hasExitAnimation = false} = props;\n let queue = useMemo(() => new ToastQueue<T>({maxVisibleToasts, hasExitAnimation}), [maxVisibleToasts, hasExitAnimation]);\n return useToastQueue(queue);\n}\n\n/**\n * Subscribes to a provided toast queue and provides methods to update it.\n */\nexport function useToastQueue<T>(queue: ToastQueue<T>): ToastState<T> {\n let subscribe = useCallback(fn => queue.subscribe(fn), [queue]);\n let getSnapshot = useCallback(() => queue.visibleToasts, [queue]);\n let visibleToasts = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n return {\n visibleToasts,\n add: (content, options) => queue.add(content, options),\n close: key => queue.close(key),\n remove: key => queue.remove(key),\n pauseAll: () => queue.pauseAll(),\n resumeAll: () => queue.resumeAll()\n };\n}\n\n/**\n * A ToastQueue is a priority queue of toasts.\n */\nexport class ToastQueue<T> {\n private queue: QueuedToast<T>[] = [];\n private subscriptions: Set<() => void> = new Set();\n private maxVisibleToasts: number;\n private hasExitAnimation: boolean;\n /** The currently visible toasts. */\n visibleToasts: QueuedToast<T>[] = [];\n\n constructor(options?: ToastStateProps) {\n this.maxVisibleToasts = options?.maxVisibleToasts ?? 1;\n this.hasExitAnimation = options?.hasExitAnimation ?? false;\n }\n\n /** Subscribes to updates to the visible toasts. */\n subscribe(fn: () => void) {\n this.subscriptions.add(fn);\n return () => this.subscriptions.delete(fn);\n }\n\n /** Adds a new toast to the queue. */\n add(content: T, options: ToastOptions = {}) {\n let toastKey = Math.random().toString(36);\n let toast: QueuedToast<T> = {\n ...options,\n content,\n key: toastKey,\n timer: options.timeout ? new Timer(() => this.close(toastKey), options.timeout) : null\n };\n\n let low = 0;\n let high = this.queue.length;\n while (low < high) {\n let mid = Math.floor((low + high) / 2);\n if ((toast.priority || 0) > (this.queue[mid].priority || 0)) {\n high = mid;\n } else {\n low = mid + 1;\n }\n }\n\n this.queue.splice(low, 0, toast);\n\n toast.animation = low < this.maxVisibleToasts ? 'entering' : 'queued';\n let i = this.maxVisibleToasts;\n while (i < this.queue.length) {\n this.queue[i++].animation = 'queued';\n }\n\n this.updateVisibleToasts({action: 'add'});\n return toastKey;\n }\n\n /**\n * Closes a toast. If `hasExitAnimation` is true, the toast\n * transitions to an \"exiting\" state instead of being removed immediately.\n */\n close(key: string) {\n let index = this.queue.findIndex(t => t.key === key);\n if (index >= 0) {\n this.queue[index].onClose?.();\n this.queue.splice(index, 1);\n }\n\n this.updateVisibleToasts({action: 'close', key});\n }\n\n /** Removes a toast from the visible toasts after an exiting animation. */\n remove(key: string) {\n this.updateVisibleToasts({action: 'remove', key});\n }\n\n private updateVisibleToasts(options: {action: 'add' | 'close' | 'remove', key?: string}) {\n let {action, key} = options;\n let toasts = this.queue.slice(0, this.maxVisibleToasts);\n\n if (action === 'add' && this.hasExitAnimation) {\n let prevToasts: QueuedToast<T>[] = this.visibleToasts\n .filter(t => !toasts.some(t2 => t.key === t2.key))\n .map(t => ({...t, animation: 'exiting'}));\n this.visibleToasts = prevToasts.concat(toasts).sort((a, b) => b.priority - a.priority);\n } else if (action === 'close' && this.hasExitAnimation) {\n // Cause a rerender to happen for exit animation\n this.visibleToasts = this.visibleToasts.map(t => {\n if (t.key !== key) {\n return t;\n } else {\n return {...t, animation: 'exiting'};\n }\n });\n } else {\n this.visibleToasts = toasts;\n }\n\n for (let fn of this.subscriptions) {\n fn();\n }\n }\n\n /** Pauses the timers for all visible toasts. */\n pauseAll() {\n for (let toast of this.visibleToasts) {\n if (toast.timer) {\n toast.timer.pause();\n }\n }\n }\n\n /** Resumes the timers for all visible toasts. */\n resumeAll() {\n for (let toast of this.visibleToasts) {\n if (toast.timer) {\n toast.timer.resume();\n }\n }\n }\n}\n\nclass Timer {\n private timerId;\n private startTime: number;\n private remaining: number;\n private callback: () => void;\n\n constructor(callback: () => void, delay: number) {\n this.remaining = delay;\n this.callback = callback;\n }\n\n reset(delay: number) {\n this.remaining = delay;\n this.resume();\n }\n\n pause() {\n if (this.timerId == null) {\n return;\n }\n\n clearTimeout(this.timerId);\n this.timerId = null;\n this.remaining -= Date.now() - this.startTime;\n }\n\n resume() {\n if (this.remaining <= 0) {\n return;\n }\n\n this.startTime = Date.now();\n this.timerId = setTimeout(() => {\n this.timerId = null;\n this.remaining = 0;\n this.callback();\n }, this.remaining);\n }\n}\n"],"names":[],"version":3,"file":"useToastState.main.js.map"}
|
package/dist/useToastState.mjs
CHANGED
|
@@ -61,12 +61,13 @@ class $77b352cf12efcf73$export$f1f8569633bbbec4 {
|
|
|
61
61
|
if ((toast.priority || 0) > (this.queue[mid].priority || 0)) high = mid;
|
|
62
62
|
else low = mid + 1;
|
|
63
63
|
}
|
|
64
|
-
|
|
65
|
-
else this.queue.unshift(toast);
|
|
64
|
+
this.queue.splice(low, 0, toast);
|
|
66
65
|
toast.animation = low < this.maxVisibleToasts ? "entering" : "queued";
|
|
67
66
|
let i = this.maxVisibleToasts;
|
|
68
67
|
while(i < this.queue.length)this.queue[i++].animation = "queued";
|
|
69
|
-
this.updateVisibleToasts(
|
|
68
|
+
this.updateVisibleToasts({
|
|
69
|
+
action: "add"
|
|
70
|
+
});
|
|
70
71
|
return toastKey;
|
|
71
72
|
}
|
|
72
73
|
/**
|
|
@@ -79,22 +80,35 @@ class $77b352cf12efcf73$export$f1f8569633bbbec4 {
|
|
|
79
80
|
(_this_queue_index_onClose = (_this_queue_index = this.queue[index]).onClose) === null || _this_queue_index_onClose === void 0 ? void 0 : _this_queue_index_onClose.call(_this_queue_index);
|
|
80
81
|
this.queue.splice(index, 1);
|
|
81
82
|
}
|
|
82
|
-
this.updateVisibleToasts(
|
|
83
|
+
this.updateVisibleToasts({
|
|
84
|
+
action: "close",
|
|
85
|
+
key: key
|
|
86
|
+
});
|
|
83
87
|
}
|
|
84
88
|
/** Removes a toast from the visible toasts after an exiting animation. */ remove(key) {
|
|
85
|
-
this.
|
|
86
|
-
|
|
89
|
+
this.updateVisibleToasts({
|
|
90
|
+
action: "remove",
|
|
91
|
+
key: key
|
|
92
|
+
});
|
|
87
93
|
}
|
|
88
|
-
updateVisibleToasts(
|
|
94
|
+
updateVisibleToasts(options) {
|
|
95
|
+
let { action: action, key: key } = options;
|
|
89
96
|
let toasts = this.queue.slice(0, this.maxVisibleToasts);
|
|
90
|
-
if (this.hasExitAnimation) {
|
|
97
|
+
if (action === "add" && this.hasExitAnimation) {
|
|
91
98
|
let prevToasts = this.visibleToasts.filter((t)=>!toasts.some((t2)=>t.key === t2.key)).map((t)=>({
|
|
92
99
|
...t,
|
|
93
100
|
animation: "exiting"
|
|
94
101
|
}));
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
102
|
+
this.visibleToasts = prevToasts.concat(toasts).sort((a, b)=>b.priority - a.priority);
|
|
103
|
+
} else if (action === "close" && this.hasExitAnimation) // Cause a rerender to happen for exit animation
|
|
104
|
+
this.visibleToasts = this.visibleToasts.map((t)=>{
|
|
105
|
+
if (t.key !== key) return t;
|
|
106
|
+
else return {
|
|
107
|
+
...t,
|
|
108
|
+
animation: "exiting"
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
else this.visibleToasts = toasts;
|
|
98
112
|
for (let fn of this.subscriptions)fn();
|
|
99
113
|
}
|
|
100
114
|
/** Pauses the timers for all visible toasts. */ pauseAll() {
|
|
@@ -61,12 +61,13 @@ class $77b352cf12efcf73$export$f1f8569633bbbec4 {
|
|
|
61
61
|
if ((toast.priority || 0) > (this.queue[mid].priority || 0)) high = mid;
|
|
62
62
|
else low = mid + 1;
|
|
63
63
|
}
|
|
64
|
-
|
|
65
|
-
else this.queue.unshift(toast);
|
|
64
|
+
this.queue.splice(low, 0, toast);
|
|
66
65
|
toast.animation = low < this.maxVisibleToasts ? "entering" : "queued";
|
|
67
66
|
let i = this.maxVisibleToasts;
|
|
68
67
|
while(i < this.queue.length)this.queue[i++].animation = "queued";
|
|
69
|
-
this.updateVisibleToasts(
|
|
68
|
+
this.updateVisibleToasts({
|
|
69
|
+
action: "add"
|
|
70
|
+
});
|
|
70
71
|
return toastKey;
|
|
71
72
|
}
|
|
72
73
|
/**
|
|
@@ -79,22 +80,35 @@ class $77b352cf12efcf73$export$f1f8569633bbbec4 {
|
|
|
79
80
|
(_this_queue_index_onClose = (_this_queue_index = this.queue[index]).onClose) === null || _this_queue_index_onClose === void 0 ? void 0 : _this_queue_index_onClose.call(_this_queue_index);
|
|
80
81
|
this.queue.splice(index, 1);
|
|
81
82
|
}
|
|
82
|
-
this.updateVisibleToasts(
|
|
83
|
+
this.updateVisibleToasts({
|
|
84
|
+
action: "close",
|
|
85
|
+
key: key
|
|
86
|
+
});
|
|
83
87
|
}
|
|
84
88
|
/** Removes a toast from the visible toasts after an exiting animation. */ remove(key) {
|
|
85
|
-
this.
|
|
86
|
-
|
|
89
|
+
this.updateVisibleToasts({
|
|
90
|
+
action: "remove",
|
|
91
|
+
key: key
|
|
92
|
+
});
|
|
87
93
|
}
|
|
88
|
-
updateVisibleToasts(
|
|
94
|
+
updateVisibleToasts(options) {
|
|
95
|
+
let { action: action, key: key } = options;
|
|
89
96
|
let toasts = this.queue.slice(0, this.maxVisibleToasts);
|
|
90
|
-
if (this.hasExitAnimation) {
|
|
97
|
+
if (action === "add" && this.hasExitAnimation) {
|
|
91
98
|
let prevToasts = this.visibleToasts.filter((t)=>!toasts.some((t2)=>t.key === t2.key)).map((t)=>({
|
|
92
99
|
...t,
|
|
93
100
|
animation: "exiting"
|
|
94
101
|
}));
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
102
|
+
this.visibleToasts = prevToasts.concat(toasts).sort((a, b)=>b.priority - a.priority);
|
|
103
|
+
} else if (action === "close" && this.hasExitAnimation) // Cause a rerender to happen for exit animation
|
|
104
|
+
this.visibleToasts = this.visibleToasts.map((t)=>{
|
|
105
|
+
if (t.key !== key) return t;
|
|
106
|
+
else return {
|
|
107
|
+
...t,
|
|
108
|
+
animation: "exiting"
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
else this.visibleToasts = toasts;
|
|
98
112
|
for (let fn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EAAE;QACnB,IAAI,CAAC,SAAS,GAAG;QACjB,IAAI,CAAC,MAAM;IACb;IAEA,QAAQ;QACN,IAAI,IAAI,CAAC,OAAO,IAAI,MAClB;QAGF,aAAa,IAAI,CAAC,OAAO;QACzB,IAAI,CAAC,OAAO,GAAG;QACf,IAAI,CAAC,SAAS,IAAI,KAAK,GAAG,KAAK,IAAI,CAAC,SAAS;IAC/C;IAEA,SAAS;QACP,IAAI,IAAI,CAAC,SAAS,IAAI,GACpB;QAGF,IAAI,CAAC,SAAS,GAAG,KAAK,GAAG;QACzB,IAAI,CAAC,OAAO,GAAG,WAAW;YACxB,IAAI,CAAC,OAAO,GAAG;YACf,IAAI,CAAC,SAAS,GAAG;YACjB,IAAI,CAAC,QAAQ;QACf,GAAG,IAAI,CAAC,SAAS;IACnB;IA/BA,YAAY,QAAoB,EAAE,KAAa,CAAE;QAC/C,IAAI,CAAC,SAAS,GAAG;QACjB,IAAI,CAAC,QAAQ,GAAG;IAClB;AA6BF","sources":["packages/@react-stately/toast/src/useToastState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {useCallback, useMemo} from 'react';\n// Shim to support React 17 and below.\nimport {useSyncExternalStore} from 'use-sync-external-store/shim/index.js';\n\nexport interface ToastStateProps {\n /** The maximum number of toasts to display at a time. */\n maxVisibleToasts?: number,\n /**\n * Whether toasts have an exit animation. If true, toasts are not\n * removed immediately but transition into an \"exiting\" state instead.\n * Once the animation is complete, call the `remove` function.\n */\n hasExitAnimation?: boolean\n}\n\nexport interface ToastOptions {\n /** Handler that is called when the toast is closed, either by the user or after a timeout. */\n onClose?: () => void,\n /** A timeout to automatically close the toast after, in milliseconds. */\n timeout?: number,\n /** The priority of the toast relative to other toasts. Larger numbers indicate higher priority. */\n priority?: number\n}\n\nexport interface QueuedToast<T> extends ToastOptions {\n /** The content of the toast. */\n content: T,\n /** A unique key for the toast. */\n key: string,\n /** A timer for the toast, if a timeout was set. */\n timer?: Timer,\n /** The current animation state for the toast. */\n animation?: 'entering' | 'queued' | 'exiting'\n}\n\nexport interface ToastState<T> {\n /** Adds a new toast to the queue. */\n add(content: T, options?: ToastOptions): string,\n /**\n * Closes a toast. If `hasExitAnimation` is true, the toast\n * transitions to an \"exiting\" state instead of being removed immediately.\n */\n close(key: string): void,\n /** Removes a toast from the visible toasts after an exiting animation. */\n remove(key: string): void,\n /** Pauses the timers for all visible toasts. */\n pauseAll(): void,\n /** Resumes the timers for all visible toasts. */\n resumeAll(): void,\n /** The visible toasts. */\n visibleToasts: QueuedToast<T>[]\n}\n\n/**\n * Provides state management for a toast queue. Toasts display brief, temporary notifications\n * of actions, errors, or other events in an application.\n */\nexport function useToastState<T>(props: ToastStateProps = {}): ToastState<T> {\n let {maxVisibleToasts = 1, hasExitAnimation = false} = props;\n let queue = useMemo(() => new ToastQueue<T>({maxVisibleToasts, hasExitAnimation}), [maxVisibleToasts, hasExitAnimation]);\n return useToastQueue(queue);\n}\n\n/**\n * Subscribes to a provided toast queue and provides methods to update it.\n */\nexport function useToastQueue<T>(queue: ToastQueue<T>): ToastState<T> {\n let subscribe = useCallback(fn => queue.subscribe(fn), [queue]);\n let getSnapshot = useCallback(() => queue.visibleToasts, [queue]);\n let visibleToasts = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n return {\n visibleToasts,\n add: (content, options) => queue.add(content, options),\n close: key => queue.close(key),\n remove: key => queue.remove(key),\n pauseAll: () => queue.pauseAll(),\n resumeAll: () => queue.resumeAll()\n };\n}\n\n/**\n * A ToastQueue is a priority queue of toasts.\n */\nexport class ToastQueue<T> {\n private queue: QueuedToast<T>[] = [];\n private subscriptions: Set<() => void> = new Set();\n private maxVisibleToasts: number;\n private hasExitAnimation: boolean;\n /** The currently visible toasts. */\n visibleToasts: QueuedToast<T>[] = [];\n\n constructor(options?: ToastStateProps) {\n this.maxVisibleToasts = options?.maxVisibleToasts ?? 1;\n this.hasExitAnimation = options?.hasExitAnimation ?? false;\n }\n\n /** Subscribes to updates to the visible toasts. */\n subscribe(fn: () => void) {\n this.subscriptions.add(fn);\n return () => this.subscriptions.delete(fn);\n }\n\n /** Adds a new toast to the queue. */\n add(content: T, options: ToastOptions = {}) {\n let toastKey = Math.random().toString(36);\n let toast: QueuedToast<T> = {\n ...options,\n content,\n key: toastKey,\n timer: options.timeout ? new Timer(() => this.close(toastKey), options.timeout) : null\n };\n\n let low = 0;\n let high = this.queue.length;\n while (low < high) {\n let mid = Math.floor((low + high) / 2);\n if ((toast.priority || 0) > (this.queue[mid].priority || 0)) {\n high = mid;\n } else {\n low = mid + 1;\n }\n }\n\n if (toast.priority) {\n this.queue.splice(low, 0, toast);\n } else {\n this.queue.unshift(toast);\n }\n\n toast.animation = low < this.maxVisibleToasts ? 'entering' : 'queued';\n let i = this.maxVisibleToasts;\n while (i < this.queue.length) {\n this.queue[i++].animation = 'queued';\n }\n\n this.updateVisibleToasts();\n return toastKey;\n }\n\n /**\n * Closes a toast. If `hasExitAnimation` is true, the toast\n * transitions to an \"exiting\" state instead of being removed immediately.\n */\n close(key: string) {\n let index = this.queue.findIndex(t => t.key === key);\n if (index >= 0) {\n this.queue[index].onClose?.();\n this.queue.splice(index, 1);\n }\n\n this.updateVisibleToasts(index);\n }\n\n /** Removes a toast from the visible toasts after an exiting animation. */\n remove(key: string) {\n this.visibleToasts = this.visibleToasts.filter(t => t.key !== key);\n this.updateVisibleToasts();\n }\n\n private updateVisibleToasts(oldIndex = -1) {\n let toasts = this.queue.slice(0, this.maxVisibleToasts);\n if (this.hasExitAnimation) {\n let prevToasts: QueuedToast<T>[] = this.visibleToasts\n .filter(t => !toasts.some(t2 => t.key === t2.key))\n .map(t => ({...t, animation: 'exiting'}));\n\n if (oldIndex !== -1) {\n toasts.splice(oldIndex, 0, prevToasts?.[0]);\n }\n\n this.visibleToasts = toasts;\n } else {\n this.visibleToasts = toasts;\n }\n\n for (let fn of this.subscriptions) {\n fn();\n }\n }\n\n /** Pauses the timers for all visible toasts. */\n pauseAll() {\n for (let toast of this.visibleToasts) {\n if (toast.timer) {\n toast.timer.pause();\n }\n }\n }\n\n /** Resumes the timers for all visible toasts. */\n resumeAll() {\n for (let toast of this.visibleToasts) {\n if (toast.timer) {\n toast.timer.resume();\n }\n }\n }\n}\n\nclass Timer {\n private timerId;\n private startTime: number;\n private remaining: number;\n private callback: () => void;\n\n constructor(callback: () => void, delay: number) {\n this.remaining = delay;\n this.callback = callback;\n }\n\n reset(delay: number) {\n this.remaining = delay;\n this.resume();\n }\n\n pause() {\n if (this.timerId == null) {\n return;\n }\n\n clearTimeout(this.timerId);\n this.timerId = null;\n this.remaining -= Date.now() - this.startTime;\n }\n\n resume() {\n if (this.remaining <= 0) {\n return;\n }\n\n this.startTime = Date.now();\n this.timerId = setTimeout(() => {\n this.timerId = null;\n this.remaining = 0;\n this.callback();\n }, this.remaining);\n }\n}\n"],"names":[],"version":3,"file":"useToastState.module.js.map"}
|
|
1
|
+
{"mappings":";;;AAAA;;;;;;;;;;CAUC;;AA2DM,SAAS,0CAAiB,QAAyB,CAAC,CAAC;IAC1D,IAAI,oBAAC,mBAAmB,qBAAG,mBAAmB,OAAM,GAAG;IACvD,IAAI,QAAQ,CAAA,GAAA,cAAM,EAAE,IAAM,IAAI,0CAAc;8BAAC;8BAAkB;QAAgB,IAAI;QAAC;QAAkB;KAAiB;IACvH,OAAO,0CAAc;AACvB;AAKO,SAAS,0CAAiB,KAAoB;IACnD,IAAI,YAAY,CAAA,GAAA,kBAAU,EAAE,CAAA,KAAM,MAAM,SAAS,CAAC,KAAK;QAAC;KAAM;IAC9D,IAAI,c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;QAGF,IAAI,CAAC,SAAS,GAAG,KAAK,GAAG;QACzB,IAAI,CAAC,OAAO,GAAG,WAAW;YACxB,IAAI,CAAC,OAAO,GAAG;YACf,IAAI,CAAC,SAAS,GAAG;YACjB,IAAI,CAAC,QAAQ;QACf,GAAG,IAAI,CAAC,SAAS;IACnB;IA/BA,YAAY,QAAoB,EAAE,KAAa,CAAE;QAC/C,IAAI,CAAC,SAAS,GAAG;QACjB,IAAI,CAAC,QAAQ,GAAG;IAClB;AA6BF","sources":["packages/@react-stately/toast/src/useToastState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {useCallback, useMemo} from 'react';\n// Shim to support React 17 and below.\nimport {useSyncExternalStore} from 'use-sync-external-store/shim/index.js';\n\nexport interface ToastStateProps {\n /** The maximum number of toasts to display at a time. */\n maxVisibleToasts?: number,\n /**\n * Whether toasts have an exit animation. If true, toasts are not\n * removed immediately but transition into an \"exiting\" state instead.\n * Once the animation is complete, call the `remove` function.\n */\n hasExitAnimation?: boolean\n}\n\nexport interface ToastOptions {\n /** Handler that is called when the toast is closed, either by the user or after a timeout. */\n onClose?: () => void,\n /** A timeout to automatically close the toast after, in milliseconds. */\n timeout?: number,\n /** The priority of the toast relative to other toasts. Larger numbers indicate higher priority. */\n priority?: number\n}\n\nexport interface QueuedToast<T> extends ToastOptions {\n /** The content of the toast. */\n content: T,\n /** A unique key for the toast. */\n key: string,\n /** A timer for the toast, if a timeout was set. */\n timer?: Timer,\n /** The current animation state for the toast. */\n animation?: 'entering' | 'queued' | 'exiting'\n}\n\nexport interface ToastState<T> {\n /** Adds a new toast to the queue. */\n add(content: T, options?: ToastOptions): string,\n /**\n * Closes a toast. If `hasExitAnimation` is true, the toast\n * transitions to an \"exiting\" state instead of being removed immediately.\n */\n close(key: string): void,\n /** Removes a toast from the visible toasts after an exiting animation. */\n remove(key: string): void,\n /** Pauses the timers for all visible toasts. */\n pauseAll(): void,\n /** Resumes the timers for all visible toasts. */\n resumeAll(): void,\n /** The visible toasts. */\n visibleToasts: QueuedToast<T>[]\n}\n\n/**\n * Provides state management for a toast queue. Toasts display brief, temporary notifications\n * of actions, errors, or other events in an application.\n */\nexport function useToastState<T>(props: ToastStateProps = {}): ToastState<T> {\n let {maxVisibleToasts = 1, hasExitAnimation = false} = props;\n let queue = useMemo(() => new ToastQueue<T>({maxVisibleToasts, hasExitAnimation}), [maxVisibleToasts, hasExitAnimation]);\n return useToastQueue(queue);\n}\n\n/**\n * Subscribes to a provided toast queue and provides methods to update it.\n */\nexport function useToastQueue<T>(queue: ToastQueue<T>): ToastState<T> {\n let subscribe = useCallback(fn => queue.subscribe(fn), [queue]);\n let getSnapshot = useCallback(() => queue.visibleToasts, [queue]);\n let visibleToasts = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n return {\n visibleToasts,\n add: (content, options) => queue.add(content, options),\n close: key => queue.close(key),\n remove: key => queue.remove(key),\n pauseAll: () => queue.pauseAll(),\n resumeAll: () => queue.resumeAll()\n };\n}\n\n/**\n * A ToastQueue is a priority queue of toasts.\n */\nexport class ToastQueue<T> {\n private queue: QueuedToast<T>[] = [];\n private subscriptions: Set<() => void> = new Set();\n private maxVisibleToasts: number;\n private hasExitAnimation: boolean;\n /** The currently visible toasts. */\n visibleToasts: QueuedToast<T>[] = [];\n\n constructor(options?: ToastStateProps) {\n this.maxVisibleToasts = options?.maxVisibleToasts ?? 1;\n this.hasExitAnimation = options?.hasExitAnimation ?? false;\n }\n\n /** Subscribes to updates to the visible toasts. */\n subscribe(fn: () => void) {\n this.subscriptions.add(fn);\n return () => this.subscriptions.delete(fn);\n }\n\n /** Adds a new toast to the queue. */\n add(content: T, options: ToastOptions = {}) {\n let toastKey = Math.random().toString(36);\n let toast: QueuedToast<T> = {\n ...options,\n content,\n key: toastKey,\n timer: options.timeout ? new Timer(() => this.close(toastKey), options.timeout) : null\n };\n\n let low = 0;\n let high = this.queue.length;\n while (low < high) {\n let mid = Math.floor((low + high) / 2);\n if ((toast.priority || 0) > (this.queue[mid].priority || 0)) {\n high = mid;\n } else {\n low = mid + 1;\n }\n }\n\n this.queue.splice(low, 0, toast);\n\n toast.animation = low < this.maxVisibleToasts ? 'entering' : 'queued';\n let i = this.maxVisibleToasts;\n while (i < this.queue.length) {\n this.queue[i++].animation = 'queued';\n }\n\n this.updateVisibleToasts({action: 'add'});\n return toastKey;\n }\n\n /**\n * Closes a toast. If `hasExitAnimation` is true, the toast\n * transitions to an \"exiting\" state instead of being removed immediately.\n */\n close(key: string) {\n let index = this.queue.findIndex(t => t.key === key);\n if (index >= 0) {\n this.queue[index].onClose?.();\n this.queue.splice(index, 1);\n }\n\n this.updateVisibleToasts({action: 'close', key});\n }\n\n /** Removes a toast from the visible toasts after an exiting animation. */\n remove(key: string) {\n this.updateVisibleToasts({action: 'remove', key});\n }\n\n private updateVisibleToasts(options: {action: 'add' | 'close' | 'remove', key?: string}) {\n let {action, key} = options;\n let toasts = this.queue.slice(0, this.maxVisibleToasts);\n\n if (action === 'add' && this.hasExitAnimation) {\n let prevToasts: QueuedToast<T>[] = this.visibleToasts\n .filter(t => !toasts.some(t2 => t.key === t2.key))\n .map(t => ({...t, animation: 'exiting'}));\n this.visibleToasts = prevToasts.concat(toasts).sort((a, b) => b.priority - a.priority);\n } else if (action === 'close' && this.hasExitAnimation) {\n // Cause a rerender to happen for exit animation\n this.visibleToasts = this.visibleToasts.map(t => {\n if (t.key !== key) {\n return t;\n } else {\n return {...t, animation: 'exiting'};\n }\n });\n } else {\n this.visibleToasts = toasts;\n }\n\n for (let fn of this.subscriptions) {\n fn();\n }\n }\n\n /** Pauses the timers for all visible toasts. */\n pauseAll() {\n for (let toast of this.visibleToasts) {\n if (toast.timer) {\n toast.timer.pause();\n }\n }\n }\n\n /** Resumes the timers for all visible toasts. */\n resumeAll() {\n for (let toast of this.visibleToasts) {\n if (toast.timer) {\n toast.timer.resume();\n }\n }\n }\n}\n\nclass Timer {\n private timerId;\n private startTime: number;\n private remaining: number;\n private callback: () => void;\n\n constructor(callback: () => void, delay: number) {\n this.remaining = delay;\n this.callback = callback;\n }\n\n reset(delay: number) {\n this.remaining = delay;\n this.resume();\n }\n\n pause() {\n if (this.timerId == null) {\n return;\n }\n\n clearTimeout(this.timerId);\n this.timerId = null;\n this.remaining -= Date.now() - this.startTime;\n }\n\n resume() {\n if (this.remaining <= 0) {\n return;\n }\n\n this.startTime = Date.now();\n this.timerId = setTimeout(() => {\n this.timerId = null;\n this.remaining = 0;\n this.callback();\n }, this.remaining);\n }\n}\n"],"names":[],"version":3,"file":"useToastState.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-stately/toast",
|
|
3
|
-
"version": "3.0.0-nightly.
|
|
3
|
+
"version": "3.0.0-nightly.4595+0e05b7e4d",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"publishConfig": {
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "0e05b7e4dab3dabb875ae5ac21b32f75145ab527"
|
|
38
38
|
}
|
package/src/useToastState.ts
CHANGED
|
@@ -134,11 +134,7 @@ export class ToastQueue<T> {
|
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
|
|
138
|
-
this.queue.splice(low, 0, toast);
|
|
139
|
-
} else {
|
|
140
|
-
this.queue.unshift(toast);
|
|
141
|
-
}
|
|
137
|
+
this.queue.splice(low, 0, toast);
|
|
142
138
|
|
|
143
139
|
toast.animation = low < this.maxVisibleToasts ? 'entering' : 'queued';
|
|
144
140
|
let i = this.maxVisibleToasts;
|
|
@@ -146,7 +142,7 @@ export class ToastQueue<T> {
|
|
|
146
142
|
this.queue[i++].animation = 'queued';
|
|
147
143
|
}
|
|
148
144
|
|
|
149
|
-
this.updateVisibleToasts();
|
|
145
|
+
this.updateVisibleToasts({action: 'add'});
|
|
150
146
|
return toastKey;
|
|
151
147
|
}
|
|
152
148
|
|
|
@@ -161,27 +157,32 @@ export class ToastQueue<T> {
|
|
|
161
157
|
this.queue.splice(index, 1);
|
|
162
158
|
}
|
|
163
159
|
|
|
164
|
-
this.updateVisibleToasts(
|
|
160
|
+
this.updateVisibleToasts({action: 'close', key});
|
|
165
161
|
}
|
|
166
162
|
|
|
167
163
|
/** Removes a toast from the visible toasts after an exiting animation. */
|
|
168
164
|
remove(key: string) {
|
|
169
|
-
this.
|
|
170
|
-
this.updateVisibleToasts();
|
|
165
|
+
this.updateVisibleToasts({action: 'remove', key});
|
|
171
166
|
}
|
|
172
167
|
|
|
173
|
-
private updateVisibleToasts(
|
|
168
|
+
private updateVisibleToasts(options: {action: 'add' | 'close' | 'remove', key?: string}) {
|
|
169
|
+
let {action, key} = options;
|
|
174
170
|
let toasts = this.queue.slice(0, this.maxVisibleToasts);
|
|
175
|
-
if (this.hasExitAnimation) {
|
|
176
|
-
let prevToasts: QueuedToast<T>[] = this.visibleToasts
|
|
177
|
-
.filter(t => !toasts.some(t2 => t.key === t2.key))
|
|
178
|
-
.map(t => ({...t, animation: 'exiting'}));
|
|
179
171
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
this.visibleToasts = toasts;
|
|
172
|
+
if (action === 'add' && this.hasExitAnimation) {
|
|
173
|
+
let prevToasts: QueuedToast<T>[] = this.visibleToasts
|
|
174
|
+
.filter(t => !toasts.some(t2 => t.key === t2.key))
|
|
175
|
+
.map(t => ({...t, animation: 'exiting'}));
|
|
176
|
+
this.visibleToasts = prevToasts.concat(toasts).sort((a, b) => b.priority - a.priority);
|
|
177
|
+
} else if (action === 'close' && this.hasExitAnimation) {
|
|
178
|
+
// Cause a rerender to happen for exit animation
|
|
179
|
+
this.visibleToasts = this.visibleToasts.map(t => {
|
|
180
|
+
if (t.key !== key) {
|
|
181
|
+
return t;
|
|
182
|
+
} else {
|
|
183
|
+
return {...t, animation: 'exiting'};
|
|
184
|
+
}
|
|
185
|
+
});
|
|
185
186
|
} else {
|
|
186
187
|
this.visibleToasts = toasts;
|
|
187
188
|
}
|