@equinor/cpl-error-snackbar-react 0.1.0 → 0.1.1
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/index.css +52 -0
- package/dist/index.d.mts +34 -2
- package/dist/index.d.ts +34 -2
- package/dist/index.js +241 -0
- package/dist/index.mjs +240 -0
- package/package.json +2 -3
package/dist/index.css
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/* src/toast/ToastContainerInner.module.css */
|
|
2
|
+
.wrapper {
|
|
3
|
+
position: fixed;
|
|
4
|
+
padding: 16px;
|
|
5
|
+
margin: -16px;
|
|
6
|
+
display: flex;
|
|
7
|
+
flex-direction: column;
|
|
8
|
+
gap: 8px;
|
|
9
|
+
max-height: min(600px, 70dvh);
|
|
10
|
+
width: 24rem;
|
|
11
|
+
overflow-y: auto;
|
|
12
|
+
overflow-x: hidden;
|
|
13
|
+
&:empty {
|
|
14
|
+
display: none;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
.top-right {
|
|
18
|
+
top: 16px;
|
|
19
|
+
right: 16px;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/* src/toast/ToastItem.module.css */
|
|
23
|
+
.slideIn {
|
|
24
|
+
animation: toast-in-right 150ms ease-out;
|
|
25
|
+
}
|
|
26
|
+
.toast {
|
|
27
|
+
transition: 50ms ease-out;
|
|
28
|
+
border-radius: 4px;
|
|
29
|
+
padding: 4px 8px 4px 16px;
|
|
30
|
+
display: flex;
|
|
31
|
+
align-items: center;
|
|
32
|
+
justify-content: stretch;
|
|
33
|
+
border: 2px solid var(--eds_ui_background__medium, rgba(220, 220, 220, 1));
|
|
34
|
+
}
|
|
35
|
+
@keyframes toast-in-right {
|
|
36
|
+
from {
|
|
37
|
+
opacity: 0;
|
|
38
|
+
transform: translateX(20%);
|
|
39
|
+
}
|
|
40
|
+
to {
|
|
41
|
+
opacity: 1;
|
|
42
|
+
transform: translateX(0);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
@keyframes toast-out-collapse {
|
|
46
|
+
from {
|
|
47
|
+
height: auto;
|
|
48
|
+
}
|
|
49
|
+
to {
|
|
50
|
+
max-height: 0;
|
|
51
|
+
}
|
|
52
|
+
}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import * as react from 'react';
|
|
3
|
-
import { ReactNode } from 'react';
|
|
3
|
+
import { ReactNode, SetStateAction } from 'react';
|
|
4
4
|
|
|
5
5
|
interface CopyToClipboardButtonProps {
|
|
6
6
|
value: string;
|
|
@@ -147,4 +147,36 @@ declare function ErrorSnackbarListContainer({ disablePositionFixed, }: ErrorSnac
|
|
|
147
147
|
|
|
148
148
|
declare function parseUnknownError(unknownError: unknown): Error;
|
|
149
149
|
|
|
150
|
-
|
|
150
|
+
type ToastType = 'info' | 'success' | 'warning' | 'danger';
|
|
151
|
+
type ToastTimeout = number | 'persistent';
|
|
152
|
+
interface Toast {
|
|
153
|
+
id: string;
|
|
154
|
+
type: ToastType;
|
|
155
|
+
title: string;
|
|
156
|
+
timeout: ToastTimeout;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
interface BuildToasterOptions {
|
|
160
|
+
timeoutOverrides?: Partial<Record<ToastType, ToastTimeout>>;
|
|
161
|
+
}
|
|
162
|
+
declare function buildToaster(options: BuildToasterOptions): {
|
|
163
|
+
useState: () => readonly [Toast[], (data: SetStateAction<Toast[]>) => void];
|
|
164
|
+
create: (toast: Omit<Toast, "id" | "timeout">) => `${string}-${string}-${string}-${string}-${string}`;
|
|
165
|
+
update: (id: string, toast: Partial<Omit<Toast, "id" | "timeout">>) => void;
|
|
166
|
+
delete: (id: string) => void;
|
|
167
|
+
deleteAll: () => void;
|
|
168
|
+
};
|
|
169
|
+
declare const DEFAULT_TIMEOUT_MS_BY_TYPE: {
|
|
170
|
+
readonly info: 3000;
|
|
171
|
+
readonly success: 3000;
|
|
172
|
+
readonly warning: 3000;
|
|
173
|
+
readonly danger: "persistent";
|
|
174
|
+
};
|
|
175
|
+
type Toaster = ReturnType<typeof buildToaster>;
|
|
176
|
+
|
|
177
|
+
interface ToastContainerProps {
|
|
178
|
+
toaster: Toaster;
|
|
179
|
+
}
|
|
180
|
+
declare function ToastContainer({ toaster }: ToastContainerProps): react_jsx_runtime.JSX.Element;
|
|
181
|
+
|
|
182
|
+
export { type AccessRequirement, type BuildToasterOptions, CopyToClipboardButton, DEFAULT_TIMEOUT_MS_BY_TYPE, ERROR_SNACKBARS_FIXED_LIST_TESTID, type Error, ErrorSnackbar, ErrorSnackbarContext, ErrorSnackbarContextProvider, ErrorSnackbarFixedList, ErrorSnackbarListContainer, type ErrorSnackbarObject, type InnerError, type Toast, ToastContainer, type ToastContainerProps, type ToastTimeout, type ToastType, type Toaster, buildToaster, parseUnknownError, useErrorSnackbarContext };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import * as react from 'react';
|
|
3
|
-
import { ReactNode } from 'react';
|
|
3
|
+
import { ReactNode, SetStateAction } from 'react';
|
|
4
4
|
|
|
5
5
|
interface CopyToClipboardButtonProps {
|
|
6
6
|
value: string;
|
|
@@ -147,4 +147,36 @@ declare function ErrorSnackbarListContainer({ disablePositionFixed, }: ErrorSnac
|
|
|
147
147
|
|
|
148
148
|
declare function parseUnknownError(unknownError: unknown): Error;
|
|
149
149
|
|
|
150
|
-
|
|
150
|
+
type ToastType = 'info' | 'success' | 'warning' | 'danger';
|
|
151
|
+
type ToastTimeout = number | 'persistent';
|
|
152
|
+
interface Toast {
|
|
153
|
+
id: string;
|
|
154
|
+
type: ToastType;
|
|
155
|
+
title: string;
|
|
156
|
+
timeout: ToastTimeout;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
interface BuildToasterOptions {
|
|
160
|
+
timeoutOverrides?: Partial<Record<ToastType, ToastTimeout>>;
|
|
161
|
+
}
|
|
162
|
+
declare function buildToaster(options: BuildToasterOptions): {
|
|
163
|
+
useState: () => readonly [Toast[], (data: SetStateAction<Toast[]>) => void];
|
|
164
|
+
create: (toast: Omit<Toast, "id" | "timeout">) => `${string}-${string}-${string}-${string}-${string}`;
|
|
165
|
+
update: (id: string, toast: Partial<Omit<Toast, "id" | "timeout">>) => void;
|
|
166
|
+
delete: (id: string) => void;
|
|
167
|
+
deleteAll: () => void;
|
|
168
|
+
};
|
|
169
|
+
declare const DEFAULT_TIMEOUT_MS_BY_TYPE: {
|
|
170
|
+
readonly info: 3000;
|
|
171
|
+
readonly success: 3000;
|
|
172
|
+
readonly warning: 3000;
|
|
173
|
+
readonly danger: "persistent";
|
|
174
|
+
};
|
|
175
|
+
type Toaster = ReturnType<typeof buildToaster>;
|
|
176
|
+
|
|
177
|
+
interface ToastContainerProps {
|
|
178
|
+
toaster: Toaster;
|
|
179
|
+
}
|
|
180
|
+
declare function ToastContainer({ toaster }: ToastContainerProps): react_jsx_runtime.JSX.Element;
|
|
181
|
+
|
|
182
|
+
export { type AccessRequirement, type BuildToasterOptions, CopyToClipboardButton, DEFAULT_TIMEOUT_MS_BY_TYPE, ERROR_SNACKBARS_FIXED_LIST_TESTID, type Error, ErrorSnackbar, ErrorSnackbarContext, ErrorSnackbarContextProvider, ErrorSnackbarFixedList, ErrorSnackbarListContainer, type ErrorSnackbarObject, type InnerError, type Toast, ToastContainer, type ToastContainerProps, type ToastTimeout, type ToastType, type Toaster, buildToaster, parseUnknownError, useErrorSnackbarContext };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __create = Object.create;
|
|
3
3
|
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
4
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
7
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
9
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
10
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __spreadValues = (a, b) => {
|
|
14
|
+
for (var prop in b || (b = {}))
|
|
15
|
+
if (__hasOwnProp.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
if (__getOwnPropSymbols)
|
|
18
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
+
if (__propIsEnum.call(b, prop))
|
|
20
|
+
__defNormalProp(a, prop, b[prop]);
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
};
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
8
25
|
var __export = (target, all) => {
|
|
9
26
|
for (var name in all)
|
|
10
27
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -51,12 +68,15 @@ var __async = (__this, __arguments, generator) => {
|
|
|
51
68
|
var index_exports = {};
|
|
52
69
|
__export(index_exports, {
|
|
53
70
|
CopyToClipboardButton: () => CopyToClipboardButton,
|
|
71
|
+
DEFAULT_TIMEOUT_MS_BY_TYPE: () => DEFAULT_TIMEOUT_MS_BY_TYPE,
|
|
54
72
|
ERROR_SNACKBARS_FIXED_LIST_TESTID: () => ERROR_SNACKBARS_FIXED_LIST_TESTID,
|
|
55
73
|
ErrorSnackbar: () => ErrorSnackbar,
|
|
56
74
|
ErrorSnackbarContext: () => ErrorSnackbarContext,
|
|
57
75
|
ErrorSnackbarContextProvider: () => ErrorSnackbarContextProvider,
|
|
58
76
|
ErrorSnackbarFixedList: () => ErrorSnackbarFixedList,
|
|
59
77
|
ErrorSnackbarListContainer: () => ErrorSnackbarListContainer,
|
|
78
|
+
ToastContainer: () => ToastContainer,
|
|
79
|
+
buildToaster: () => buildToaster,
|
|
60
80
|
parseUnknownError: () => parseUnknownError,
|
|
61
81
|
useErrorSnackbarContext: () => useErrorSnackbarContext
|
|
62
82
|
});
|
|
@@ -405,15 +425,236 @@ function getTypedPropertyIfExists(object, key, ...acceptedTypes) {
|
|
|
405
425
|
function itemIsDefined(e) {
|
|
406
426
|
return Boolean(e);
|
|
407
427
|
}
|
|
428
|
+
|
|
429
|
+
// src/toast/createToaster.ts
|
|
430
|
+
var import_react3 = require("react");
|
|
431
|
+
function buildToaster(options) {
|
|
432
|
+
const { timeoutOverrides = {} } = options;
|
|
433
|
+
const KEY = "toasts";
|
|
434
|
+
const state = { [KEY]: [] };
|
|
435
|
+
const listeners = [];
|
|
436
|
+
function subscribe(callback) {
|
|
437
|
+
const index = listeners.push(callback);
|
|
438
|
+
function unsubscribe() {
|
|
439
|
+
listeners.splice(index, 1);
|
|
440
|
+
}
|
|
441
|
+
return { unsubscribe };
|
|
442
|
+
}
|
|
443
|
+
function setState(data) {
|
|
444
|
+
data = typeof data === "function" ? data(state[KEY]) : data;
|
|
445
|
+
state[KEY] = data;
|
|
446
|
+
listeners.forEach((callback) => callback(data));
|
|
447
|
+
}
|
|
448
|
+
function useState4() {
|
|
449
|
+
const [values, setValues] = (0, import_react3.useState)(state[KEY]);
|
|
450
|
+
(0, import_react3.useEffect)(() => {
|
|
451
|
+
const subscriber = subscribe((data) => {
|
|
452
|
+
setValues(data);
|
|
453
|
+
});
|
|
454
|
+
return () => {
|
|
455
|
+
subscriber.unsubscribe();
|
|
456
|
+
};
|
|
457
|
+
}, [setValues]);
|
|
458
|
+
return [values, setState];
|
|
459
|
+
}
|
|
460
|
+
return {
|
|
461
|
+
useState: useState4,
|
|
462
|
+
create: (toast) => {
|
|
463
|
+
const id = crypto.randomUUID();
|
|
464
|
+
setState((old) => {
|
|
465
|
+
var _a;
|
|
466
|
+
return [
|
|
467
|
+
__spreadProps(__spreadValues({
|
|
468
|
+
id
|
|
469
|
+
}, toast), {
|
|
470
|
+
timeout: (_a = timeoutOverrides[toast.type]) != null ? _a : DEFAULT_TIMEOUT_MS_BY_TYPE[toast.type]
|
|
471
|
+
}),
|
|
472
|
+
...old
|
|
473
|
+
];
|
|
474
|
+
});
|
|
475
|
+
return id;
|
|
476
|
+
},
|
|
477
|
+
update: (id, toast) => {
|
|
478
|
+
setState((old) => {
|
|
479
|
+
return old.map((item) => item.id === id ? __spreadValues(__spreadValues({}, item), toast) : item);
|
|
480
|
+
});
|
|
481
|
+
},
|
|
482
|
+
delete: (id) => {
|
|
483
|
+
setState((old) => old.filter((item) => item.id !== id));
|
|
484
|
+
},
|
|
485
|
+
deleteAll: () => {
|
|
486
|
+
setState([]);
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
var DEFAULT_TIMEOUT_MS_BY_TYPE = {
|
|
491
|
+
info: 3e3,
|
|
492
|
+
// 3 seconds
|
|
493
|
+
success: 3e3,
|
|
494
|
+
// 3 seconds
|
|
495
|
+
warning: 3e3,
|
|
496
|
+
// 3 seconds
|
|
497
|
+
danger: "persistent"
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
// src/toast/ToastContainerInner.module.css
|
|
501
|
+
var ToastContainerInner_default = {};
|
|
502
|
+
|
|
503
|
+
// src/toast/ToastItem.tsx
|
|
504
|
+
var import_eds_core_react3 = require("@equinor/eds-core-react");
|
|
505
|
+
var import_eds_icons3 = require("@equinor/eds-icons");
|
|
506
|
+
var import_eds_tokens2 = require("@equinor/eds-tokens");
|
|
507
|
+
var import_react4 = require("react");
|
|
508
|
+
var import_styled_components4 = __toESM(require("styled-components"));
|
|
509
|
+
|
|
510
|
+
// src/toast/ToastItem.module.css
|
|
511
|
+
var ToastItem_default = {};
|
|
512
|
+
|
|
513
|
+
// src/toast/ToastItem.tsx
|
|
514
|
+
var import_jsx_runtime6 = require("react/jsx-runtime");
|
|
515
|
+
function ToastItem({ toast, onDelete }) {
|
|
516
|
+
const [isHovered, setIsHovered] = (0, import_react4.useState)(false);
|
|
517
|
+
(0, import_react4.useEffect)(() => {
|
|
518
|
+
if (isHovered || toast.timeout === "persistent") {
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
const timeout = setTimeout(() => {
|
|
522
|
+
onDelete(toast.id);
|
|
523
|
+
}, toast.timeout);
|
|
524
|
+
return () => {
|
|
525
|
+
clearTimeout(timeout);
|
|
526
|
+
};
|
|
527
|
+
}, [isHovered, onDelete, toast.id, toast.timeout]);
|
|
528
|
+
return /* @__PURE__ */ (0, import_jsx_runtime6.jsxs)(
|
|
529
|
+
StyledPaper,
|
|
530
|
+
{
|
|
531
|
+
elevation: "raised",
|
|
532
|
+
$toastType: toast.type,
|
|
533
|
+
className: `${ToastItem_default.toast} ${ToastItem_default.slideIn}`,
|
|
534
|
+
onMouseOver: () => setIsHovered(true),
|
|
535
|
+
onMouseOut: () => setIsHovered(false),
|
|
536
|
+
children: [
|
|
537
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_eds_core_react3.Icon, { data: getIconByType(toast.type), color: "primary", size: 24, style: { marginRight: 8 } }),
|
|
538
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_eds_core_react3.Typography, { children: toast.title }),
|
|
539
|
+
/* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
|
|
540
|
+
import_eds_core_react3.Button,
|
|
541
|
+
{
|
|
542
|
+
variant: "ghost_icon",
|
|
543
|
+
onClick: () => onDelete(toast.id),
|
|
544
|
+
style: { marginLeft: "auto" },
|
|
545
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(import_eds_core_react3.Icon, { data: import_eds_icons3.clear, title: "clear", color: "primary", size: 24 })
|
|
546
|
+
}
|
|
547
|
+
)
|
|
548
|
+
]
|
|
549
|
+
}
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
function getIconByType(toastType) {
|
|
553
|
+
switch (toastType) {
|
|
554
|
+
case "success":
|
|
555
|
+
return import_eds_icons3.check;
|
|
556
|
+
case "danger":
|
|
557
|
+
return import_eds_icons3.error_filled;
|
|
558
|
+
case "warning":
|
|
559
|
+
return import_eds_icons3.warning_filled;
|
|
560
|
+
default:
|
|
561
|
+
return import_eds_icons3.info_circle;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
var fill = false;
|
|
565
|
+
var StyledPaper = (0, import_styled_components4.default)(import_eds_core_react3.Paper)`
|
|
566
|
+
& > p {
|
|
567
|
+
color: inherit;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
border: 0;
|
|
571
|
+
padding: 12px 16px;
|
|
572
|
+
|
|
573
|
+
${({ $toastType }) => {
|
|
574
|
+
switch ($toastType) {
|
|
575
|
+
case "info":
|
|
576
|
+
return fill ? import_styled_components4.css`
|
|
577
|
+
background-color: ${import_eds_tokens2.tokens.colors.ui.background__default.rgba};
|
|
578
|
+
` : import_styled_components4.css``;
|
|
579
|
+
case "success":
|
|
580
|
+
return fill ? import_styled_components4.css`
|
|
581
|
+
color: ${import_eds_tokens2.tokens.colors.text.static_icons__primary_white.rgba};
|
|
582
|
+
border-color: ${import_eds_tokens2.tokens.colors.interactive.success__resting.rgba};
|
|
583
|
+
background-color: ${import_eds_tokens2.tokens.colors.interactive.success__resting.rgba};
|
|
584
|
+
` : import_styled_components4.css`
|
|
585
|
+
border-color: ${import_eds_tokens2.tokens.colors.interactive.success__resting.rgba};
|
|
586
|
+
|
|
587
|
+
& > svg:first-of-type {
|
|
588
|
+
fill: ${import_eds_tokens2.tokens.colors.interactive.success__text.rgba};
|
|
589
|
+
}
|
|
590
|
+
`;
|
|
591
|
+
case "warning":
|
|
592
|
+
return fill ? import_styled_components4.css`
|
|
593
|
+
border-color: ${import_eds_tokens2.tokens.colors.interactive.warning__resting.rgba};
|
|
594
|
+
background-color: ${import_eds_tokens2.tokens.colors.interactive.warning__resting.rgba};
|
|
595
|
+
` : import_styled_components4.css`
|
|
596
|
+
border-color: ${import_eds_tokens2.tokens.colors.interactive.warning__resting.rgba};
|
|
597
|
+
|
|
598
|
+
& > svg:first-of-type {
|
|
599
|
+
fill: ${import_eds_tokens2.tokens.colors.interactive.warning__text.rgba};
|
|
600
|
+
}
|
|
601
|
+
`;
|
|
602
|
+
case "danger":
|
|
603
|
+
return fill ? import_styled_components4.css`
|
|
604
|
+
border-color: ${import_eds_tokens2.tokens.colors.interactive.danger__resting.rgba};
|
|
605
|
+
background-color: ${import_eds_tokens2.tokens.colors.interactive.danger__resting.rgba};
|
|
606
|
+
` : import_styled_components4.css`
|
|
607
|
+
border-color: ${import_eds_tokens2.tokens.colors.interactive.danger__resting.rgba};
|
|
608
|
+
& > svg:first-of-type {
|
|
609
|
+
fill: ${import_eds_tokens2.tokens.colors.interactive.danger__text.rgba};
|
|
610
|
+
}
|
|
611
|
+
`;
|
|
612
|
+
}
|
|
613
|
+
}};
|
|
614
|
+
`;
|
|
615
|
+
|
|
616
|
+
// src/toast/ToastContainerInner.tsx
|
|
617
|
+
var import_jsx_runtime7 = require("react/jsx-runtime");
|
|
618
|
+
function ToastContainerInner({
|
|
619
|
+
deleteToastById,
|
|
620
|
+
toasts,
|
|
621
|
+
placement = "top-right",
|
|
622
|
+
toastContainerClassName = ""
|
|
623
|
+
}) {
|
|
624
|
+
return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
|
|
625
|
+
"div",
|
|
626
|
+
{
|
|
627
|
+
className: `${ToastContainerInner_default.wrapper} ${getClassNameByPlacement(placement)} ${toastContainerClassName}`,
|
|
628
|
+
children: toasts.map((toast) => /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(ToastItem, { toast, onDelete: deleteToastById }, toast.id))
|
|
629
|
+
}
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
function getClassNameByPlacement(placement) {
|
|
633
|
+
switch (placement) {
|
|
634
|
+
case "top-right": {
|
|
635
|
+
return ToastContainerInner_default["top-right"];
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// src/toast/ToastContainer.tsx
|
|
641
|
+
var import_jsx_runtime8 = require("react/jsx-runtime");
|
|
642
|
+
function ToastContainer({ toaster }) {
|
|
643
|
+
const [toasts] = toaster.useState();
|
|
644
|
+
return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(ToastContainerInner, { toasts, deleteToastById: toaster.delete });
|
|
645
|
+
}
|
|
408
646
|
// Annotate the CommonJS export names for ESM import in node:
|
|
409
647
|
0 && (module.exports = {
|
|
410
648
|
CopyToClipboardButton,
|
|
649
|
+
DEFAULT_TIMEOUT_MS_BY_TYPE,
|
|
411
650
|
ERROR_SNACKBARS_FIXED_LIST_TESTID,
|
|
412
651
|
ErrorSnackbar,
|
|
413
652
|
ErrorSnackbarContext,
|
|
414
653
|
ErrorSnackbarContextProvider,
|
|
415
654
|
ErrorSnackbarFixedList,
|
|
416
655
|
ErrorSnackbarListContainer,
|
|
656
|
+
ToastContainer,
|
|
657
|
+
buildToaster,
|
|
417
658
|
parseUnknownError,
|
|
418
659
|
useErrorSnackbarContext
|
|
419
660
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
1
20
|
var __async = (__this, __arguments, generator) => {
|
|
2
21
|
return new Promise((resolve, reject) => {
|
|
3
22
|
var fulfilled = (value) => {
|
|
@@ -362,14 +381,235 @@ function getTypedPropertyIfExists(object, key, ...acceptedTypes) {
|
|
|
362
381
|
function itemIsDefined(e) {
|
|
363
382
|
return Boolean(e);
|
|
364
383
|
}
|
|
384
|
+
|
|
385
|
+
// src/toast/createToaster.ts
|
|
386
|
+
import { useEffect as useEffect2, useState as useReactState } from "react";
|
|
387
|
+
function buildToaster(options) {
|
|
388
|
+
const { timeoutOverrides = {} } = options;
|
|
389
|
+
const KEY = "toasts";
|
|
390
|
+
const state = { [KEY]: [] };
|
|
391
|
+
const listeners = [];
|
|
392
|
+
function subscribe(callback) {
|
|
393
|
+
const index = listeners.push(callback);
|
|
394
|
+
function unsubscribe() {
|
|
395
|
+
listeners.splice(index, 1);
|
|
396
|
+
}
|
|
397
|
+
return { unsubscribe };
|
|
398
|
+
}
|
|
399
|
+
function setState(data) {
|
|
400
|
+
data = typeof data === "function" ? data(state[KEY]) : data;
|
|
401
|
+
state[KEY] = data;
|
|
402
|
+
listeners.forEach((callback) => callback(data));
|
|
403
|
+
}
|
|
404
|
+
function useState4() {
|
|
405
|
+
const [values, setValues] = useReactState(state[KEY]);
|
|
406
|
+
useEffect2(() => {
|
|
407
|
+
const subscriber = subscribe((data) => {
|
|
408
|
+
setValues(data);
|
|
409
|
+
});
|
|
410
|
+
return () => {
|
|
411
|
+
subscriber.unsubscribe();
|
|
412
|
+
};
|
|
413
|
+
}, [setValues]);
|
|
414
|
+
return [values, setState];
|
|
415
|
+
}
|
|
416
|
+
return {
|
|
417
|
+
useState: useState4,
|
|
418
|
+
create: (toast) => {
|
|
419
|
+
const id = crypto.randomUUID();
|
|
420
|
+
setState((old) => {
|
|
421
|
+
var _a;
|
|
422
|
+
return [
|
|
423
|
+
__spreadProps(__spreadValues({
|
|
424
|
+
id
|
|
425
|
+
}, toast), {
|
|
426
|
+
timeout: (_a = timeoutOverrides[toast.type]) != null ? _a : DEFAULT_TIMEOUT_MS_BY_TYPE[toast.type]
|
|
427
|
+
}),
|
|
428
|
+
...old
|
|
429
|
+
];
|
|
430
|
+
});
|
|
431
|
+
return id;
|
|
432
|
+
},
|
|
433
|
+
update: (id, toast) => {
|
|
434
|
+
setState((old) => {
|
|
435
|
+
return old.map((item) => item.id === id ? __spreadValues(__spreadValues({}, item), toast) : item);
|
|
436
|
+
});
|
|
437
|
+
},
|
|
438
|
+
delete: (id) => {
|
|
439
|
+
setState((old) => old.filter((item) => item.id !== id));
|
|
440
|
+
},
|
|
441
|
+
deleteAll: () => {
|
|
442
|
+
setState([]);
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
var DEFAULT_TIMEOUT_MS_BY_TYPE = {
|
|
447
|
+
info: 3e3,
|
|
448
|
+
// 3 seconds
|
|
449
|
+
success: 3e3,
|
|
450
|
+
// 3 seconds
|
|
451
|
+
warning: 3e3,
|
|
452
|
+
// 3 seconds
|
|
453
|
+
danger: "persistent"
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
// src/toast/ToastContainerInner.module.css
|
|
457
|
+
var ToastContainerInner_default = {};
|
|
458
|
+
|
|
459
|
+
// src/toast/ToastItem.tsx
|
|
460
|
+
import { Button as Button3, Icon as Icon3, Paper, Typography as Typography2 } from "@equinor/eds-core-react";
|
|
461
|
+
import { check as check2, clear, error_filled as error_filled2, info_circle, warning_filled } from "@equinor/eds-icons";
|
|
462
|
+
import { tokens as tokens2 } from "@equinor/eds-tokens";
|
|
463
|
+
import { useEffect as useEffect3, useState as useState3 } from "react";
|
|
464
|
+
import styled4, { css } from "styled-components";
|
|
465
|
+
|
|
466
|
+
// src/toast/ToastItem.module.css
|
|
467
|
+
var ToastItem_default = {};
|
|
468
|
+
|
|
469
|
+
// src/toast/ToastItem.tsx
|
|
470
|
+
import { jsx as jsx6, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
471
|
+
function ToastItem({ toast, onDelete }) {
|
|
472
|
+
const [isHovered, setIsHovered] = useState3(false);
|
|
473
|
+
useEffect3(() => {
|
|
474
|
+
if (isHovered || toast.timeout === "persistent") {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
const timeout = setTimeout(() => {
|
|
478
|
+
onDelete(toast.id);
|
|
479
|
+
}, toast.timeout);
|
|
480
|
+
return () => {
|
|
481
|
+
clearTimeout(timeout);
|
|
482
|
+
};
|
|
483
|
+
}, [isHovered, onDelete, toast.id, toast.timeout]);
|
|
484
|
+
return /* @__PURE__ */ jsxs3(
|
|
485
|
+
StyledPaper,
|
|
486
|
+
{
|
|
487
|
+
elevation: "raised",
|
|
488
|
+
$toastType: toast.type,
|
|
489
|
+
className: `${ToastItem_default.toast} ${ToastItem_default.slideIn}`,
|
|
490
|
+
onMouseOver: () => setIsHovered(true),
|
|
491
|
+
onMouseOut: () => setIsHovered(false),
|
|
492
|
+
children: [
|
|
493
|
+
/* @__PURE__ */ jsx6(Icon3, { data: getIconByType(toast.type), color: "primary", size: 24, style: { marginRight: 8 } }),
|
|
494
|
+
/* @__PURE__ */ jsx6(Typography2, { children: toast.title }),
|
|
495
|
+
/* @__PURE__ */ jsx6(
|
|
496
|
+
Button3,
|
|
497
|
+
{
|
|
498
|
+
variant: "ghost_icon",
|
|
499
|
+
onClick: () => onDelete(toast.id),
|
|
500
|
+
style: { marginLeft: "auto" },
|
|
501
|
+
children: /* @__PURE__ */ jsx6(Icon3, { data: clear, title: "clear", color: "primary", size: 24 })
|
|
502
|
+
}
|
|
503
|
+
)
|
|
504
|
+
]
|
|
505
|
+
}
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
function getIconByType(toastType) {
|
|
509
|
+
switch (toastType) {
|
|
510
|
+
case "success":
|
|
511
|
+
return check2;
|
|
512
|
+
case "danger":
|
|
513
|
+
return error_filled2;
|
|
514
|
+
case "warning":
|
|
515
|
+
return warning_filled;
|
|
516
|
+
default:
|
|
517
|
+
return info_circle;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
var fill = false;
|
|
521
|
+
var StyledPaper = styled4(Paper)`
|
|
522
|
+
& > p {
|
|
523
|
+
color: inherit;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
border: 0;
|
|
527
|
+
padding: 12px 16px;
|
|
528
|
+
|
|
529
|
+
${({ $toastType }) => {
|
|
530
|
+
switch ($toastType) {
|
|
531
|
+
case "info":
|
|
532
|
+
return fill ? css`
|
|
533
|
+
background-color: ${tokens2.colors.ui.background__default.rgba};
|
|
534
|
+
` : css``;
|
|
535
|
+
case "success":
|
|
536
|
+
return fill ? css`
|
|
537
|
+
color: ${tokens2.colors.text.static_icons__primary_white.rgba};
|
|
538
|
+
border-color: ${tokens2.colors.interactive.success__resting.rgba};
|
|
539
|
+
background-color: ${tokens2.colors.interactive.success__resting.rgba};
|
|
540
|
+
` : css`
|
|
541
|
+
border-color: ${tokens2.colors.interactive.success__resting.rgba};
|
|
542
|
+
|
|
543
|
+
& > svg:first-of-type {
|
|
544
|
+
fill: ${tokens2.colors.interactive.success__text.rgba};
|
|
545
|
+
}
|
|
546
|
+
`;
|
|
547
|
+
case "warning":
|
|
548
|
+
return fill ? css`
|
|
549
|
+
border-color: ${tokens2.colors.interactive.warning__resting.rgba};
|
|
550
|
+
background-color: ${tokens2.colors.interactive.warning__resting.rgba};
|
|
551
|
+
` : css`
|
|
552
|
+
border-color: ${tokens2.colors.interactive.warning__resting.rgba};
|
|
553
|
+
|
|
554
|
+
& > svg:first-of-type {
|
|
555
|
+
fill: ${tokens2.colors.interactive.warning__text.rgba};
|
|
556
|
+
}
|
|
557
|
+
`;
|
|
558
|
+
case "danger":
|
|
559
|
+
return fill ? css`
|
|
560
|
+
border-color: ${tokens2.colors.interactive.danger__resting.rgba};
|
|
561
|
+
background-color: ${tokens2.colors.interactive.danger__resting.rgba};
|
|
562
|
+
` : css`
|
|
563
|
+
border-color: ${tokens2.colors.interactive.danger__resting.rgba};
|
|
564
|
+
& > svg:first-of-type {
|
|
565
|
+
fill: ${tokens2.colors.interactive.danger__text.rgba};
|
|
566
|
+
}
|
|
567
|
+
`;
|
|
568
|
+
}
|
|
569
|
+
}};
|
|
570
|
+
`;
|
|
571
|
+
|
|
572
|
+
// src/toast/ToastContainerInner.tsx
|
|
573
|
+
import { jsx as jsx7 } from "react/jsx-runtime";
|
|
574
|
+
function ToastContainerInner({
|
|
575
|
+
deleteToastById,
|
|
576
|
+
toasts,
|
|
577
|
+
placement = "top-right",
|
|
578
|
+
toastContainerClassName = ""
|
|
579
|
+
}) {
|
|
580
|
+
return /* @__PURE__ */ jsx7(
|
|
581
|
+
"div",
|
|
582
|
+
{
|
|
583
|
+
className: `${ToastContainerInner_default.wrapper} ${getClassNameByPlacement(placement)} ${toastContainerClassName}`,
|
|
584
|
+
children: toasts.map((toast) => /* @__PURE__ */ jsx7(ToastItem, { toast, onDelete: deleteToastById }, toast.id))
|
|
585
|
+
}
|
|
586
|
+
);
|
|
587
|
+
}
|
|
588
|
+
function getClassNameByPlacement(placement) {
|
|
589
|
+
switch (placement) {
|
|
590
|
+
case "top-right": {
|
|
591
|
+
return ToastContainerInner_default["top-right"];
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// src/toast/ToastContainer.tsx
|
|
597
|
+
import { jsx as jsx8 } from "react/jsx-runtime";
|
|
598
|
+
function ToastContainer({ toaster }) {
|
|
599
|
+
const [toasts] = toaster.useState();
|
|
600
|
+
return /* @__PURE__ */ jsx8(ToastContainerInner, { toasts, deleteToastById: toaster.delete });
|
|
601
|
+
}
|
|
365
602
|
export {
|
|
366
603
|
CopyToClipboardButton,
|
|
604
|
+
DEFAULT_TIMEOUT_MS_BY_TYPE,
|
|
367
605
|
ERROR_SNACKBARS_FIXED_LIST_TESTID,
|
|
368
606
|
ErrorSnackbar,
|
|
369
607
|
ErrorSnackbarContext,
|
|
370
608
|
ErrorSnackbarContextProvider,
|
|
371
609
|
ErrorSnackbarFixedList,
|
|
372
610
|
ErrorSnackbarListContainer,
|
|
611
|
+
ToastContainer,
|
|
612
|
+
buildToaster,
|
|
373
613
|
parseUnknownError,
|
|
374
614
|
useErrorSnackbarContext
|
|
375
615
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@equinor/cpl-error-snackbar-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -14,8 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@equinor/eds-core-react": "^0.42.5",
|
|
17
|
-
"@storybook/react": "^
|
|
18
|
-
"@storybook/test": "^8.6.14",
|
|
17
|
+
"@storybook/react": "^9.0.12",
|
|
19
18
|
"@types/react": "^18.3.18",
|
|
20
19
|
"@types/react-dom": "^18.3.5",
|
|
21
20
|
"@types/styled-components": "^5.1.34",
|