fis-component 0.2.5 → 0.2.6
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/cjs/index.js +81 -10
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/src/components/Toast/Toast.stories.d.ts +11 -0
- package/dist/cjs/types/src/components/Toast/ToastContext.d.ts +14 -2
- package/dist/cjs/types/src/components/Toast/index.d.ts +6 -0
- package/dist/cjs/types/src/index.d.ts +2 -0
- package/dist/esm/index.js +81 -10
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/src/components/Toast/Toast.stories.d.ts +11 -0
- package/dist/esm/types/src/components/Toast/ToastContext.d.ts +14 -2
- package/dist/esm/types/src/components/Toast/index.d.ts +6 -0
- package/dist/esm/types/src/index.d.ts +2 -0
- package/dist/index.d.ts +20 -1
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -24324,7 +24324,7 @@ var __rest$m = undefined && undefined.__rest || function (s, e) {
|
|
|
24324
24324
|
return t;
|
|
24325
24325
|
};
|
|
24326
24326
|
const DEFAULT_OFFSET = 24;
|
|
24327
|
-
const DEFAULT_DURATION = 4.5;
|
|
24327
|
+
const DEFAULT_DURATION$1 = 4.5;
|
|
24328
24328
|
const DEFAULT_PLACEMENT = 'topRight';
|
|
24329
24329
|
const Wrapper = ({
|
|
24330
24330
|
children,
|
|
@@ -24382,7 +24382,7 @@ const Holder = /*#__PURE__*/React.forwardRef((props, ref) => {
|
|
|
24382
24382
|
motion: getNotificationMotion,
|
|
24383
24383
|
closable: true,
|
|
24384
24384
|
closeIcon: getCloseIcon(prefixCls),
|
|
24385
|
-
duration: duration !== null && duration !== void 0 ? duration : DEFAULT_DURATION,
|
|
24385
|
+
duration: duration !== null && duration !== void 0 ? duration : DEFAULT_DURATION$1,
|
|
24386
24386
|
getContainer: () => (staticGetContainer === null || staticGetContainer === void 0 ? void 0 : staticGetContainer()) || (getPopupContainer === null || getPopupContainer === void 0 ? void 0 : getPopupContainer()) || document.body,
|
|
24387
24387
|
maxCount,
|
|
24388
24388
|
pauseOnHover,
|
|
@@ -69895,12 +69895,12 @@ const dividerColorMap = {
|
|
|
69895
69895
|
caution: "com/toast/theme/divider/color-stroke/caution",
|
|
69896
69896
|
negative: "com/toast/theme/divider/color-stroke/negative",
|
|
69897
69897
|
};
|
|
69898
|
-
const FISToast$1 = ({ className, theme = "neutral", type = "link", actionPosition = "inline", iconLeft, noIcon = false, title, actionHref, actionLabel, closable = true, onClickAction, }) => {
|
|
69898
|
+
const FISToast$1 = ({ className, theme = "neutral", type = "link", actionPosition = "inline", iconLeft, noIcon = false, title, actionHref, actionLabel, closable = true, onClickAction, onClose, }) => {
|
|
69899
69899
|
const [visible, setVisible] = React.useState(true);
|
|
69900
69900
|
const resolvedIconLeft = React.useMemo(() => (theme === "neutral" ? iconLeft : themeIcons[theme]), [theme, iconLeft]);
|
|
69901
69901
|
return (jsxRuntime.jsxs(DivContainerSC$3, { className: classNames(className, {
|
|
69902
69902
|
hidden: !visible,
|
|
69903
|
-
}), "$theme": theme, children: [!noIcon && jsxRuntime.jsx(DivIconSC$1, { children: resolvedIconLeft }), jsxRuntime.jsxs(DivTitleWrap, { "$actionPosition": actionPosition, "$type": type, children: [jsxRuntime.jsx(PTitleSC, { "$theme": theme, "$actionPosition": actionPosition, children: title }), type === "link" && actionHref && (jsxRuntime.jsx(LinkSC, { href: actionHref, target: "_blank", children: actionLabel })), type === "button" && actionLabel && (jsxRuntime.jsx(FISButton$1, { variant: "tertiary", size: "sm", onClick: onClickAction, children: actionLabel }))] }), closable && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(FISDivider$1, { direction: "vertical", headpointOffset: "tiny", color: dividerColorMap[theme] }), jsxRuntime.jsx(FISIconButton$1, { size: "sm", icon: jsxRuntime.jsx(ExitIcon, {}), variant: "tertiary-invisible", onClick: () => setVisible(false), style: { alignSelf: "center" } })] }))] }));
|
|
69903
|
+
}), "$theme": theme, children: [!noIcon && jsxRuntime.jsx(DivIconSC$1, { children: resolvedIconLeft }), jsxRuntime.jsxs(DivTitleWrap, { "$actionPosition": actionPosition, "$type": type, children: [jsxRuntime.jsx(PTitleSC, { "$theme": theme, "$actionPosition": actionPosition, children: title }), type === "link" && actionHref && (jsxRuntime.jsx(LinkSC, { href: actionHref, target: "_blank", children: actionLabel })), type === "button" && actionLabel && (jsxRuntime.jsx(FISButton$1, { variant: "tertiary", size: "sm", onClick: onClickAction, children: actionLabel }))] }), closable && (jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [jsxRuntime.jsx(FISDivider$1, { direction: "vertical", headpointOffset: "tiny", color: dividerColorMap[theme] }), jsxRuntime.jsx(FISIconButton$1, { size: "sm", icon: jsxRuntime.jsx(ExitIcon, {}), variant: "tertiary-invisible", onClick: () => (onClose ? onClose() : setVisible(false)), style: { alignSelf: "center" } })] }))] }));
|
|
69904
69904
|
};
|
|
69905
69905
|
FISToast$1.displayName = "FISToast";
|
|
69906
69906
|
|
|
@@ -76821,16 +76821,87 @@ function GlobalStyle({ theme }) {
|
|
|
76821
76821
|
}
|
|
76822
76822
|
|
|
76823
76823
|
const ToastContext = React.createContext(undefined);
|
|
76824
|
+
const DEFAULT_DURATION = 3000;
|
|
76825
|
+
const DEFAULT_POSITION = "top-right";
|
|
76826
|
+
/** Giá trị lớn nhất setTimeout chấp nhận; lớn hơn sẽ bị coi là 0 */
|
|
76827
|
+
const MAX_TIMEOUT = 2147483647;
|
|
76828
|
+
const getIdentity = (options) => options.key !== undefined
|
|
76829
|
+
? JSON.stringify(["key", options.key])
|
|
76830
|
+
: JSON.stringify(["content", options.title, options.theme || "neutral"]);
|
|
76831
|
+
/** Trả về null khi toast không tự tắt */
|
|
76832
|
+
const resolveDuration = (duration) => {
|
|
76833
|
+
const value = duration || DEFAULT_DURATION;
|
|
76834
|
+
if (!Number.isFinite(value))
|
|
76835
|
+
return null;
|
|
76836
|
+
return Math.min(value, MAX_TIMEOUT);
|
|
76837
|
+
};
|
|
76824
76838
|
const ToastProvider = ({ children, }) => {
|
|
76825
76839
|
const [toasts, setToasts] = React.useState([]);
|
|
76840
|
+
const nextIdRef = React.useRef(0);
|
|
76841
|
+
const timersRef = React.useRef(new Map());
|
|
76842
|
+
/** danh tính → id của toast đang hiện, dùng để gộp toast trùng */
|
|
76843
|
+
const identitiesRef = React.useRef(new Map());
|
|
76844
|
+
// Chỉ thao tác ref/timer ở đây; updater của setState luôn thuần
|
|
76845
|
+
const removeToast = React.useCallback((id, identity) => {
|
|
76846
|
+
const timer = timersRef.current.get(id);
|
|
76847
|
+
if (timer !== undefined) {
|
|
76848
|
+
clearTimeout(timer);
|
|
76849
|
+
timersRef.current.delete(id);
|
|
76850
|
+
}
|
|
76851
|
+
if (identitiesRef.current.get(identity) === id) {
|
|
76852
|
+
identitiesRef.current.delete(identity);
|
|
76853
|
+
}
|
|
76854
|
+
setToasts((prev) => prev.filter((toast) => toast.id !== id));
|
|
76855
|
+
}, []);
|
|
76826
76856
|
const showToast = React.useCallback((options) => {
|
|
76827
|
-
const
|
|
76828
|
-
|
|
76829
|
-
|
|
76830
|
-
|
|
76831
|
-
|
|
76857
|
+
const identity = getIdentity(options);
|
|
76858
|
+
const previousId = identitiesRef.current.get(identity);
|
|
76859
|
+
if (previousId !== undefined) {
|
|
76860
|
+
const timer = timersRef.current.get(previousId);
|
|
76861
|
+
if (timer !== undefined) {
|
|
76862
|
+
clearTimeout(timer);
|
|
76863
|
+
timersRef.current.delete(previousId);
|
|
76864
|
+
}
|
|
76865
|
+
}
|
|
76866
|
+
const id = ++nextIdRef.current;
|
|
76867
|
+
identitiesRef.current.set(identity, id);
|
|
76868
|
+
const duration = resolveDuration(options.duration);
|
|
76869
|
+
if (duration !== null) {
|
|
76870
|
+
timersRef.current.set(id, setTimeout(() => removeToast(id, identity), duration));
|
|
76871
|
+
}
|
|
76872
|
+
setToasts((prev) => [
|
|
76873
|
+
...prev.filter((toast) => toast.id !== previousId),
|
|
76874
|
+
{ id, identity, options },
|
|
76875
|
+
]);
|
|
76876
|
+
}, [removeToast]);
|
|
76877
|
+
React.useEffect(() => {
|
|
76878
|
+
const timers = timersRef.current;
|
|
76879
|
+
return () => {
|
|
76880
|
+
timers.forEach((timer) => clearTimeout(timer));
|
|
76881
|
+
timers.clear();
|
|
76882
|
+
};
|
|
76832
76883
|
}, []);
|
|
76833
|
-
|
|
76884
|
+
const groups = React.useMemo(() => {
|
|
76885
|
+
const byPosition = new Map();
|
|
76886
|
+
toasts.forEach((toast) => {
|
|
76887
|
+
const position = toast.options.position ?? DEFAULT_POSITION;
|
|
76888
|
+
const group = byPosition.get(position);
|
|
76889
|
+
if (group)
|
|
76890
|
+
group.push(toast);
|
|
76891
|
+
else
|
|
76892
|
+
byPosition.set(position, [toast]);
|
|
76893
|
+
});
|
|
76894
|
+
return Array.from(byPosition.entries());
|
|
76895
|
+
}, [toasts]);
|
|
76896
|
+
const contextValue = React.useMemo(() => ({ showToast }), [showToast]);
|
|
76897
|
+
return (jsxRuntime.jsxs(ToastContext.Provider, { value: contextValue, children: [children, groups.map(([position, entries]) => (jsxRuntime.jsx(ToastWrapper, { "$position": position, "data-position": position, children: entries.map(({ id, identity, options }) => {
|
|
76898
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
76899
|
+
const { duration, position: _position, key, ...toastProps } = options;
|
|
76900
|
+
return (jsxRuntime.jsx(FISToast$1, { ...toastProps, onClose: () => {
|
|
76901
|
+
options.onClose?.();
|
|
76902
|
+
removeToast(id, identity);
|
|
76903
|
+
} }, id));
|
|
76904
|
+
}) }, position)))] }));
|
|
76834
76905
|
};
|
|
76835
76906
|
|
|
76836
76907
|
const FISThemeProvider = ({ theme: theme$1 = theme, config, children, }) => (jsxRuntime.jsxs(styled.ThemeProvider, { theme: theme$1, children: [jsxRuntime.jsx(GlobalStyle, { theme: theme$1 }), jsxRuntime.jsx(FISConfigProvider, { config: config, children: jsxRuntime.jsx(NotificationProvider, { children: jsxRuntime.jsx(ToastProvider, { children: children }) }) })] }));
|