@snack-uikit/toaster 0.3.21-preview-85c5f47b.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/CHANGELOG.md +317 -0
- package/LICENSE +201 -0
- package/README.md +84 -0
- package/dist/components/ToastSystemEvent/ToastSystemEvent.d.ts +23 -0
- package/dist/components/ToastSystemEvent/ToastSystemEvent.js +36 -0
- package/dist/components/ToastSystemEvent/constants.d.ts +15 -0
- package/dist/components/ToastSystemEvent/constants.js +16 -0
- package/dist/components/ToastSystemEvent/index.d.ts +1 -0
- package/dist/components/ToastSystemEvent/index.js +1 -0
- package/dist/components/ToastSystemEvent/styles.module.css +183 -0
- package/dist/components/ToastSystemEvent/utils.d.ts +2 -0
- package/dist/components/ToastSystemEvent/utils.js +17 -0
- package/dist/components/ToastUserAction/ToastUserAction.d.ts +19 -0
- package/dist/components/ToastUserAction/ToastUserAction.js +26 -0
- package/dist/components/ToastUserAction/constants.d.ts +10 -0
- package/dist/components/ToastUserAction/constants.js +12 -0
- package/dist/components/ToastUserAction/index.d.ts +1 -0
- package/dist/components/ToastUserAction/index.js +1 -0
- package/dist/components/ToastUserAction/styles.module.css +56 -0
- package/dist/components/ToastUserAction/utils.d.ts +2 -0
- package/dist/components/ToastUserAction/utils.js +16 -0
- package/dist/components/ToasterContainer/ToasterContainer.d.ts +21 -0
- package/dist/components/ToasterContainer/ToasterContainer.js +36 -0
- package/dist/components/ToasterContainer/index.d.ts +1 -0
- package/dist/components/ToasterContainer/index.js +1 -0
- package/dist/components/ToasterContainer/styles.module.css +78 -0
- package/dist/components/index.d.ts +3 -0
- package/dist/components/index.js +3 -0
- package/dist/constants.d.ts +9 -0
- package/dist/constants.js +22 -0
- package/dist/helpers.d.ts +6 -0
- package/dist/helpers.js +199 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/testIds.d.ts +16 -0
- package/dist/testIds.js +18 -0
- package/dist/types.d.ts +67 -0
- package/dist/types.js +5 -0
- package/package.json +43 -0
- package/src/components/ToastSystemEvent/ToastSystemEvent.tsx +119 -0
- package/src/components/ToastSystemEvent/constants.ts +17 -0
- package/src/components/ToastSystemEvent/index.ts +1 -0
- package/src/components/ToastSystemEvent/styles.module.scss +194 -0
- package/src/components/ToastSystemEvent/utils.tsx +18 -0
- package/src/components/ToastUserAction/ToastUserAction.tsx +68 -0
- package/src/components/ToastUserAction/constants.tsx +11 -0
- package/src/components/ToastUserAction/index.ts +1 -0
- package/src/components/ToastUserAction/styles.module.scss +66 -0
- package/src/components/ToastUserAction/utils.tsx +17 -0
- package/src/components/ToasterContainer/ToasterContainer.tsx +93 -0
- package/src/components/ToasterContainer/index.ts +1 -0
- package/src/components/ToasterContainer/styles.module.scss +81 -0
- package/src/components/index.ts +3 -0
- package/src/constants.ts +25 -0
- package/src/helpers.tsx +293 -0
- package/src/index.ts +3 -0
- package/src/testIds.ts +21 -0
- package/src/types.ts +82 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AlarmFilledSVG, CheckFilledSVG, CrossFilledSVG, InfoFilledSVG } from '@snack-uikit/icons';
|
|
2
|
+
|
|
3
|
+
import { ToastSystemEventAppearance } from './constants';
|
|
4
|
+
|
|
5
|
+
export function getIcon(appearance: ToastSystemEventAppearance) {
|
|
6
|
+
switch (appearance) {
|
|
7
|
+
case ToastSystemEventAppearance.Success:
|
|
8
|
+
return <CheckFilledSVG />;
|
|
9
|
+
case ToastSystemEventAppearance.Error:
|
|
10
|
+
case ToastSystemEventAppearance.ErrorCritical:
|
|
11
|
+
return <CrossFilledSVG />;
|
|
12
|
+
case ToastSystemEventAppearance.Warning:
|
|
13
|
+
return <AlarmFilledSVG />;
|
|
14
|
+
case ToastSystemEventAppearance.Neutral:
|
|
15
|
+
default:
|
|
16
|
+
return <InfoFilledSVG />;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import cn from 'classnames';
|
|
2
|
+
import { MouseEvent, useMemo } from 'react';
|
|
3
|
+
import { ToastContentProps as RtToastContentProps } from 'react-toastify';
|
|
4
|
+
|
|
5
|
+
import { Link } from '@snack-uikit/link';
|
|
6
|
+
import { extractSupportProps, WithSupportProps } from '@snack-uikit/utils';
|
|
7
|
+
|
|
8
|
+
import { TOAST_USER_ACTION_TEST_IDS } from '../../testIds';
|
|
9
|
+
import { ToastUserActionAppearance, Variant } from './constants';
|
|
10
|
+
import styles from './styles.module.scss';
|
|
11
|
+
import { getIcon } from './utils';
|
|
12
|
+
|
|
13
|
+
export type ToastUserActionLink = {
|
|
14
|
+
text: string;
|
|
15
|
+
href: string;
|
|
16
|
+
onClick?(e: MouseEvent<HTMLAnchorElement>): void;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type ToastUserActionProps = Partial<RtToastContentProps> &
|
|
20
|
+
WithSupportProps<{
|
|
21
|
+
label: string;
|
|
22
|
+
appearance?: ToastUserActionAppearance;
|
|
23
|
+
link?: ToastUserActionLink;
|
|
24
|
+
className?: string;
|
|
25
|
+
}>;
|
|
26
|
+
|
|
27
|
+
export function ToastUserAction({
|
|
28
|
+
appearance = ToastUserActionAppearance.Neutral,
|
|
29
|
+
label,
|
|
30
|
+
link,
|
|
31
|
+
className,
|
|
32
|
+
...rest
|
|
33
|
+
}: ToastUserActionProps) {
|
|
34
|
+
const icon = useMemo(() => getIcon(appearance), [appearance]);
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div
|
|
38
|
+
className={cn(styles.container, className)}
|
|
39
|
+
{...extractSupportProps(rest)}
|
|
40
|
+
data-appearance={appearance}
|
|
41
|
+
data-variant={icon ? Variant.WithIcon : Variant.LabelOnly}
|
|
42
|
+
>
|
|
43
|
+
<div className={styles.content}>
|
|
44
|
+
{icon && (
|
|
45
|
+
<span className={styles.icon} data-test-id={TOAST_USER_ACTION_TEST_IDS.icon}>
|
|
46
|
+
{icon}
|
|
47
|
+
</span>
|
|
48
|
+
)}
|
|
49
|
+
<span className={styles.label} data-test-id={TOAST_USER_ACTION_TEST_IDS.label}>
|
|
50
|
+
{label}
|
|
51
|
+
</span>
|
|
52
|
+
</div>
|
|
53
|
+
|
|
54
|
+
{link && (
|
|
55
|
+
<Link
|
|
56
|
+
size={Link.sizes.M}
|
|
57
|
+
text={link.text}
|
|
58
|
+
href={link.href}
|
|
59
|
+
onClick={link.onClick}
|
|
60
|
+
onColor={Link.onColors.InvertNeutral}
|
|
61
|
+
data-test-id={TOAST_USER_ACTION_TEST_IDS.link}
|
|
62
|
+
/>
|
|
63
|
+
)}
|
|
64
|
+
</div>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
ToastUserAction.appearances = ToastUserActionAppearance;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ToastUserAction';
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
@import '@snack-uikit/figma-tokens/build/scss/components/styles-tokens-toaster-toasterUserAction';
|
|
2
|
+
@import '@snack-uikit/figma-tokens/build/scss/components/styles-tokens-element';
|
|
3
|
+
@import '@snack-uikit/figma-tokens/build/scss/styles-theme-variables';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
.icon {
|
|
7
|
+
display: flex;
|
|
8
|
+
align-items: center;
|
|
9
|
+
justify-content: center;
|
|
10
|
+
|
|
11
|
+
svg {
|
|
12
|
+
width: $icon-s !important; /* stylelint-disable-line declaration-no-important */
|
|
13
|
+
height: $icon-s !important; /* stylelint-disable-line declaration-no-important */
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.label {
|
|
18
|
+
@include composite-var($sans-label-l);
|
|
19
|
+
|
|
20
|
+
color: $sys-invert-neutral-text-main;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.content {
|
|
24
|
+
@include composite-var($toaster-user-action-toast-content);
|
|
25
|
+
|
|
26
|
+
display: flex;
|
|
27
|
+
align-items: center
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
.container {
|
|
32
|
+
display: flex;
|
|
33
|
+
align-items: center;
|
|
34
|
+
justify-content: center;
|
|
35
|
+
|
|
36
|
+
box-sizing: border-box;
|
|
37
|
+
|
|
38
|
+
background-color: $sys-invert-neutral-background;
|
|
39
|
+
|
|
40
|
+
&[data-variant='label-only'] {
|
|
41
|
+
@include composite-var($toaster-user-action-toast-container-label-only);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
&[data-variant='with-icon'] {
|
|
45
|
+
@include composite-var($toaster-user-action-toast-container-with-icon);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
&[data-appearance='error'] {
|
|
49
|
+
.icon {
|
|
50
|
+
color: $sys-red-accent-default;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
&[data-appearance='warning'] {
|
|
55
|
+
.icon {
|
|
56
|
+
color: $sys-yellow-accent-default;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
&[data-appearance='success'] {
|
|
61
|
+
.icon {
|
|
62
|
+
color: $sys-green-accent-default;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AlarmFilledSVG, CheckFilledSVG, CrossFilledSVG } from '@snack-uikit/icons';
|
|
2
|
+
|
|
3
|
+
import { ToastUserActionAppearance } from './constants';
|
|
4
|
+
|
|
5
|
+
export function getIcon(appearance: ToastUserActionAppearance) {
|
|
6
|
+
switch (appearance) {
|
|
7
|
+
case ToastUserActionAppearance.Success:
|
|
8
|
+
return <CheckFilledSVG />;
|
|
9
|
+
case ToastUserActionAppearance.Error:
|
|
10
|
+
return <CrossFilledSVG />;
|
|
11
|
+
case ToastUserActionAppearance.Warning:
|
|
12
|
+
return <AlarmFilledSVG />;
|
|
13
|
+
case ToastUserActionAppearance.Neutral:
|
|
14
|
+
default:
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import 'react-toastify/dist/ReactToastify.css';
|
|
2
|
+
|
|
3
|
+
import cn from 'classnames';
|
|
4
|
+
import { useEffect, useState } from 'react';
|
|
5
|
+
import {
|
|
6
|
+
toast,
|
|
7
|
+
ToastContainer as RtToastContainer,
|
|
8
|
+
ToastContainerProps as RtToastContainerProps,
|
|
9
|
+
ToastItem,
|
|
10
|
+
ToastPosition,
|
|
11
|
+
} from 'react-toastify';
|
|
12
|
+
|
|
13
|
+
import { TOASTER_CONTAINER_PREFIX } from '../../constants';
|
|
14
|
+
import { TOAST_SYSTEM_EVENT_TEST_IDS } from '../../testIds';
|
|
15
|
+
import { ToasterType } from '../../types';
|
|
16
|
+
import styles from './styles.module.scss';
|
|
17
|
+
|
|
18
|
+
export type ToasterContainerProps = {
|
|
19
|
+
position?: ToastPosition;
|
|
20
|
+
limit?: number;
|
|
21
|
+
containerId?: RtToastContainerProps['containerId'];
|
|
22
|
+
displayCloseAllButton?: boolean;
|
|
23
|
+
type?: ToasterType;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function ToasterContainer({
|
|
27
|
+
position = toast.POSITION.BOTTOM_RIGHT,
|
|
28
|
+
limit = 5,
|
|
29
|
+
containerId,
|
|
30
|
+
displayCloseAllButton,
|
|
31
|
+
type = ToasterType.SystemEvent,
|
|
32
|
+
}: ToasterContainerProps) {
|
|
33
|
+
const [notificationCounter, setNotificationCounter] = useState(0);
|
|
34
|
+
const [isCloseAllButtonHidden, setIsCloseAllButtonHidden] = useState(false);
|
|
35
|
+
const closeAll = () => {
|
|
36
|
+
toast.dismiss();
|
|
37
|
+
setIsCloseAllButtonHidden(true);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
const unsubscribe = toast.onChange(({ status, containerId }: ToastItem) => {
|
|
42
|
+
if (containerId === `${TOASTER_CONTAINER_PREFIX}${ToasterType.SystemEvent}`) {
|
|
43
|
+
if (status === 'added') {
|
|
44
|
+
setIsCloseAllButtonHidden(false);
|
|
45
|
+
setNotificationCounter(prev => prev + 1);
|
|
46
|
+
}
|
|
47
|
+
if (status === 'removed') {
|
|
48
|
+
setNotificationCounter(prev => prev - 1);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return unsubscribe;
|
|
54
|
+
}, []);
|
|
55
|
+
|
|
56
|
+
const hasCloseAllButton = displayCloseAllButton && notificationCounter > 2 && !isCloseAllButtonHidden;
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<>
|
|
60
|
+
<RtToastContainer
|
|
61
|
+
hideProgressBar
|
|
62
|
+
closeOnClick={false}
|
|
63
|
+
autoClose={false}
|
|
64
|
+
closeButton={false}
|
|
65
|
+
draggable={false}
|
|
66
|
+
className={cn(styles[`container-${type}`], styles[position], {
|
|
67
|
+
[styles.containerWithCloseAllButton]: hasCloseAllButton,
|
|
68
|
+
})}
|
|
69
|
+
toastClassName={styles.toaster}
|
|
70
|
+
bodyClassName={styles.toaster}
|
|
71
|
+
position={position}
|
|
72
|
+
limit={limit}
|
|
73
|
+
containerId={containerId}
|
|
74
|
+
enableMultiContainer={Boolean(containerId)}
|
|
75
|
+
/>
|
|
76
|
+
|
|
77
|
+
{hasCloseAllButton && (
|
|
78
|
+
<div className={cn(styles.buttonCloseColumnWrapper, styles[position])}>
|
|
79
|
+
<button
|
|
80
|
+
className={styles.buttonCloseColumn}
|
|
81
|
+
onClick={closeAll}
|
|
82
|
+
data-test-id={TOAST_SYSTEM_EVENT_TEST_IDS.buttonCloseColumn}
|
|
83
|
+
>
|
|
84
|
+
{/* TODO add translations */}
|
|
85
|
+
Закрыть все
|
|
86
|
+
</button>
|
|
87
|
+
</div>
|
|
88
|
+
)}
|
|
89
|
+
</>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
ToasterContainer.position = toast.POSITION;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ToasterContainer';
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
@import '@snack-uikit/figma-tokens/build/scss/components/styles-tokens-toaster-toasterSystemEvent';
|
|
2
|
+
@import '@snack-uikit/figma-tokens/build/scss/components/styles-tokens-toaster-toasterUserAction';
|
|
3
|
+
@import '@snack-uikit/figma-tokens/build/scss/styles-theme-variables';
|
|
4
|
+
@import '@snack-uikit/figma-tokens/build/scss/components/styles-tokens-element';
|
|
5
|
+
|
|
6
|
+
.buttonCloseColumnWrapper {
|
|
7
|
+
@include composite-var($toaster-system-event-toaster-container);
|
|
8
|
+
|
|
9
|
+
position: fixed;
|
|
10
|
+
box-sizing: border-box;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.buttonCloseColumn {
|
|
14
|
+
@include composite-var($toaster-system-event-toaster-button-close-column);
|
|
15
|
+
@include composite-var($sans-body-m);
|
|
16
|
+
|
|
17
|
+
cursor: pointer;
|
|
18
|
+
|
|
19
|
+
box-sizing: border-box;
|
|
20
|
+
width: 100%;
|
|
21
|
+
|
|
22
|
+
color: $sys-invert-neutral-text-main;
|
|
23
|
+
|
|
24
|
+
background-color: $sys-invert-neutral-background;
|
|
25
|
+
border: none;
|
|
26
|
+
outline: none;
|
|
27
|
+
|
|
28
|
+
&:hover {
|
|
29
|
+
background-color: $sys-invert-neutral-decor-hovered;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
&:focus-visible {
|
|
33
|
+
@include outline-var($container-focused-s);
|
|
34
|
+
|
|
35
|
+
background-color: $sys-invert-neutral-decor-hovered;
|
|
36
|
+
outline-color: $sys-available-complementary;
|
|
37
|
+
outline-offset: $spacing-state-focus-offset;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
&:active {
|
|
41
|
+
color: $sys-invert-neutral-text-light;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
.container-system-event {
|
|
46
|
+
@include composite-var($toaster-system-event-toaster-container);
|
|
47
|
+
|
|
48
|
+
display: flex;
|
|
49
|
+
flex-direction: column;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.container-user-action {
|
|
53
|
+
@include composite-var($toaster-user-action-toaster-container);
|
|
54
|
+
|
|
55
|
+
display: flex;
|
|
56
|
+
flex-direction: column;
|
|
57
|
+
align-items: center;
|
|
58
|
+
width: fit-content;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.bottom-right {
|
|
62
|
+
right: 0;
|
|
63
|
+
bottom: 0;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.bottom-center {
|
|
67
|
+
bottom: 0
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
.toaster {
|
|
71
|
+
min-height: 0;
|
|
72
|
+
margin: 0;
|
|
73
|
+
padding: 0;
|
|
74
|
+
|
|
75
|
+
background-color: transparent;
|
|
76
|
+
box-shadow: none;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.containerWithCloseAllButton {
|
|
80
|
+
margin-bottom: calc($size-toaster-button-close-column + $space-toaster-system-event-gap);
|
|
81
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ToasterContainer, ToasterContainerProps } from './components';
|
|
2
|
+
import { ToasterType } from './types';
|
|
3
|
+
|
|
4
|
+
export const TOASTER_CONTAINER_DEFAULT_PROPS: Record<ToasterType, ToasterContainerProps> = {
|
|
5
|
+
[ToasterType.SystemEvent]: {
|
|
6
|
+
limit: 5,
|
|
7
|
+
position: ToasterContainer.position.BOTTOM_RIGHT,
|
|
8
|
+
displayCloseAllButton: true,
|
|
9
|
+
type: ToasterType.SystemEvent,
|
|
10
|
+
},
|
|
11
|
+
[ToasterType.UserAction]: {
|
|
12
|
+
limit: 2,
|
|
13
|
+
position: ToasterContainer.position.BOTTOM_CENTER,
|
|
14
|
+
displayCloseAllButton: false,
|
|
15
|
+
type: ToasterType.UserAction,
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const AUTO_CLOSE_TIME = {
|
|
20
|
+
[ToasterType.SystemEvent]: 5000,
|
|
21
|
+
[ToasterType.UserAction]: 2000,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const TOASTER_ROOT_ID = 'toaster-root';
|
|
25
|
+
export const TOASTER_CONTAINER_PREFIX = 'toaster-container__';
|
package/src/helpers.tsx
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// eslint-disable-next-line react/no-deprecated
|
|
2
|
+
import { render } from 'react-dom';
|
|
3
|
+
import { toast, ToastOptions as RtToastOptions } from 'react-toastify';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
ToasterContainer,
|
|
7
|
+
ToasterContainerProps,
|
|
8
|
+
ToastSystemEvent,
|
|
9
|
+
ToastSystemEventProps,
|
|
10
|
+
ToastUserAction,
|
|
11
|
+
ToastUserActionProps,
|
|
12
|
+
} from './components';
|
|
13
|
+
import {
|
|
14
|
+
AUTO_CLOSE_TIME,
|
|
15
|
+
TOASTER_CONTAINER_DEFAULT_PROPS,
|
|
16
|
+
TOASTER_CONTAINER_PREFIX,
|
|
17
|
+
TOASTER_ROOT_ID,
|
|
18
|
+
} from './constants';
|
|
19
|
+
import {
|
|
20
|
+
OpenToast,
|
|
21
|
+
Toaster,
|
|
22
|
+
ToasterId,
|
|
23
|
+
ToasterPropsMap,
|
|
24
|
+
ToasterType,
|
|
25
|
+
ToastOptions,
|
|
26
|
+
UpdateToast,
|
|
27
|
+
UserActionOptions,
|
|
28
|
+
} from './types';
|
|
29
|
+
|
|
30
|
+
function getToasterContainer({
|
|
31
|
+
type,
|
|
32
|
+
toasterParent,
|
|
33
|
+
containerProps,
|
|
34
|
+
}: {
|
|
35
|
+
type: ToasterType;
|
|
36
|
+
toasterParent: HTMLElement;
|
|
37
|
+
containerProps?: ToasterContainerProps;
|
|
38
|
+
}) {
|
|
39
|
+
const containerId = containerProps?.containerId || `${TOASTER_CONTAINER_PREFIX}${type}`;
|
|
40
|
+
|
|
41
|
+
const toasterContainerProps = {
|
|
42
|
+
...TOASTER_CONTAINER_DEFAULT_PROPS[type],
|
|
43
|
+
...(containerProps || {}),
|
|
44
|
+
containerId,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const toasterRootId = `${TOASTER_ROOT_ID}__${type}`;
|
|
48
|
+
|
|
49
|
+
let rootInDOM = toasterParent.querySelector(`#${toasterRootId}`);
|
|
50
|
+
|
|
51
|
+
if (!rootInDOM) {
|
|
52
|
+
rootInDOM = document.createElement('div');
|
|
53
|
+
rootInDOM.id = toasterRootId;
|
|
54
|
+
toasterParent.appendChild(rootInDOM);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
toasterContainer: rootInDOM,
|
|
59
|
+
toasterContainerProps,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function getToastOptions<T extends keyof ToasterPropsMap>({
|
|
64
|
+
type,
|
|
65
|
+
toastOptions,
|
|
66
|
+
containerId,
|
|
67
|
+
}: {
|
|
68
|
+
type: T;
|
|
69
|
+
toasterProps?: ToasterPropsMap[T];
|
|
70
|
+
toastOptions?: ToastOptions;
|
|
71
|
+
containerId?: ToasterContainerProps['containerId'];
|
|
72
|
+
}): RtToastOptions {
|
|
73
|
+
return {
|
|
74
|
+
toastId: toastOptions?.id,
|
|
75
|
+
autoClose: AUTO_CLOSE_TIME[type],
|
|
76
|
+
containerId: containerId || `${TOASTER_CONTAINER_PREFIX}${type}`,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function getToastComponent<T extends keyof ToasterPropsMap>({
|
|
81
|
+
type,
|
|
82
|
+
toasterProps,
|
|
83
|
+
}: {
|
|
84
|
+
type: T;
|
|
85
|
+
toasterProps: ToasterPropsMap[T];
|
|
86
|
+
}) {
|
|
87
|
+
switch (type) {
|
|
88
|
+
case ToasterType.UserAction:
|
|
89
|
+
return <ToastUserAction {...(toasterProps as ToastUserActionProps)} />;
|
|
90
|
+
case ToasterType.SystemEvent:
|
|
91
|
+
return <ToastSystemEvent {...(toasterProps as ToastSystemEventProps)} />;
|
|
92
|
+
default:
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export const openToast: OpenToast = ({
|
|
98
|
+
type,
|
|
99
|
+
toasterProps,
|
|
100
|
+
containerProps,
|
|
101
|
+
toastOptions,
|
|
102
|
+
toasterParent = document.body,
|
|
103
|
+
}) => {
|
|
104
|
+
const { toasterContainer, toasterContainerProps } = getToasterContainer({
|
|
105
|
+
type,
|
|
106
|
+
toasterParent,
|
|
107
|
+
containerProps,
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
const options = getToastOptions({
|
|
111
|
+
type,
|
|
112
|
+
toasterProps,
|
|
113
|
+
toastOptions,
|
|
114
|
+
containerId: toasterContainerProps.containerId,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const toasterComponent = getToastComponent({
|
|
118
|
+
type,
|
|
119
|
+
toasterProps,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
return new Promise(resolve => {
|
|
123
|
+
render(<ToasterContainer {...toasterContainerProps} />, toasterContainer, () => {
|
|
124
|
+
setTimeout(() => {
|
|
125
|
+
resolve(toast(toasterComponent, options));
|
|
126
|
+
}, 0);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export const updateToast: UpdateToast = (id, { type, toasterProps, toastOptions, containerId }) => {
|
|
132
|
+
const options = getToastOptions({ type, toasterProps, toastOptions, containerId });
|
|
133
|
+
|
|
134
|
+
const toastComponent = getToastComponent({
|
|
135
|
+
type,
|
|
136
|
+
toasterProps,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
return toast.update(id, {
|
|
140
|
+
...options,
|
|
141
|
+
render: toastComponent,
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export const dismissToast = toast.dismiss;
|
|
146
|
+
export const isToastActive = toast.isActive;
|
|
147
|
+
|
|
148
|
+
const userAction = {
|
|
149
|
+
success(options: UserActionOptions) {
|
|
150
|
+
return openToast({
|
|
151
|
+
type: ToasterType.UserAction,
|
|
152
|
+
toasterProps: { ...options, appearance: ToastUserAction.appearances.Success },
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
|
|
156
|
+
neutral(options: UserActionOptions) {
|
|
157
|
+
return openToast({
|
|
158
|
+
type: ToasterType.UserAction,
|
|
159
|
+
toasterProps: { ...options, appearance: ToastUserAction.appearances.Neutral },
|
|
160
|
+
});
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
error(options: UserActionOptions) {
|
|
164
|
+
return openToast({
|
|
165
|
+
type: ToasterType.UserAction,
|
|
166
|
+
toasterProps: { ...options, appearance: ToastUserAction.appearances.Error },
|
|
167
|
+
});
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
warning(options: UserActionOptions) {
|
|
171
|
+
return openToast({
|
|
172
|
+
type: ToasterType.UserAction,
|
|
173
|
+
toasterProps: { ...options, appearance: ToastUserAction.appearances.Warning },
|
|
174
|
+
});
|
|
175
|
+
},
|
|
176
|
+
|
|
177
|
+
update: {
|
|
178
|
+
success(id: ToasterId, options: UserActionOptions) {
|
|
179
|
+
updateToast(id, {
|
|
180
|
+
type: ToasterType.UserAction,
|
|
181
|
+
toasterProps: { ...options, appearance: ToastUserAction.appearances.Success },
|
|
182
|
+
});
|
|
183
|
+
},
|
|
184
|
+
|
|
185
|
+
neutral(id: ToasterId, options: UserActionOptions) {
|
|
186
|
+
updateToast(id, {
|
|
187
|
+
type: ToasterType.UserAction,
|
|
188
|
+
toasterProps: { ...options, appearance: ToastUserAction.appearances.Neutral },
|
|
189
|
+
});
|
|
190
|
+
},
|
|
191
|
+
|
|
192
|
+
warning(id: ToasterId, options: UserActionOptions) {
|
|
193
|
+
updateToast(id, {
|
|
194
|
+
type: ToasterType.UserAction,
|
|
195
|
+
toasterProps: { ...options, appearance: ToastUserAction.appearances.Warning },
|
|
196
|
+
});
|
|
197
|
+
},
|
|
198
|
+
|
|
199
|
+
error(id: ToasterId, options: UserActionOptions) {
|
|
200
|
+
updateToast(id, {
|
|
201
|
+
type: ToasterType.UserAction,
|
|
202
|
+
toasterProps: { ...options, appearance: ToastUserAction.appearances.Error },
|
|
203
|
+
});
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
dismiss(id?: ToasterId) {
|
|
208
|
+
return toast.dismiss(id);
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const systemEvent = {
|
|
213
|
+
success(options: ToastSystemEventProps) {
|
|
214
|
+
return openToast({
|
|
215
|
+
type: ToasterType.SystemEvent,
|
|
216
|
+
toasterProps: { ...options, appearance: ToastSystemEvent.appearances.Success },
|
|
217
|
+
});
|
|
218
|
+
},
|
|
219
|
+
|
|
220
|
+
neutral(options: ToastSystemEventProps) {
|
|
221
|
+
return openToast({
|
|
222
|
+
type: ToasterType.SystemEvent,
|
|
223
|
+
toasterProps: { ...options, appearance: ToastSystemEvent.appearances.Neutral },
|
|
224
|
+
});
|
|
225
|
+
},
|
|
226
|
+
|
|
227
|
+
warning(options: ToastSystemEventProps) {
|
|
228
|
+
return openToast({
|
|
229
|
+
type: ToasterType.SystemEvent,
|
|
230
|
+
toasterProps: { ...options, appearance: ToastSystemEvent.appearances.Warning },
|
|
231
|
+
});
|
|
232
|
+
},
|
|
233
|
+
|
|
234
|
+
error(options: ToastSystemEventProps) {
|
|
235
|
+
return openToast({
|
|
236
|
+
type: ToasterType.SystemEvent,
|
|
237
|
+
toasterProps: { ...options, appearance: ToastSystemEvent.appearances.Error },
|
|
238
|
+
});
|
|
239
|
+
},
|
|
240
|
+
|
|
241
|
+
errorCritical(options: ToastSystemEventProps) {
|
|
242
|
+
return openToast({
|
|
243
|
+
type: ToasterType.SystemEvent,
|
|
244
|
+
toasterProps: { ...options, appearance: ToastSystemEvent.appearances.ErrorCritical },
|
|
245
|
+
});
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
update: {
|
|
249
|
+
success(id: ToasterId, options: ToastSystemEventProps) {
|
|
250
|
+
return updateToast(id, {
|
|
251
|
+
type: ToasterType.SystemEvent,
|
|
252
|
+
toasterProps: { ...options, appearance: ToastSystemEvent.appearances.Success },
|
|
253
|
+
});
|
|
254
|
+
},
|
|
255
|
+
|
|
256
|
+
neutral(id: ToasterId, options: ToastSystemEventProps) {
|
|
257
|
+
return updateToast(id, {
|
|
258
|
+
type: ToasterType.SystemEvent,
|
|
259
|
+
toasterProps: { ...options, appearance: ToastSystemEvent.appearances.Neutral },
|
|
260
|
+
});
|
|
261
|
+
},
|
|
262
|
+
|
|
263
|
+
warning(id: ToasterId, options: ToastSystemEventProps) {
|
|
264
|
+
return updateToast(id, {
|
|
265
|
+
type: ToasterType.SystemEvent,
|
|
266
|
+
toasterProps: { ...options, appearance: ToastSystemEvent.appearances.Warning },
|
|
267
|
+
});
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
error(id: ToasterId, options: ToastSystemEventProps) {
|
|
271
|
+
return updateToast(id, {
|
|
272
|
+
type: ToasterType.SystemEvent,
|
|
273
|
+
toasterProps: { ...options, appearance: ToastSystemEvent.appearances.Error },
|
|
274
|
+
});
|
|
275
|
+
},
|
|
276
|
+
|
|
277
|
+
errorCritical(id: ToasterId, options: ToastSystemEventProps) {
|
|
278
|
+
return updateToast(id, {
|
|
279
|
+
type: ToasterType.SystemEvent,
|
|
280
|
+
toasterProps: { ...options, appearance: ToastSystemEvent.appearances.ErrorCritical },
|
|
281
|
+
});
|
|
282
|
+
},
|
|
283
|
+
},
|
|
284
|
+
|
|
285
|
+
dismiss(id?: ToasterId) {
|
|
286
|
+
return toast.dismiss(id);
|
|
287
|
+
},
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
export const toaster: Toaster = {
|
|
291
|
+
userAction,
|
|
292
|
+
systemEvent,
|
|
293
|
+
};
|
package/src/index.ts
ADDED