@react-stately/toast 3.1.2 → 3.2.0
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 +2 -2
- package/dist/main.js +4 -4
- package/dist/main.js.map +1 -1
- package/dist/module.js +2 -2
- package/dist/module.js.map +1 -1
- package/dist/types/src/index.d.ts +2 -0
- package/package.json +16 -12
- package/src/index.ts +2 -2
- package/dist/types.d.ts +0 -73
- package/dist/types.d.ts.map +0 -1
- package/dist/useToastState.main.js +0 -136
- package/dist/useToastState.main.js.map +0 -1
- package/dist/useToastState.mjs +0 -129
- package/dist/useToastState.module.js +0 -129
- package/dist/useToastState.module.js.map +0 -1
- package/src/useToastState.ts +0 -214
package/dist/import.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {useToastState as $4290df6e7db479cf$re_export$useToastState, ToastQueue as $4290df6e7db479cf$re_export$ToastQueue, useToastQueue as $4290df6e7db479cf$re_export$useToastQueue} from "react-stately/useToastState";
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
4
|
* Copyright 2020 Adobe. All rights reserved.
|
|
@@ -13,5 +13,5 @@ import {ToastQueue as $77b352cf12efcf73$export$f1f8569633bbbec4, useToastQueue a
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
export {$
|
|
16
|
+
export {$4290df6e7db479cf$re_export$useToastState as useToastState, $4290df6e7db479cf$re_export$ToastQueue as ToastQueue, $4290df6e7db479cf$re_export$useToastQueue as useToastQueue};
|
|
17
17
|
//# sourceMappingURL=module.js.map
|
package/dist/main.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
var $
|
|
1
|
+
var $aoza1$reactstatelyuseToastState = require("react-stately/useToastState");
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
function $parcel$export(e, n, v, s) {
|
|
5
5
|
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
$parcel$export(module.exports, "useToastState", ()
|
|
9
|
-
$parcel$export(module.exports, "ToastQueue", ()
|
|
10
|
-
$parcel$export(module.exports, "useToastQueue", ()
|
|
8
|
+
$parcel$export(module.exports, "useToastState", function () { return $aoza1$reactstatelyuseToastState.useToastState; });
|
|
9
|
+
$parcel$export(module.exports, "ToastQueue", function () { return $aoza1$reactstatelyuseToastState.ToastQueue; });
|
|
10
|
+
$parcel$export(module.exports, "useToastQueue", function () { return $aoza1$reactstatelyuseToastState.useToastQueue; });
|
|
11
11
|
/*
|
|
12
12
|
* Copyright 2020 Adobe. All rights reserved.
|
|
13
13
|
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
package/dist/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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 '
|
|
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 'react-stately/useToastState';\n\nexport type {ToastState, QueuedToast, ToastStateProps, ToastOptions} from 'react-stately/useToastState';\n"],"names":[],"version":3,"file":"main.js.map"}
|
package/dist/module.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {useToastState as $4290df6e7db479cf$re_export$useToastState, ToastQueue as $4290df6e7db479cf$re_export$ToastQueue, useToastQueue as $4290df6e7db479cf$re_export$useToastQueue} from "react-stately/useToastState";
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
4
|
* Copyright 2020 Adobe. All rights reserved.
|
|
@@ -13,5 +13,5 @@ import {ToastQueue as $77b352cf12efcf73$export$f1f8569633bbbec4, useToastQueue a
|
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
export {$
|
|
16
|
+
export {$4290df6e7db479cf$re_export$useToastState as useToastState, $4290df6e7db479cf$re_export$ToastQueue as ToastQueue, $4290df6e7db479cf$re_export$useToastQueue as useToastQueue};
|
|
17
17
|
//# sourceMappingURL=module.js.map
|
package/dist/module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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 '
|
|
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 'react-stately/useToastState';\n\nexport type {ToastState, QueuedToast, ToastStateProps, ToastOptions} from 'react-stately/useToastState';\n"],"names":[],"version":3,"file":"module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-stately/toast",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
7
7
|
"module": "dist/module.js",
|
|
8
8
|
"exports": {
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
"./dist/types.d.ts",
|
|
12
|
-
"./
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
9
|
+
".": {
|
|
10
|
+
"source": "./src/index.ts",
|
|
11
|
+
"types": "./dist/types/src/index.d.ts",
|
|
12
|
+
"import": "./dist/import.mjs",
|
|
13
|
+
"require": "./dist/main.js"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
16
|
},
|
|
17
|
-
"types": "dist/types.d.ts",
|
|
17
|
+
"types": "dist/types/src/index.d.ts",
|
|
18
18
|
"source": "src/index.ts",
|
|
19
19
|
"files": [
|
|
20
20
|
"dist",
|
|
@@ -27,16 +27,20 @@
|
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@swc/helpers": "^0.5.0",
|
|
30
|
-
"
|
|
30
|
+
"react-stately": "3.46.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/use-sync-external-store": "^0.0.3"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1"
|
|
36
|
+
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1",
|
|
37
|
+
"react-dom": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0-rc.1"
|
|
37
38
|
},
|
|
38
39
|
"publishConfig": {
|
|
39
40
|
"access": "public"
|
|
40
41
|
},
|
|
41
|
-
"
|
|
42
|
+
"targets": {
|
|
43
|
+
"types": false
|
|
44
|
+
},
|
|
45
|
+
"gitHead": "a6999bdf494a2e9c0381a5881908328bdd22ddae"
|
|
42
46
|
}
|
package/src/index.ts
CHANGED
|
@@ -9,6 +9,6 @@
|
|
|
9
9
|
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
|
-
export {useToastState, ToastQueue, useToastQueue} from '
|
|
12
|
+
export {useToastState, ToastQueue, useToastQueue} from 'react-stately/useToastState';
|
|
13
13
|
|
|
14
|
-
export type {ToastState, QueuedToast, ToastStateProps, ToastOptions} from '
|
|
14
|
+
export type {ToastState, QueuedToast, ToastStateProps, ToastOptions} from 'react-stately/useToastState';
|
package/dist/types.d.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
type ToastAction = 'add' | 'remove' | 'clear';
|
|
2
|
-
export interface ToastStateProps {
|
|
3
|
-
/** The maximum number of toasts to display at a time. */
|
|
4
|
-
maxVisibleToasts?: number;
|
|
5
|
-
/** Function to wrap updates in (i.e. document.startViewTransition()). */
|
|
6
|
-
wrapUpdate?: (fn: () => void, action: ToastAction) => void;
|
|
7
|
-
}
|
|
8
|
-
export interface ToastOptions {
|
|
9
|
-
/** Handler that is called when the toast is closed, either by the user or after a timeout. */
|
|
10
|
-
onClose?: () => void;
|
|
11
|
-
/** A timeout to automatically close the toast after, in milliseconds. */
|
|
12
|
-
timeout?: number;
|
|
13
|
-
}
|
|
14
|
-
export interface QueuedToast<T> extends ToastOptions {
|
|
15
|
-
/** The content of the toast. */
|
|
16
|
-
content: T;
|
|
17
|
-
/** A unique key for the toast. */
|
|
18
|
-
key: string;
|
|
19
|
-
/** A timer for the toast, if a timeout was set. */
|
|
20
|
-
timer?: Timer;
|
|
21
|
-
}
|
|
22
|
-
export interface ToastState<T> {
|
|
23
|
-
/** Adds a new toast to the queue. */
|
|
24
|
-
add(content: T, options?: ToastOptions): string;
|
|
25
|
-
/**
|
|
26
|
-
* Closes a toast.
|
|
27
|
-
*/
|
|
28
|
-
close(key: string): void;
|
|
29
|
-
/** Pauses the timers for all visible toasts. */
|
|
30
|
-
pauseAll(): void;
|
|
31
|
-
/** Resumes the timers for all visible toasts. */
|
|
32
|
-
resumeAll(): void;
|
|
33
|
-
/** The visible toasts. */
|
|
34
|
-
visibleToasts: QueuedToast<T>[];
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Provides state management for a toast queue. Toasts display brief, temporary notifications
|
|
38
|
-
* of actions, errors, or other events in an application.
|
|
39
|
-
*/
|
|
40
|
-
export function useToastState<T>(props?: ToastStateProps): ToastState<T>;
|
|
41
|
-
/**
|
|
42
|
-
* Subscribes to a provided toast queue and provides methods to update it.
|
|
43
|
-
*/
|
|
44
|
-
export function useToastQueue<T>(queue: ToastQueue<T>): ToastState<T>;
|
|
45
|
-
/**
|
|
46
|
-
* A ToastQueue manages the order of toasts.
|
|
47
|
-
*/
|
|
48
|
-
export class ToastQueue<T> {
|
|
49
|
-
/** The currently visible toasts. */
|
|
50
|
-
visibleToasts: QueuedToast<T>[];
|
|
51
|
-
constructor(options?: ToastStateProps);
|
|
52
|
-
/** Subscribes to updates to the visible toasts. */
|
|
53
|
-
subscribe(fn: () => void): () => void;
|
|
54
|
-
/** Adds a new toast to the queue. */
|
|
55
|
-
add(content: T, options?: ToastOptions): string;
|
|
56
|
-
/**
|
|
57
|
-
* Closes a toast.
|
|
58
|
-
*/
|
|
59
|
-
close(key: string): void;
|
|
60
|
-
/** Pauses the timers for all visible toasts. */
|
|
61
|
-
pauseAll(): void;
|
|
62
|
-
/** Resumes the timers for all visible toasts. */
|
|
63
|
-
resumeAll(): void;
|
|
64
|
-
clear(): void;
|
|
65
|
-
}
|
|
66
|
-
declare class Timer {
|
|
67
|
-
constructor(callback: () => void, delay: number);
|
|
68
|
-
reset(delay: number): void;
|
|
69
|
-
pause(): void;
|
|
70
|
-
resume(): void;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":"AAgBA,mBAAmB,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;AAC9C;IACE,yDAAyD;IACzD,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,yEAAyE;IACzE,UAAU,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,IAAI,EAAE,MAAM,EAAE,WAAW,KAAK,IAAI,CAAA;CAC3D;AAED;IACE,8FAA8F;IAC9F,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB,yEAAyE;IACzE,OAAO,CAAC,EAAE,MAAM,CAAA;CACjB;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,CAAA;CACd;AAED,4BAA4B,CAAC;IAC3B,qCAAqC;IACrC,GAAG,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,MAAM,CAAC;IAChD;;OAEG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,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,CAYpE;AAED;;GAEG;AACH,wBAAwB,CAAC;IAKvB,oCAAoC;IACpC,aAAa,EAAE,YAAY,CAAC,CAAC,EAAE,CAAM;gBAEzB,OAAO,CAAC,EAAE,eAAe;IAarC,mDAAmD;IACnD,SAAS,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,MAAM,IAAI;IAKrC,qCAAqC;IACrC,GAAG,CAAC,OAAO,EAAE,CAAC,EAAE,OAAO,GAAE,YAAiB,GAAG,MAAM;IAenD;;OAEG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAoBxB,gDAAgD;IAChD,QAAQ,IAAI,IAAI;IAQhB,iDAAiD;IACjD,SAAS,IAAI,IAAI;IAQjB,KAAK,IAAI,IAAI;CAId;AAED;gBAMc,QAAQ,EAAE,MAAM,IAAI,EAAE,KAAK,EAAE,MAAM;IAK/C,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAK1B,KAAK,IAAI,IAAI;IAUb,MAAM,IAAI,IAAI;CAYf","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"}
|
|
@@ -1,136 +0,0 @@
|
|
|
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, wrapUpdate: wrapUpdate } = props;
|
|
26
|
-
let queue = (0, $pwpJQ$react.useMemo)(()=>new $c995945a643dde76$export$f1f8569633bbbec4({
|
|
27
|
-
maxVisibleToasts: maxVisibleToasts,
|
|
28
|
-
wrapUpdate: wrapUpdate
|
|
29
|
-
}), [
|
|
30
|
-
maxVisibleToasts,
|
|
31
|
-
wrapUpdate
|
|
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
|
-
pauseAll: ()=>queue.pauseAll(),
|
|
48
|
-
resumeAll: ()=>queue.resumeAll()
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
class $c995945a643dde76$export$f1f8569633bbbec4 {
|
|
52
|
-
runWithWrapUpdate(fn, action) {
|
|
53
|
-
if (this.wrapUpdate) this.wrapUpdate(fn, action);
|
|
54
|
-
else fn();
|
|
55
|
-
}
|
|
56
|
-
/** Subscribes to updates to the visible toasts. */ subscribe(fn) {
|
|
57
|
-
this.subscriptions.add(fn);
|
|
58
|
-
return ()=>this.subscriptions.delete(fn);
|
|
59
|
-
}
|
|
60
|
-
/** Adds a new toast to the queue. */ add(content, options = {}) {
|
|
61
|
-
let toastKey = '_' + Math.random().toString(36).slice(2);
|
|
62
|
-
let toast = {
|
|
63
|
-
...options,
|
|
64
|
-
content: content,
|
|
65
|
-
key: toastKey,
|
|
66
|
-
timer: options.timeout ? new $c995945a643dde76$var$Timer(()=>this.close(toastKey), options.timeout) : undefined
|
|
67
|
-
};
|
|
68
|
-
this.queue.unshift(toast);
|
|
69
|
-
this.updateVisibleToasts('add');
|
|
70
|
-
return toastKey;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Closes a toast.
|
|
74
|
-
*/ close(key) {
|
|
75
|
-
let index = this.queue.findIndex((t)=>t.key === key);
|
|
76
|
-
if (index >= 0) {
|
|
77
|
-
var _this_queue_index_onClose, _this_queue_index;
|
|
78
|
-
(_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);
|
|
79
|
-
this.queue.splice(index, 1);
|
|
80
|
-
}
|
|
81
|
-
this.updateVisibleToasts('remove');
|
|
82
|
-
}
|
|
83
|
-
updateVisibleToasts(action) {
|
|
84
|
-
this.visibleToasts = this.queue.slice(0, this.maxVisibleToasts);
|
|
85
|
-
this.runWithWrapUpdate(()=>{
|
|
86
|
-
for (let fn of this.subscriptions)fn();
|
|
87
|
-
}, action);
|
|
88
|
-
}
|
|
89
|
-
/** Pauses the timers for all visible toasts. */ pauseAll() {
|
|
90
|
-
for (let toast of this.visibleToasts)if (toast.timer) toast.timer.pause();
|
|
91
|
-
}
|
|
92
|
-
/** Resumes the timers for all visible toasts. */ resumeAll() {
|
|
93
|
-
for (let toast of this.visibleToasts)if (toast.timer) toast.timer.resume();
|
|
94
|
-
}
|
|
95
|
-
clear() {
|
|
96
|
-
this.queue = [];
|
|
97
|
-
this.updateVisibleToasts('clear');
|
|
98
|
-
}
|
|
99
|
-
constructor(options){
|
|
100
|
-
this.queue = [];
|
|
101
|
-
this.subscriptions = new Set();
|
|
102
|
-
/** The currently visible toasts. */ this.visibleToasts = [];
|
|
103
|
-
var _options_maxVisibleToasts;
|
|
104
|
-
this.maxVisibleToasts = (_options_maxVisibleToasts = options === null || options === void 0 ? void 0 : options.maxVisibleToasts) !== null && _options_maxVisibleToasts !== void 0 ? _options_maxVisibleToasts : Infinity;
|
|
105
|
-
this.wrapUpdate = options === null || options === void 0 ? void 0 : options.wrapUpdate;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
class $c995945a643dde76$var$Timer {
|
|
109
|
-
reset(delay) {
|
|
110
|
-
this.remaining = delay;
|
|
111
|
-
this.resume();
|
|
112
|
-
}
|
|
113
|
-
pause() {
|
|
114
|
-
if (this.timerId == null) return;
|
|
115
|
-
clearTimeout(this.timerId);
|
|
116
|
-
this.timerId = null;
|
|
117
|
-
this.remaining -= Date.now() - this.startTime;
|
|
118
|
-
}
|
|
119
|
-
resume() {
|
|
120
|
-
if (this.remaining <= 0) return;
|
|
121
|
-
this.startTime = Date.now();
|
|
122
|
-
this.timerId = setTimeout(()=>{
|
|
123
|
-
this.timerId = null;
|
|
124
|
-
this.remaining = 0;
|
|
125
|
-
this.callback();
|
|
126
|
-
}, this.remaining);
|
|
127
|
-
}
|
|
128
|
-
constructor(callback, delay){
|
|
129
|
-
this.startTime = null;
|
|
130
|
-
this.remaining = delay;
|
|
131
|
-
this.callback = callback;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
//# sourceMappingURL=useToastState.main.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;AAAA;;;;;;;;;;CAUC;;AAiDM,SAAS,0CAAiB,QAAyB,CAAC,CAAC;IAC1D,IAAI,oBAAC,mBAAmB,eAAG,UAAU,EAAC,GAAG;IACzC,IAAI,QAAQ,CAAA,GAAA,oBAAM,EAAE,IAAM,IAAI,0CAAc;8BAAC;wBAAkB;QAAU,IAAI;QA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,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,SAAe;QACb,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;aAJzC,YAA2B;QAKjC,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\ntype ToastAction = 'add' | 'remove' | 'clear';\nexport interface ToastStateProps {\n /** The maximum number of toasts to display at a time. */\n maxVisibleToasts?: number,\n /** Function to wrap updates in (i.e. document.startViewTransition()). */\n wrapUpdate?: (fn: () => void, action: ToastAction) => void\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}\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}\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.\n */\n close(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, wrapUpdate} = props;\n let queue = useMemo(() => new ToastQueue<T>({maxVisibleToasts, wrapUpdate}), [maxVisibleToasts, wrapUpdate]);\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 pauseAll: () => queue.pauseAll(),\n resumeAll: () => queue.resumeAll()\n };\n}\n\n/**\n * A ToastQueue manages the order 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 wrapUpdate?: (fn: () => void, action: ToastAction) => void;\n /** The currently visible toasts. */\n visibleToasts: QueuedToast<T>[] = [];\n\n constructor(options?: ToastStateProps) {\n this.maxVisibleToasts = options?.maxVisibleToasts ?? Infinity;\n this.wrapUpdate = options?.wrapUpdate;\n }\n\n private runWithWrapUpdate(fn: () => void, action: ToastAction): void {\n if (this.wrapUpdate) {\n this.wrapUpdate(fn, action);\n } else {\n fn();\n }\n }\n\n /** Subscribes to updates to the visible toasts. */\n subscribe(fn: () => void): () => 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 = {}): string {\n let toastKey = '_' + Math.random().toString(36).slice(2);\n let toast: QueuedToast<T> = {\n ...options,\n content,\n key: toastKey,\n timer: options.timeout ? new Timer(() => this.close(toastKey), options.timeout) : undefined\n };\n\n this.queue.unshift(toast);\n\n this.updateVisibleToasts('add');\n return toastKey;\n }\n\n /**\n * Closes a toast.\n */\n close(key: string): void {\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('remove');\n }\n\n private updateVisibleToasts(action: ToastAction) {\n this.visibleToasts = this.queue.slice(0, this.maxVisibleToasts);\n\n this.runWithWrapUpdate(() => {\n for (let fn of this.subscriptions) {\n fn();\n }\n }, action);\n }\n\n /** Pauses the timers for all visible toasts. */\n pauseAll(): void {\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(): void {\n for (let toast of this.visibleToasts) {\n if (toast.timer) {\n toast.timer.resume();\n }\n }\n }\n\n clear(): void {\n this.queue = [];\n this.updateVisibleToasts('clear');\n }\n}\n\nclass Timer {\n private timerId;\n private startTime: number | null = null;\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): void {\n this.remaining = delay;\n this.resume();\n }\n\n pause(): void {\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(): void {\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
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
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, wrapUpdate: wrapUpdate } = props;
|
|
18
|
-
let queue = (0, $cNx9A$useMemo)(()=>new $77b352cf12efcf73$export$f1f8569633bbbec4({
|
|
19
|
-
maxVisibleToasts: maxVisibleToasts,
|
|
20
|
-
wrapUpdate: wrapUpdate
|
|
21
|
-
}), [
|
|
22
|
-
maxVisibleToasts,
|
|
23
|
-
wrapUpdate
|
|
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
|
-
pauseAll: ()=>queue.pauseAll(),
|
|
40
|
-
resumeAll: ()=>queue.resumeAll()
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
class $77b352cf12efcf73$export$f1f8569633bbbec4 {
|
|
44
|
-
runWithWrapUpdate(fn, action) {
|
|
45
|
-
if (this.wrapUpdate) this.wrapUpdate(fn, action);
|
|
46
|
-
else fn();
|
|
47
|
-
}
|
|
48
|
-
/** Subscribes to updates to the visible toasts. */ subscribe(fn) {
|
|
49
|
-
this.subscriptions.add(fn);
|
|
50
|
-
return ()=>this.subscriptions.delete(fn);
|
|
51
|
-
}
|
|
52
|
-
/** Adds a new toast to the queue. */ add(content, options = {}) {
|
|
53
|
-
let toastKey = '_' + Math.random().toString(36).slice(2);
|
|
54
|
-
let toast = {
|
|
55
|
-
...options,
|
|
56
|
-
content: content,
|
|
57
|
-
key: toastKey,
|
|
58
|
-
timer: options.timeout ? new $77b352cf12efcf73$var$Timer(()=>this.close(toastKey), options.timeout) : undefined
|
|
59
|
-
};
|
|
60
|
-
this.queue.unshift(toast);
|
|
61
|
-
this.updateVisibleToasts('add');
|
|
62
|
-
return toastKey;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Closes a toast.
|
|
66
|
-
*/ close(key) {
|
|
67
|
-
let index = this.queue.findIndex((t)=>t.key === key);
|
|
68
|
-
if (index >= 0) {
|
|
69
|
-
var _this_queue_index_onClose, _this_queue_index;
|
|
70
|
-
(_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);
|
|
71
|
-
this.queue.splice(index, 1);
|
|
72
|
-
}
|
|
73
|
-
this.updateVisibleToasts('remove');
|
|
74
|
-
}
|
|
75
|
-
updateVisibleToasts(action) {
|
|
76
|
-
this.visibleToasts = this.queue.slice(0, this.maxVisibleToasts);
|
|
77
|
-
this.runWithWrapUpdate(()=>{
|
|
78
|
-
for (let fn of this.subscriptions)fn();
|
|
79
|
-
}, action);
|
|
80
|
-
}
|
|
81
|
-
/** Pauses the timers for all visible toasts. */ pauseAll() {
|
|
82
|
-
for (let toast of this.visibleToasts)if (toast.timer) toast.timer.pause();
|
|
83
|
-
}
|
|
84
|
-
/** Resumes the timers for all visible toasts. */ resumeAll() {
|
|
85
|
-
for (let toast of this.visibleToasts)if (toast.timer) toast.timer.resume();
|
|
86
|
-
}
|
|
87
|
-
clear() {
|
|
88
|
-
this.queue = [];
|
|
89
|
-
this.updateVisibleToasts('clear');
|
|
90
|
-
}
|
|
91
|
-
constructor(options){
|
|
92
|
-
this.queue = [];
|
|
93
|
-
this.subscriptions = new Set();
|
|
94
|
-
/** The currently visible toasts. */ this.visibleToasts = [];
|
|
95
|
-
var _options_maxVisibleToasts;
|
|
96
|
-
this.maxVisibleToasts = (_options_maxVisibleToasts = options === null || options === void 0 ? void 0 : options.maxVisibleToasts) !== null && _options_maxVisibleToasts !== void 0 ? _options_maxVisibleToasts : Infinity;
|
|
97
|
-
this.wrapUpdate = options === null || options === void 0 ? void 0 : options.wrapUpdate;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
class $77b352cf12efcf73$var$Timer {
|
|
101
|
-
reset(delay) {
|
|
102
|
-
this.remaining = delay;
|
|
103
|
-
this.resume();
|
|
104
|
-
}
|
|
105
|
-
pause() {
|
|
106
|
-
if (this.timerId == null) return;
|
|
107
|
-
clearTimeout(this.timerId);
|
|
108
|
-
this.timerId = null;
|
|
109
|
-
this.remaining -= Date.now() - this.startTime;
|
|
110
|
-
}
|
|
111
|
-
resume() {
|
|
112
|
-
if (this.remaining <= 0) return;
|
|
113
|
-
this.startTime = Date.now();
|
|
114
|
-
this.timerId = setTimeout(()=>{
|
|
115
|
-
this.timerId = null;
|
|
116
|
-
this.remaining = 0;
|
|
117
|
-
this.callback();
|
|
118
|
-
}, this.remaining);
|
|
119
|
-
}
|
|
120
|
-
constructor(callback, delay){
|
|
121
|
-
this.startTime = null;
|
|
122
|
-
this.remaining = delay;
|
|
123
|
-
this.callback = callback;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
export {$77b352cf12efcf73$export$c7b26b20d3ced9c5 as useToastState, $77b352cf12efcf73$export$f1f8569633bbbec4 as ToastQueue, $77b352cf12efcf73$export$84726ef35ca2129a as useToastQueue};
|
|
129
|
-
//# sourceMappingURL=useToastState.module.js.map
|
|
@@ -1,129 +0,0 @@
|
|
|
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, wrapUpdate: wrapUpdate } = props;
|
|
18
|
-
let queue = (0, $cNx9A$useMemo)(()=>new $77b352cf12efcf73$export$f1f8569633bbbec4({
|
|
19
|
-
maxVisibleToasts: maxVisibleToasts,
|
|
20
|
-
wrapUpdate: wrapUpdate
|
|
21
|
-
}), [
|
|
22
|
-
maxVisibleToasts,
|
|
23
|
-
wrapUpdate
|
|
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
|
-
pauseAll: ()=>queue.pauseAll(),
|
|
40
|
-
resumeAll: ()=>queue.resumeAll()
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
class $77b352cf12efcf73$export$f1f8569633bbbec4 {
|
|
44
|
-
runWithWrapUpdate(fn, action) {
|
|
45
|
-
if (this.wrapUpdate) this.wrapUpdate(fn, action);
|
|
46
|
-
else fn();
|
|
47
|
-
}
|
|
48
|
-
/** Subscribes to updates to the visible toasts. */ subscribe(fn) {
|
|
49
|
-
this.subscriptions.add(fn);
|
|
50
|
-
return ()=>this.subscriptions.delete(fn);
|
|
51
|
-
}
|
|
52
|
-
/** Adds a new toast to the queue. */ add(content, options = {}) {
|
|
53
|
-
let toastKey = '_' + Math.random().toString(36).slice(2);
|
|
54
|
-
let toast = {
|
|
55
|
-
...options,
|
|
56
|
-
content: content,
|
|
57
|
-
key: toastKey,
|
|
58
|
-
timer: options.timeout ? new $77b352cf12efcf73$var$Timer(()=>this.close(toastKey), options.timeout) : undefined
|
|
59
|
-
};
|
|
60
|
-
this.queue.unshift(toast);
|
|
61
|
-
this.updateVisibleToasts('add');
|
|
62
|
-
return toastKey;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Closes a toast.
|
|
66
|
-
*/ close(key) {
|
|
67
|
-
let index = this.queue.findIndex((t)=>t.key === key);
|
|
68
|
-
if (index >= 0) {
|
|
69
|
-
var _this_queue_index_onClose, _this_queue_index;
|
|
70
|
-
(_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);
|
|
71
|
-
this.queue.splice(index, 1);
|
|
72
|
-
}
|
|
73
|
-
this.updateVisibleToasts('remove');
|
|
74
|
-
}
|
|
75
|
-
updateVisibleToasts(action) {
|
|
76
|
-
this.visibleToasts = this.queue.slice(0, this.maxVisibleToasts);
|
|
77
|
-
this.runWithWrapUpdate(()=>{
|
|
78
|
-
for (let fn of this.subscriptions)fn();
|
|
79
|
-
}, action);
|
|
80
|
-
}
|
|
81
|
-
/** Pauses the timers for all visible toasts. */ pauseAll() {
|
|
82
|
-
for (let toast of this.visibleToasts)if (toast.timer) toast.timer.pause();
|
|
83
|
-
}
|
|
84
|
-
/** Resumes the timers for all visible toasts. */ resumeAll() {
|
|
85
|
-
for (let toast of this.visibleToasts)if (toast.timer) toast.timer.resume();
|
|
86
|
-
}
|
|
87
|
-
clear() {
|
|
88
|
-
this.queue = [];
|
|
89
|
-
this.updateVisibleToasts('clear');
|
|
90
|
-
}
|
|
91
|
-
constructor(options){
|
|
92
|
-
this.queue = [];
|
|
93
|
-
this.subscriptions = new Set();
|
|
94
|
-
/** The currently visible toasts. */ this.visibleToasts = [];
|
|
95
|
-
var _options_maxVisibleToasts;
|
|
96
|
-
this.maxVisibleToasts = (_options_maxVisibleToasts = options === null || options === void 0 ? void 0 : options.maxVisibleToasts) !== null && _options_maxVisibleToasts !== void 0 ? _options_maxVisibleToasts : Infinity;
|
|
97
|
-
this.wrapUpdate = options === null || options === void 0 ? void 0 : options.wrapUpdate;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
class $77b352cf12efcf73$var$Timer {
|
|
101
|
-
reset(delay) {
|
|
102
|
-
this.remaining = delay;
|
|
103
|
-
this.resume();
|
|
104
|
-
}
|
|
105
|
-
pause() {
|
|
106
|
-
if (this.timerId == null) return;
|
|
107
|
-
clearTimeout(this.timerId);
|
|
108
|
-
this.timerId = null;
|
|
109
|
-
this.remaining -= Date.now() - this.startTime;
|
|
110
|
-
}
|
|
111
|
-
resume() {
|
|
112
|
-
if (this.remaining <= 0) return;
|
|
113
|
-
this.startTime = Date.now();
|
|
114
|
-
this.timerId = setTimeout(()=>{
|
|
115
|
-
this.timerId = null;
|
|
116
|
-
this.remaining = 0;
|
|
117
|
-
this.callback();
|
|
118
|
-
}, this.remaining);
|
|
119
|
-
}
|
|
120
|
-
constructor(callback, delay){
|
|
121
|
-
this.startTime = null;
|
|
122
|
-
this.remaining = delay;
|
|
123
|
-
this.callback = callback;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
export {$77b352cf12efcf73$export$c7b26b20d3ced9c5 as useToastState, $77b352cf12efcf73$export$f1f8569633bbbec4 as ToastQueue, $77b352cf12efcf73$export$84726ef35ca2129a as useToastQueue};
|
|
129
|
-
//# sourceMappingURL=useToastState.module.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;AAAA;;;;;;;;;;CAUC;;AAiDM,SAAS,0CAAiB,QAAyB,CAAC,CAAC;IAC1D,IAAI,oBAAC,mBAAmB,eAAG,UAAU,EAAC,GAAG;IACzC,IAAI,QAAQ,CAAA,GAAA,cAAM,EAAE,IAAM,IAAI,0CAAc;8B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,MAAM;IACb;IAEA,QAAc;QACZ,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,SAAe;QACb,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;aAJzC,YAA2B;QAKjC,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\ntype ToastAction = 'add' | 'remove' | 'clear';\nexport interface ToastStateProps {\n /** The maximum number of toasts to display at a time. */\n maxVisibleToasts?: number,\n /** Function to wrap updates in (i.e. document.startViewTransition()). */\n wrapUpdate?: (fn: () => void, action: ToastAction) => void\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}\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}\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.\n */\n close(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, wrapUpdate} = props;\n let queue = useMemo(() => new ToastQueue<T>({maxVisibleToasts, wrapUpdate}), [maxVisibleToasts, wrapUpdate]);\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 pauseAll: () => queue.pauseAll(),\n resumeAll: () => queue.resumeAll()\n };\n}\n\n/**\n * A ToastQueue manages the order 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 wrapUpdate?: (fn: () => void, action: ToastAction) => void;\n /** The currently visible toasts. */\n visibleToasts: QueuedToast<T>[] = [];\n\n constructor(options?: ToastStateProps) {\n this.maxVisibleToasts = options?.maxVisibleToasts ?? Infinity;\n this.wrapUpdate = options?.wrapUpdate;\n }\n\n private runWithWrapUpdate(fn: () => void, action: ToastAction): void {\n if (this.wrapUpdate) {\n this.wrapUpdate(fn, action);\n } else {\n fn();\n }\n }\n\n /** Subscribes to updates to the visible toasts. */\n subscribe(fn: () => void): () => 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 = {}): string {\n let toastKey = '_' + Math.random().toString(36).slice(2);\n let toast: QueuedToast<T> = {\n ...options,\n content,\n key: toastKey,\n timer: options.timeout ? new Timer(() => this.close(toastKey), options.timeout) : undefined\n };\n\n this.queue.unshift(toast);\n\n this.updateVisibleToasts('add');\n return toastKey;\n }\n\n /**\n * Closes a toast.\n */\n close(key: string): void {\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('remove');\n }\n\n private updateVisibleToasts(action: ToastAction) {\n this.visibleToasts = this.queue.slice(0, this.maxVisibleToasts);\n\n this.runWithWrapUpdate(() => {\n for (let fn of this.subscriptions) {\n fn();\n }\n }, action);\n }\n\n /** Pauses the timers for all visible toasts. */\n pauseAll(): void {\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(): void {\n for (let toast of this.visibleToasts) {\n if (toast.timer) {\n toast.timer.resume();\n }\n }\n }\n\n clear(): void {\n this.queue = [];\n this.updateVisibleToasts('clear');\n }\n}\n\nclass Timer {\n private timerId;\n private startTime: number | null = null;\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): void {\n this.remaining = delay;\n this.resume();\n }\n\n pause(): void {\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(): void {\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/src/useToastState.ts
DELETED
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
*
|
|
7
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
-
* governing permissions and limitations under the License.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import {useCallback, useMemo} from 'react';
|
|
14
|
-
// Shim to support React 17 and below.
|
|
15
|
-
import {useSyncExternalStore} from 'use-sync-external-store/shim/index.js';
|
|
16
|
-
|
|
17
|
-
type ToastAction = 'add' | 'remove' | 'clear';
|
|
18
|
-
export interface ToastStateProps {
|
|
19
|
-
/** The maximum number of toasts to display at a time. */
|
|
20
|
-
maxVisibleToasts?: number,
|
|
21
|
-
/** Function to wrap updates in (i.e. document.startViewTransition()). */
|
|
22
|
-
wrapUpdate?: (fn: () => void, action: ToastAction) => void
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface ToastOptions {
|
|
26
|
-
/** Handler that is called when the toast is closed, either by the user or after a timeout. */
|
|
27
|
-
onClose?: () => void,
|
|
28
|
-
/** A timeout to automatically close the toast after, in milliseconds. */
|
|
29
|
-
timeout?: number
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface QueuedToast<T> extends ToastOptions {
|
|
33
|
-
/** The content of the toast. */
|
|
34
|
-
content: T,
|
|
35
|
-
/** A unique key for the toast. */
|
|
36
|
-
key: string,
|
|
37
|
-
/** A timer for the toast, if a timeout was set. */
|
|
38
|
-
timer?: Timer
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface ToastState<T> {
|
|
42
|
-
/** Adds a new toast to the queue. */
|
|
43
|
-
add(content: T, options?: ToastOptions): string,
|
|
44
|
-
/**
|
|
45
|
-
* Closes a toast.
|
|
46
|
-
*/
|
|
47
|
-
close(key: string): void,
|
|
48
|
-
/** Pauses the timers for all visible toasts. */
|
|
49
|
-
pauseAll(): void,
|
|
50
|
-
/** Resumes the timers for all visible toasts. */
|
|
51
|
-
resumeAll(): void,
|
|
52
|
-
/** The visible toasts. */
|
|
53
|
-
visibleToasts: QueuedToast<T>[]
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Provides state management for a toast queue. Toasts display brief, temporary notifications
|
|
58
|
-
* of actions, errors, or other events in an application.
|
|
59
|
-
*/
|
|
60
|
-
export function useToastState<T>(props: ToastStateProps = {}): ToastState<T> {
|
|
61
|
-
let {maxVisibleToasts = 1, wrapUpdate} = props;
|
|
62
|
-
let queue = useMemo(() => new ToastQueue<T>({maxVisibleToasts, wrapUpdate}), [maxVisibleToasts, wrapUpdate]);
|
|
63
|
-
return useToastQueue(queue);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Subscribes to a provided toast queue and provides methods to update it.
|
|
68
|
-
*/
|
|
69
|
-
export function useToastQueue<T>(queue: ToastQueue<T>): ToastState<T> {
|
|
70
|
-
let subscribe = useCallback(fn => queue.subscribe(fn), [queue]);
|
|
71
|
-
let getSnapshot = useCallback(() => queue.visibleToasts, [queue]);
|
|
72
|
-
let visibleToasts = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
visibleToasts,
|
|
76
|
-
add: (content, options) => queue.add(content, options),
|
|
77
|
-
close: key => queue.close(key),
|
|
78
|
-
pauseAll: () => queue.pauseAll(),
|
|
79
|
-
resumeAll: () => queue.resumeAll()
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* A ToastQueue manages the order of toasts.
|
|
85
|
-
*/
|
|
86
|
-
export class ToastQueue<T> {
|
|
87
|
-
private queue: QueuedToast<T>[] = [];
|
|
88
|
-
private subscriptions: Set<() => void> = new Set();
|
|
89
|
-
private maxVisibleToasts: number;
|
|
90
|
-
private wrapUpdate?: (fn: () => void, action: ToastAction) => void;
|
|
91
|
-
/** The currently visible toasts. */
|
|
92
|
-
visibleToasts: QueuedToast<T>[] = [];
|
|
93
|
-
|
|
94
|
-
constructor(options?: ToastStateProps) {
|
|
95
|
-
this.maxVisibleToasts = options?.maxVisibleToasts ?? Infinity;
|
|
96
|
-
this.wrapUpdate = options?.wrapUpdate;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
private runWithWrapUpdate(fn: () => void, action: ToastAction): void {
|
|
100
|
-
if (this.wrapUpdate) {
|
|
101
|
-
this.wrapUpdate(fn, action);
|
|
102
|
-
} else {
|
|
103
|
-
fn();
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/** Subscribes to updates to the visible toasts. */
|
|
108
|
-
subscribe(fn: () => void): () => void {
|
|
109
|
-
this.subscriptions.add(fn);
|
|
110
|
-
return () => this.subscriptions.delete(fn);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/** Adds a new toast to the queue. */
|
|
114
|
-
add(content: T, options: ToastOptions = {}): string {
|
|
115
|
-
let toastKey = '_' + Math.random().toString(36).slice(2);
|
|
116
|
-
let toast: QueuedToast<T> = {
|
|
117
|
-
...options,
|
|
118
|
-
content,
|
|
119
|
-
key: toastKey,
|
|
120
|
-
timer: options.timeout ? new Timer(() => this.close(toastKey), options.timeout) : undefined
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
this.queue.unshift(toast);
|
|
124
|
-
|
|
125
|
-
this.updateVisibleToasts('add');
|
|
126
|
-
return toastKey;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Closes a toast.
|
|
131
|
-
*/
|
|
132
|
-
close(key: string): void {
|
|
133
|
-
let index = this.queue.findIndex(t => t.key === key);
|
|
134
|
-
if (index >= 0) {
|
|
135
|
-
this.queue[index].onClose?.();
|
|
136
|
-
this.queue.splice(index, 1);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
this.updateVisibleToasts('remove');
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
private updateVisibleToasts(action: ToastAction) {
|
|
143
|
-
this.visibleToasts = this.queue.slice(0, this.maxVisibleToasts);
|
|
144
|
-
|
|
145
|
-
this.runWithWrapUpdate(() => {
|
|
146
|
-
for (let fn of this.subscriptions) {
|
|
147
|
-
fn();
|
|
148
|
-
}
|
|
149
|
-
}, action);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/** Pauses the timers for all visible toasts. */
|
|
153
|
-
pauseAll(): void {
|
|
154
|
-
for (let toast of this.visibleToasts) {
|
|
155
|
-
if (toast.timer) {
|
|
156
|
-
toast.timer.pause();
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** Resumes the timers for all visible toasts. */
|
|
162
|
-
resumeAll(): void {
|
|
163
|
-
for (let toast of this.visibleToasts) {
|
|
164
|
-
if (toast.timer) {
|
|
165
|
-
toast.timer.resume();
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
clear(): void {
|
|
171
|
-
this.queue = [];
|
|
172
|
-
this.updateVisibleToasts('clear');
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
class Timer {
|
|
177
|
-
private timerId;
|
|
178
|
-
private startTime: number | null = null;
|
|
179
|
-
private remaining: number;
|
|
180
|
-
private callback: () => void;
|
|
181
|
-
|
|
182
|
-
constructor(callback: () => void, delay: number) {
|
|
183
|
-
this.remaining = delay;
|
|
184
|
-
this.callback = callback;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
reset(delay: number): void {
|
|
188
|
-
this.remaining = delay;
|
|
189
|
-
this.resume();
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
pause(): void {
|
|
193
|
-
if (this.timerId == null) {
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
clearTimeout(this.timerId);
|
|
198
|
-
this.timerId = null;
|
|
199
|
-
this.remaining -= Date.now() - this.startTime!;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
resume(): void {
|
|
203
|
-
if (this.remaining <= 0) {
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
this.startTime = Date.now();
|
|
208
|
-
this.timerId = setTimeout(() => {
|
|
209
|
-
this.timerId = null;
|
|
210
|
-
this.remaining = 0;
|
|
211
|
-
this.callback();
|
|
212
|
-
}, this.remaining);
|
|
213
|
-
}
|
|
214
|
-
}
|