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.
@@ -21,6 +21,7 @@ declare const meta: {
21
21
  actionLabel?: string | undefined;
22
22
  closable?: boolean | undefined;
23
23
  onClickAction?: (() => void) | undefined;
24
+ onClose?: (() => void) | undefined;
24
25
  }>) => import("react/jsx-runtime").JSX.Element)[];
25
26
  argTypes: {
26
27
  theme: {
@@ -144,3 +145,13 @@ export declare const NoIcon: import("@storybook/core/csf").AnnotatedStoryFn<impo
144
145
  export declare const NoCloseButton: import("@storybook/core/csf").AnnotatedStoryFn<import("@storybook/react").ReactRenderer, import(".").ToastProps>;
145
146
  export declare const NoAction: import("@storybook/core/csf").AnnotatedStoryFn<import("@storybook/react").ReactRenderer, import(".").ToastProps>;
146
147
  export declare const BottomAction: import("@storybook/core/csf").AnnotatedStoryFn<import("@storybook/react").ReactRenderer, import(".").ToastProps>;
148
+ export declare const QueueBehavior: {
149
+ (): import("react/jsx-runtime").JSX.Element;
150
+ parameters: {
151
+ docs: {
152
+ description: {
153
+ story: string;
154
+ };
155
+ };
156
+ };
157
+ };
@@ -1,8 +1,21 @@
1
1
  import React from "react";
2
2
  import { ToastProps, ToastPositionType } from ".";
3
- interface ToastOptions extends ToastProps {
3
+ export interface ToastOptions extends ToastProps {
4
+ /**
5
+ * Thời gian hiển thị (ms). Trống hoặc 0 → 3000.
6
+ * Giá trị không hữu hạn (Infinity) → không tự tắt, chỉ mất khi bấm ✕ hoặc bị thay.
7
+ * Giá trị lớn hơn 2147483647 được kẹp về 2147483647.
8
+ */
4
9
  duration?: number;
10
+ /** Vị trí hiển thị. Mặc định "top-right". */
5
11
  position?: ToastPositionType;
12
+ /**
13
+ * Danh tính của toast. Toast mới cùng `key` sẽ thay toast cũ.
14
+ * Không truyền thì hai toast coi là trùng khi cùng `title` và `theme`.
15
+ */
16
+ key?: string;
17
+ /** Gọi khi người dùng bấm ✕, trước khi toast bị gỡ. */
18
+ onClose?: () => void;
6
19
  }
7
20
  export interface ToastContextType {
8
21
  showToast: (options: ToastOptions) => void;
@@ -11,4 +24,3 @@ export declare const ToastContext: React.Context<ToastContextType | undefined>;
11
24
  export declare const ToastProvider: React.FC<{
12
25
  children: React.ReactNode;
13
26
  }>;
14
- export {};
@@ -15,6 +15,12 @@ export type ToastProps = {
15
15
  actionLabel?: string;
16
16
  closable?: boolean;
17
17
  onClickAction?: () => void;
18
+ /**
19
+ * Gọi khi bấm nút đóng (✕). Khi có prop này, toast không tự ẩn mà để
20
+ * component cha quyết định (ví dụ ToastProvider gỡ toast khỏi danh sách).
21
+ * Không truyền thì toast tự ẩn tại chỗ như trước.
22
+ */
23
+ onClose?: () => void;
18
24
  };
19
25
  declare const FISToast: React.FC<ToastProps>;
20
26
  export default FISToast;
@@ -52,6 +52,8 @@ export type { FISTableRef, TableProps as FISTableProps, } from "./components/Tab
52
52
  export type { ApiSelectProps, ApiSelectRef, FetchAll, FetchPage, FetchPageParams, FetchPageResult, FetchAllParams, } from "./components/ApiSelect/types";
53
53
  export { default as useNotification } from "./components/Notification/useNotification";
54
54
  export { default as useToast } from "./components/Toast/useToast";
55
+ export type { ToastOptions, ToastContextType, } from "./components/Toast/ToastContext";
56
+ export type { ToastProps, ToastPositionType } from "./components/Toast";
55
57
  export type { PaginationProps as FISPaginationProps } from "./components/Pagination";
56
58
  export declare const FISButton: typeof RawButton;
57
59
  export declare const FISLinkButton: typeof RawLinkButton;
package/dist/esm/index.js CHANGED
@@ -24304,7 +24304,7 @@ var __rest$m = undefined && undefined.__rest || function (s, e) {
24304
24304
  return t;
24305
24305
  };
24306
24306
  const DEFAULT_OFFSET = 24;
24307
- const DEFAULT_DURATION = 4.5;
24307
+ const DEFAULT_DURATION$1 = 4.5;
24308
24308
  const DEFAULT_PLACEMENT = 'topRight';
24309
24309
  const Wrapper = ({
24310
24310
  children,
@@ -24362,7 +24362,7 @@ const Holder = /*#__PURE__*/React__default.forwardRef((props, ref) => {
24362
24362
  motion: getNotificationMotion,
24363
24363
  closable: true,
24364
24364
  closeIcon: getCloseIcon(prefixCls),
24365
- duration: duration !== null && duration !== void 0 ? duration : DEFAULT_DURATION,
24365
+ duration: duration !== null && duration !== void 0 ? duration : DEFAULT_DURATION$1,
24366
24366
  getContainer: () => (staticGetContainer === null || staticGetContainer === void 0 ? void 0 : staticGetContainer()) || (getPopupContainer === null || getPopupContainer === void 0 ? void 0 : getPopupContainer()) || document.body,
24367
24367
  maxCount,
24368
24368
  pauseOnHover,
@@ -69875,12 +69875,12 @@ const dividerColorMap = {
69875
69875
  caution: "com/toast/theme/divider/color-stroke/caution",
69876
69876
  negative: "com/toast/theme/divider/color-stroke/negative",
69877
69877
  };
69878
- const FISToast$1 = ({ className, theme = "neutral", type = "link", actionPosition = "inline", iconLeft, noIcon = false, title, actionHref, actionLabel, closable = true, onClickAction, }) => {
69878
+ const FISToast$1 = ({ className, theme = "neutral", type = "link", actionPosition = "inline", iconLeft, noIcon = false, title, actionHref, actionLabel, closable = true, onClickAction, onClose, }) => {
69879
69879
  const [visible, setVisible] = useState(true);
69880
69880
  const resolvedIconLeft = useMemo$1(() => (theme === "neutral" ? iconLeft : themeIcons[theme]), [theme, iconLeft]);
69881
69881
  return (jsxs(DivContainerSC$3, { className: classNames(className, {
69882
69882
  hidden: !visible,
69883
- }), "$theme": theme, children: [!noIcon && jsx(DivIconSC$1, { children: resolvedIconLeft }), jsxs(DivTitleWrap, { "$actionPosition": actionPosition, "$type": type, children: [jsx(PTitleSC, { "$theme": theme, "$actionPosition": actionPosition, children: title }), type === "link" && actionHref && (jsx(LinkSC, { href: actionHref, target: "_blank", children: actionLabel })), type === "button" && actionLabel && (jsx(FISButton$1, { variant: "tertiary", size: "sm", onClick: onClickAction, children: actionLabel }))] }), closable && (jsxs(Fragment, { children: [jsx(FISDivider$1, { direction: "vertical", headpointOffset: "tiny", color: dividerColorMap[theme] }), jsx(FISIconButton$1, { size: "sm", icon: jsx(ExitIcon, {}), variant: "tertiary-invisible", onClick: () => setVisible(false), style: { alignSelf: "center" } })] }))] }));
69883
+ }), "$theme": theme, children: [!noIcon && jsx(DivIconSC$1, { children: resolvedIconLeft }), jsxs(DivTitleWrap, { "$actionPosition": actionPosition, "$type": type, children: [jsx(PTitleSC, { "$theme": theme, "$actionPosition": actionPosition, children: title }), type === "link" && actionHref && (jsx(LinkSC, { href: actionHref, target: "_blank", children: actionLabel })), type === "button" && actionLabel && (jsx(FISButton$1, { variant: "tertiary", size: "sm", onClick: onClickAction, children: actionLabel }))] }), closable && (jsxs(Fragment, { children: [jsx(FISDivider$1, { direction: "vertical", headpointOffset: "tiny", color: dividerColorMap[theme] }), jsx(FISIconButton$1, { size: "sm", icon: jsx(ExitIcon, {}), variant: "tertiary-invisible", onClick: () => (onClose ? onClose() : setVisible(false)), style: { alignSelf: "center" } })] }))] }));
69884
69884
  };
69885
69885
  FISToast$1.displayName = "FISToast";
69886
69886
 
@@ -76801,16 +76801,87 @@ function GlobalStyle({ theme }) {
76801
76801
  }
76802
76802
 
76803
76803
  const ToastContext = createContext$1(undefined);
76804
+ const DEFAULT_DURATION = 3000;
76805
+ const DEFAULT_POSITION = "top-right";
76806
+ /** Giá trị lớn nhất setTimeout chấp nhận; lớn hơn sẽ bị coi là 0 */
76807
+ const MAX_TIMEOUT = 2147483647;
76808
+ const getIdentity = (options) => options.key !== undefined
76809
+ ? JSON.stringify(["key", options.key])
76810
+ : JSON.stringify(["content", options.title, options.theme || "neutral"]);
76811
+ /** Trả về null khi toast không tự tắt */
76812
+ const resolveDuration = (duration) => {
76813
+ const value = duration || DEFAULT_DURATION;
76814
+ if (!Number.isFinite(value))
76815
+ return null;
76816
+ return Math.min(value, MAX_TIMEOUT);
76817
+ };
76804
76818
  const ToastProvider = ({ children, }) => {
76805
76819
  const [toasts, setToasts] = useState([]);
76820
+ const nextIdRef = useRef(0);
76821
+ const timersRef = useRef(new Map());
76822
+ /** danh tính → id của toast đang hiện, dùng để gộp toast trùng */
76823
+ const identitiesRef = useRef(new Map());
76824
+ // Chỉ thao tác ref/timer ở đây; updater của setState luôn thuần
76825
+ const removeToast = useCallback((id, identity) => {
76826
+ const timer = timersRef.current.get(id);
76827
+ if (timer !== undefined) {
76828
+ clearTimeout(timer);
76829
+ timersRef.current.delete(id);
76830
+ }
76831
+ if (identitiesRef.current.get(identity) === id) {
76832
+ identitiesRef.current.delete(identity);
76833
+ }
76834
+ setToasts((prev) => prev.filter((toast) => toast.id !== id));
76835
+ }, []);
76806
76836
  const showToast = useCallback((options) => {
76807
- const duration = options.duration || 3000;
76808
- setToasts((prev) => [...prev, options]);
76809
- setTimeout(() => {
76810
- setToasts((prev) => prev.slice(1));
76811
- }, duration);
76837
+ const identity = getIdentity(options);
76838
+ const previousId = identitiesRef.current.get(identity);
76839
+ if (previousId !== undefined) {
76840
+ const timer = timersRef.current.get(previousId);
76841
+ if (timer !== undefined) {
76842
+ clearTimeout(timer);
76843
+ timersRef.current.delete(previousId);
76844
+ }
76845
+ }
76846
+ const id = ++nextIdRef.current;
76847
+ identitiesRef.current.set(identity, id);
76848
+ const duration = resolveDuration(options.duration);
76849
+ if (duration !== null) {
76850
+ timersRef.current.set(id, setTimeout(() => removeToast(id, identity), duration));
76851
+ }
76852
+ setToasts((prev) => [
76853
+ ...prev.filter((toast) => toast.id !== previousId),
76854
+ { id, identity, options },
76855
+ ]);
76856
+ }, [removeToast]);
76857
+ useEffect(() => {
76858
+ const timers = timersRef.current;
76859
+ return () => {
76860
+ timers.forEach((timer) => clearTimeout(timer));
76861
+ timers.clear();
76862
+ };
76812
76863
  }, []);
76813
- return (jsxs(ToastContext.Provider, { value: { showToast }, children: [children, toasts.length > 0 && (jsx(ToastWrapper, { "$position": toasts[0].position || "top-right", children: toasts.map((toast, index) => (jsx(FISToast$1, { ...toast }, index))) }))] }));
76864
+ const groups = useMemo$1(() => {
76865
+ const byPosition = new Map();
76866
+ toasts.forEach((toast) => {
76867
+ const position = toast.options.position ?? DEFAULT_POSITION;
76868
+ const group = byPosition.get(position);
76869
+ if (group)
76870
+ group.push(toast);
76871
+ else
76872
+ byPosition.set(position, [toast]);
76873
+ });
76874
+ return Array.from(byPosition.entries());
76875
+ }, [toasts]);
76876
+ const contextValue = useMemo$1(() => ({ showToast }), [showToast]);
76877
+ return (jsxs(ToastContext.Provider, { value: contextValue, children: [children, groups.map(([position, entries]) => (jsx(ToastWrapper, { "$position": position, "data-position": position, children: entries.map(({ id, identity, options }) => {
76878
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
76879
+ const { duration, position: _position, key, ...toastProps } = options;
76880
+ return (jsx(FISToast$1, { ...toastProps, onClose: () => {
76881
+ options.onClose?.();
76882
+ removeToast(id, identity);
76883
+ } }, id));
76884
+ }) }, position)))] }));
76814
76885
  };
76815
76886
 
76816
76887
  const FISThemeProvider = ({ theme: theme$1 = theme, config, children, }) => (jsxs(ThemeProvider, { theme: theme$1, children: [jsx(GlobalStyle, { theme: theme$1 }), jsx(FISConfigProvider, { config: config, children: jsx(NotificationProvider, { children: jsx(ToastProvider, { children: children }) }) })] }));