@react-stately/toast 3.0.0-nightly.4569 → 3.0.0-nightly.4578
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 +1 -2
- package/dist/useToastState.main.js.map +1 -1
- package/dist/useToastState.mjs +1 -2
- package/dist/useToastState.module.js +1 -2
- package/dist/useToastState.module.js.map +1 -1
- package/package.json +2 -2
- package/src/useToastState.ts +1 -2
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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C;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;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;IAoC1C;;;OAGG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM;IAUjB,0EAA0E;IAC1E,MAAM,CAAC,GAAG,EAAE,MAAM;IA0BlB,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"}
|
|
@@ -90,9 +90,8 @@ class $c995945a643dde76$export$f1f8569633bbbec4 {
|
|
|
90
90
|
this.updateVisibleToasts(index);
|
|
91
91
|
}
|
|
92
92
|
/** Removes a toast from the visible toasts after an exiting animation. */ remove(key) {
|
|
93
|
-
let index = this.queue.findIndex((t)=>t.key === key);
|
|
94
93
|
this.visibleToasts = this.visibleToasts.filter((t)=>t.key !== key);
|
|
95
|
-
this.updateVisibleToasts(
|
|
94
|
+
this.updateVisibleToasts();
|
|
96
95
|
}
|
|
97
96
|
updateVisibleToasts(oldIndex = -1) {
|
|
98
97
|
let toasts = this.queue.slice(0, this.maxVisibleToasts);
|
|
@@ -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;IACnD,IAAI,YAAY,CAAA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AK,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 let index = this.queue.findIndex(t => t.key === key);\n this.visibleToasts = this.visibleToasts.filter(t => t.key !== key);\n this.updateVisibleToasts(index);\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,UAA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AAa,CAClC,IAAI,MAAM,KAAK,EACb,MAAM,KAAK,CAAC,MAAM;IAGxB;IAzGA,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;AAuGF;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 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"}
|
package/dist/useToastState.mjs
CHANGED
|
@@ -82,9 +82,8 @@ class $77b352cf12efcf73$export$f1f8569633bbbec4 {
|
|
|
82
82
|
this.updateVisibleToasts(index);
|
|
83
83
|
}
|
|
84
84
|
/** Removes a toast from the visible toasts after an exiting animation. */ remove(key) {
|
|
85
|
-
let index = this.queue.findIndex((t)=>t.key === key);
|
|
86
85
|
this.visibleToasts = this.visibleToasts.filter((t)=>t.key !== key);
|
|
87
|
-
this.updateVisibleToasts(
|
|
86
|
+
this.updateVisibleToasts();
|
|
88
87
|
}
|
|
89
88
|
updateVisibleToasts(oldIndex = -1) {
|
|
90
89
|
let toasts = this.queue.slice(0, this.maxVisibleToasts);
|
|
@@ -82,9 +82,8 @@ class $77b352cf12efcf73$export$f1f8569633bbbec4 {
|
|
|
82
82
|
this.updateVisibleToasts(index);
|
|
83
83
|
}
|
|
84
84
|
/** Removes a toast from the visible toasts after an exiting animation. */ remove(key) {
|
|
85
|
-
let index = this.queue.findIndex((t)=>t.key === key);
|
|
86
85
|
this.visibleToasts = this.visibleToasts.filter((t)=>t.key !== key);
|
|
87
|
-
this.updateVisibleToasts(
|
|
86
|
+
this.updateVisibleToasts();
|
|
88
87
|
}
|
|
89
88
|
updateVisibleToasts(oldIndex = -1) {
|
|
90
89
|
let toasts = this.queue.slice(0, this.maxVisibleToasts);
|
|
@@ -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,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,SA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AAW;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 let index = this.queue.findIndex(t => t.key === key);\n this.visibleToasts = this.visibleToasts.filter(t => t.key !== key);\n this.updateVisibleToasts(index);\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,cAAc,CAAA,GAAA,kBAAU,EAAE,IAAM,MAAM,aAAa,EAAE;QAAC;KAAM;IAChE,IAAI,gBAAgB,CAAA,GAAA,2BAAmB,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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MAAM;IAGxB;IAzGA,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;AAuGF;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 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"}
|
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.4578+b09a14eb1",
|
|
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": "b09a14eb1854d9d52e08739e30aa8fae51f1595a"
|
|
38
38
|
}
|
package/src/useToastState.ts
CHANGED
|
@@ -166,9 +166,8 @@ export class ToastQueue<T> {
|
|
|
166
166
|
|
|
167
167
|
/** Removes a toast from the visible toasts after an exiting animation. */
|
|
168
168
|
remove(key: string) {
|
|
169
|
-
let index = this.queue.findIndex(t => t.key === key);
|
|
170
169
|
this.visibleToasts = this.visibleToasts.filter(t => t.key !== key);
|
|
171
|
-
this.updateVisibleToasts(
|
|
170
|
+
this.updateVisibleToasts();
|
|
172
171
|
}
|
|
173
172
|
|
|
174
173
|
private updateVisibleToasts(oldIndex = -1) {
|