@react-stately/toast 3.0.0-nightly.4552 → 3.0.0-nightly.4558

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/import.mjs CHANGED
@@ -1,17 +1,6 @@
1
- import {useMemo as $jopoU$useMemo, useCallback as $jopoU$useCallback} from "react";
2
- import {useSyncExternalStore as $jopoU$useSyncExternalStore} from "use-sync-external-store/shim/index.js";
1
+ import {ToastQueue as $77b352cf12efcf73$export$f1f8569633bbbec4, useToastQueue as $77b352cf12efcf73$export$84726ef35ca2129a, useToastState as $77b352cf12efcf73$export$c7b26b20d3ced9c5} from "./useToastState.mjs";
3
2
 
4
3
  /*
5
- * Copyright 2020 Adobe. All rights reserved.
6
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7
- * you may not use this file except in compliance with the License. You may obtain a copy
8
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software distributed under
11
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12
- * OF ANY KIND, either express or implied. See the License for the specific language
13
- * governing permissions and limitations under the License.
14
- */ /*
15
4
  * Copyright 2020 Adobe. All rights reserved.
16
5
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
17
6
  * you may not use this file except in compliance with the License. You may obtain a copy
@@ -23,135 +12,6 @@ import {useSyncExternalStore as $jopoU$useSyncExternalStore} from "use-sync-exte
23
12
  * governing permissions and limitations under the License.
24
13
  */
25
14
 
