@mrktg-common/internal-ui 1.13.0 → 1.14.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/Toast.d.ts +9 -1
- package/dist/Toast.js +58 -15
- package/dist/index.d.ts +1 -1
- package/package.json +1 -1
- package/tokens/tokens.json +1 -1
package/dist/Toast.d.ts
CHANGED
|
@@ -5,6 +5,10 @@ export type TToastAction = {
|
|
|
5
5
|
label: string;
|
|
6
6
|
onClick: () => void;
|
|
7
7
|
icon?: LucideIcon;
|
|
8
|
+
/** Действие идёт: спиннер вместо иконки, нажатия не проходят, таймер тоста стоит. */
|
|
9
|
+
busy?: boolean;
|
|
10
|
+
/** Нажатие не закрывает тост: результат придёт позже, через update. */
|
|
11
|
+
keepOpen?: boolean;
|
|
8
12
|
};
|
|
9
13
|
export type TToast = {
|
|
10
14
|
/** neutral — сохранено, удалено, скопировано; success — только «vN is live» (на Production или Test). */
|
|
@@ -20,9 +24,13 @@ export type TToast = {
|
|
|
20
24
|
/** Сколько висит, мс; null — пока не закроют. По умолчанию 6 с, с Undo 10 с, ошибки висят. */
|
|
21
25
|
duration?: number | null;
|
|
22
26
|
};
|
|
27
|
+
/** Поле со значением undefined убирается: `{ action: undefined }` снимает кнопку. */
|
|
28
|
+
export type TToastPatch = Partial<Pick<TToast, 'tone' | 'icon' | 'title' | 'description' | 'action' | 'duration'>>;
|
|
23
29
|
export type TToastApi = {
|
|
24
30
|
show: (toast: TToast) => number;
|
|
25
31
|
dismiss: (id: number) => void;
|
|
32
|
+
/** Меняет показанный тост на месте и запускает его таймер заново; закрытый тост не возвращает. */
|
|
33
|
+
update: (id: number, patch: TToastPatch) => void;
|
|
26
34
|
};
|
|
27
35
|
export declare const TOAST_MS = 6000;
|
|
28
36
|
export declare const TOAST_UNDO_MS = 10000;
|
|
@@ -36,7 +44,7 @@ export declare function useToast(): TToastApi;
|
|
|
36
44
|
export declare function useToastInsetEnd(width: number | null): void;
|
|
37
45
|
/**
|
|
38
46
|
* Что только что произошло: справа снизу, до трёх штук, новый внизу. Таймеры стоят, пока указатель
|
|
39
|
-
* над стопкой или фокус внутри
|
|
47
|
+
* над стопкой или фокус внутри неё, и пока идёт действие тоста. Закрыть можно всегда.
|
|
40
48
|
*/
|
|
41
49
|
export declare function ToastProvider({ children }: {
|
|
42
50
|
children: ReactNode;
|
package/dist/Toast.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
4
4
|
import { Check, CircleCheck, CircleX, Info, TriangleAlert, Undo2, X } from 'lucide-react';
|
|
5
|
+
import { Button } from './Button.js';
|
|
5
6
|
import { cx } from './cx.js';
|
|
6
7
|
import { FixedPortal } from './portal.js';
|
|
7
8
|
export const TOAST_MS = 6000;
|
|
@@ -39,7 +40,7 @@ export function useToastInsetEnd(width) {
|
|
|
39
40
|
}
|
|
40
41
|
/**
|
|
41
42
|
* Что только что произошло: справа снизу, до трёх штук, новый внизу. Таймеры стоят, пока указатель
|
|
42
|
-
* над стопкой или фокус внутри
|
|
43
|
+
* над стопкой или фокус внутри неё, и пока идёт действие тоста. Закрыть можно всегда.
|
|
43
44
|
*/
|
|
44
45
|
export function ToastProvider({ children }) {
|
|
45
46
|
const [toasts, setToasts] = useState([]);
|
|
@@ -48,34 +49,66 @@ export function ToastProvider({ children }) {
|
|
|
48
49
|
const dismiss = useCallback((id) => setToasts((current) => current.filter((toast) => toast.id !== id)), []);
|
|
49
50
|
const show = useCallback((toast) => {
|
|
50
51
|
const id = nextId.current++;
|
|
51
|
-
setToasts((current) => [...current, { ...toast, id }].slice(-MAX_TOASTS));
|
|
52
|
+
setToasts((current) => [...current, { ...toast, id, node: 0, revision: 0 }].slice(-MAX_TOASTS));
|
|
52
53
|
return id;
|
|
53
54
|
}, []);
|
|
54
|
-
const
|
|
55
|
+
const update = useCallback((id, patch) => {
|
|
56
|
+
setToasts((current) => {
|
|
57
|
+
const shown = current.find((toast) => toast.id === id);
|
|
58
|
+
if (shown === undefined)
|
|
59
|
+
return current;
|
|
60
|
+
const next = { ...shown, ...patch, revision: shown.revision + 1 };
|
|
61
|
+
// role="alert" читается, только если стоит на узле в момент появления: ошибка приходит новым тостом
|
|
62
|
+
if (next.tone === 'danger' && shown.tone !== 'danger') {
|
|
63
|
+
return [...current.filter((toast) => toast !== shown), { ...next, node: shown.node + 1 }];
|
|
64
|
+
}
|
|
65
|
+
return current.map((toast) => (toast === shown ? next : toast));
|
|
66
|
+
});
|
|
67
|
+
}, []);
|
|
68
|
+
const api = useMemo(() => ({ show, dismiss, update }), [show, dismiss, update]);
|
|
55
69
|
return (_jsx(ToastContext.Provider, { value: api, children: _jsxs(ToastInsetContext.Provider, { value: setInsetEnd, children: [children, _jsx(ToastStack, { toasts: toasts, insetEnd: insetEnd, onDismiss: dismiss })] }) }));
|
|
56
70
|
}
|
|
57
71
|
function ToastStack({ toasts, insetEnd, onDismiss }) {
|
|
58
72
|
const [hovered, setHovered] = useState(false);
|
|
59
73
|
const [focused, setFocused] = useState(false);
|
|
60
74
|
const paused = hovered || focused;
|
|
75
|
+
const stack = useRef(null);
|
|
76
|
+
// Тост ушёл вместе с кнопкой, на которой был фокус: blur не приходит, а таймеры остальных стоять не должны
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
if (stack.current?.contains(document.activeElement) !== true)
|
|
79
|
+
setFocused(false);
|
|
80
|
+
}, [toasts]);
|
|
61
81
|
if (toasts.length === 0)
|
|
62
82
|
return null;
|
|
63
83
|
const style = { '--toast-inset-end': `${insetEnd}px` };
|
|
64
|
-
return (_jsx(FixedPortal, { children: _jsx("div", { className: "toasts is-fixed", style: style, onMouseEnter: () => setHovered(true), onMouseLeave: () => setHovered(false), onFocus: () => setFocused(true), onBlur: (event) => {
|
|
84
|
+
return (_jsx(FixedPortal, { children: _jsx("div", { ref: stack, className: "toasts is-fixed", style: style, onMouseEnter: () => setHovered(true), onMouseLeave: () => setHovered(false), onFocus: () => setFocused(true), onBlur: (event) => {
|
|
65
85
|
if (!event.currentTarget.contains(event.relatedTarget))
|
|
66
86
|
setFocused(false);
|
|
67
|
-
}, children: toasts.map((toast) => (_jsx(ToastItem, { toast: toast, paused: paused, onDismiss: () => onDismiss(toast.id) }, toast.id))) }) }));
|
|
87
|
+
}, children: toasts.map((toast) => (_jsx(ToastItem, { toast: toast, paused: paused, onDismiss: () => onDismiss(toast.id) }, `${toast.id}:${toast.node}`))) }) }));
|
|
68
88
|
}
|
|
69
89
|
function ToastItem({ toast, paused, onDismiss }) {
|
|
70
90
|
const duration = toastDuration(toast);
|
|
91
|
+
const tone = toast.tone ?? 'neutral';
|
|
92
|
+
const Icon = toast.icon ?? TONE_ICON[tone];
|
|
93
|
+
const action = toast.undo === undefined ? toast.action : { label: 'Undo', onClick: toast.undo, icon: Undo2 };
|
|
94
|
+
const ActionIcon = action?.icon;
|
|
95
|
+
const busy = action?.busy === true;
|
|
96
|
+
const hasAction = action !== undefined;
|
|
97
|
+
const stopped = paused || busy;
|
|
71
98
|
// Остаток времени копится между паузами: наведение не сбрасывает таймер на полный срок
|
|
72
99
|
const remaining = useRef(duration);
|
|
100
|
+
const revision = useRef(toast.revision);
|
|
73
101
|
const dismissRef = useRef(onDismiss);
|
|
74
102
|
useEffect(() => {
|
|
75
103
|
dismissRef.current = onDismiss;
|
|
76
104
|
});
|
|
77
105
|
useEffect(() => {
|
|
78
|
-
|
|
106
|
+
// После update срок идёт заново, по правилам нового содержимого
|
|
107
|
+
if (revision.current !== toast.revision) {
|
|
108
|
+
revision.current = toast.revision;
|
|
109
|
+
remaining.current = duration;
|
|
110
|
+
}
|
|
111
|
+
if (stopped || remaining.current === null)
|
|
79
112
|
return;
|
|
80
113
|
const startedAt = Date.now();
|
|
81
114
|
const timer = window.setTimeout(() => dismissRef.current(), remaining.current);
|
|
@@ -84,13 +117,23 @@ function ToastItem({ toast, paused, onDismiss }) {
|
|
|
84
117
|
if (remaining.current !== null)
|
|
85
118
|
remaining.current = Math.max(0, remaining.current - (Date.now() - startedAt));
|
|
86
119
|
};
|
|
87
|
-
}, [
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
120
|
+
}, [stopped, toast.revision, duration]);
|
|
121
|
+
// update убрал кнопку, на которой был фокус: он не должен уйти из тоста на страницу
|
|
122
|
+
const dismissButton = useRef(null);
|
|
123
|
+
const actionFocused = useRef(false);
|
|
124
|
+
useLayoutEffect(() => {
|
|
125
|
+
if (hasAction || !actionFocused.current)
|
|
126
|
+
return;
|
|
127
|
+
actionFocused.current = false;
|
|
128
|
+
dismissButton.current?.focus();
|
|
129
|
+
}, [hasAction]);
|
|
130
|
+
return (_jsxs("div", { className: "toast", role: tone === 'danger' ? 'alert' : 'status', "aria-atomic": "true", children: [_jsx(Icon, { className: cx('ic', `t-${tone}`), "aria-hidden": true }), _jsxs("div", { className: "toast-body", children: [_jsx("span", { className: "toast-title", children: toast.title }), toast.description !== undefined && _jsx("span", { className: "toast-desc", children: toast.description }), action !== undefined && (_jsx("div", { className: "toast-act", children: _jsxs(Button, { size: "sm", busy: busy, onFocus: () => {
|
|
131
|
+
actionFocused.current = true;
|
|
132
|
+
}, onBlur: () => {
|
|
133
|
+
actionFocused.current = false;
|
|
134
|
+
}, onClick: () => {
|
|
135
|
+
if (action.keepOpen !== true)
|
|
136
|
+
onDismiss();
|
|
94
137
|
action.onClick();
|
|
95
|
-
}, children: [ActionIcon && _jsx(ActionIcon, { className: "ic ic-sm", "aria-hidden": true }), action.label] }) }))] }), _jsx("button", { type: "button", className: "btn btn-ghost btn-sm btn-icon", "aria-label": "Dismiss", onClick: onDismiss, children: _jsx(X, { className: "ic", "aria-hidden": true }) })] }));
|
|
138
|
+
}, children: [ActionIcon && !busy && _jsx(ActionIcon, { className: "ic ic-sm", "aria-hidden": true }), action.label] }) }))] }), _jsx("button", { ref: dismissButton, type: "button", className: "btn btn-ghost btn-sm btn-icon", "aria-label": "Dismiss", onClick: onDismiss, children: _jsx(X, { className: "ic", "aria-hidden": true }) })] }));
|
|
96
139
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ export type { IAlertProps, TAlertTone } from './Alert.tsx';
|
|
|
42
42
|
export { Banner } from './Banner.tsx';
|
|
43
43
|
export type { IBannerProps, TBannerTone } from './Banner.tsx';
|
|
44
44
|
export { ToastProvider, useToast, useToastInsetEnd, toastDuration, TOAST_MS, TOAST_UNDO_MS, MAX_TOASTS } from './Toast.tsx';
|
|
45
|
-
export type { TToast, TToastApi, TToastTone, TToastAction } from './Toast.tsx';
|
|
45
|
+
export type { TToast, TToastApi, TToastTone, TToastAction, TToastPatch } from './Toast.tsx';
|
|
46
46
|
export { Tooltip, TOOLTIP_DELAY_MS } from './Tooltip.tsx';
|
|
47
47
|
export type { ITooltipProps } from './Tooltip.tsx';
|
|
48
48
|
export { Skeleton } from './Skeleton.tsx';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mrktg-common/internal-ui",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.0",
|
|
4
4
|
"description": "Общий интерфейс внутренних инструментов маркетинга (Landings Admin, LP Constructor): токены, классы компонентов и тонкие React-обёртки над ними.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|