@seip/bluebird-js 0.1.7 → 0.2.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/AGENTS.md +726 -620
- package/README.md +615 -607
- package/app/models/user.js +19 -0
- package/app/resources/html/home.html +137 -0
- package/app/resources/html/layouts/main.html +54 -0
- package/app/resources/html/notFound.html +14 -0
- package/app/resources/html/terms.html +46 -0
- package/app/resources/index.html +27 -0
- package/app/resources/layouts/AppLayout.jsx +89 -0
- package/app/resources/main.jsx +18 -0
- package/app/resources/pages/Dashboard.jsx +98 -0
- package/app/resources/pages/Home.jsx +140 -0
- package/app/resources/pages/Login.jsx +35 -0
- package/app/resources/pages/NotFound.jsx +16 -0
- package/app/resources/pages/Register.jsx +55 -0
- package/app/resources/routes.jsx +54 -0
- package/app/resources/spa.js +67 -0
- package/app/resources/styles.css +9 -0
- package/app/routes/api.js +85 -50
- package/app/routes/auth.js +209 -0
- package/app/routes/web.js +59 -47
- package/core/ai.js +30 -10
- package/core/app.d.ts +2 -18
- package/core/app.js +987 -801
- package/core/auth.d.ts +2 -13
- package/core/auth.js +444 -326
- package/core/cli/build.js +37 -0
- package/core/cli/dev.js +155 -0
- package/core/cli/doctor.js +37 -2
- package/core/cli/export.js +191 -0
- package/core/cli/init.js +91 -322
- package/core/cli/nginx.js +1 -1
- package/core/cli/templates.js +139 -0
- package/core/cli/view.js +98 -161
- package/core/config.js +44 -2
- package/core/csrf.js +156 -24
- package/core/database.js +1184 -1131
- package/core/debug.js +1 -1
- package/core/html.js +8 -1
- package/core/index.d.ts +126 -310
- package/core/paths.js +55 -0
- package/core/ratelimit.js +10 -1
- package/core/react/client/api.js +237 -0
- package/core/react/client/auth.js +236 -0
- package/core/react/client/components.js +1321 -0
- package/core/react/client/i18n.js +338 -0
- package/core/react/client/index.d.ts +249 -0
- package/core/react/client/index.js +49 -0
- package/core/react/client/router.js +26 -0
- package/core/react/client/theme.css +512 -0
- package/core/render-cache.js +250 -212
- package/core/render.d.ts +25 -21
- package/core/render.js +738 -736
- package/core/router.d.ts +2 -3
- package/core/router.js +171 -171
- package/core/seo.js +10 -0
- package/core/spa.js +599 -0
- package/core/view-loader.js +155 -613
- package/core/vite-plugin.d.ts +18 -0
- package/core/vite-plugin.js +162 -0
- package/core/vite.js +250 -145
- package/index.js +22 -18
- package/jsconfig.json +5 -3
- package/package.json +114 -92
- package/production.md +8 -3
- package/public/js/bluebird.js +1160 -0
- package/templates/_auth/app/database.js +19 -0
- package/templates/_auth/app/models/user.js +22 -0
- package/templates/_auth/app/routes/auth-router.js +304 -0
- package/templates/_base/app/resources/html/layouts/main.html +55 -0
- package/templates/_base/app/resources/html/notFound.html +14 -0
- package/templates/_base/app/resources/index.html +27 -0
- package/templates/_base/app/resources/styles.css +9 -0
- package/templates/api/app/models/note.js +18 -0
- package/templates/api/app/routes/api.js +82 -0
- package/templates/api/app/routes/auth.js +11 -0
- package/templates/api/index.js +19 -0
- package/templates/api/template.json +13 -0
- package/templates/auth/app/mail.js +11 -0
- package/templates/auth/app/models/password_reset.js +18 -0
- package/templates/auth/app/resources/html/home.html +21 -0
- package/templates/auth/app/resources/html/partials/nav.html +2 -0
- package/templates/auth/app/resources/layouts/AppLayout.jsx +60 -0
- package/templates/auth/app/resources/main.jsx +18 -0
- package/templates/auth/app/resources/pages/Dashboard.jsx +51 -0
- package/templates/auth/app/resources/pages/ForgotPassword.jsx +34 -0
- package/templates/auth/app/resources/pages/Login.jsx +36 -0
- package/templates/auth/app/resources/pages/NotFound.jsx +16 -0
- package/templates/auth/app/resources/pages/Profile.jsx +99 -0
- package/templates/auth/app/resources/pages/Register.jsx +50 -0
- package/templates/auth/app/resources/pages/ResetPassword.jsx +50 -0
- package/templates/auth/app/resources/routes.jsx +54 -0
- package/templates/auth/app/resources/spa.js +29 -0
- package/templates/auth/app/routes/api.js +17 -0
- package/templates/auth/app/routes/auth.js +13 -0
- package/templates/auth/app/routes/web.js +21 -0
- package/templates/auth/index.js +19 -0
- package/templates/auth/template.json +15 -0
- package/templates/blank/app/models/.gitkeep +0 -0
- package/templates/blank/app/resources/html/home.html +27 -0
- package/templates/blank/app/resources/html/partials/nav.html +1 -0
- package/templates/blank/app/resources/main.jsx +16 -0
- package/templates/blank/app/resources/pages/Home.jsx +42 -0
- package/templates/blank/app/resources/pages/NotFound.jsx +27 -0
- package/templates/blank/app/resources/routes.jsx +15 -0
- package/templates/blank/app/resources/spa.js +24 -0
- package/templates/blank/app/routes/api.js +11 -0
- package/templates/blank/app/routes/web.js +21 -0
- package/templates/blank/index.js +13 -0
- package/templates/blank/template.json +12 -0
- package/templates/blog/app/lib/blog.js +149 -0
- package/templates/blog/app/models/post.js +22 -0
- package/templates/blog/app/resources/html/home.html +28 -0
- package/templates/blog/app/resources/html/not-found.html +5 -0
- package/templates/blog/app/resources/html/partials/nav.html +2 -0
- package/templates/blog/app/resources/html/partials/post-card.html +9 -0
- package/templates/blog/app/resources/html/post.html +8 -0
- package/templates/blog/app/resources/layouts/AdminLayout.jsx +54 -0
- package/templates/blog/app/resources/main.jsx +18 -0
- package/templates/blog/app/resources/pages/Login.jsx +31 -0
- package/templates/blog/app/resources/pages/NotFound.jsx +16 -0
- package/templates/blog/app/resources/pages/PostEditor.jsx +51 -0
- package/templates/blog/app/resources/pages/Posts.jsx +83 -0
- package/templates/blog/app/resources/pages/Register.jsx +43 -0
- package/templates/blog/app/resources/routes.jsx +51 -0
- package/templates/blog/app/resources/spa.js +25 -0
- package/templates/blog/app/resources/styles.css +40 -0
- package/templates/blog/app/routes/api.js +77 -0
- package/templates/blog/app/routes/auth.js +10 -0
- package/templates/blog/app/routes/web.js +114 -0
- package/templates/blog/index.js +21 -0
- package/templates/blog/template.json +14 -0
- package/vite.config.js +11 -0
- package/app/views/components/Header.jsx +0 -42
- package/app/views/components/LiveCounter.jsx +0 -67
- package/app/views/layouts/main/view.jsx +0 -201
- package/app/views/legal/terms.html +0 -29
- package/app/views/pages/about.jsx +0 -98
- package/app/views/pages/dashboard.jsx +0 -278
- package/app/views/pages/index.jsx +0 -123
- package/app/views/pages/login.jsx +0 -141
- package/core/actions.d.ts +0 -4
- package/core/actions.js +0 -355
- package/core/jsx.d.ts +0 -38
- package/core/jsx.js +0 -1597
- package/core/vite.d.ts +0 -3
- package/public/js/bluebird-client.js +0 -3265
- package/public/js/tailwind.js +0 -8
|
@@ -0,0 +1,1321 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
createElement as h,
|
|
4
|
+
forwardRef,
|
|
5
|
+
useCallback,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useId,
|
|
9
|
+
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
useSyncExternalStore,
|
|
12
|
+
} from "react";
|
|
13
|
+
import { NavLink, useNavigation } from "react-router";
|
|
14
|
+
import { ApiError, api, getCsrfToken } from "./api.js";
|
|
15
|
+
import { t as translate, useLocale } from "./i18n.js";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Joins class names, skipping falsy values.
|
|
19
|
+
* @param {...any} parts
|
|
20
|
+
* @returns {string}
|
|
21
|
+
*/
|
|
22
|
+
export function cn(...parts) {
|
|
23
|
+
return parts.flat(Infinity).filter(Boolean).join(" ");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Returns the OS preferred color scheme (`"dark"` or `"light"`).
|
|
28
|
+
* @returns {"dark"|"light"}
|
|
29
|
+
*/
|
|
30
|
+
function getSystemScheme() {
|
|
31
|
+
if (typeof window === "undefined") return "dark";
|
|
32
|
+
return window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Synchronizes `html.dark`, `[data-theme]`, and `<meta name="color-scheme">`.
|
|
37
|
+
* @param {"dark"|"light"} resolved
|
|
38
|
+
* @param {string|null} storedOverride
|
|
39
|
+
*/
|
|
40
|
+
function syncDocumentTheme(resolved, storedOverride) {
|
|
41
|
+
if (typeof document === "undefined") return;
|
|
42
|
+
const root = document.documentElement;
|
|
43
|
+
root.classList.toggle("dark", resolved === "dark");
|
|
44
|
+
root.setAttribute("data-theme", resolved);
|
|
45
|
+
const meta = document.querySelector('meta[name="color-scheme"]');
|
|
46
|
+
if (meta) {
|
|
47
|
+
meta.setAttribute("content", storedOverride === "light" || storedOverride === "dark" ? storedOverride : "light dark");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Synchronizes dark/light theme preference with `localStorage`, system preference, and `<meta name="color-scheme">`.
|
|
53
|
+
* @returns {{ theme: "dark"|"light", isDark: boolean, setTheme: (mode: "dark"|"light") => void, toggleTheme: () => void }}
|
|
54
|
+
*/
|
|
55
|
+
export function useTheme() {
|
|
56
|
+
const getInitial = () => {
|
|
57
|
+
if (typeof window === "undefined" || typeof document === "undefined") return "dark";
|
|
58
|
+
try {
|
|
59
|
+
const saved = window.localStorage.getItem("bb-theme");
|
|
60
|
+
if (saved === "light" || saved === "dark") return saved;
|
|
61
|
+
} catch {}
|
|
62
|
+
if (document.documentElement.classList.contains("dark")) return "dark";
|
|
63
|
+
if (document.documentElement.getAttribute("data-theme") === "light") return "light";
|
|
64
|
+
return getSystemScheme();
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const [theme, setThemeState] = useState(getInitial);
|
|
68
|
+
|
|
69
|
+
const commitTheme = useCallback((next, preferUnsetMatchingSystem = false) => {
|
|
70
|
+
const resolved = next === "light" ? "light" : "dark";
|
|
71
|
+
const sys = getSystemScheme();
|
|
72
|
+
let stored = resolved;
|
|
73
|
+
try {
|
|
74
|
+
if (preferUnsetMatchingSystem && resolved === sys) {
|
|
75
|
+
window.localStorage.removeItem("bb-theme");
|
|
76
|
+
stored = null;
|
|
77
|
+
} else {
|
|
78
|
+
window.localStorage.setItem("bb-theme", resolved);
|
|
79
|
+
}
|
|
80
|
+
} catch {}
|
|
81
|
+
setThemeState(resolved);
|
|
82
|
+
syncDocumentTheme(resolved, stored);
|
|
83
|
+
if (typeof window !== "undefined") {
|
|
84
|
+
window.dispatchEvent(new CustomEvent("bluebird:theme", { detail: { theme: resolved, stored } }));
|
|
85
|
+
}
|
|
86
|
+
}, []);
|
|
87
|
+
|
|
88
|
+
const applyTheme = useCallback((next) => commitTheme(next, false), [commitTheme]);
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
let saved = null;
|
|
92
|
+
try {
|
|
93
|
+
saved = window.localStorage.getItem("bb-theme");
|
|
94
|
+
} catch {}
|
|
95
|
+
syncDocumentTheme(theme, saved === "light" || saved === "dark" ? saved : null);
|
|
96
|
+
}, [theme]);
|
|
97
|
+
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
if (typeof window === "undefined") return;
|
|
100
|
+
const onSync = (e) => {
|
|
101
|
+
const next = e.detail?.theme;
|
|
102
|
+
if (next === "light" || next === "dark") setThemeState(next);
|
|
103
|
+
};
|
|
104
|
+
const onStorage = (e) => {
|
|
105
|
+
if (e.key !== "bb-theme") return;
|
|
106
|
+
const resolved = e.newValue === "light" || e.newValue === "dark" ? e.newValue : getSystemScheme();
|
|
107
|
+
setThemeState(resolved);
|
|
108
|
+
syncDocumentTheme(resolved, e.newValue);
|
|
109
|
+
};
|
|
110
|
+
const mq = window.matchMedia?.("(prefers-color-scheme: dark)");
|
|
111
|
+
const onMediaChange = () => {
|
|
112
|
+
let saved = null;
|
|
113
|
+
try {
|
|
114
|
+
saved = window.localStorage.getItem("bb-theme");
|
|
115
|
+
} catch {}
|
|
116
|
+
if (saved !== "light" && saved !== "dark") {
|
|
117
|
+
const resolved = mq?.matches ? "dark" : "light";
|
|
118
|
+
setThemeState(resolved);
|
|
119
|
+
syncDocumentTheme(resolved, null);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
window.addEventListener("bluebird:theme", onSync);
|
|
123
|
+
window.addEventListener("storage", onStorage);
|
|
124
|
+
mq?.addEventListener?.("change", onMediaChange);
|
|
125
|
+
return () => {
|
|
126
|
+
window.removeEventListener("bluebird:theme", onSync);
|
|
127
|
+
window.removeEventListener("storage", onStorage);
|
|
128
|
+
mq?.removeEventListener?.("change", onMediaChange);
|
|
129
|
+
};
|
|
130
|
+
}, []);
|
|
131
|
+
|
|
132
|
+
const toggleTheme = useCallback(() => {
|
|
133
|
+
commitTheme(theme === "dark" ? "light" : "dark", true);
|
|
134
|
+
}, [theme, commitTheme]);
|
|
135
|
+
|
|
136
|
+
return { theme, isDark: theme === "dark", setTheme: applyTheme, toggleTheme };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const ICON_PROPS = {
|
|
140
|
+
viewBox: "0 0 24 24",
|
|
141
|
+
fill: "none",
|
|
142
|
+
stroke: "currentColor",
|
|
143
|
+
strokeWidth: 2,
|
|
144
|
+
strokeLinecap: "round",
|
|
145
|
+
strokeLinejoin: "round",
|
|
146
|
+
"aria-hidden": "true",
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Sun icon (shown in dark mode to switch to light).
|
|
151
|
+
* @returns {import('react').ReactElement}
|
|
152
|
+
*/
|
|
153
|
+
function SunIcon() {
|
|
154
|
+
return h(
|
|
155
|
+
"svg",
|
|
156
|
+
ICON_PROPS,
|
|
157
|
+
h("circle", { cx: 12, cy: 12, r: 4 }),
|
|
158
|
+
h("path", { d: "M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41" })
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Moon icon (shown in light mode to switch to dark).
|
|
164
|
+
* @returns {import('react').ReactElement}
|
|
165
|
+
*/
|
|
166
|
+
function MoonIcon() {
|
|
167
|
+
return h("svg", ICON_PROPS, h("path", { d: "M20.985 12.486a9 9 0 1 1-9.473-9.472c.405-.022.617.46.402.803a6 6 0 0 0 8.268 8.268c.344-.215.825-.004.803.401" }));
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Close (x) icon for dialogs, drawers, and toasts.
|
|
172
|
+
* @returns {import('react').ReactElement}
|
|
173
|
+
*/
|
|
174
|
+
function CloseIcon() {
|
|
175
|
+
return h("svg", ICON_PROPS, h("path", { d: "M18 6 6 18M6 6l12 12" }));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Directional chevron icon for carousel controls.
|
|
180
|
+
* @param {{ direction: "prev"|"next" }} props
|
|
181
|
+
* @returns {import('react').ReactElement}
|
|
182
|
+
*/
|
|
183
|
+
function ChevronIcon({ direction }) {
|
|
184
|
+
return h(
|
|
185
|
+
"svg",
|
|
186
|
+
ICON_PROPS,
|
|
187
|
+
h("path", { d: direction === "prev" ? "m15 18-6-6 6-6" : "m9 18 6-6-6-6" })
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Accessible dark/light theme switcher (icon button).
|
|
193
|
+
* @param {{ className?: string }} [props]
|
|
194
|
+
*/
|
|
195
|
+
export function ThemeToggle({ className } = {}) {
|
|
196
|
+
const { isDark, toggleTheme } = useTheme();
|
|
197
|
+
const i18n = useLocale();
|
|
198
|
+
const label = i18n.t(isDark ? "theme.toLight" : "theme.toDark");
|
|
199
|
+
return h(
|
|
200
|
+
"button",
|
|
201
|
+
{
|
|
202
|
+
type: "button",
|
|
203
|
+
onClick: toggleTheme,
|
|
204
|
+
"aria-label": label,
|
|
205
|
+
title: label,
|
|
206
|
+
className: cn("btn btn-ghost btn-icon btn-sm text-muted-foreground hover:text-foreground", className),
|
|
207
|
+
},
|
|
208
|
+
isDark ? h(SunIcon) : h(MoonIcon)
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Animated loading spinner.
|
|
214
|
+
* @param {{ className?: string }} [props]
|
|
215
|
+
*/
|
|
216
|
+
export function Spinner({ className } = {}) {
|
|
217
|
+
return h(
|
|
218
|
+
"svg",
|
|
219
|
+
{ className: cn("size-4 animate-spin", className), viewBox: "0 0 24 24", fill: "none", "aria-hidden": "true" },
|
|
220
|
+
h("circle", { cx: 12, cy: 12, r: 10, stroke: "currentColor", strokeWidth: 4, className: "opacity-25" }),
|
|
221
|
+
h("path", { d: "M4 12a8 8 0 018-8", stroke: "currentColor", strokeWidth: 4, strokeLinecap: "round", className: "opacity-75" })
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Skeleton placeholder block.
|
|
227
|
+
* @param {{ className?: string, style?: Object }} [props]
|
|
228
|
+
*/
|
|
229
|
+
export function Skeleton({ className, style } = {}) {
|
|
230
|
+
return h("div", {
|
|
231
|
+
className: cn("animate-pulse rounded-xl bg-muted", className),
|
|
232
|
+
style,
|
|
233
|
+
"aria-hidden": "true",
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Full-width page skeleton placeholder.
|
|
239
|
+
*/
|
|
240
|
+
export function PageSkeleton() {
|
|
241
|
+
return h(
|
|
242
|
+
"div",
|
|
243
|
+
{ className: "w-full space-y-6 py-2", role: "status", "aria-live": "polite", "aria-busy": "true" },
|
|
244
|
+
h("span", { className: "sr-only" }, translate("loading")),
|
|
245
|
+
h(Skeleton, { className: "h-9 w-2/3 max-w-md" }),
|
|
246
|
+
h(Skeleton, { className: "h-4 w-full max-w-2xl" }),
|
|
247
|
+
h(Skeleton, { className: "h-4 w-5/6 max-w-xl" }),
|
|
248
|
+
h(
|
|
249
|
+
"div",
|
|
250
|
+
{ className: "grid gap-4 pt-2 sm:grid-cols-2 lg:grid-cols-3" },
|
|
251
|
+
h(Skeleton, { className: "h-32" }),
|
|
252
|
+
h(Skeleton, { className: "h-32" }),
|
|
253
|
+
h(Skeleton, { className: "h-32 hidden lg:block" })
|
|
254
|
+
)
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Thin top progress bar driven explicitly or automatically by React Router navigation state.
|
|
260
|
+
* @param {{ active?: boolean }} [props]
|
|
261
|
+
*/
|
|
262
|
+
export function ProgressBar({ active } = {}) {
|
|
263
|
+
let routerActive = false;
|
|
264
|
+
try {
|
|
265
|
+
const nav = useNavigation();
|
|
266
|
+
routerActive = nav.state !== "idle";
|
|
267
|
+
} catch {}
|
|
268
|
+
const isActive = active !== undefined ? Boolean(active) : routerActive;
|
|
269
|
+
|
|
270
|
+
return h(
|
|
271
|
+
"div",
|
|
272
|
+
{
|
|
273
|
+
className: cn(
|
|
274
|
+
"pointer-events-none fixed inset-x-0 top-0 z-[100] h-0.5 transition-opacity duration-300",
|
|
275
|
+
isActive ? "opacity-100" : "opacity-0"
|
|
276
|
+
),
|
|
277
|
+
"aria-hidden": "true",
|
|
278
|
+
},
|
|
279
|
+
h("div", {
|
|
280
|
+
className: cn(
|
|
281
|
+
"h-full rounded-r-full bg-primary transition-[width] ease-out",
|
|
282
|
+
isActive ? "w-4/5 duration-[2000ms]" : "w-full duration-200"
|
|
283
|
+
),
|
|
284
|
+
})
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Client-side navigation link wrapping React Router `NavLink` with `href`/`to` compatibility.
|
|
290
|
+
* External links and `reloadDocument` links render standard anchors.
|
|
291
|
+
* @param {Object} props
|
|
292
|
+
* @param {string} [props.href]
|
|
293
|
+
* @param {string} [props.to]
|
|
294
|
+
* @param {string} [props.activeClassName]
|
|
295
|
+
* @param {string} [props.inactiveClassName]
|
|
296
|
+
* @param {boolean} [props.exact=false]
|
|
297
|
+
*/
|
|
298
|
+
export const Link = forwardRef(function Link(props, ref) {
|
|
299
|
+
const {
|
|
300
|
+
href,
|
|
301
|
+
to,
|
|
302
|
+
activeClassName,
|
|
303
|
+
inactiveClassName,
|
|
304
|
+
exact = false,
|
|
305
|
+
className,
|
|
306
|
+
children,
|
|
307
|
+
...rest
|
|
308
|
+
} = props;
|
|
309
|
+
const target = to ?? href ?? "/";
|
|
310
|
+
const isExternal =
|
|
311
|
+
typeof target === "string" && (/^(https?:|mailto:|tel:)/i.test(target) || props.target === "_blank");
|
|
312
|
+
|
|
313
|
+
if (isExternal) {
|
|
314
|
+
return h("a", { ...rest, ref, href: target, className: cn(className, inactiveClassName) }, children);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return h(
|
|
318
|
+
NavLink,
|
|
319
|
+
{
|
|
320
|
+
...rest,
|
|
321
|
+
ref,
|
|
322
|
+
to: target,
|
|
323
|
+
end: exact || target === "/",
|
|
324
|
+
className: ({ isActive }) => cn(className, isActive ? activeClassName : inactiveClassName),
|
|
325
|
+
},
|
|
326
|
+
children
|
|
327
|
+
);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
const BUTTON_VARIANTS = new Set(["primary", "secondary", "outline", "ghost", "danger", "link"]);
|
|
331
|
+
const BUTTON_SIZES = { sm: "btn-sm", md: "", lg: "btn-lg", icon: "btn-icon" };
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Returns the button classes for a variant and size, to style links or any element like a `<Button>`
|
|
335
|
+
* (e.g. `<Link to="/new" className={buttonVariants({ variant: "outline" })}>`).
|
|
336
|
+
* @param {{ variant?: "primary"|"secondary"|"outline"|"ghost"|"danger"|"link", size?: "sm"|"md"|"lg"|"icon", className?: string }} [options]
|
|
337
|
+
* @returns {string}
|
|
338
|
+
*/
|
|
339
|
+
export function buttonVariants({ variant = "primary", size = "md", className } = {}) {
|
|
340
|
+
return cn("btn", `btn-${BUTTON_VARIANTS.has(variant) ? variant : "primary"}`, BUTTON_SIZES[size], className);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Button component. When `url` is provided, sends a REST request via `api()` with pending state.
|
|
345
|
+
* Use `primary` for the main action of a view, `secondary`/`outline`/`ghost` for the rest, and `danger` for destructive actions.
|
|
346
|
+
* @param {Object} props
|
|
347
|
+
* @param {"primary"|"secondary"|"outline"|"ghost"|"danger"|"link"} [props.variant="primary"]
|
|
348
|
+
* @param {"sm"|"md"|"lg"|"icon"} [props.size="md"]
|
|
349
|
+
* @param {boolean} [props.loading]
|
|
350
|
+
* @param {string} [props.loadingText]
|
|
351
|
+
* @param {string} [props.url]
|
|
352
|
+
* @param {string} [props.method="POST"]
|
|
353
|
+
* @param {Object} [props.payload]
|
|
354
|
+
* @param {string} [props.confirm]
|
|
355
|
+
* @param {(data: any) => void} [props.onSuccess]
|
|
356
|
+
* @param {(error: Error) => void} [props.onError]
|
|
357
|
+
*/
|
|
358
|
+
export const Button = forwardRef(function Button(props, ref) {
|
|
359
|
+
const {
|
|
360
|
+
variant = "primary",
|
|
361
|
+
size = "md",
|
|
362
|
+
loading,
|
|
363
|
+
loadingText,
|
|
364
|
+
url,
|
|
365
|
+
method = "POST",
|
|
366
|
+
payload,
|
|
367
|
+
confirm,
|
|
368
|
+
onSuccess,
|
|
369
|
+
onError,
|
|
370
|
+
onClick,
|
|
371
|
+
className,
|
|
372
|
+
disabled,
|
|
373
|
+
type,
|
|
374
|
+
children,
|
|
375
|
+
...rest
|
|
376
|
+
} = props;
|
|
377
|
+
const [busy, setBusy] = useState(false);
|
|
378
|
+
const isLoading = Boolean(loading || busy);
|
|
379
|
+
|
|
380
|
+
const handleClick = async (e) => {
|
|
381
|
+
onClick?.(e);
|
|
382
|
+
if (e.defaultPrevented || !url) return;
|
|
383
|
+
if (confirm && typeof window !== "undefined" && !window.confirm(confirm)) return;
|
|
384
|
+
setBusy(true);
|
|
385
|
+
try {
|
|
386
|
+
const data = await api(url, { method, body: payload });
|
|
387
|
+
onSuccess?.(data);
|
|
388
|
+
} catch (err) {
|
|
389
|
+
if (onError) onError(err);
|
|
390
|
+
else toast({ type: "error", title: err.message || translate("request.failed") });
|
|
391
|
+
} finally {
|
|
392
|
+
setBusy(false);
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
return h(
|
|
397
|
+
"button",
|
|
398
|
+
{
|
|
399
|
+
...rest,
|
|
400
|
+
ref,
|
|
401
|
+
type: type || "button",
|
|
402
|
+
disabled: disabled || isLoading,
|
|
403
|
+
"aria-busy": isLoading || undefined,
|
|
404
|
+
onClick: handleClick,
|
|
405
|
+
className: buttonVariants({ variant, size, className }),
|
|
406
|
+
},
|
|
407
|
+
isLoading ? h(Spinner, null) : null,
|
|
408
|
+
isLoading && loadingText ? loadingText : children
|
|
409
|
+
);
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
const FormContext = createContext({ errors: {}, pending: false });
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Returns the state of the closest `<Form>`: `{ errors, pending }`.
|
|
416
|
+
* @returns {{ errors: Record<string, string>, pending: boolean }}
|
|
417
|
+
*/
|
|
418
|
+
export function useFormState() {
|
|
419
|
+
return useContext(FormContext);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Collects form fields into a plain object.
|
|
424
|
+
* @param {HTMLFormElement} form
|
|
425
|
+
* @returns {Record<string, any>}
|
|
426
|
+
*/
|
|
427
|
+
function formToObject(form) {
|
|
428
|
+
const out = {};
|
|
429
|
+
for (const el of Array.from(form.elements)) {
|
|
430
|
+
if (!el.name || el.disabled || el.type === "submit" || el.type === "button" || el.type === "file") continue;
|
|
431
|
+
if ((el.type === "checkbox" || el.type === "radio") && !el.checked) {
|
|
432
|
+
if (el.type === "checkbox" && !el.hasAttribute("value") && !(el.name in out)) out[el.name] = false;
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
const value = el.type === "checkbox" && !el.hasAttribute("value") ? true : el.value;
|
|
436
|
+
if (el.name in out && el.type !== "radio") {
|
|
437
|
+
out[el.name] = [].concat(out[el.name], value);
|
|
438
|
+
} else {
|
|
439
|
+
out[el.name] = value;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return out;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Form bound to a REST endpoint (`url`) or custom `onSubmitRequest(values)` handler.
|
|
447
|
+
* Injects the CSRF token automatically and exposes field errors to `<Input>` / `<Textarea>` / `<Select>`.
|
|
448
|
+
* `className` applies to the wrapper around the fields, so layout utilities like `space-y-4` or `grid` work as expected.
|
|
449
|
+
* @param {Object} props
|
|
450
|
+
* @param {string} [props.url]
|
|
451
|
+
* @param {string} [props.method="POST"]
|
|
452
|
+
* @param {Object|((values: Object) => Object)} [props.data]
|
|
453
|
+
* @param {(values: Object) => Promise<any>} [props.onSubmitRequest]
|
|
454
|
+
* @param {(values: Object) => boolean|void} [props.onBefore]
|
|
455
|
+
* @param {(data: any, values: Object) => void} [props.onSuccess]
|
|
456
|
+
* @param {(error: Error) => void} [props.onError]
|
|
457
|
+
* @param {boolean} [props.resetOnSuccess=false]
|
|
458
|
+
* @param {string} [props.successMessage]
|
|
459
|
+
*/
|
|
460
|
+
export function Form(props) {
|
|
461
|
+
const {
|
|
462
|
+
url,
|
|
463
|
+
method = "POST",
|
|
464
|
+
data,
|
|
465
|
+
onSubmitRequest,
|
|
466
|
+
onBefore,
|
|
467
|
+
onSuccess,
|
|
468
|
+
onError,
|
|
469
|
+
resetOnSuccess = false,
|
|
470
|
+
successMessage,
|
|
471
|
+
className,
|
|
472
|
+
children,
|
|
473
|
+
...rest
|
|
474
|
+
} = props;
|
|
475
|
+
const [pending, setPending] = useState(false);
|
|
476
|
+
const [errors, setErrors] = useState({});
|
|
477
|
+
|
|
478
|
+
const onSubmit = async (e) => {
|
|
479
|
+
e.preventDefault();
|
|
480
|
+
const form = e.currentTarget;
|
|
481
|
+
let values = formToObject(form);
|
|
482
|
+
const extra = typeof data === "function" ? data(values) : data;
|
|
483
|
+
if (extra && typeof extra === "object") values = { ...values, ...extra };
|
|
484
|
+
if (onBefore && onBefore(values) === false) return;
|
|
485
|
+
const hasFiles = Array.from(form.elements).some((el) => el.type === "file" && el.files?.length);
|
|
486
|
+
|
|
487
|
+
setPending(true);
|
|
488
|
+
setErrors({});
|
|
489
|
+
try {
|
|
490
|
+
let body = values;
|
|
491
|
+
if (hasFiles) {
|
|
492
|
+
body = new FormData(form);
|
|
493
|
+
for (const [k, v] of Object.entries(extra || {})) body.set(k, v);
|
|
494
|
+
}
|
|
495
|
+
const result = onSubmitRequest
|
|
496
|
+
? await onSubmitRequest(body)
|
|
497
|
+
: await api(url || "", { method, body });
|
|
498
|
+
if (resetOnSuccess) form.reset();
|
|
499
|
+
if (successMessage) toast({ type: "success", title: successMessage });
|
|
500
|
+
onSuccess?.(result, values);
|
|
501
|
+
} catch (err) {
|
|
502
|
+
if (err instanceof ApiError && err.errors && typeof err.errors === "object") {
|
|
503
|
+
setErrors(err.errors);
|
|
504
|
+
}
|
|
505
|
+
if (onError) onError(err);
|
|
506
|
+
else toast({ type: "error", title: err.message || translate("request.failed") });
|
|
507
|
+
} finally {
|
|
508
|
+
setPending(false);
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
return h(
|
|
513
|
+
FormContext.Provider,
|
|
514
|
+
{ value: { errors, pending } },
|
|
515
|
+
h(
|
|
516
|
+
"form",
|
|
517
|
+
{
|
|
518
|
+
...rest,
|
|
519
|
+
method: method.toLowerCase() === "get" ? "get" : "post",
|
|
520
|
+
action: url,
|
|
521
|
+
onSubmit,
|
|
522
|
+
"aria-busy": pending || undefined,
|
|
523
|
+
},
|
|
524
|
+
h("input", { type: "hidden", name: "_csrf", value: getCsrfToken() }),
|
|
525
|
+
h("fieldset", { disabled: pending, className: cn("m-0 min-w-0 border-0 p-0", className) }, children)
|
|
526
|
+
)
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Submit button for `<Form>` showing a spinner and `loadingText` while the form is pending.
|
|
532
|
+
* @param {Object} props
|
|
533
|
+
*/
|
|
534
|
+
export function SubmitButton({ loadingText, children, ...rest }) {
|
|
535
|
+
const { pending } = useFormState();
|
|
536
|
+
return h(Button, { ...rest, type: "submit", loading: pending, loadingText }, children);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Wraps a form control with its label, hint above the control, and error message below.
|
|
541
|
+
* @param {{ id: string, label?: any, hint?: any, error?: string, className?: string, children: any }} props
|
|
542
|
+
*/
|
|
543
|
+
function Field({ id, label, hint, error, className, children }) {
|
|
544
|
+
return h(
|
|
545
|
+
"div",
|
|
546
|
+
{ className: cn("space-y-1.5", className) },
|
|
547
|
+
label ? h("label", { htmlFor: id, className: "field-label" }, label) : null,
|
|
548
|
+
hint ? h("p", { id: `${id}-hint`, className: "field-hint" }, hint) : null,
|
|
549
|
+
children,
|
|
550
|
+
error ? h("p", { id: `${id}-error`, className: "field-error" }, error) : null
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Builds a form control component (`input`, `textarea`, `select`) with label and `<Form>` error binding.
|
|
556
|
+
* @param {string} tag
|
|
557
|
+
* @returns {Function}
|
|
558
|
+
*/
|
|
559
|
+
function control(tag) {
|
|
560
|
+
return forwardRef(function FormControl(props, ref) {
|
|
561
|
+
const { label, hint, error, className, wrapperClassName, children, onBlur, onInput, ...rest } = props;
|
|
562
|
+
const autoId = useId();
|
|
563
|
+
const id = rest.id || autoId;
|
|
564
|
+
const { errors } = useFormState();
|
|
565
|
+
const [clientError, setClientError] = useState("");
|
|
566
|
+
const serverMessage = error || (rest.name ? errors?.[rest.name] : undefined);
|
|
567
|
+
const serverText = Array.isArray(serverMessage) ? serverMessage[0] : serverMessage;
|
|
568
|
+
const activeError = serverText || clientError || "";
|
|
569
|
+
const describedBy = [hint ? `${id}-hint` : null, activeError ? `${id}-error` : null].filter(Boolean).join(" ") || undefined;
|
|
570
|
+
|
|
571
|
+
const handleBlur = (e) => {
|
|
572
|
+
onBlur?.(e);
|
|
573
|
+
const el = e.currentTarget;
|
|
574
|
+
if (el.checkValidity && !el.validity.valid) {
|
|
575
|
+
setClientError(el.validationMessage || translate("form.invalid"));
|
|
576
|
+
} else {
|
|
577
|
+
setClientError("");
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
const handleInput = (e) => {
|
|
582
|
+
onInput?.(e);
|
|
583
|
+
if (clientError && e.currentTarget.checkValidity?.()) {
|
|
584
|
+
setClientError("");
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
return h(
|
|
589
|
+
Field,
|
|
590
|
+
{ id, label, hint, error: activeError, className: wrapperClassName },
|
|
591
|
+
h(
|
|
592
|
+
tag,
|
|
593
|
+
{
|
|
594
|
+
...rest,
|
|
595
|
+
ref,
|
|
596
|
+
id,
|
|
597
|
+
onBlur: handleBlur,
|
|
598
|
+
onInput: handleInput,
|
|
599
|
+
"aria-invalid": activeError ? "true" : "false",
|
|
600
|
+
"aria-describedby": describedBy,
|
|
601
|
+
className: cn("field", tag === "textarea" && "min-h-24", tag === "select" && "h-9 py-0", className),
|
|
602
|
+
},
|
|
603
|
+
children
|
|
604
|
+
)
|
|
605
|
+
);
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
export const Input = control("input");
|
|
610
|
+
export const Textarea = control("textarea");
|
|
611
|
+
export const Select = control("select");
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Card container with light/dark Material elevation and optional translucent glass surface.
|
|
615
|
+
* @param {{ title?: any, description?: any, footer?: any, variant?: "default"|"glass", className?: string, children?: any }} props
|
|
616
|
+
*/
|
|
617
|
+
export function Card({ title, description, footer, variant = "default", className, children, ...rest }) {
|
|
618
|
+
return h(
|
|
619
|
+
"section",
|
|
620
|
+
{
|
|
621
|
+
...rest,
|
|
622
|
+
className: cn(variant === "glass" ? "glass-card" : "card", className),
|
|
623
|
+
},
|
|
624
|
+
title ? h("h3", { className: "card-title" }, title) : null,
|
|
625
|
+
description ? h("p", { className: "card-description" }, description) : null,
|
|
626
|
+
children ? h("div", { className: title || description ? "mt-5" : undefined }, children) : null,
|
|
627
|
+
footer ? h("div", { className: "mt-6 flex items-center justify-end gap-2" }, footer) : null
|
|
628
|
+
);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
const TONE_DOTS = {
|
|
632
|
+
info: "bg-info",
|
|
633
|
+
success: "bg-success",
|
|
634
|
+
warning: "bg-warning",
|
|
635
|
+
danger: "bg-destructive",
|
|
636
|
+
error: "bg-destructive",
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
const TONE_BADGES = {
|
|
640
|
+
info: "badge-tone badge-tone-info",
|
|
641
|
+
success: "badge-tone badge-tone-success",
|
|
642
|
+
warning: "badge-tone badge-tone-warning",
|
|
643
|
+
danger: "badge-tone badge-tone-danger",
|
|
644
|
+
error: "badge-tone badge-tone-error",
|
|
645
|
+
};
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* Neutral or softly tinted status badge with a colored indicator dot.
|
|
649
|
+
* @param {{ tone?: "default"|"info"|"success"|"warning"|"danger"|"error", soft?: boolean, className?: string, children?: any }} props
|
|
650
|
+
*/
|
|
651
|
+
export function Badge({ tone = "default", soft = false, className, children }) {
|
|
652
|
+
const baseClass = soft && TONE_BADGES[tone] ? TONE_BADGES[tone] : "badge";
|
|
653
|
+
return h(
|
|
654
|
+
"span",
|
|
655
|
+
{ className: cn(baseClass, className) },
|
|
656
|
+
TONE_DOTS[tone] ? h("span", { className: cn("badge-dot", TONE_DOTS[tone]), "aria-hidden": "true" }) : null,
|
|
657
|
+
children
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* Accessible native scroll-snap carousel with Previous/Next controls, pill markers, keyboard support, and polite status announcements.
|
|
663
|
+
* @param {Object} props
|
|
664
|
+
* @param {Array<{ title?: any, description?: any, badge?: any, content?: any }>} props.slides
|
|
665
|
+
* @param {number} [props.autoPlayMs=0]
|
|
666
|
+
* @param {boolean} [props.loop=true]
|
|
667
|
+
* @param {string} [props.label] - Accessible name (defaults to the translated "Featured showcase").
|
|
668
|
+
* @param {string} [props.className]
|
|
669
|
+
*/
|
|
670
|
+
export function Carousel({ slides = [], autoPlayMs = 0, loop = true, label, className }) {
|
|
671
|
+
const i18n = useLocale();
|
|
672
|
+
const total = slides.length;
|
|
673
|
+
const trackRef = useRef(null);
|
|
674
|
+
const prevBtnRef = useRef(null);
|
|
675
|
+
const nextBtnRef = useRef(null);
|
|
676
|
+
const pendingRef = useRef(null);
|
|
677
|
+
const rafRef = useRef(null);
|
|
678
|
+
const debounceRef = useRef(null);
|
|
679
|
+
const settledRef = useRef(0);
|
|
680
|
+
const userInitiatedRef = useRef(false);
|
|
681
|
+
const [index, setIndex] = useState(0);
|
|
682
|
+
const [paused, setPaused] = useState(false);
|
|
683
|
+
const [announcement, setAnnouncement] = useState("");
|
|
684
|
+
|
|
685
|
+
const getNearestIndex = useCallback(() => {
|
|
686
|
+
const track = trackRef.current;
|
|
687
|
+
if (!track || !track.children.length) return 0;
|
|
688
|
+
const center = track.scrollLeft + track.clientWidth / 2;
|
|
689
|
+
let bestIdx = 0;
|
|
690
|
+
let bestDist = Infinity;
|
|
691
|
+
Array.from(track.children).forEach((child, i) => {
|
|
692
|
+
const childCenter = child.offsetLeft + child.offsetWidth / 2;
|
|
693
|
+
const dist = Math.abs(childCenter - center);
|
|
694
|
+
if (dist < bestDist) {
|
|
695
|
+
bestDist = dist;
|
|
696
|
+
bestIdx = i;
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
return bestIdx;
|
|
700
|
+
}, []);
|
|
701
|
+
|
|
702
|
+
const scrollToSlide = useCallback(
|
|
703
|
+
(rawIdx, markUserInitiated = true) => {
|
|
704
|
+
const track = trackRef.current;
|
|
705
|
+
if (!track || total === 0) return;
|
|
706
|
+
const base = pendingRef.current !== null ? pendingRef.current : index;
|
|
707
|
+
const resolved = loop
|
|
708
|
+
? ((rawIdx % total) + total) % total
|
|
709
|
+
: Math.max(0, Math.min(total - 1, rawIdx));
|
|
710
|
+
if (markUserInitiated) userInitiatedRef.current = true;
|
|
711
|
+
pendingRef.current = resolved;
|
|
712
|
+
setIndex(resolved);
|
|
713
|
+
const slideEl = track.children[resolved];
|
|
714
|
+
if (!slideEl) return;
|
|
715
|
+
const reduced =
|
|
716
|
+
typeof window !== "undefined" && window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
|
|
717
|
+
const targetLeft = Math.max(0, slideEl.offsetLeft - (track.clientWidth - slideEl.offsetWidth) / 2);
|
|
718
|
+
if (Math.abs(track.scrollLeft - targetLeft) < 1) {
|
|
719
|
+
pendingRef.current = null;
|
|
720
|
+
return;
|
|
721
|
+
}
|
|
722
|
+
track.scrollTo({ left: targetLeft, behavior: reduced ? "auto" : "smooth" });
|
|
723
|
+
if (resolved === base && !markUserInitiated) pendingRef.current = null;
|
|
724
|
+
},
|
|
725
|
+
[index, loop, total]
|
|
726
|
+
);
|
|
727
|
+
|
|
728
|
+
const settleScroll = useCallback(() => {
|
|
729
|
+
const settled = getNearestIndex();
|
|
730
|
+
pendingRef.current = null;
|
|
731
|
+
setIndex(settled);
|
|
732
|
+
if (userInitiatedRef.current && settled !== settledRef.current) {
|
|
733
|
+
setAnnouncement(translate("carousel.status", { n: settled + 1, total }));
|
|
734
|
+
}
|
|
735
|
+
settledRef.current = settled;
|
|
736
|
+
userInitiatedRef.current = false;
|
|
737
|
+
if (!loop && total > 1) {
|
|
738
|
+
if (settled === 0 && typeof document !== "undefined" && document.activeElement === prevBtnRef.current) {
|
|
739
|
+
nextBtnRef.current?.focus();
|
|
740
|
+
} else if (
|
|
741
|
+
settled === total - 1 &&
|
|
742
|
+
typeof document !== "undefined" &&
|
|
743
|
+
document.activeElement === nextBtnRef.current
|
|
744
|
+
) {
|
|
745
|
+
prevBtnRef.current?.focus();
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}, [getNearestIndex, loop, total]);
|
|
749
|
+
|
|
750
|
+
useEffect(() => {
|
|
751
|
+
const track = trackRef.current;
|
|
752
|
+
if (!track || total <= 1) return;
|
|
753
|
+
const supportsScrollEnd = "onscrollend" in window;
|
|
754
|
+
const onScroll = () => {
|
|
755
|
+
if (pendingRef.current === null) {
|
|
756
|
+
if (!rafRef.current) {
|
|
757
|
+
rafRef.current = window.requestAnimationFrame(() => {
|
|
758
|
+
rafRef.current = null;
|
|
759
|
+
const live = getNearestIndex();
|
|
760
|
+
setIndex((prevIdx) => (prevIdx === live ? prevIdx : live));
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
if (!supportsScrollEnd) {
|
|
765
|
+
clearTimeout(debounceRef.current);
|
|
766
|
+
debounceRef.current = setTimeout(settleScroll, 140);
|
|
767
|
+
}
|
|
768
|
+
};
|
|
769
|
+
const onScrollEnd = () => {
|
|
770
|
+
settleScroll();
|
|
771
|
+
};
|
|
772
|
+
track.addEventListener("scroll", onScroll, { passive: true });
|
|
773
|
+
if (supportsScrollEnd) track.addEventListener("scrollend", onScrollEnd);
|
|
774
|
+
return () => {
|
|
775
|
+
track.removeEventListener("scroll", onScroll);
|
|
776
|
+
if (supportsScrollEnd) track.removeEventListener("scrollend", onScrollEnd);
|
|
777
|
+
if (rafRef.current) window.cancelAnimationFrame(rafRef.current);
|
|
778
|
+
clearTimeout(debounceRef.current);
|
|
779
|
+
};
|
|
780
|
+
}, [getNearestIndex, settleScroll, total]);
|
|
781
|
+
|
|
782
|
+
useEffect(() => {
|
|
783
|
+
if (!autoPlayMs || total <= 1 || paused) return;
|
|
784
|
+
const timer = setInterval(() => {
|
|
785
|
+
scrollToSlide(index + 1, false);
|
|
786
|
+
}, autoPlayMs);
|
|
787
|
+
return () => clearInterval(timer);
|
|
788
|
+
}, [autoPlayMs, index, paused, scrollToSlide, total]);
|
|
789
|
+
|
|
790
|
+
if (total === 0) return null;
|
|
791
|
+
|
|
792
|
+
const baseIndex = pendingRef.current !== null ? pendingRef.current : index;
|
|
793
|
+
|
|
794
|
+
return h(
|
|
795
|
+
"section",
|
|
796
|
+
{
|
|
797
|
+
className: cn("card relative overflow-hidden p-0", className),
|
|
798
|
+
onMouseEnter: () => setPaused(true),
|
|
799
|
+
onMouseLeave: () => setPaused(false),
|
|
800
|
+
onFocusCapture: () => setPaused(true),
|
|
801
|
+
onBlurCapture: (e) => {
|
|
802
|
+
if (!e.currentTarget.contains(e.relatedTarget)) setPaused(false);
|
|
803
|
+
},
|
|
804
|
+
role: "region",
|
|
805
|
+
"aria-roledescription": i18n.t("carousel.role"),
|
|
806
|
+
"aria-label": label || i18n.t("carousel.label"),
|
|
807
|
+
},
|
|
808
|
+
h(
|
|
809
|
+
"div",
|
|
810
|
+
{
|
|
811
|
+
ref: trackRef,
|
|
812
|
+
className: "bb-carousel-track",
|
|
813
|
+
tabIndex: 0,
|
|
814
|
+
role: "group",
|
|
815
|
+
"aria-label": i18n.t("carousel.slides"),
|
|
816
|
+
},
|
|
817
|
+
slides.map((slide, idx) =>
|
|
818
|
+
h(
|
|
819
|
+
"div",
|
|
820
|
+
{
|
|
821
|
+
key: idx,
|
|
822
|
+
role: "group",
|
|
823
|
+
"aria-roledescription": i18n.t("carousel.slide"),
|
|
824
|
+
"aria-label": i18n.t("carousel.position", { n: idx + 1, total }),
|
|
825
|
+
className: "bb-carousel-slide p-6 md:p-8",
|
|
826
|
+
},
|
|
827
|
+
slide.badge ? h(Badge, { className: "mb-3" }, slide.badge) : null,
|
|
828
|
+
slide.title ? h("h3", { className: "text-xl font-semibold tracking-tight text-foreground" }, slide.title) : null,
|
|
829
|
+
slide.description ? h("p", { className: "mt-2 max-w-2xl text-sm leading-relaxed text-muted-foreground" }, slide.description) : null,
|
|
830
|
+
slide.content ? h("div", { className: "mt-4" }, slide.content) : null
|
|
831
|
+
)
|
|
832
|
+
)
|
|
833
|
+
),
|
|
834
|
+
h("p", { className: "visually-hidden", role: "status", "aria-live": "polite" }, announcement),
|
|
835
|
+
total > 1
|
|
836
|
+
? h(
|
|
837
|
+
"nav",
|
|
838
|
+
{
|
|
839
|
+
"aria-label": i18n.t("carousel.choose"),
|
|
840
|
+
className: "flex items-center justify-between border-t border-border/80 bg-muted/25 px-5 py-2.5",
|
|
841
|
+
},
|
|
842
|
+
h(
|
|
843
|
+
"div",
|
|
844
|
+
{ className: "flex items-center gap-0.5" },
|
|
845
|
+
slides.map((_, idx) => {
|
|
846
|
+
const active = idx === index;
|
|
847
|
+
return h(
|
|
848
|
+
"button",
|
|
849
|
+
{
|
|
850
|
+
key: idx,
|
|
851
|
+
type: "button",
|
|
852
|
+
onClick: () => scrollToSlide(idx, true),
|
|
853
|
+
"aria-label": i18n.t("carousel.goTo", { n: idx + 1 }),
|
|
854
|
+
"aria-current": active ? "true" : undefined,
|
|
855
|
+
className: "bb-carousel-dot-hit group",
|
|
856
|
+
},
|
|
857
|
+
h("span", {
|
|
858
|
+
className: cn(
|
|
859
|
+
"bb-carousel-dot-pill",
|
|
860
|
+
active
|
|
861
|
+
? "w-6 bg-foreground shadow-2xs"
|
|
862
|
+
: "w-1.5 bg-muted-foreground/35 group-hover:bg-muted-foreground/65"
|
|
863
|
+
),
|
|
864
|
+
})
|
|
865
|
+
);
|
|
866
|
+
})
|
|
867
|
+
),
|
|
868
|
+
h(
|
|
869
|
+
"div",
|
|
870
|
+
{ className: "flex items-center gap-1.5" },
|
|
871
|
+
h(
|
|
872
|
+
Button,
|
|
873
|
+
{
|
|
874
|
+
ref: prevBtnRef,
|
|
875
|
+
size: "sm",
|
|
876
|
+
variant: "outline",
|
|
877
|
+
disabled: !loop && index === 0,
|
|
878
|
+
onClick: () => scrollToSlide(baseIndex - 1, true),
|
|
879
|
+
"aria-label": i18n.t("carousel.prev"),
|
|
880
|
+
},
|
|
881
|
+
h(ChevronIcon, { direction: "prev" }),
|
|
882
|
+
h("span", null, i18n.t("carousel.prevShort"))
|
|
883
|
+
),
|
|
884
|
+
h(
|
|
885
|
+
Button,
|
|
886
|
+
{
|
|
887
|
+
ref: nextBtnRef,
|
|
888
|
+
size: "sm",
|
|
889
|
+
variant: "outline",
|
|
890
|
+
disabled: !loop && index === total - 1,
|
|
891
|
+
onClick: () => scrollToSlide(baseIndex + 1, true),
|
|
892
|
+
"aria-label": i18n.t("carousel.next"),
|
|
893
|
+
},
|
|
894
|
+
h("span", null, i18n.t("carousel.nextShort")),
|
|
895
|
+
h(ChevronIcon, { direction: "next" })
|
|
896
|
+
)
|
|
897
|
+
)
|
|
898
|
+
)
|
|
899
|
+
: null
|
|
900
|
+
);
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
let cachedDisplayTransitionSupport;
|
|
904
|
+
|
|
905
|
+
/**
|
|
906
|
+
* Detects whether discrete CSS transitions on `display` are genuinely supported (guarding against Firefox bug 1882408).
|
|
907
|
+
* @returns {boolean}
|
|
908
|
+
*/
|
|
909
|
+
function canTransitionDisplay() {
|
|
910
|
+
if (cachedDisplayTransitionSupport !== undefined) return cachedDisplayTransitionSupport;
|
|
911
|
+
if (typeof window === "undefined" || !window.CSS?.supports?.("transition-behavior", "allow-discrete") || !document.body) {
|
|
912
|
+
return false;
|
|
913
|
+
}
|
|
914
|
+
const probe = document.createElement("div");
|
|
915
|
+
probe.style.cssText = "transition: display 1s allow-discrete !important; display: block;";
|
|
916
|
+
document.body.appendChild(probe);
|
|
917
|
+
window.getComputedStyle(probe).display;
|
|
918
|
+
probe.style.display = "none";
|
|
919
|
+
cachedDisplayTransitionSupport = window.getComputedStyle(probe).display === "block";
|
|
920
|
+
probe.remove();
|
|
921
|
+
return cachedDisplayTransitionSupport;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
/**
|
|
925
|
+
* Checks whether both `overlay` and discrete `display` transitions are supported cleanly on exit.
|
|
926
|
+
* @returns {boolean}
|
|
927
|
+
*/
|
|
928
|
+
function supportsTopLayerExit() {
|
|
929
|
+
if (typeof window === "undefined") return false;
|
|
930
|
+
return Boolean(window.CSS?.supports?.("overlay", "auto")) && canTransitionDisplay();
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
/**
|
|
934
|
+
* Animates a `<dialog>` out of the top layer when exit discrete transitions aren't supported natively, then closes it.
|
|
935
|
+
* @param {HTMLDialogElement} dialog
|
|
936
|
+
*/
|
|
937
|
+
async function closeTopLayerDialog(dialog) {
|
|
938
|
+
if (!dialog || !dialog.open) return;
|
|
939
|
+
if (!supportsTopLayerExit()) {
|
|
940
|
+
dialog.setAttribute("data-closing", "");
|
|
941
|
+
const animations = dialog.getAnimations ? dialog.getAnimations({ subtree: true }) : [];
|
|
942
|
+
if (animations.length > 0) {
|
|
943
|
+
await Promise.race([
|
|
944
|
+
Promise.allSettled(animations.map((a) => a.finished)),
|
|
945
|
+
new Promise((r) => setTimeout(r, 400)),
|
|
946
|
+
]);
|
|
947
|
+
}
|
|
948
|
+
if (!dialog.hasAttribute("data-closing")) return;
|
|
949
|
+
dialog.removeAttribute("data-closing");
|
|
950
|
+
}
|
|
951
|
+
if (dialog.open) dialog.close();
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
/**
|
|
955
|
+
* Wires native `<dialog>` open/close synchronization, light-dismiss fallback (`closedby="any"`), and animated `cancel`.
|
|
956
|
+
* @param {import('react').RefObject<HTMLDialogElement|null>} dialogRef
|
|
957
|
+
* @param {boolean} open
|
|
958
|
+
* @param {Function} onClose
|
|
959
|
+
*/
|
|
960
|
+
function useNativeDialog(dialogRef, open, onClose) {
|
|
961
|
+
const onCloseRef = useRef(onClose);
|
|
962
|
+
onCloseRef.current = onClose;
|
|
963
|
+
|
|
964
|
+
useEffect(() => {
|
|
965
|
+
const dialog = dialogRef.current;
|
|
966
|
+
if (!dialog) return;
|
|
967
|
+
if (open) {
|
|
968
|
+
dialog.removeAttribute("data-closing");
|
|
969
|
+
if (!dialog.open) {
|
|
970
|
+
if (typeof dialog.showModal === "function") dialog.showModal();
|
|
971
|
+
else dialog.setAttribute("open", "");
|
|
972
|
+
}
|
|
973
|
+
} else if (dialog.open) {
|
|
974
|
+
closeTopLayerDialog(dialog);
|
|
975
|
+
}
|
|
976
|
+
}, [dialogRef, open]);
|
|
977
|
+
|
|
978
|
+
useEffect(() => {
|
|
979
|
+
const dialog = dialogRef.current;
|
|
980
|
+
if (!dialog) return;
|
|
981
|
+
|
|
982
|
+
const onCancel = (e) => {
|
|
983
|
+
if (e.cancelable) {
|
|
984
|
+
e.preventDefault();
|
|
985
|
+
closeTopLayerDialog(dialog);
|
|
986
|
+
}
|
|
987
|
+
};
|
|
988
|
+
const onCloseEvent = () => {
|
|
989
|
+
dialog.removeAttribute("data-closing");
|
|
990
|
+
onCloseRef.current?.();
|
|
991
|
+
};
|
|
992
|
+
dialog.addEventListener("cancel", onCancel);
|
|
993
|
+
dialog.addEventListener("close", onCloseEvent);
|
|
994
|
+
|
|
995
|
+
let cleanupLightDismiss = null;
|
|
996
|
+
if (typeof HTMLDialogElement !== "undefined" && !("closedBy" in HTMLDialogElement.prototype)) {
|
|
997
|
+
const outside = (e) => {
|
|
998
|
+
const r = dialog.getBoundingClientRect();
|
|
999
|
+
return e.clientX < r.left || e.clientX > r.right || e.clientY < r.top || e.clientY > r.bottom;
|
|
1000
|
+
};
|
|
1001
|
+
let pressedOutside = false;
|
|
1002
|
+
const onDown = (e) => {
|
|
1003
|
+
pressedOutside = outside(e);
|
|
1004
|
+
};
|
|
1005
|
+
const onUp = (e) => {
|
|
1006
|
+
if (pressedOutside && outside(e)) closeTopLayerDialog(dialog);
|
|
1007
|
+
};
|
|
1008
|
+
dialog.addEventListener("pointerdown", onDown);
|
|
1009
|
+
dialog.addEventListener("pointerup", onUp);
|
|
1010
|
+
cleanupLightDismiss = () => {
|
|
1011
|
+
dialog.removeEventListener("pointerdown", onDown);
|
|
1012
|
+
dialog.removeEventListener("pointerup", onUp);
|
|
1013
|
+
};
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
return () => {
|
|
1017
|
+
dialog.removeEventListener("cancel", onCancel);
|
|
1018
|
+
dialog.removeEventListener("close", onCloseEvent);
|
|
1019
|
+
cleanupLightDismiss?.();
|
|
1020
|
+
};
|
|
1021
|
+
}, [dialogRef]);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
/**
|
|
1025
|
+
* Accessible native `<dialog>` modal overlay with top-layer rendering, `closedby="any"` light dismiss, and CSS backdrop scroll lock.
|
|
1026
|
+
* @param {{ open: boolean, onClose: Function, title?: any, description?: any, footer?: any, size?: "sm"|"md"|"lg"|"xl", className?: string, children?: any }} props
|
|
1027
|
+
*/
|
|
1028
|
+
export function Modal({ open, onClose, title, description, footer, size = "md", className, children }) {
|
|
1029
|
+
const dialogRef = useRef(null);
|
|
1030
|
+
const titleId = useId();
|
|
1031
|
+
const i18n = useLocale();
|
|
1032
|
+
useNativeDialog(dialogRef, open, onClose);
|
|
1033
|
+
const widths = { sm: "w-full max-w-sm", md: "w-full max-w-lg", lg: "w-full max-w-2xl", xl: "w-full max-w-4xl" };
|
|
1034
|
+
|
|
1035
|
+
return h(
|
|
1036
|
+
"dialog",
|
|
1037
|
+
{
|
|
1038
|
+
ref: dialogRef,
|
|
1039
|
+
closedby: "any",
|
|
1040
|
+
"aria-labelledby": title ? titleId : undefined,
|
|
1041
|
+
"aria-label": title ? undefined : i18n.t("dialog.label"),
|
|
1042
|
+
className: cn("bb-dialog", widths[size] || widths.md),
|
|
1043
|
+
},
|
|
1044
|
+
h(
|
|
1045
|
+
"div",
|
|
1046
|
+
{
|
|
1047
|
+
className: cn(
|
|
1048
|
+
"relative rounded-2xl border border-border/85 bg-popover/95 p-6 text-popover-foreground shadow-2xl backdrop-blur-xl",
|
|
1049
|
+
className
|
|
1050
|
+
),
|
|
1051
|
+
},
|
|
1052
|
+
h(
|
|
1053
|
+
"button",
|
|
1054
|
+
{
|
|
1055
|
+
type: "button",
|
|
1056
|
+
onClick: () => closeTopLayerDialog(dialogRef.current),
|
|
1057
|
+
className: "btn btn-ghost btn-icon btn-sm absolute right-3 top-3 text-muted-foreground",
|
|
1058
|
+
"aria-label": i18n.t("dialog.close"),
|
|
1059
|
+
},
|
|
1060
|
+
h(CloseIcon)
|
|
1061
|
+
),
|
|
1062
|
+
title ? h("h2", { id: titleId, className: "pr-8 text-lg font-semibold tracking-tight" }, title) : null,
|
|
1063
|
+
description ? h("p", { className: "mt-1 text-sm text-muted-foreground" }, description) : null,
|
|
1064
|
+
h("div", { className: title || description ? "mt-4" : undefined }, children),
|
|
1065
|
+
footer ? h("div", { className: "mt-6 flex justify-end gap-2" }, footer) : null
|
|
1066
|
+
)
|
|
1067
|
+
);
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
/**
|
|
1071
|
+
* Accessible slide-over native `<dialog>` drawer rendered in the top layer.
|
|
1072
|
+
* @param {{ open: boolean, onClose: Function, side?: "left"|"right"|"top"|"bottom", title?: any, className?: string, children?: any }} props
|
|
1073
|
+
*/
|
|
1074
|
+
export function Drawer({ open, onClose, side = "right", title, className, children }) {
|
|
1075
|
+
const dialogRef = useRef(null);
|
|
1076
|
+
const titleId = useId();
|
|
1077
|
+
const i18n = useLocale();
|
|
1078
|
+
useNativeDialog(dialogRef, open, onClose);
|
|
1079
|
+
const dimensions = {
|
|
1080
|
+
left: "h-full w-80 max-w-[85vw] border-r",
|
|
1081
|
+
right: "h-full w-80 max-w-[85vw] border-l",
|
|
1082
|
+
top: "w-full max-h-[85vh] border-b",
|
|
1083
|
+
bottom: "w-full max-h-[85vh] border-t rounded-t-2xl",
|
|
1084
|
+
};
|
|
1085
|
+
|
|
1086
|
+
return h(
|
|
1087
|
+
"dialog",
|
|
1088
|
+
{
|
|
1089
|
+
ref: dialogRef,
|
|
1090
|
+
closedby: "any",
|
|
1091
|
+
"data-drawer-side": side,
|
|
1092
|
+
"aria-labelledby": title ? titleId : undefined,
|
|
1093
|
+
"aria-label": title ? undefined : i18n.t("drawer.label"),
|
|
1094
|
+
className: "bb-dialog",
|
|
1095
|
+
},
|
|
1096
|
+
h(
|
|
1097
|
+
"div",
|
|
1098
|
+
{
|
|
1099
|
+
className: cn(
|
|
1100
|
+
"flex flex-col overflow-y-auto border-border/85 bg-popover/95 p-5 text-popover-foreground shadow-2xl backdrop-blur-xl",
|
|
1101
|
+
dimensions[side] || dimensions.right,
|
|
1102
|
+
className
|
|
1103
|
+
),
|
|
1104
|
+
},
|
|
1105
|
+
h(
|
|
1106
|
+
"div",
|
|
1107
|
+
{ className: "mb-4 flex items-center justify-between gap-3" },
|
|
1108
|
+
h("h2", { id: titleId, className: "text-base font-semibold tracking-tight" }, title || ""),
|
|
1109
|
+
h(
|
|
1110
|
+
"button",
|
|
1111
|
+
{
|
|
1112
|
+
type: "button",
|
|
1113
|
+
onClick: () => closeTopLayerDialog(dialogRef.current),
|
|
1114
|
+
className: "btn btn-ghost btn-icon btn-sm text-muted-foreground",
|
|
1115
|
+
"aria-label": i18n.t("dialog.close"),
|
|
1116
|
+
},
|
|
1117
|
+
h(CloseIcon)
|
|
1118
|
+
)
|
|
1119
|
+
),
|
|
1120
|
+
children
|
|
1121
|
+
)
|
|
1122
|
+
);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
let toasts = [];
|
|
1126
|
+
let toastSeq = 0;
|
|
1127
|
+
const toastListeners = new Set();
|
|
1128
|
+
const toastTimers = new Map();
|
|
1129
|
+
const EMPTY = [];
|
|
1130
|
+
|
|
1131
|
+
/**
|
|
1132
|
+
* Notifies toast subscribers.
|
|
1133
|
+
*/
|
|
1134
|
+
function emitToasts() {
|
|
1135
|
+
for (const fn of toastListeners) fn();
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
/**
|
|
1139
|
+
* Announces a toast message without reading interactive button chrome.
|
|
1140
|
+
* @param {string} text
|
|
1141
|
+
*/
|
|
1142
|
+
function announceToastText(text) {
|
|
1143
|
+
if (!text || typeof navigator === "undefined" || typeof document === "undefined") return;
|
|
1144
|
+
if (typeof navigator.ariaNotify === "function") {
|
|
1145
|
+
navigator.ariaNotify(text);
|
|
1146
|
+
return;
|
|
1147
|
+
}
|
|
1148
|
+
let live = document.getElementById("bb-react-toast-live");
|
|
1149
|
+
if (!live) {
|
|
1150
|
+
live = document.createElement("p");
|
|
1151
|
+
live.id = "bb-react-toast-live";
|
|
1152
|
+
live.className = "visually-hidden";
|
|
1153
|
+
live.setAttribute("aria-live", "polite");
|
|
1154
|
+
document.body.appendChild(live);
|
|
1155
|
+
}
|
|
1156
|
+
live.textContent = "";
|
|
1157
|
+
window.setTimeout(() => {
|
|
1158
|
+
live.textContent = text;
|
|
1159
|
+
}, 30);
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
/**
|
|
1163
|
+
* Pauses auto-dismissal for a toast while hovered or keyboard-focused.
|
|
1164
|
+
* @param {number} id
|
|
1165
|
+
*/
|
|
1166
|
+
function pauseToastTimer(id) {
|
|
1167
|
+
const entry = toastTimers.get(id);
|
|
1168
|
+
if (!entry || entry.paused) return;
|
|
1169
|
+
clearTimeout(entry.timer);
|
|
1170
|
+
entry.remaining = Math.max(0, entry.remaining - (Date.now() - entry.startedAt));
|
|
1171
|
+
entry.paused = true;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
/**
|
|
1175
|
+
* Resumes auto-dismissal for a toast once pointer and focus leave it.
|
|
1176
|
+
* @param {number} id
|
|
1177
|
+
*/
|
|
1178
|
+
function resumeToastTimer(id) {
|
|
1179
|
+
const entry = toastTimers.get(id);
|
|
1180
|
+
if (!entry || !entry.paused || entry.remaining <= 0) return;
|
|
1181
|
+
entry.paused = false;
|
|
1182
|
+
entry.startedAt = Date.now();
|
|
1183
|
+
entry.timer = setTimeout(() => dismissToast(id), entry.remaining);
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
/**
|
|
1187
|
+
* Shows a toast notification rendered by `<Toaster />` inside a top-layer `popover="manual"` region.
|
|
1188
|
+
* @param {string|{ title?: string, description?: string, badge?: string, type?: "info"|"success"|"error"|"warning", duration?: number }} input
|
|
1189
|
+
* @returns {number}
|
|
1190
|
+
*/
|
|
1191
|
+
export function toast(input) {
|
|
1192
|
+
const opts = typeof input === "string" ? { title: input } : input || {};
|
|
1193
|
+
const id = ++toastSeq;
|
|
1194
|
+
const duration = opts.duration !== undefined ? Number(opts.duration) : 4500;
|
|
1195
|
+
const triggerEl = typeof document !== "undefined" ? document.activeElement : null;
|
|
1196
|
+
const item = { id, type: "info", duration, triggerEl, ...opts };
|
|
1197
|
+
toasts = [...toasts, item];
|
|
1198
|
+
emitToasts();
|
|
1199
|
+
|
|
1200
|
+
if (typeof window !== "undefined") {
|
|
1201
|
+
const spoken = [item.title, item.description].filter(Boolean).join(". ");
|
|
1202
|
+
announceToastText(spoken);
|
|
1203
|
+
if (duration > 0) {
|
|
1204
|
+
const timer = setTimeout(() => dismissToast(id), duration);
|
|
1205
|
+
toastTimers.set(id, { timer, remaining: duration, startedAt: Date.now(), paused: false });
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
return id;
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
/**
|
|
1212
|
+
* Dismisses a toast by id and restores focus cleanly when dismissed from keyboard focus.
|
|
1213
|
+
* @param {number} id
|
|
1214
|
+
* @param {boolean} [fromKeyboard]
|
|
1215
|
+
*/
|
|
1216
|
+
export function dismissToast(id, fromKeyboard = false) {
|
|
1217
|
+
const entry = toastTimers.get(id);
|
|
1218
|
+
if (entry) {
|
|
1219
|
+
clearTimeout(entry.timer);
|
|
1220
|
+
toastTimers.delete(id);
|
|
1221
|
+
}
|
|
1222
|
+
const idx = toasts.findIndex((t) => t.id === id);
|
|
1223
|
+
const removed = idx >= 0 ? toasts[idx] : null;
|
|
1224
|
+
toasts = toasts.filter((t) => t.id !== id);
|
|
1225
|
+
emitToasts();
|
|
1226
|
+
|
|
1227
|
+
if (fromKeyboard && typeof document !== "undefined") {
|
|
1228
|
+
window.requestAnimationFrame(() => {
|
|
1229
|
+
const remainingButtons = document.querySelectorAll("[data-bb-toast-close]");
|
|
1230
|
+
if (remainingButtons.length > 0) {
|
|
1231
|
+
const nextBtn = remainingButtons[Math.min(idx, remainingButtons.length - 1)];
|
|
1232
|
+
nextBtn?.focus?.();
|
|
1233
|
+
} else if (removed?.triggerEl?.isConnected) {
|
|
1234
|
+
removed.triggerEl.focus?.();
|
|
1235
|
+
}
|
|
1236
|
+
});
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
toast.success = (title, description) => toast({ type: "success", title, description });
|
|
1241
|
+
toast.error = (title, description) => toast({ type: "error", title, description });
|
|
1242
|
+
toast.info = (title, description) => toast({ type: "info", title, description });
|
|
1243
|
+
toast.warning = (title, description) => toast({ type: "warning", title, description });
|
|
1244
|
+
|
|
1245
|
+
/**
|
|
1246
|
+
* Top-layer `popover="manual"` toast notification stack: neutral frosted glass cards with a small color-coded type badge.
|
|
1247
|
+
*/
|
|
1248
|
+
export function Toaster() {
|
|
1249
|
+
const stackRef = useRef(null);
|
|
1250
|
+
const i18n = useLocale();
|
|
1251
|
+
const items = useSyncExternalStore(
|
|
1252
|
+
(fn) => {
|
|
1253
|
+
toastListeners.add(fn);
|
|
1254
|
+
return () => toastListeners.delete(fn);
|
|
1255
|
+
},
|
|
1256
|
+
() => toasts,
|
|
1257
|
+
() => EMPTY
|
|
1258
|
+
);
|
|
1259
|
+
|
|
1260
|
+
useEffect(() => {
|
|
1261
|
+
const el = stackRef.current;
|
|
1262
|
+
if (!el || typeof window === "undefined") return;
|
|
1263
|
+
const supportsPopover = Object.hasOwn(HTMLElement.prototype, "popover") || typeof el.showPopover === "function";
|
|
1264
|
+
if (supportsPopover && !el.hasAttribute("popover")) {
|
|
1265
|
+
el.setAttribute("popover", "manual");
|
|
1266
|
+
}
|
|
1267
|
+
if (typeof el.showPopover === "function") {
|
|
1268
|
+
try {
|
|
1269
|
+
if (items.length > 0 && !el.matches(":popover-open")) el.showPopover();
|
|
1270
|
+
else if (items.length === 0 && el.matches(":popover-open")) el.hidePopover();
|
|
1271
|
+
} catch {}
|
|
1272
|
+
}
|
|
1273
|
+
}, [items.length]);
|
|
1274
|
+
|
|
1275
|
+
if (!items.length) return null;
|
|
1276
|
+
|
|
1277
|
+
return h(
|
|
1278
|
+
"div",
|
|
1279
|
+
{
|
|
1280
|
+
ref: stackRef,
|
|
1281
|
+
role: "region",
|
|
1282
|
+
"aria-label": i18n.t("toast.region"),
|
|
1283
|
+
className: "bb-toast-stack",
|
|
1284
|
+
},
|
|
1285
|
+
items.map((t) => {
|
|
1286
|
+
const tone = TONE_BADGES[t.type] ? t.type : "info";
|
|
1287
|
+
const badgeText = t.badge || i18n.t(`toast.${tone}`);
|
|
1288
|
+
return h(
|
|
1289
|
+
"div",
|
|
1290
|
+
{
|
|
1291
|
+
key: t.id,
|
|
1292
|
+
className: "bb-toast-card",
|
|
1293
|
+
onPointerEnter: () => pauseToastTimer(t.id),
|
|
1294
|
+
onPointerLeave: () => resumeToastTimer(t.id),
|
|
1295
|
+
onFocusCapture: () => pauseToastTimer(t.id),
|
|
1296
|
+
onBlurCapture: (e) => {
|
|
1297
|
+
if (!e.currentTarget.contains(e.relatedTarget)) resumeToastTimer(t.id);
|
|
1298
|
+
},
|
|
1299
|
+
},
|
|
1300
|
+
h(
|
|
1301
|
+
"div",
|
|
1302
|
+
{ className: "flex items-center justify-between gap-2" },
|
|
1303
|
+
h(Badge, { tone, soft: true }, badgeText),
|
|
1304
|
+
h(
|
|
1305
|
+
"button",
|
|
1306
|
+
{
|
|
1307
|
+
type: "button",
|
|
1308
|
+
"data-bb-toast-close": String(t.id),
|
|
1309
|
+
onClick: (e) => dismissToast(t.id, e.detail === 0),
|
|
1310
|
+
className: "btn btn-ghost btn-icon btn-sm -mr-1 -mt-1 size-7 text-muted-foreground hover:text-foreground [&_svg]:size-3.5",
|
|
1311
|
+
"aria-label": i18n.t("toast.dismiss"),
|
|
1312
|
+
},
|
|
1313
|
+
h(CloseIcon)
|
|
1314
|
+
)
|
|
1315
|
+
),
|
|
1316
|
+
t.title ? h("p", { className: "mt-2 font-semibold leading-snug text-popover-foreground" }, t.title) : null,
|
|
1317
|
+
t.description ? h("p", { className: "mt-1 text-[13px] leading-relaxed text-muted-foreground" }, t.description) : null
|
|
1318
|
+
);
|
|
1319
|
+
})
|
|
1320
|
+
);
|
|
1321
|
+
}
|