26
- function $77b352cf12efcf73$export$c7b26b20d3ced9c5(props = {}) {
27
- let { maxVisibleToasts: maxVisibleToasts = 1, hasExitAnimation: hasExitAnimation = false } = props;
28
- let queue = (0, $jopoU$useMemo)(()=>new $77b352cf12efcf73$export$f1f8569633bbbec4({
29
- maxVisibleToasts: maxVisibleToasts,
30
- hasExitAnimation: hasExitAnimation
31
- }), [
32
- maxVisibleToasts,
33
- hasExitAnimation
34
- ]);
35
- return $77b352cf12efcf73$export$84726ef35ca2129a(queue);
36
- }
37
- function $77b352cf12efcf73$export$84726ef35ca2129a(queue) {
38
- let subscribe = (0, $jopoU$useCallback)((fn)=>queue.subscribe(fn), [
39
- queue
40
- ]);
41
- let getSnapshot = (0, $jopoU$useCallback)(()=>queue.visibleToasts, [
42
- queue
43
- ]);
44
- let visibleToasts = (0, $jopoU$useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
45
- return {
46
- visibleToasts: visibleToasts,
47
- add: (content, options)=>queue.add(content, options),
48
- close: (key)=>queue.close(key),
49
- remove: (key)=>queue.remove(key),
50
- pauseAll: ()=>queue.pauseAll(),
51
- resumeAll: ()=>queue.resumeAll()
52
- };
53
- }
54
- class $77b352cf12efcf73$export$f1f8569633bbbec4 {
55
- /** Subscribes to updates to the visible toasts. */ subscribe(fn) {
56
- this.subscriptions.add(fn);
57
- return ()=>this.subscriptions.delete(fn);
58
- }
59
- /** Adds a new toast to the queue. */ add(content, options = {}) {
60
- let toastKey = Math.random().toString(36);
61
- let toast = {
62
- ...options,
63
- content: content,
64
- key: toastKey,
65
- timer: options.timeout ? new $77b352cf12efcf73$var$Timer(()=>this.close(toastKey), options.timeout) : null
66
- };
67
- let low = 0;
68
- let high = this.queue.length;
69
- while(low < high){
70
- let mid = Math.floor((low + high) / 2);
71
- if ((toast.priority || 0) > (this.queue[mid].priority || 0)) high = mid;
72
- else low = mid + 1;
73
- }
74
- if (toast.priority) this.queue.splice(low, 0, toast);
75
- else this.queue.unshift(toast);
76
- toast.animation = low < this.maxVisibleToasts ? "entering" : "queued";
77
- let i = this.maxVisibleToasts;
78
- while(i < this.queue.length)this.queue[i++].animation = "queued";
79
- this.updateVisibleToasts();
80
- return toastKey;
81
- }
82
- /**
83
- * Closes a toast. If `hasExitAnimation` is true, the toast
84
- * transitions to an "exiting" state instead of being removed immediately.
85
- */ close(key) {
86
- let index = this.queue.findIndex((t)=>t.key === key);
87
- if (index >= 0) {
88
- var _this_queue_index_onClose, _this_queue_index;
89
- (_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);
90
- this.queue.splice(index, 1);
91
- }
92
- this.updateVisibleToasts(index);
93
- }
94
- /** Removes a toast from the visible toasts after an exiting animation. */ remove(key) {
95
- let index = this.queue.findIndex((t)=>t.key === key);
96
- this.visibleToasts = this.visibleToasts.filter((t)=>t.key !== key);
97
- this.updateVisibleToasts(index);
98
- }
99
- updateVisibleToasts(oldIndex = -1) {
100
- let toasts = this.queue.slice(0, this.maxVisibleToasts);
101
- if (this.hasExitAnimation) {
102
- let prevToasts = this.visibleToasts.filter((t)=>!toasts.some((t2)=>t.key === t2.key)).map((t)=>({
103
- ...t,
104
- animation: "exiting"
105
- }));
106
- if (oldIndex !== -1) toasts.splice(oldIndex, 0, prevToasts === null || prevToasts === void 0 ? void 0 : prevToasts[0]);
107
- this.visibleToasts = toasts;
108
- } else this.visibleToasts = toasts;
109
- for (let fn of this.subscriptions)fn();
110
- }
111
- /** Pauses the timers for all visible toasts. */ pauseAll() {
112
- for (let toast of this.visibleToasts)if (toast.timer) toast.timer.pause();
113
- }
114
- /** Resumes the timers for all visible toasts. */ resumeAll() {
115
- for (let toast of this.visibleToasts)if (toast.timer) toast.timer.resume();
116
- }
117
- constructor(options){
118
- this.queue = [];
119
- this.subscriptions = new Set();
120
- /** The currently visible toasts. */ this.visibleToasts = [];
121
- var _options_maxVisibleToasts;
122
- this.maxVisibleToasts = (_options_maxVisibleToasts = options === null || options === void 0 ? void 0 : options.maxVisibleToasts) !== null && _options_maxVisibleToasts !== void 0 ? _options_maxVisibleToasts : 1;
123
- var _options_hasExitAnimation;
124
- this.hasExitAnimation = (_options_hasExitAnimation = options === null || options === void 0 ? void 0 : options.hasExitAnimation) !== null && _options_hasExitAnimation !== void 0 ? _options_hasExitAnimation : false;
125
- }
126
- }
127
- class $77b352cf12efcf73$var$Timer {
128
- reset(delay) {
129
- this.remaining = delay;
130
- this.resume();
131
- }
132
- pause() {
133
- if (this.timerId == null) return;
134
- clearTimeout(this.timerId);
135
- this.timerId = null;
136
- this.remaining -= Date.now() - this.startTime;
137
- }
138
- resume() {
139
- if (this.remaining <= 0) return;
140
- this.startTime = Date.now();
141
- this.timerId = setTimeout(()=>{
142
- this.timerId = null;
143
- this.remaining = 0;
144
- this.callback();
145
- }, this.remaining);
146
- }
147
- constructor(callback, delay){
148
- this.remaining = delay;
149
- this.callback = callback;
150
- }
151
- }
152
-
153
-
154
-
155
15
 
156
16
  export {$77b352cf12efcf73$export$c7b26b20d3ced9c5 as useToastState, $77b352cf12efcf73$export$f1f8569633bbbec4 as ToastQueue, $77b352cf12efcf73$export$84726ef35ca2129a as useToastQueue};
157
17
  //# sourceMappingURL=module.js.map
package/dist/main.js CHANGED
@@ -1,25 +1,14 @@
1
- var $1uBRd$react = require("react");
2
- var $1uBRd$usesyncexternalstoreshimindexjs = require("use-sync-external-store/shim/index.js");
1
+ var $c995945a643dde76$exports = require("./useToastState.main.js");
3
2
 
4
3
 
5
4
  function $parcel$export(e, n, v, s) {
6
5
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
7
6
  }
8
7
 
9
- $parcel$export(module.exports, "useToastState", () => $c995945a643dde76$export$c7b26b20d3ced9c5);
10
- $parcel$export(module.exports, "ToastQueue", () => $c995945a643dde76$export$f1f8569633bbbec4);
11
- $parcel$export(module.exports, "useToastQueue", () => $c995945a643dde76$export$84726ef35ca2129a);
8
+ $parcel$export(module.exports, "useToastState", () => $c995945a643dde76$exports.useToastState);
9
+ $parcel$export(module.exports, "ToastQueue", () => $c995945a643dde76$exports.ToastQueue);
10
+ $parcel$export(module.exports, "useToastQueue", () => $c995945a643dde76$exports.useToastQueue);
12
11
  /*
13
- * Copyright 2020 Adobe. All rights reserved.
14
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
15
- * you may not use this file except in compliance with the License. You may obtain a copy
16
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
17
- *
18
- * Unless required by applicable law or agreed to in writing, software distributed under
19
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
20
- * OF ANY KIND, either express or implied. See the License for the specific language
21
- * governing permissions and limitations under the License.
22
- */ /*
23
12
  * Copyright 2020 Adobe. All rights reserved.
24
13
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
25
14
  * you may not use this file except in compliance with the License. You may obtain a copy
@@ -31,134 +20,5 @@ $parcel$export(module.exports, "useToastQueue", () => $c995945a643dde76$export$8
31
20
  * governing permissions and limitations under the License.
32
21
  */
33
22
 
34
- function $c995945a643dde76$export$c7b26b20d3ced9c5(props = {}) {
35
- let { maxVisibleToasts: maxVisibleToasts = 1, hasExitAnimation: hasExitAnimation = false } = props;
36
- let queue = (0, $1uBRd$react.useMemo)(()=>new $c995945a643dde76$export$f1f8569633bbbec4({
37
- maxVisibleToasts: maxVisibleToasts,
38
- hasExitAnimation: hasExitAnimation
39
- }), [
40
- maxVisibleToasts,
41
- hasExitAnimation
42
- ]);
43
- return $c995945a643dde76$export$84726ef35ca2129a(queue);
44
- }
45
- function $c995945a643dde76$export$84726ef35ca2129a(queue) {
46
- let subscribe = (0, $1uBRd$react.useCallback)((fn)=>queue.subscribe(fn), [
47
- queue
48
- ]);
49
- let getSnapshot = (0, $1uBRd$react.useCallback)(()=>queue.visibleToasts, [
50
- queue
51
- ]);
52
- let visibleToasts = (0, $1uBRd$usesyncexternalstoreshimindexjs.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
53
- return {
54
- visibleToasts: visibleToasts,
55
- add: (content, options)=>queue.add(content, options),
56
- close: (key)=>queue.close(key),
57
- remove: (key)=>queue.remove(key),
58
- pauseAll: ()=>queue.pauseAll(),
59
- resumeAll: ()=>queue.resumeAll()
60
- };
61
- }
62
- class $c995945a643dde76$export$f1f8569633bbbec4 {
63
- /** Subscribes to updates to the visible toasts. */ subscribe(fn) {
64
- this.subscriptions.add(fn);
65
- return ()=>this.subscriptions.delete(fn);
66
- }
67
- /** Adds a new toast to the queue. */ add(content, options = {}) {
68
- let toastKey = Math.random().toString(36);
69
- let toast = {
70
- ...options,
71
- content: content,
72
- key: toastKey,
73
- timer: options.timeout ? new $c995945a643dde76$var$Timer(()=>this.close(toastKey), options.timeout) : null
74
- };
75
- let low = 0;
76
- let high = this.queue.length;
77
- while(low < high){
78
- let mid = Math.floor((low + high) / 2);
79
- if ((toast.priority || 0) > (this.queue[mid].priority || 0)) high = mid;
80
- else low = mid + 1;
81
- }
82
- if (toast.priority) this.queue.splice(low, 0, toast);
83
- else this.queue.unshift(toast);
84
- toast.animation = low < this.maxVisibleToasts ? "entering" : "queued";
85
- let i = this.maxVisibleToasts;
86
- while(i < this.queue.length)this.queue[i++].animation = "queued";
87
- this.updateVisibleToasts();
88
- return toastKey;
89
- }
90
- /**
91
- * Closes a toast. If `hasExitAnimation` is true, the toast
92
- * transitions to an "exiting" state instead of being removed immediately.
93
- */ close(key) {
94
- let index = this.queue.findIndex((t)=>t.key === key);
95
- if (index >= 0) {
96
- var _this_queue_index_onClose, _this_queue_index;
97
- (_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);
98
- this.queue.splice(index, 1);
99
- }
100
- this.updateVisibleToasts(index);
101
- }
102
- /** Removes a toast from the visible toasts after an exiting animation. */ remove(key) {
103
- let index = this.queue.findIndex((t)=>t.key === key);
104
- this.visibleToasts = this.visibleToasts.filter((t)=>t.key !== key);
105
- this.updateVisibleToasts(index);
106
- }
107
- updateVisibleToasts(oldIndex = -1) {
108
- let toasts = this.queue.slice(0, this.maxVisibleToasts);
109
- if (this.hasExitAnimation) {
110
- let prevToasts = this.visibleToasts.filter((t)=>!toasts.some((t2)=>t.key === t2.key)).map((t)=>({
111
- ...t,
112
- animation: "exiting"
113
- }));
114
- if (oldIndex !== -1) toasts.splice(oldIndex, 0, prevToasts === null || prevToasts === void 0 ? void 0 : prevToasts[0]);
115
- this.visibleToasts = toasts;
116
- } else this.visibleToasts = toasts;
117
- for (let fn of this.subscriptions)fn();
118
- }
119
- /** Pauses the timers for all visible toasts. */ pauseAll() {
120
- for (let toast of this.visibleToasts)if (toast.timer) toast.timer.pause();
121
- }
122
- /** Resumes the timers for all visible toasts. */ resumeAll() {
123
- for (let toast of this.visibleToasts)if (toast.timer) toast.timer.resume();
124
- }
125
- constructor(options){
126
- this.queue = [];
127
- this.subscriptions = new Set();
128
- /** The currently visible toasts. */ this.visibleToasts = [];
129
- var _options_maxVisibleToasts;
130
- this.maxVisibleToasts = (_options_maxVisibleToasts = options === null || options === void 0 ? void 0 : options.maxVisibleToasts) !== null && _options_maxVisibleToasts !== void 0 ? _options_maxVisibleToasts : 1;
131
- var _options_hasExitAnimation;
132
- this.hasExitAnimation = (_options_hasExitAnimation = options === null || options === void 0 ? void 0 : options.hasExitAnimation) !== null && _options_hasExitAnimation !== void 0 ? _options_hasExitAnimation : false;
133
- }
134
- }
135
- class $c995945a643dde76$var$Timer {
136
- reset(delay) {
137
- this.remaining = delay;
138
- this.resume();
139
- }
140
- pause() {
141
- if (this.timerId == null) return;
142
- clearTimeout(this.timerId);
143
- this.timerId = null;
144
- this.remaining -= Date.now() - this.startTime;
145
- }
146
- resume() {
147
- if (this.remaining <= 0) return;
148
- this.startTime = Date.now();
149
- this.timerId = setTimeout(()=>{
150
- this.timerId = null;
151
- this.remaining = 0;
152
- this.callback();
153
- }, this.remaining);
154
- }
155
- constructor(callback, delay){
156
- this.remaining = delay;
157
- this.callback = callback;
158
- }
159
- }
160
-
161
-
162
-
163
23
 
164
24
  //# sourceMappingURL=main.js.map
package/dist/main.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;;AA2DM,SAAS,0CAAiB,QAAyB,CAAC,CAAC;IAC1D,IAAI,oBAAC,mBAAmB,qBAAG,m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a,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;;CDjPC","sources":["packages/@react-stately/toast/src/index.ts","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 */\nexport {useToastState, ToastQueue, useToastQueue} from './useToastState';\n\nexport type {ToastState, QueuedToast, ToastStateProps, ToastOptions} from './useToastState';\n","/*\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":"main.js.map"}
1
+ {"mappings":";;;;;;;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/toast/src/index.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 */\nexport {useToastState, ToastQueue, useToastQueue} from './useToastState';\n\nexport type {ToastState, QueuedToast, ToastStateProps, ToastOptions} from './useToastState';\n"],"names":[],"version":3,"file":"main.js.map"}
package/dist/module.js CHANGED
@@ -1,17 +1,6 @@
1
- import {useMemo as $jopoU$useMemo, useCallback as $jopoU$useCallback} from "react";
2
- import {useSyncExternalStore as $jopoU$useSyncExternalStore} from "use-sync-external-store/shim/index.js";
1
+ import {ToastQueue as $77b352cf12efcf73$export$f1f8569633bbbec4, useToastQueue as $77b352cf12efcf73$export$84726ef35ca2129a, useToastState as $77b352cf12efcf73$export$c7b26b20d3ced9c5} from "./useToastState.module.js";
3
2
 
4
3
  /*
5
- * Copyright 2020 Adobe. All rights reserved.
6
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7
- * you may not use this file except in compliance with the License. You may obtain a copy
8
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software distributed under
11
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12
- * OF ANY KIND, either express or implied. See the License for the specific language
13
- * governing permissions and limitations under the License.
14
- */ /*
15
4
  * Copyright 2020 Adobe. All rights reserved.
16
5
  * This file is licensed to you under the Apache License, Version 2.0 (the "License");
17
6
  * you may not use this file except in compliance with the License. You may obtain a copy
@@ -23,135 +12,6 @@ import {useSyncExternalStore as $jopoU$useSyncExternalStore} from "use-sync-exte
23
12
  * governing permissions and limitations under the License.
24
13
  */
25
14
 
26
- function $77b352cf12efcf73$export$c7b26b20d3ced9c5(props = {}) {
27
- let { maxVisibleToasts: maxVisibleToasts = 1, hasExitAnimation: hasExitAnimation = false } = props;
28
- let queue = (0, $jopoU$useMemo)(()=>new $77b352cf12efcf73$export$f1f8569633bbbec4({
29
- maxVisibleToasts: maxVisibleToasts,
30
- hasExitAnimation: hasExitAnimation
31
- }), [
32
- maxVisibleToasts,
33
- hasExitAnimation
34
- ]);
35
- return $77b352cf12efcf73$export$84726ef35ca2129a(queue);
36
- }
37
- function $77b352cf12efcf73$export$84726ef35ca2129a(queue) {
38
- let subscribe = (0, $jopoU$useCallback)((fn)=>queue.subscribe(fn), [
39
- queue
40
- ]);
41
- let getSnapshot = (0, $jopoU$useCallback)(()=>queue.visibleToasts, [
42
- queue
43
- ]);
44
- let visibleToasts = (0, $jopoU$useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
45
- return {
46
- visibleToasts: visibleToasts,
47
- add: (content, options)=>queue.add(content, options),
48
- close: (key)=>queue.close(key),
49
- remove: (key)=>queue.remove(key),
50
- pauseAll: ()=>queue.pauseAll(),
51
- resumeAll: ()=>queue.resumeAll()
52
- };
53
- }
54
- class $77b352cf12efcf73$export$f1f8569633bbbec4 {
55
- /** Subscribes to updates to the visible toasts. */ subscribe(fn) {
56
- this.subscriptions.add(fn);
57
- return ()=>this.subscriptions.delete(fn);
58
- }
59
- /** Adds a new toast to the queue. */ add(content, options = {}) {
60
- let toastKey = Math.random().toString(36);
61
- let toast = {
62
- ...options,
63
- content: content,
64
- key: toastKey,
65
- timer: options.timeout ? new $77b352cf12efcf73$var$Timer(()=>this.close(toastKey), options.timeout) : null
66
- };
67
- let low = 0;
68
- let high = this.queue.length;
69
- while(low < high){
70
- let mid = Math.floor((low + high) / 2);
71
- if ((toast.priority || 0) > (this.queue[mid].priority || 0)) high = mid;
72
- else low = mid + 1;
73
- }
74
- if (toast.priority) this.queue.splice(low, 0, toast);
75
- else this.queue.unshift(toast);
76
- toast.animation = low < this.maxVisibleToasts ? "entering" : "queued";
77
- let i = this.maxVisibleToasts;
78
- while(i < this.queue.length)this.queue[i++].animation = "queued";
79
- this.updateVisibleToasts();
80
- return toastKey;
81
- }
82
- /**
83
- * Closes a toast. If `hasExitAnimation` is true, the toast
84
- * transitions to an "exiting" state instead of being removed immediately.
85
- */ close(key) {
86
- let index = this.queue.findIndex((t)=>t.key === key);
87
- if (index >= 0) {
88
- var _this_queue_index_onClose, _this_queue_index;
89
- (_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);
90
- this.queue.splice(index, 1);
91
- }
92
- this.updateVisibleToasts(index);
93
- }
94
- /** Removes a toast from the visible toasts after an exiting animation. */ remove(key) {
95
- let index = this.queue.findIndex((t)=>t.key === key);
96
- this.visibleToasts = this.visibleToasts.filter((t)=>t.key !== key);
97
- this.updateVisibleToasts(index);
98
- }
99
- updateVisibleToasts(oldIndex = -1) {
100
- let toasts = this.queue.slice(0, this.maxVisibleToasts);
101
- if (this.hasExitAnimation) {
102
- let prevToasts = this.visibleToasts.filter((t)=>!toasts.some((t2)=>t.key === t2.key)).map((t)=>({
103
- ...t,
104
- animation: "exiting"
105
- }));
106
- if (oldIndex !== -1) toasts.splice(oldIndex, 0, prevToasts === null || prevToasts === void 0 ? void 0 : prevToasts[0]);
107
- this.visibleToasts = toasts;
108
- } else this.visibleToasts = toasts;
109
- for (let fn of this.subscriptions)fn();
110
- }
111
- /** Pauses the timers for all visible toasts. */ pauseAll() {
112
- for (let toast of this.visibleToasts)if (toast.timer) toast.timer.pause();
113
- }
114
- /** Resumes the timers for all visible toasts. */ resumeAll() {
115
- for (let toast of this.visibleToasts)if (toast.timer) toast.timer.resume();
116
- }
117
- constructor(options){
118
- this.queue = [];
119
- this.subscriptions = new Set();
120
- /** The currently visible toasts. */ this.visibleToasts = [];
121
- var _options_maxVisibleToasts;
122
- this.maxVisibleToasts = (_options_maxVisibleToasts = options === null || options === void 0 ? void 0 : options.maxVisibleToasts) !== null && _options_maxVisibleToasts !== void 0 ? _options_maxVisibleToasts : 1;
123
- var _options_hasExitAnimation;
124
- this.hasExitAnimation = (_options_hasExitAnimation = options === null || options === void 0 ? void 0 : options.hasExitAnimation) !== null && _options_hasExitAnimation !== void 0 ? _options_hasExitAnimation : false;
125
- }
126
- }
127
- class $77b352cf12efcf73$var$Timer {
128
- reset(delay) {
129
- this.remaining = delay;
130
- this.resume();
131
- }
132
- pause() {
133
- if (this.timerId == null) return;
134
- clearTimeout(this.timerId);
135
- this.timerId = null;
136
- this.remaining -= Date.now() - this.startTime;
137
- }
138
- resume() {
139
- if (this.remaining <= 0) return;
140
- this.startTime = Date.now();
141
- this.timerId = setTimeout(()=>{
142
- this.timerId = null;
143
- this.remaining = 0;
144
- this.callback();
145
- }, this.remaining);
146
- }
147
- constructor(callback, delay){
148
- this.remaining = delay;
149
- this.callback = callback;
150
- }
151
- }
152
-
153
-
154
-
155
15
 
156
16
  export {$77b352cf12efcf73$export$c7b26b20d3ced9c5 as useToastState, $77b352cf12efcf73$export$f1f8569633bbbec4 as ToastQueue, $77b352cf12efcf73$export$84726ef35ca2129a as useToastQueue};
157
17
  //# sourceMappingURL=module.js.map
@@ -1 +1 @@
1
- {"mappings":";;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;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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AAG,IAAI,CAAC,SAAS;IACnB;IA/BA,YAAY,QAAoB,EAAE,KAAa,CAAE;QAC/C,IAAI,CAAC,SAAS,GAAG;QACjB,IAAI,CAAC,QAAQ,GAAG;IAClB;AA6BF;;CDjPC","sources":["packages/@react-stately/toast/src/index.ts","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 */\nexport {useToastState, ToastQueue, useToastQueue} from './useToastState';\n\nexport type {ToastState, QueuedToast, ToastStateProps, ToastOptions} from './useToastState';\n","/*\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":"module.js.map"}
1
+ {"mappings":";;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/toast/src/index.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 */\nexport {useToastState, ToastQueue, useToastQueue} from './useToastState';\n\nexport type {ToastState, QueuedToast, ToastStateProps, ToastOptions} from './useToastState';\n"],"names":[],"version":3,"file":"module.js.map"}
@@ -0,0 +1,152 @@
1
+ var $pwpJQ$react = require("react");
2
+ var $pwpJQ$usesyncexternalstoreshimindexjs = require("use-sync-external-store/shim/index.js");
3
+
4
+
5
+ function $parcel$export(e, n, v, s) {
6
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
7
+ }
8
+
9
+ $parcel$export(module.exports, "useToastState", () => $c995945a643dde76$export$c7b26b20d3ced9c5);
10
+ $parcel$export(module.exports, "ToastQueue", () => $c995945a643dde76$export$f1f8569633bbbec4);
11
+ $parcel$export(module.exports, "useToastQueue", () => $c995945a643dde76$export$84726ef35ca2129a);
12
+ /*
13
+ * Copyright 2020 Adobe. All rights reserved.
14
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
15
+ * you may not use this file except in compliance with the License. You may obtain a copy
16
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software distributed under
19
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
20
+ * OF ANY KIND, either express or implied. See the License for the specific language
21
+ * governing permissions and limitations under the License.
22
+ */
23
+
24
+ function $c995945a643dde76$export$c7b26b20d3ced9c5(props = {}) {
25
+ let { maxVisibleToasts: maxVisibleToasts = 1, hasExitAnimation: hasExitAnimation = false } = props;
26
+ let queue = (0, $pwpJQ$react.useMemo)(()=>new $c995945a643dde76$export$f1f8569633bbbec4({
27
+ maxVisibleToasts: maxVisibleToasts,
28
+ hasExitAnimation: hasExitAnimation
29
+ }), [
30
+ maxVisibleToasts,
31
+ hasExitAnimation
32
+ ]);
33
+ return $c995945a643dde76$export$84726ef35ca2129a(queue);
34
+ }
35
+ function $c995945a643dde76$export$84726ef35ca2129a(queue) {
36
+ let subscribe = (0, $pwpJQ$react.useCallback)((fn)=>queue.subscribe(fn), [
37
+ queue
38
+ ]);
39
+ let getSnapshot = (0, $pwpJQ$react.useCallback)(()=>queue.visibleToasts, [
40
+ queue
41
+ ]);
42
+ let visibleToasts = (0, $pwpJQ$usesyncexternalstoreshimindexjs.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
43
+ return {
44
+ visibleToasts: visibleToasts,
45
+ add: (content, options)=>queue.add(content, options),
46
+ close: (key)=>queue.close(key),
47
+ remove: (key)=>queue.remove(key),
48
+ pauseAll: ()=>queue.pauseAll(),
49
+ resumeAll: ()=>queue.resumeAll()
50
+ };
51
+ }
52
+ class $c995945a643dde76$export$f1f8569633bbbec4 {
53
+ /** Subscribes to updates to the visible toasts. */ subscribe(fn) {
54
+ this.subscriptions.add(fn);
55
+ return ()=>this.subscriptions.delete(fn);
56
+ }
57
+ /** Adds a new toast to the queue. */ add(content, options = {}) {
58
+ let toastKey = Math.random().toString(36);
59
+ let toast = {
60
+ ...options,
61
+ content: content,
62
+ key: toastKey,
63
+ timer: options.timeout ? new $c995945a643dde76$var$Timer(()=>this.close(toastKey), options.timeout) : null
64
+ };
65
+ let low = 0;
66
+ let high = this.queue.length;
67
+ while(low < high){
68
+ let mid = Math.floor((low + high) / 2);
69
+ if ((toast.priority || 0) > (this.queue[mid].priority || 0)) high = mid;
70
+ else low = mid + 1;
71
+ }
72
+ if (toast.priority) this.queue.splice(low, 0, toast);
73
+ else this.queue.unshift(toast);
74
+ toast.animation = low < this.maxVisibleToasts ? "entering" : "queued";
75
+ let i = this.maxVisibleToasts;
76
+ while(i < this.queue.length)this.queue[i++].animation = "queued";
77
+ this.updateVisibleToasts();
78
+ return toastKey;
79
+ }
80
+ /**
81
+ * Closes a toast. If `hasExitAnimation` is true, the toast
82
+ * transitions to an "exiting" state instead of being removed immediately.
83
+ */ close(key) {
84
+ let index = this.queue.findIndex((t)=>t.key === key);
85
+ if (index >= 0) {
86
+ var _this_queue_index_onClose, _this_queue_index;
87
+ (_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
+ this.queue.splice(index, 1);
89
+ }
90
+ this.updateVisibleToasts(index);
91
+ }
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
+ this.visibleToasts = this.visibleToasts.filter((t)=>t.key !== key);
95
+ this.updateVisibleToasts(index);
96
+ }
97
+ updateVisibleToasts(oldIndex = -1) {
98
+ let toasts = this.queue.slice(0, this.maxVisibleToasts);
99
+ if (this.hasExitAnimation) {
100
+ let prevToasts = this.visibleToasts.filter((t)=>!toasts.some((t2)=>t.key === t2.key)).map((t)=>({
101
+ ...t,
102
+ animation: "exiting"
103
+ }));
104
+ if (oldIndex !== -1) toasts.splice(oldIndex, 0, prevToasts === null || prevToasts === void 0 ? void 0 : prevToasts[0]);
105
+ this.visibleToasts = toasts;
106
+ } else this.visibleToasts = toasts;
107
+ for (let fn of this.subscriptions)fn();
108
+ }
109
+ /** Pauses the timers for all visible toasts. */ pauseAll() {
110
+ for (let toast of this.visibleToasts)if (toast.timer) toast.timer.pause();
111
+ }
112
+ /** Resumes the timers for all visible toasts. */ resumeAll() {
113
+ for (let toast of this.visibleToasts)if (toast.timer) toast.timer.resume();
114
+ }
115
+ constructor(options){
116
+ this.queue = [];
117
+ this.subscriptions = new Set();
118
+ /** The currently visible toasts. */ this.visibleToasts = [];
119
+ var _options_maxVisibleToasts;
120
+ this.maxVisibleToasts = (_options_maxVisibleToasts = options === null || options === void 0 ? void 0 : options.maxVisibleToasts) !== null && _options_maxVisibleToasts !== void 0 ? _options_maxVisibleToasts : 1;
121
+ var _options_hasExitAnimation;
122
+ this.hasExitAnimation = (_options_hasExitAnimation = options === null || options === void 0 ? void 0 : options.hasExitAnimation) !== null && _options_hasExitAnimation !== void 0 ? _options_hasExitAnimation : false;
123
+ }
124
+ }
125
+ class $c995945a643dde76$var$Timer {
126
+ reset(delay) {
127
+ this.remaining = delay;
128
+ this.resume();
129
+ }
130
+ pause() {
131
+ if (this.timerId == null) return;
132
+ clearTimeout(this.timerId);
133
+ this.timerId = null;
134
+ this.remaining -= Date.now() - this.startTime;
135
+ }
136
+ resume() {
137
+ if (this.remaining <= 0) return;
138
+ this.startTime = Date.now();
139
+ this.timerId = setTimeout(()=>{
140
+ this.timerId = null;
141
+ this.remaining = 0;
142
+ this.callback();
143
+ }, this.remaining);
144
+ }
145
+ constructor(callback, delay){
146
+ this.remaining = delay;
147
+ this.callback = callback;
148
+ }
149
+ }
150
+
151
+
152
+ //# sourceMappingURL=useToastState.main.js.map
@@ -0,0 +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,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,UAAU,EAAc,EAAE;QACxB,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC;QACvB,OAAO,IAAM,IAAI,CAAC,aAAa,C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0B,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 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"}
@@ -0,0 +1,145 @@
1
+ import {useMemo as $cNx9A$useMemo, useCallback as $cNx9A$useCallback} from "react";
2
+ import {useSyncExternalStore as $cNx9A$useSyncExternalStore} from "use-sync-external-store/shim/index.js";
3
+
4
+ /*
5
+ * Copyright 2020 Adobe. All rights reserved.
6
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License. You may obtain a copy
8
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software distributed under
11
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12
+ * OF ANY KIND, either express or implied. See the License for the specific language
13
+ * governing permissions and limitations under the License.
14
+ */
15
+
16
+ function $77b352cf12efcf73$export$c7b26b20d3ced9c5(props = {}) {
17
+ let { maxVisibleToasts: maxVisibleToasts = 1, hasExitAnimation: hasExitAnimation = false } = props;
18
+ let queue = (0, $cNx9A$useMemo)(()=>new $77b352cf12efcf73$export$f1f8569633bbbec4({
19
+ maxVisibleToasts: maxVisibleToasts,
20
+ hasExitAnimation: hasExitAnimation
21
+ }), [
22
+ maxVisibleToasts,
23
+ hasExitAnimation
24
+ ]);
25
+ return $77b352cf12efcf73$export$84726ef35ca2129a(queue);
26
+ }
27
+ function $77b352cf12efcf73$export$84726ef35ca2129a(queue) {
28
+ let subscribe = (0, $cNx9A$useCallback)((fn)=>queue.subscribe(fn), [
29
+ queue
30
+ ]);
31
+ let getSnapshot = (0, $cNx9A$useCallback)(()=>queue.visibleToasts, [
32
+ queue
33
+ ]);
34
+ let visibleToasts = (0, $cNx9A$useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
35
+ return {
36
+ visibleToasts: visibleToasts,
37
+ add: (content, options)=>queue.add(content, options),
38
+ close: (key)=>queue.close(key),
39
+ remove: (key)=>queue.remove(key),
40
+ pauseAll: ()=>queue.pauseAll(),
41
+ resumeAll: ()=>queue.resumeAll()
42
+ };
43
+ }
44
+ class $77b352cf12efcf73$export$f1f8569633bbbec4 {
45
+ /** Subscribes to updates to the visible toasts. */ subscribe(fn) {
46
+ this.subscriptions.add(fn);
47
+ return ()=>this.subscriptions.delete(fn);
48
+ }
49
+ /** Adds a new toast to the queue. */ add(content, options = {}) {
50
+ let toastKey = Math.random().toString(36);
51
+ let toast = {
52
+ ...options,
53
+ content: content,
54
+ key: toastKey,
55
+ timer: options.timeout ? new $77b352cf12efcf73$var$Timer(()=>this.close(toastKey), options.timeout) : null
56
+ };
57
+ let low = 0;
58
+ let high = this.queue.length;
59
+ while(low < high){
60
+ let mid = Math.floor((low + high) / 2);
61
+ if ((toast.priority || 0) > (this.queue[mid].priority || 0)) high = mid;
62
+ else low = mid + 1;
63
+ }
64
+ if (toast.priority) this.queue.splice(low, 0, toast);
65
+ else this.queue.unshift(toast);
66
+ toast.animation = low < this.maxVisibleToasts ? "entering" : "queued";
67
+ let i = this.maxVisibleToasts;
68
+ while(i < this.queue.length)this.queue[i++].animation = "queued";
69
+ this.updateVisibleToasts();
70
+ return toastKey;
71
+ }
72
+ /**
73
+ * Closes a toast. If `hasExitAnimation` is true, the toast
74
+ * transitions to an "exiting" state instead of being removed immediately.
75
+ */ close(key) {
76
+ let index = this.queue.findIndex((t)=>t.key === key);
77
+ if (index >= 0) {
78
+ var _this_queue_index_onClose, _this_queue_index;
79
+ (_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
+ this.queue.splice(index, 1);
81
+ }
82
+ this.updateVisibleToasts(index);
83
+ }
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
+ this.visibleToasts = this.visibleToasts.filter((t)=>t.key !== key);
87
+ this.updateVisibleToasts(index);
88
+ }
89
+ updateVisibleToasts(oldIndex = -1) {
90
+ let toasts = this.queue.slice(0, this.maxVisibleToasts);
91
+ if (this.hasExitAnimation) {
92
+ let prevToasts = this.visibleToasts.filter((t)=>!toasts.some((t2)=>t.key === t2.key)).map((t)=>({
93
+ ...t,
94
+ animation: "exiting"
95
+ }));
96
+ if (oldIndex !== -1) toasts.splice(oldIndex, 0, prevToasts === null || prevToasts === void 0 ? void 0 : prevToasts[0]);
97
+ this.visibleToasts = toasts;
98
+ } else this.visibleToasts = toasts;
99
+ for (let fn of this.subscriptions)fn();
100
+ }
101
+ /** Pauses the timers for all visible toasts. */ pauseAll() {
102
+ for (let toast of this.visibleToasts)if (toast.timer) toast.timer.pause();
103
+ }
104
+ /** Resumes the timers for all visible toasts. */ resumeAll() {
105
+ for (let toast of this.visibleToasts)if (toast.timer) toast.timer.resume();
106
+ }
107
+ constructor(options){
108
+ this.queue = [];
109
+ this.subscriptions = new Set();
110
+ /** The currently visible toasts. */ this.visibleToasts = [];
111
+ var _options_maxVisibleToasts;
112
+ this.maxVisibleToasts = (_options_maxVisibleToasts = options === null || options === void 0 ? void 0 : options.maxVisibleToasts) !== null && _options_maxVisibleToasts !== void 0 ? _options_maxVisibleToasts : 1;
113
+ var _options_hasExitAnimation;
114
+ this.hasExitAnimation = (_options_hasExitAnimation = options === null || options === void 0 ? void 0 : options.hasExitAnimation) !== null && _options_hasExitAnimation !== void 0 ? _options_hasExitAnimation : false;
115
+ }
116
+ }
117
+ class $77b352cf12efcf73$var$Timer {
118
+ reset(delay) {
119
+ this.remaining = delay;
120
+ this.resume();
121
+ }
122
+ pause() {
123
+ if (this.timerId == null) return;
124
+ clearTimeout(this.timerId);
125
+ this.timerId = null;
126
+ this.remaining -= Date.now() - this.startTime;
127
+ }
128
+ resume() {
129
+ if (this.remaining <= 0) return;
130
+ this.startTime = Date.now();
131
+ this.timerId = setTimeout(()=>{
132
+ this.timerId = null;
133
+ this.remaining = 0;
134
+ this.callback();
135
+ }, this.remaining);
136
+ }
137
+ constructor(callback, delay){
138
+ this.remaining = delay;
139
+ this.callback = callback;
140
+ }
141
+ }
142
+
143
+
144
+ export {$77b352cf12efcf73$export$c7b26b20d3ced9c5 as useToastState, $77b352cf12efcf73$export$f1f8569633bbbec4 as ToastQueue, $77b352cf12efcf73$export$84726ef35ca2129a as useToastQueue};
145
+ //# sourceMappingURL=useToastState.mjs.map
@@ -0,0 +1,145 @@
1
+ import {useMemo as $cNx9A$useMemo, useCallback as $cNx9A$useCallback} from "react";
2
+ import {useSyncExternalStore as $cNx9A$useSyncExternalStore} from "use-sync-external-store/shim/index.js";
3
+
4
+ /*
5
+ * Copyright 2020 Adobe. All rights reserved.
6
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License. You may obtain a copy
8
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software distributed under
11
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
12
+ * OF ANY KIND, either express or implied. See the License for the specific language
13
+ * governing permissions and limitations under the License.
14
+ */
15
+
16
+ function $77b352cf12efcf73$export$c7b26b20d3ced9c5(props = {}) {
17
+ let { maxVisibleToasts: maxVisibleToasts = 1, hasExitAnimation: hasExitAnimation = false } = props;
18
+ let queue = (0, $cNx9A$useMemo)(()=>new $77b352cf12efcf73$export$f1f8569633bbbec4({
19
+ maxVisibleToasts: maxVisibleToasts,
20
+ hasExitAnimation: hasExitAnimation
21
+ }), [
22
+ maxVisibleToasts,
23
+ hasExitAnimation
24
+ ]);
25
+ return $77b352cf12efcf73$export$84726ef35ca2129a(queue);
26
+ }
27
+ function $77b352cf12efcf73$export$84726ef35ca2129a(queue) {
28
+ let subscribe = (0, $cNx9A$useCallback)((fn)=>queue.subscribe(fn), [
29
+ queue
30
+ ]);
31
+ let getSnapshot = (0, $cNx9A$useCallback)(()=>queue.visibleToasts, [
32
+ queue
33
+ ]);
34
+ let visibleToasts = (0, $cNx9A$useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
35
+ return {
36
+ visibleToasts: visibleToasts,
37
+ add: (content, options)=>queue.add(content, options),
38
+ close: (key)=>queue.close(key),
39
+ remove: (key)=>queue.remove(key),
40
+ pauseAll: ()=>queue.pauseAll(),
41
+ resumeAll: ()=>queue.resumeAll()
42
+ };
43
+ }
44
+ class $77b352cf12efcf73$export$f1f8569633bbbec4 {
45
+ /** Subscribes to updates to the visible toasts. */ subscribe(fn) {
46
+ this.subscriptions.add(fn);
47
+ return ()=>this.subscriptions.delete(fn);
48
+ }
49
+ /** Adds a new toast to the queue. */ add(content, options = {}) {
50
+ let toastKey = Math.random().toString(36);
51
+ let toast = {
52
+ ...options,
53
+ content: content,
54
+ key: toastKey,
55
+ timer: options.timeout ? new $77b352cf12efcf73$var$Timer(()=>this.close(toastKey), options.timeout) : null
56
+ };
57
+ let low = 0;
58
+ let high = this.queue.length;
59
+ while(low < high){
60
+ let mid = Math.floor((low + high) / 2);
61
+ if ((toast.priority || 0) > (this.queue[mid].priority || 0)) high = mid;
62
+ else low = mid + 1;
63
+ }
64
+ if (toast.priority) this.queue.splice(low, 0, toast);
65
+ else this.queue.unshift(toast);
66
+ toast.animation = low < this.maxVisibleToasts ? "entering" : "queued";
67
+ let i = this.maxVisibleToasts;
68
+ while(i < this.queue.length)this.queue[i++].animation = "queued";
69
+ this.updateVisibleToasts();
70
+ return toastKey;
71
+ }
72
+ /**
73
+ * Closes a toast. If `hasExitAnimation` is true, the toast
74
+ * transitions to an "exiting" state instead of being removed immediately.
75
+ */ close(key) {
76
+ let index = this.queue.findIndex((t)=>t.key === key);
77
+ if (index >= 0) {
78
+ var _this_queue_index_onClose, _this_queue_index;
79
+ (_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
+ this.queue.splice(index, 1);
81
+ }
82
+ this.updateVisibleToasts(index);
83
+ }
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
+ this.visibleToasts = this.visibleToasts.filter((t)=>t.key !== key);
87
+ this.updateVisibleToasts(index);
88
+ }
89
+ updateVisibleToasts(oldIndex = -1) {
90
+ let toasts = this.queue.slice(0, this.maxVisibleToasts);
91
+ if (this.hasExitAnimation) {
92
+ let prevToasts = this.visibleToasts.filter((t)=>!toasts.some((t2)=>t.key === t2.key)).map((t)=>({
93
+ ...t,
94
+ animation: "exiting"
95
+ }));
96
+ if (oldIndex !== -1) toasts.splice(oldIndex, 0, prevToasts === null || prevToasts === void 0 ? void 0 : prevToasts[0]);
97
+ this.visibleToasts = toasts;
98
+ } else this.visibleToasts = toasts;
99
+ for (let fn of this.subscriptions)fn();
100
+ }
101
+ /** Pauses the timers for all visible toasts. */ pauseAll() {
102
+ for (let toast of this.visibleToasts)if (toast.timer) toast.timer.pause();
103
+ }
104
+ /** Resumes the timers for all visible toasts. */ resumeAll() {
105
+ for (let toast of this.visibleToasts)if (toast.timer) toast.timer.resume();
106
+ }
107
+ constructor(options){
108
+ this.queue = [];
109
+ this.subscriptions = new Set();
110
+ /** The currently visible toasts. */ this.visibleToasts = [];
111
+ var _options_maxVisibleToasts;
112
+ this.maxVisibleToasts = (_options_maxVisibleToasts = options === null || options === void 0 ? void 0 : options.maxVisibleToasts) !== null && _options_maxVisibleToasts !== void 0 ? _options_maxVisibleToasts : 1;
113
+ var _options_hasExitAnimation;
114
+ this.hasExitAnimation = (_options_hasExitAnimation = options === null || options === void 0 ? void 0 : options.hasExitAnimation) !== null && _options_hasExitAnimation !== void 0 ? _options_hasExitAnimation : false;
115
+ }
116
+ }
117
+ class $77b352cf12efcf73$var$Timer {
118
+ reset(delay) {
119
+ this.remaining = delay;
120
+ this.resume();
121
+ }
122
+ pause() {
123
+ if (this.timerId == null) return;
124
+ clearTimeout(this.timerId);
125
+ this.timerId = null;
126
+ this.remaining -= Date.now() - this.startTime;
127
+ }
128
+ resume() {
129
+ if (this.remaining <= 0) return;
130
+ this.startTime = Date.now();
131
+ this.timerId = setTimeout(()=>{
132
+ this.timerId = null;
133
+ this.remaining = 0;
134
+ this.callback();
135
+ }, this.remaining);
136
+ }
137
+ constructor(callback, delay){
138
+ this.remaining = delay;
139
+ this.callback = callback;
140
+ }
141
+ }
142
+
143
+
144
+ export {$77b352cf12efcf73$export$c7b26b20d3ced9c5 as useToastState, $77b352cf12efcf73$export$f1f8569633bbbec4 as ToastQueue, $77b352cf12efcf73$export$84726ef35ca2129a as useToastQueue};
145
+ //# sourceMappingURL=useToastState.module.js.map
@@ -0,0 +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,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;IAClC;AACF;AAKO,MAAM;IAaX,iDAAiD,GACjD,UAAU,EAAc,EAAE;QACxB,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC;QACvB,OAAO,IAAM,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;IACzC;IAEA,mCAAmC,GACnC,IAAI,OAAU,EAAE,U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AAE;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 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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-stately/toast",
3
- "version": "3.0.0-nightly.4552+64ed13090",
3
+ "version": "3.0.0-nightly.4558+c5e4b3701",
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": "64ed13090ce77cc0e4cb4cd5602e75f655bff6bb"
37
+ "gitHead": "c5e4b3701fdb89eb551f1b3697ac253f06ef68fa"
38
38
  }