@luscii-healthtech/web-ui 2.2.0 → 2.4.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/components/Icons/WarningIcon.d.ts +3 -0
- package/dist/components/Icons/types/IconProps.type.d.ts +1 -0
- package/dist/components/List/List.d.ts +1 -1
- package/dist/components/List/List.types.d.ts +1 -0
- package/dist/components/List/ListItemSkeleton.d.ts +2 -0
- package/dist/components/List/ListSkeleton.d.ts +7 -0
- package/dist/components/Toaster/Toaster.d.ts +17 -0
- package/dist/components/Toaster/toast-elements-getter.d.ts +22 -0
- package/dist/components/Toaster/toast-progress-animator.d.ts +12 -0
- package/dist/components/Toaster/toast.d.ts +12 -0
- package/dist/index.d.ts +2 -1
- package/dist/web-ui-tailwind.css +45 -0
- package/dist/web-ui.cjs.development.js +411 -147
- package/dist/web-ui.cjs.development.js.map +1 -1
- package/dist/web-ui.cjs.production.min.js +1 -1
- package/dist/web-ui.cjs.production.min.js.map +1 -1
- package/dist/web-ui.esm.js +409 -146
- package/dist/web-ui.esm.js.map +1 -1
- package/package.json +5 -8
- package/src/components/Icons/CheckIcon.tsx +1 -0
- package/src/components/Icons/CrossIcon.tsx +1 -0
- package/src/components/Icons/WarningIcon.tsx +24 -0
- package/src/components/Icons/types/IconProps.type.ts +1 -0
- package/src/components/List/List.tsx +13 -9
- package/src/components/List/List.types.ts +1 -0
- package/src/components/List/ListItemSkeleton.tsx +26 -0
- package/src/components/List/ListSkeleton.scss +5 -0
- package/src/components/List/ListSkeleton.tsx +30 -0
- package/src/components/Toaster/Toaster.scss +53 -0
- package/src/components/Toaster/Toaster.tsx +100 -0
- package/src/components/Toaster/toast-elements-getter.ts +72 -0
- package/src/components/Toaster/toast-progress-animator.ts +53 -0
- package/src/components/Toaster/toast.ts +112 -0
- package/src/index.tsx +6 -4
- package/src/styles/_skeleton.scss +63 -0
- package/dist/components/Acknowledgement/Acknowledgement.d.ts +0 -22
- package/src/components/Acknowledgement/Acknowledgement.js +0 -61
- package/src/components/Acknowledgement/Acknowledgement.scss +0 -49
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.
|
|
2
|
+
"version": "2.4.0",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"typings": "dist/index.d.ts",
|
|
@@ -22,17 +22,13 @@
|
|
|
22
22
|
"build-storybook": "build-storybook",
|
|
23
23
|
"chromatic": "chromatic --exit-zero-on-changes",
|
|
24
24
|
"build-tailwind": "NODE_ENV=production yarn run tailwindcss build tailwind.css -o ./dist/web-ui-tailwind.css",
|
|
25
|
-
"test-copy-build": "yarn build && cp -a ./dist/. ../cVitals-Web/node_modules/@luscii-healthtech/web-ui/dist"
|
|
25
|
+
"test-copy-build": "yarn build && cp -a ./dist/. ../cVitals-Web/node_modules/@luscii-healthtech/web-ui/dist",
|
|
26
|
+
"prepare": "husky install"
|
|
26
27
|
},
|
|
27
28
|
"peerDependencies": {
|
|
28
29
|
"react": ">=16",
|
|
29
30
|
"react-tooltip": "^4.2.21"
|
|
30
31
|
},
|
|
31
|
-
"husky": {
|
|
32
|
-
"hooks": {
|
|
33
|
-
"pre-commit": "tsdx lint"
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
32
|
"prettier": {
|
|
37
33
|
"printWidth": 80,
|
|
38
34
|
"semi": true,
|
|
@@ -78,11 +74,12 @@
|
|
|
78
74
|
"css-loader": "^0.28.11",
|
|
79
75
|
"eslint": "^8.18.0",
|
|
80
76
|
"eslint-plugin-chai-friendly": "^0.7.2",
|
|
81
|
-
"husky": "^8.0.
|
|
77
|
+
"husky": "^8.0.0",
|
|
82
78
|
"postcss": "^8.4.14",
|
|
83
79
|
"postcss-loader": "4.2",
|
|
84
80
|
"postcss-url": "^10.1.3",
|
|
85
81
|
"prettier": "^2.7.1",
|
|
82
|
+
"pretty-quick": "^3.1.3",
|
|
86
83
|
"react": "^18.2.0",
|
|
87
84
|
"react-dom": "^18.2.0",
|
|
88
85
|
"react-is": "^18.2.0",
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
import { IconProps } from "./types/IconProps.type";
|
|
4
|
+
|
|
5
|
+
export const WarningIcon: React.VoidFunctionComponent<IconProps> = (props) => {
|
|
6
|
+
return (
|
|
7
|
+
<svg
|
|
8
|
+
className={props.className}
|
|
9
|
+
onClick={props.onClick}
|
|
10
|
+
role={props.onClick ? "button" : undefined}
|
|
11
|
+
data-test-id={props["data-test-id"]}
|
|
12
|
+
width="20"
|
|
13
|
+
height="20"
|
|
14
|
+
viewBox="0 0 20 20"
|
|
15
|
+
fill="none"
|
|
16
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
17
|
+
>
|
|
18
|
+
<path
|
|
19
|
+
d="M10 0C4.48 0 0 4.48 0 10C0 15.52 4.48 20 10 20C15.52 20 20 15.52 20 10C20 4.48 15.52 0 10 0ZM10 11C9.45 11 9 10.55 9 10V6C9 5.45 9.45 5 10 5C10.55 5 11 5.45 11 6V10C11 10.55 10.55 11 10 11ZM11 15H9V13H11V15Z"
|
|
20
|
+
fill="currentColor"
|
|
21
|
+
/>
|
|
22
|
+
</svg>
|
|
23
|
+
);
|
|
24
|
+
};
|
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
OnAssetLoadErrorPayload,
|
|
13
13
|
Dragula,
|
|
14
14
|
} from "./List.types";
|
|
15
|
+
import { ListSkeleton } from "./ListSkeleton";
|
|
15
16
|
|
|
16
17
|
export { ListProps, ListItemProps, OnAssetLoadErrorPayload };
|
|
17
18
|
|
|
@@ -23,6 +24,7 @@ export const List = ({
|
|
|
23
24
|
onAssetLoadError,
|
|
24
25
|
onDragEnd,
|
|
25
26
|
emptyStateMessage,
|
|
27
|
+
isLoading,
|
|
26
28
|
}: ListProps): JSX.Element => {
|
|
27
29
|
const listRef = useRef<HTMLUListElement | null>(null);
|
|
28
30
|
const dragulaRef = useRef<Dragula | null>(null);
|
|
@@ -43,17 +45,15 @@ export const List = ({
|
|
|
43
45
|
const draggedItemId = element.dataset["id"];
|
|
44
46
|
|
|
45
47
|
if (listRef.current && draggedItemId) {
|
|
46
|
-
const itemIdsWithOldOrder = items.map(item => item.itemId.toString());
|
|
48
|
+
const itemIdsWithOldOrder = items.map((item) => item.itemId.toString());
|
|
47
49
|
const itemIdsWithNewOrder = Array.from(listRef.current.children)
|
|
48
|
-
.map(child => (child as HTMLElement).dataset["id"])
|
|
50
|
+
.map((child) => (child as HTMLElement).dataset["id"])
|
|
49
51
|
.filter((itemId): itemId is string => !!itemId);
|
|
50
52
|
|
|
51
|
-
const oldIndexOfDraggedItemId =
|
|
52
|
-
draggedItemId
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
draggedItemId
|
|
56
|
-
);
|
|
53
|
+
const oldIndexOfDraggedItemId =
|
|
54
|
+
itemIdsWithOldOrder.indexOf(draggedItemId);
|
|
55
|
+
const newIndexOfDraggedItemId =
|
|
56
|
+
itemIdsWithNewOrder.indexOf(draggedItemId);
|
|
57
57
|
|
|
58
58
|
if (oldIndexOfDraggedItemId !== newIndexOfDraggedItemId) {
|
|
59
59
|
onDragEnd?.(draggedItemId, newIndexOfDraggedItemId);
|
|
@@ -72,6 +72,10 @@ export const List = ({
|
|
|
72
72
|
|
|
73
73
|
const roundTop = !hasHeader || (hasHeader && headerTransparent);
|
|
74
74
|
|
|
75
|
+
if (isLoading) {
|
|
76
|
+
return <ListSkeleton items={items.length} />;
|
|
77
|
+
}
|
|
78
|
+
|
|
75
79
|
return (
|
|
76
80
|
<div data-test-id="list-component">
|
|
77
81
|
{(title || headerButton) && (
|
|
@@ -103,7 +107,7 @@ export const List = ({
|
|
|
103
107
|
)}
|
|
104
108
|
|
|
105
109
|
<ul ref={listRef}>
|
|
106
|
-
{items.map(item => (
|
|
110
|
+
{items.map((item) => (
|
|
107
111
|
<ListItem
|
|
108
112
|
{...item}
|
|
109
113
|
roundTop={roundTop}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
export const ListItemSkeleton = (): JSX.Element => {
|
|
4
|
+
return (
|
|
5
|
+
<div className="flex flex-row items-center p-4">
|
|
6
|
+
<div
|
|
7
|
+
className="skeleton-box is-circle mr-2"
|
|
8
|
+
style={{ width: `${32}px`, height: `${32}px` }}
|
|
9
|
+
/>
|
|
10
|
+
<div className="flex flex-col">
|
|
11
|
+
<div
|
|
12
|
+
className="skeleton-box mb-1"
|
|
13
|
+
style={{ width: `${160}px`, height: `${14}px` }}
|
|
14
|
+
/>
|
|
15
|
+
<div
|
|
16
|
+
className="skeleton-box"
|
|
17
|
+
style={{ width: `${110}px`, height: `${14}px` }}
|
|
18
|
+
/>
|
|
19
|
+
</div>
|
|
20
|
+
<div
|
|
21
|
+
className="skeleton-box is-button ml-auto rounded-full"
|
|
22
|
+
style={{ width: `${24}px`, height: `${24}px` }}
|
|
23
|
+
/>
|
|
24
|
+
</div>
|
|
25
|
+
);
|
|
26
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
import "./ListSkeleton.scss";
|
|
4
|
+
import { ListItemSkeleton } from "./ListItemSkeleton";
|
|
5
|
+
|
|
6
|
+
type ListSkeletonProps = {
|
|
7
|
+
items: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const ListSkeleton = ({ items }: ListSkeletonProps): JSX.Element => {
|
|
11
|
+
const skeletonItems = Array.from({ length: items || 5 }, (_, i) => {
|
|
12
|
+
return <ListItemSkeleton key={i} />;
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
return (
|
|
16
|
+
<div className="flex flex-col divide-y divide-slate-200 bg-white border-slate-50 border rounded-lg shadow list-skeleton">
|
|
17
|
+
<div className="flex flex-row items-center px-4 py-2">
|
|
18
|
+
<div
|
|
19
|
+
className="skeleton-box"
|
|
20
|
+
style={{ width: `${160}px`, height: `${14}px` }}
|
|
21
|
+
/>
|
|
22
|
+
<div
|
|
23
|
+
className="skeleton-box is-button ml-auto"
|
|
24
|
+
style={{ width: `${110}px`, height: `${44}px` }}
|
|
25
|
+
/>
|
|
26
|
+
</div>
|
|
27
|
+
{skeletonItems}
|
|
28
|
+
</div>
|
|
29
|
+
);
|
|
30
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#application-toaster {
|
|
2
|
+
position: fixed;
|
|
3
|
+
left: 50%;
|
|
4
|
+
transform: translate(-50%, 150%);
|
|
5
|
+
bottom: 15px;
|
|
6
|
+
|
|
7
|
+
// https://easings.net/#easeInOutBack
|
|
8
|
+
transition: transform 0.3s cubic-bezier(0.68, -0.6, 0.32, 1.6);
|
|
9
|
+
|
|
10
|
+
&.shelved {
|
|
11
|
+
transform: translate(-50%, 150%);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
&.expanded {
|
|
15
|
+
transform: translate(-50%, 0);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
&.type-success {
|
|
19
|
+
[data-test-id="toaster-title"] {
|
|
20
|
+
@apply text-green-700;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
.failure-icon {
|
|
24
|
+
@apply hidden;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
[data-test-id="toaster-progress-bar-container"] {
|
|
28
|
+
@apply bg-green-50;
|
|
29
|
+
|
|
30
|
+
[data-test-id="toaster-progress-bar"] {
|
|
31
|
+
@apply bg-green-400;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
&.type-failure {
|
|
37
|
+
[data-test-id="toaster-title"] {
|
|
38
|
+
@apply text-red-700;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.success-icon {
|
|
42
|
+
@apply hidden;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
[data-test-id="toaster-progress-bar-container"] {
|
|
46
|
+
@apply bg-red-50;
|
|
47
|
+
|
|
48
|
+
[data-test-id="toaster-progress-bar"] {
|
|
49
|
+
@apply bg-red-400;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import classNames from "classnames";
|
|
3
|
+
|
|
4
|
+
import { Text } from "../Text/Text";
|
|
5
|
+
import { CheckIcon } from "../Icons/CheckIcon";
|
|
6
|
+
import { CrossIcon } from "../Icons/CrossIcon";
|
|
7
|
+
import { WarningIcon } from "../Icons/WarningIcon";
|
|
8
|
+
|
|
9
|
+
import "./Toaster.scss";
|
|
10
|
+
|
|
11
|
+
export interface ToasterProps {
|
|
12
|
+
type: "success" | "failure";
|
|
13
|
+
message: string;
|
|
14
|
+
title: string;
|
|
15
|
+
isVisible?: boolean;
|
|
16
|
+
/** if you need to reposition the toaster for whatever reason you can use these props */
|
|
17
|
+
styleOverwrite?: Pick<
|
|
18
|
+
React.CSSProperties,
|
|
19
|
+
"top" | "bottom" | "left" | "right"
|
|
20
|
+
>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const TOASTER_TYPE_OPTIONS = {
|
|
24
|
+
SUCCESS: "success",
|
|
25
|
+
FAILURE: "failure",
|
|
26
|
+
} as const;
|
|
27
|
+
export type ToasterType =
|
|
28
|
+
typeof TOASTER_TYPE_OPTIONS[keyof typeof TOASTER_TYPE_OPTIONS];
|
|
29
|
+
|
|
30
|
+
const Toaster: React.VoidFunctionComponent<ToasterProps> = ({
|
|
31
|
+
message = "",
|
|
32
|
+
title = "",
|
|
33
|
+
type = TOASTER_TYPE_OPTIONS.SUCCESS,
|
|
34
|
+
isVisible,
|
|
35
|
+
styleOverwrite,
|
|
36
|
+
}) => {
|
|
37
|
+
const isSuccess = type === TOASTER_TYPE_OPTIONS.SUCCESS;
|
|
38
|
+
const isFailure = type === TOASTER_TYPE_OPTIONS.FAILURE;
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<div
|
|
42
|
+
style={styleOverwrite}
|
|
43
|
+
id="application-toaster"
|
|
44
|
+
data-test-id={`toaster-panel-${type}`}
|
|
45
|
+
className={classNames(
|
|
46
|
+
"bg-white cursor-pointer",
|
|
47
|
+
"rounded-md shadow-md",
|
|
48
|
+
"min-h-13 max-h-19 w-104 transition-transform",
|
|
49
|
+
|
|
50
|
+
{
|
|
51
|
+
shelved: !isVisible,
|
|
52
|
+
expanded: isVisible,
|
|
53
|
+
"type-success": isSuccess,
|
|
54
|
+
"type-failure": isFailure,
|
|
55
|
+
}
|
|
56
|
+
)}
|
|
57
|
+
>
|
|
58
|
+
<div className="relative p-4">
|
|
59
|
+
<div className="flex flex-row">
|
|
60
|
+
<div
|
|
61
|
+
data-test-id="success-toaster-icon"
|
|
62
|
+
className={classNames("success-icon text-green-700", {
|
|
63
|
+
hidden: isFailure,
|
|
64
|
+
})}
|
|
65
|
+
>
|
|
66
|
+
<CheckIcon />
|
|
67
|
+
</div>
|
|
68
|
+
<div
|
|
69
|
+
data-test-id="failure-toaster-icon"
|
|
70
|
+
className={classNames("failure-icon text-red-700", {
|
|
71
|
+
hidden: isSuccess,
|
|
72
|
+
})}
|
|
73
|
+
>
|
|
74
|
+
<WarningIcon />
|
|
75
|
+
</div>
|
|
76
|
+
<div className="ml-3">
|
|
77
|
+
<Text type="strong" data-test-id="toaster-title" text={title} />
|
|
78
|
+
<Text data-test-id="toaster-message" text={message} />
|
|
79
|
+
</div>
|
|
80
|
+
<CrossIcon
|
|
81
|
+
data-test-id="toaster-close-button"
|
|
82
|
+
className="ml-auto cursor-pointer text-slate-500"
|
|
83
|
+
/>
|
|
84
|
+
</div>
|
|
85
|
+
</div>
|
|
86
|
+
|
|
87
|
+
<div
|
|
88
|
+
data-test-id="toaster-progress-bar-container"
|
|
89
|
+
className={classNames("h-1 absolute bottom-0 w-full rounded-b-md")}
|
|
90
|
+
>
|
|
91
|
+
<div
|
|
92
|
+
data-test-id="toaster-progress-bar"
|
|
93
|
+
className={classNames("h-full")}
|
|
94
|
+
/>
|
|
95
|
+
</div>
|
|
96
|
+
</div>
|
|
97
|
+
);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export default Toaster;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
interface GetToasterElementsParams {
|
|
2
|
+
timeoutId: NodeJS.Timeout;
|
|
3
|
+
progressBarAnimationFrameHandler: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
interface GetToasterElementsReturn {
|
|
7
|
+
toasterElementMessage: HTMLElement;
|
|
8
|
+
toasterProgressBar: HTMLElement;
|
|
9
|
+
toasterElementTitle: HTMLElement;
|
|
10
|
+
toasterElementSuccessIcon: HTMLElement;
|
|
11
|
+
toasterElementFailureIcon: HTMLElement;
|
|
12
|
+
toasterElementContainer: HTMLElement;
|
|
13
|
+
toasterCloseButton: HTMLElement;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @param params.timeoutId - the timeoutId (that makes the toaster vanish), in case of a shortcut
|
|
19
|
+
* @param params.progressBarAnimationFrameHandler - animation frame id to be canceled in case of a shortcut
|
|
20
|
+
* @returns a collection of html elements to be manipulated by the animation
|
|
21
|
+
*/
|
|
22
|
+
export const getToasterElements = (
|
|
23
|
+
params: GetToasterElementsParams
|
|
24
|
+
): GetToasterElementsReturn => {
|
|
25
|
+
const { timeoutId, progressBarAnimationFrameHandler } = params;
|
|
26
|
+
const toasterElementContainer = document.querySelector<HTMLElement>(
|
|
27
|
+
"#application-toaster"
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
if (!toasterElementContainer) {
|
|
31
|
+
clearTimeout(timeoutId);
|
|
32
|
+
clearTimeout(progressBarAnimationFrameHandler);
|
|
33
|
+
|
|
34
|
+
throw new Error(
|
|
35
|
+
"Make sure that the Toaster component element is rendered with an id [application-toaster]"
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const toasterElementMessage = toasterElementContainer?.querySelector(
|
|
40
|
+
'[data-test-id="toaster-message"]'
|
|
41
|
+
) as HTMLElement;
|
|
42
|
+
|
|
43
|
+
const toasterElementTitle = toasterElementContainer?.querySelector(
|
|
44
|
+
'[data-test-id="toaster-title"]'
|
|
45
|
+
) as HTMLElement;
|
|
46
|
+
|
|
47
|
+
const toasterElementSuccessIcon = toasterElementContainer?.querySelector(
|
|
48
|
+
'[data-test-id="success-toaster-icon"]'
|
|
49
|
+
) as HTMLElement;
|
|
50
|
+
|
|
51
|
+
const toasterElementFailureIcon = toasterElementContainer?.querySelector(
|
|
52
|
+
'[data-test-id="failure-toaster-icon"]'
|
|
53
|
+
) as HTMLElement;
|
|
54
|
+
|
|
55
|
+
const toasterCloseButton = toasterElementContainer?.querySelector(
|
|
56
|
+
'[data-test-id="toaster-close-button"]'
|
|
57
|
+
) as HTMLElement;
|
|
58
|
+
|
|
59
|
+
const toasterProgressBar = toasterElementContainer?.querySelector(
|
|
60
|
+
'[data-test-id="toaster-progress-bar"]'
|
|
61
|
+
) as HTMLElement;
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
toasterElementContainer,
|
|
65
|
+
toasterElementMessage,
|
|
66
|
+
toasterElementTitle,
|
|
67
|
+
toasterProgressBar,
|
|
68
|
+
toasterElementSuccessIcon,
|
|
69
|
+
toasterElementFailureIcon,
|
|
70
|
+
toasterCloseButton,
|
|
71
|
+
};
|
|
72
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
interface AnimateProgressParams {
|
|
2
|
+
animationDuration: number;
|
|
3
|
+
progressBarElement: HTMLElement;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Reference: https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame
|
|
8
|
+
* @param params.animationDuration - the amount of seconds the duration will take
|
|
9
|
+
* @param params.progressBarElement - the html element for the progress bar
|
|
10
|
+
* @returns the animation frame id
|
|
11
|
+
*/
|
|
12
|
+
export const animateProgress = (params: AnimateProgressParams): number => {
|
|
13
|
+
const { animationDuration, progressBarElement } = params;
|
|
14
|
+
|
|
15
|
+
let progressBarAnimationFrameHandler: number;
|
|
16
|
+
|
|
17
|
+
let startingTimestamp = 0;
|
|
18
|
+
let previousTimestamp = 0;
|
|
19
|
+
let done = false;
|
|
20
|
+
const animationTick = 100 / animationDuration;
|
|
21
|
+
|
|
22
|
+
const animate = (animationTimestamp: number) => {
|
|
23
|
+
if (!startingTimestamp) {
|
|
24
|
+
startingTimestamp = animationTimestamp;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const elapsed = animationTimestamp - startingTimestamp;
|
|
28
|
+
|
|
29
|
+
if (previousTimestamp !== animationTimestamp) {
|
|
30
|
+
const count = 100 - animationTick * elapsed;
|
|
31
|
+
progressBarElement.style.width = `${count}%`;
|
|
32
|
+
if (count === 0) {
|
|
33
|
+
done = true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (elapsed < animationDuration) {
|
|
38
|
+
// Stop the animation after duration is finished
|
|
39
|
+
previousTimestamp = animationTimestamp;
|
|
40
|
+
|
|
41
|
+
if (!done) {
|
|
42
|
+
progressBarAnimationFrameHandler =
|
|
43
|
+
window.requestAnimationFrame(animate);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
progressBarElement.style.width = "100%";
|
|
49
|
+
|
|
50
|
+
progressBarAnimationFrameHandler = requestAnimationFrame(animate);
|
|
51
|
+
|
|
52
|
+
return progressBarAnimationFrameHandler;
|
|
53
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { getToasterElements } from "./toast-elements-getter";
|
|
2
|
+
import { animateProgress } from "./toast-progress-animator";
|
|
3
|
+
|
|
4
|
+
let timeoutId: NodeJS.Timeout;
|
|
5
|
+
let progressBarAnimationFrameHandler: number;
|
|
6
|
+
|
|
7
|
+
const defaultDurationInSeconds = 5;
|
|
8
|
+
|
|
9
|
+
type ToastParams =
|
|
10
|
+
| {
|
|
11
|
+
message: string;
|
|
12
|
+
title?: string;
|
|
13
|
+
showIcon?: boolean;
|
|
14
|
+
type: "success" | "failure";
|
|
15
|
+
}
|
|
16
|
+
| string;
|
|
17
|
+
|
|
18
|
+
const showToaster = (params: ToastParams) => {
|
|
19
|
+
clearTimeout(timeoutId);
|
|
20
|
+
clearTimeout(progressBarAnimationFrameHandler);
|
|
21
|
+
|
|
22
|
+
const {
|
|
23
|
+
toasterElementContainer,
|
|
24
|
+
toasterElementMessage,
|
|
25
|
+
toasterElementTitle,
|
|
26
|
+
toasterProgressBar,
|
|
27
|
+
toasterElementSuccessIcon,
|
|
28
|
+
toasterElementFailureIcon,
|
|
29
|
+
toasterCloseButton,
|
|
30
|
+
} = getToasterElements({
|
|
31
|
+
timeoutId,
|
|
32
|
+
progressBarAnimationFrameHandler,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const animationDuration = defaultDurationInSeconds * 1000;
|
|
36
|
+
|
|
37
|
+
progressBarAnimationFrameHandler = animateProgress({
|
|
38
|
+
animationDuration,
|
|
39
|
+
progressBarElement: toasterProgressBar,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
timeoutId = setTimeout(() => {
|
|
43
|
+
toasterElementContainer.classList.remove("expanded");
|
|
44
|
+
clearTimeout(timeoutId);
|
|
45
|
+
cancelAnimationFrame(progressBarAnimationFrameHandler);
|
|
46
|
+
}, animationDuration);
|
|
47
|
+
|
|
48
|
+
function shortcutAndClose() {
|
|
49
|
+
toasterElementContainer.classList.remove("expanded");
|
|
50
|
+
toasterElementContainer.classList.add("shelved");
|
|
51
|
+
|
|
52
|
+
clearTimeout(timeoutId);
|
|
53
|
+
cancelAnimationFrame(progressBarAnimationFrameHandler);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
toasterCloseButton.removeEventListener("click", shortcutAndClose, true);
|
|
57
|
+
toasterCloseButton.addEventListener("click", shortcutAndClose);
|
|
58
|
+
|
|
59
|
+
toasterElementContainer.removeEventListener("click", shortcutAndClose, true);
|
|
60
|
+
toasterElementContainer.addEventListener("click", shortcutAndClose);
|
|
61
|
+
|
|
62
|
+
// Basic usage, no need to manipulate any other element;
|
|
63
|
+
if (typeof params === "string") {
|
|
64
|
+
toasterElementMessage.textContent = params;
|
|
65
|
+
toasterElementTitle.classList.add("hidden");
|
|
66
|
+
|
|
67
|
+
toasterElementSuccessIcon.classList.remove("hidden");
|
|
68
|
+
toasterElementFailureIcon.classList.add("hidden");
|
|
69
|
+
|
|
70
|
+
toasterElementContainer.classList.remove("type-failure");
|
|
71
|
+
toasterElementContainer.classList.add("type-success");
|
|
72
|
+
toasterElementContainer.classList.add("expanded");
|
|
73
|
+
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const { title = "", showIcon = true, message, type } = params;
|
|
78
|
+
|
|
79
|
+
toasterElementTitle.classList.toggle("hidden", !title);
|
|
80
|
+
toasterElementTitle.textContent = title;
|
|
81
|
+
|
|
82
|
+
toasterElementSuccessIcon.classList.toggle("hidden", !showIcon);
|
|
83
|
+
toasterElementFailureIcon.classList.toggle("hidden", !showIcon);
|
|
84
|
+
|
|
85
|
+
toasterElementMessage.textContent = message;
|
|
86
|
+
toasterElementContainer.classList.remove("type-success");
|
|
87
|
+
toasterElementContainer.classList.remove("type-failure");
|
|
88
|
+
toasterElementContainer.classList.add("expanded");
|
|
89
|
+
toasterElementContainer.classList.add(`type-${type}`);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export const toast = (params: ToastParams): void => {
|
|
93
|
+
showToaster(params);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
toast.error = (message: string): void => {
|
|
97
|
+
showToaster({
|
|
98
|
+
message: message,
|
|
99
|
+
type: "failure",
|
|
100
|
+
showIcon: true,
|
|
101
|
+
title: "",
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
toast.info = (message: string): void => {
|
|
106
|
+
showToaster({
|
|
107
|
+
message: message,
|
|
108
|
+
type: "success",
|
|
109
|
+
showIcon: true,
|
|
110
|
+
title: "",
|
|
111
|
+
});
|
|
112
|
+
};
|
package/src/index.tsx
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export {
|
|
2
|
-
default as
|
|
3
|
-
|
|
4
|
-
} from "./components/
|
|
2
|
+
default as Toaster,
|
|
3
|
+
TOASTER_TYPE_OPTIONS,
|
|
4
|
+
} from "./components/Toaster/Toaster";
|
|
5
|
+
export { toast } from "./components/Toaster/toast";
|
|
6
|
+
|
|
5
7
|
export { default as Avatar } from "./components/Avatar/Avatar";
|
|
6
8
|
export { default as Badge } from "./components/Badge/Badge";
|
|
7
9
|
export {
|
|
@@ -44,7 +46,7 @@ export {
|
|
|
44
46
|
} from "./components/List/List";
|
|
45
47
|
export {
|
|
46
48
|
CheckboxList,
|
|
47
|
-
|
|
49
|
+
CheckboxListProps,
|
|
48
50
|
} from "./components/CheckboxList/CheckboxList";
|
|
49
51
|
|
|
50
52
|
export { MultiSelect } from "./components/MultiSelect/MultiSelect";
|