@fumadocs/base-ui 16.16.1 → 16.17.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.
Files changed (37) hide show
  1. package/css/generated/docs.css +1 -1
  2. package/css/generated/glass.css +1 -1
  3. package/css/generated/home.css +1 -1
  4. package/css/generated/notebook.css +1 -1
  5. package/css/generated/shared.css +1 -1
  6. package/css/generated/spacious.css +1 -1
  7. package/dist/components/callout.js +1 -1
  8. package/dist/components/codeblock.js +1 -1
  9. package/dist/components/dialog/search-algolia.js +5 -8
  10. package/dist/components/dialog/search-default.js +3 -5
  11. package/dist/components/dialog/search-orama.js +5 -8
  12. package/dist/components/dialog/search.d.ts +50 -27
  13. package/dist/components/dialog/search.js +149 -97
  14. package/dist/components/sidebar/base.d.ts +7 -1
  15. package/dist/components/sidebar/base.js +10 -1
  16. package/dist/components/tabs.js +2 -7
  17. package/dist/components/ui/tabs.d.ts +1 -1
  18. package/dist/components/ui/tabs.js +44 -25
  19. package/dist/contexts/search.js +3 -8
  20. package/dist/contexts/tree.js +4 -4
  21. package/dist/layouts/docs/client.js +1 -0
  22. package/dist/layouts/flux/slots/sidebar.d.ts +1 -1
  23. package/dist/layouts/flux/slots/sidebar.js +9 -11
  24. package/dist/layouts/glass/slots/sidebar.js +9 -0
  25. package/dist/layouts/notebook/client.js +1 -0
  26. package/dist/layouts/shared/client.js +1 -1
  27. package/dist/layouts/spacious/index.js +1 -1
  28. package/dist/layouts/spacious/page/index.js +35 -36
  29. package/dist/layouts/spacious/page/slots/toc.js +4 -4
  30. package/dist/layouts/spacious/slots/actions.js +5 -6
  31. package/dist/layouts/spacious/slots/header.js +1 -1
  32. package/dist/layouts/spacious/slots/sidebar.d.ts +1 -1
  33. package/dist/layouts/spacious/slots/sidebar.js +3 -3
  34. package/dist/provider/base.js +3 -25
  35. package/dist/style.css +203 -40
  36. package/dist/utils/hotkey.js +29 -0
  37. package/package.json +8 -11
@@ -3,26 +3,19 @@ import { cn } from "../../utils/cn.js";
3
3
  import { buttonVariants } from "../ui/button.js";
4
4
  import { mergeRefs } from "../../utils/merge-refs.js";
5
5
  import { useRouter } from "fumadocs-core/framework";
6
+ import * as JsxRuntime from "react/jsx-runtime";
6
7
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
7
8
  import { ChevronRight, Hash, SearchIcon } from "lucide-react";
8
9
  import { cva } from "class-variance-authority";
9
- import { Fragment as Fragment$1, createContext, use, useCallback, useEffect, useEffectEvent, useMemo, useRef, useState } from "react";
10
+ import { Fragment as Fragment$1, createContext, use, useCallback, useEffect, useEffectEvent, useLayoutEffect, useMemo, useRef, useSyncExternalStore } from "react";
10
11
  import { T, useTranslations } from "@fuma-translate/react";
11
12
  import { Dialog } from "@base-ui/react/dialog";
12
- import { useOnChange } from "fumadocs-core/utils/use-on-change";
13
13
  import scrollIntoView from "scroll-into-view-if-needed";
14
- import { createMarkdownRenderer } from "fumadocs-core/content/md";
15
- import rehypeRaw from "rehype-raw";
16
- import { visit } from "unist-util-visit";
14
+ import { useHighlightQuery } from "fumadocs-core/search/client";
15
+ import { toJsxRuntime } from "hast-util-to-jsx-runtime";
17
16
  //#region src/components/dialog/search.tsx
18
17
  const RootContext = createContext(null);
19
- const ListContext = createContext(null);
20
18
  const TagsListContext = createContext(null);
21
- const PreContext = createContext(false);
22
- const mdRenderer = createMarkdownRenderer({
23
- remarkRehypeOptions: { allowDangerousHtml: true },
24
- rehypePlugins: [rehypeRaw, rehypeCustomElements]
25
- });
26
19
  const mdComponents = {
27
20
  mark(props) {
28
21
  return /* @__PURE__ */ jsx("span", {
@@ -37,34 +30,20 @@ const mdComponents = {
37
30
  className: "min-w-0"
38
31
  });
39
32
  },
40
- strong(props) {
41
- return /* @__PURE__ */ jsx("strong", {
42
- ...props,
43
- className: "text-fd-accent-foreground font-medium"
44
- });
45
- },
46
- code(props) {
47
- if (use(PreContext)) return /* @__PURE__ */ jsx("code", {
48
- ...props,
49
- className: "mask-[linear-gradient(to_bottom,white,white_30px,transparent_80px)]"
50
- });
51
- return /* @__PURE__ */ jsx("code", {
52
- ...props,
53
- className: "border rounded-md px-px bg-fd-secondary text-fd-secondary-foreground"
54
- });
55
- },
56
- custom({ _tagName = "fragment", children, ...rest }) {
33
+ custom({ tagName, children, ...rest }) {
57
34
  return /* @__PURE__ */ jsxs("span", {
58
35
  className: "inline-flex max-w-full items-center border p-0.5 rounded-md bg-fd-card text-fd-card-foreground divide-x divide-fd-border",
59
36
  children: [
60
37
  /* @__PURE__ */ jsx("code", {
61
- className: "rounded-sm px-0.5 me-1 bg-fd-primary font-medium text-xs text-fd-primary-foreground border-none",
62
- children: _tagName
38
+ "data-highlight-ignore": "",
39
+ className: "not-prose rounded-sm px-0.5 me-1 bg-fd-primary font-medium text-xs text-fd-primary-foreground border-none",
40
+ children: tagName
63
41
  }),
64
42
  Object.entries(rest).map(([k, v]) => {
65
43
  if (typeof v !== "string") return;
66
44
  return /* @__PURE__ */ jsxs("code", {
67
- className: "truncate text-xs text-fd-muted-foreground px-1",
45
+ "data-highlight-ignore": "",
46
+ className: "not-prose truncate text-xs text-fd-muted-foreground px-1",
68
47
  children: [/* @__PURE__ */ jsxs("span", {
69
48
  className: "text-fd-card-foreground",
70
49
  children: [k, ": "]
@@ -81,25 +60,44 @@ const mdComponents = {
81
60
  pre(props) {
82
61
  return /* @__PURE__ */ jsx("pre", {
83
62
  ...props,
84
- className: cn("flex flex-col border rounded-md my-0.5 p-2 bg-fd-secondary text-fd-secondary-foreground max-h-20 overflow-hidden", props.className),
85
- children: /* @__PURE__ */ jsx(PreContext, {
86
- value: true,
87
- children: props.children
88
- })
63
+ className: cn("not-prose flex flex-col border rounded-md my-0.5 p-2 bg-fd-secondary text-fd-secondary-foreground max-h-20 overflow-hidden *:mask-[linear-gradient(to_bottom,white,white_30px,transparent_80px)]", props.className)
89
64
  });
90
65
  }
91
66
  };
92
- function rehypeCustomElements() {
93
- return (tree) => {
94
- visit(tree, (node) => {
95
- if (node.type === "element" && document.createElement(node.tagName) instanceof HTMLUnknownElement) {
96
- node.properties._tagName = node.tagName;
97
- node.tagName = "custom";
98
- }
99
- });
100
- };
67
+ const renderOptions = {
68
+ development: false,
69
+ components: mdComponents,
70
+ ...JsxRuntime
71
+ };
72
+ function Children({ children }) {
73
+ return children;
74
+ }
75
+ function TableCell(props) {
76
+ return /* @__PURE__ */ jsx("div", {
77
+ ...props,
78
+ className: "min-w-0 not-last:truncate last:line-clamp-2"
79
+ });
101
80
  }
102
- function SearchDialog({ open, onOpenChange, search, onSearchChange, isLoading = false, onSelect: onSelectProp, children, dialogHandle }) {
81
+ const tableRenderOptions = {
82
+ ...renderOptions,
83
+ components: {
84
+ ...mdComponents,
85
+ table: Children,
86
+ thead: Children,
87
+ tbody: Children,
88
+ tr: Children,
89
+ th: TableCell,
90
+ td: TableCell
91
+ }
92
+ };
93
+ const rowRenderOptions = {
94
+ ...tableRenderOptions,
95
+ components: {
96
+ ...tableRenderOptions.components,
97
+ thead: () => null
98
+ }
99
+ };
100
+ function SearchDialog({ open, onOpenChange, search, onSearchChange, isLoading = false, data, onSelect: onSelectProp, children, dialogHandle }) {
103
101
  const router = useRouter();
104
102
  const onOpenChangeCallback = useRef(onOpenChange);
105
103
  onOpenChangeCallback.current = onOpenChange;
@@ -114,22 +112,42 @@ function SearchDialog({ open, onOpenChange, search, onSearchChange, isLoading =
114
112
  };
115
113
  const onSelectCallback = useRef(onSelect);
116
114
  onSelectCallback.current = onSelect;
115
+ const activeItem = useMemo(() => {
116
+ let active = null;
117
+ const listeners = /* @__PURE__ */ new Set();
118
+ return {
119
+ getActive: () => active,
120
+ setActive(id) {
121
+ active = id;
122
+ for (const listener of listeners) listener();
123
+ },
124
+ subscribeActive(listener) {
125
+ listeners.add(listener);
126
+ return () => void listeners.delete(listener);
127
+ }
128
+ };
129
+ }, []);
117
130
  return /* @__PURE__ */ jsx(Dialog.Root, {
118
131
  open,
119
132
  onOpenChange,
120
133
  handle: dialogHandle,
121
134
  children: /* @__PURE__ */ jsx(RootContext, {
122
135
  value: useMemo(() => ({
136
+ ...activeItem,
123
137
  open,
124
138
  search,
125
139
  isLoading,
140
+ data,
141
+ query: data ? data.query : search,
126
142
  onOpenChange: (v) => onOpenChangeCallback.current(v),
127
143
  onSearchChange: (v) => onSearchChangeCallback.current(v),
128
144
  onSelect: (v) => onSelectCallback.current(v)
129
145
  }), [
146
+ activeItem,
130
147
  isLoading,
131
148
  open,
132
- search
149
+ search,
150
+ data
133
151
  ]),
134
152
  children
135
153
  })
@@ -143,10 +161,13 @@ function SearchDialogHeader(props) {
143
161
  }
144
162
  function SearchDialogInput(props) {
145
163
  const t = useTranslations({ note: "search dialog" });
146
- const { search, onSearchChange } = useSearch();
164
+ const { search, onSearchChange, getActive, subscribeActive } = useSearch();
165
+ const active = useSyncExternalStore(subscribeActive, getActive, getActive);
147
166
  return /* @__PURE__ */ jsx("input", {
148
167
  "data-fd-search-dialog-input": "",
149
168
  role: "combobox",
169
+ "aria-expanded": active !== null,
170
+ "aria-activedescendant": active !== null ? `fd-search-option-${active}` : void 0,
150
171
  "aria-label": t("Search"),
151
172
  "aria-autocomplete": "list",
152
173
  "aria-controls": "fd-search-list",
@@ -208,30 +229,32 @@ function SearchDialogContent({ ref, children, className, ...props }) {
208
229
  }), children]
209
230
  }) });
210
231
  }
211
- function SearchDialogList({ items = null, Empty = () => /* @__PURE__ */ jsx("div", {
232
+ function SearchDialogList({ items: itemsProp, defaultItems = null, Empty = () => /* @__PURE__ */ jsx("div", {
212
233
  role: "status",
213
234
  className: "py-12 text-center text-sm text-fd-muted-foreground",
214
235
  children: /* @__PURE__ */ jsx(T, {
215
236
  text: "No results found",
216
237
  note: "search dialog"
217
238
  })
218
- }), Item = (props) => /* @__PURE__ */ jsx(SearchDialogListItem, { ...props }), ...props }) {
239
+ }), Item = (props) => /* @__PURE__ */ jsx(SearchDialogListItem, { ...props }), Table = (props) => /* @__PURE__ */ jsx(SearchDialogListTable, { ...props }), ...props }) {
219
240
  const ref = useRef(null);
220
241
  const t = useTranslations({ note: "search dialog" });
221
- const { onSelect } = useSearch();
222
- const [active, setActive] = useState(() => items && items.length > 0 ? items[0].id : null);
242
+ const { onSelect, data, getActive, setActive } = useSearch();
243
+ const items = itemsProp === void 0 ? data?.items ?? defaultItems : itemsProp;
244
+ useLayoutEffect(() => setActive(items?.[0]?.id ?? null), [setActive, items]);
223
245
  const onKey = useEffectEvent((e) => {
224
246
  if (!items || e.isComposing || e.keyCode === 229) return;
247
+ const list = flattenItems(items);
225
248
  if (e.key === "ArrowDown" || e.key == "ArrowUp") {
226
- let idx = items.findIndex((item) => item.id === active);
249
+ let idx = list.findIndex((item) => item.id === getActive());
227
250
  if (idx === -1) idx = 0;
228
251
  else if (e.key === "ArrowDown") idx++;
229
252
  else idx--;
230
- setActive(items.at(idx % items.length)?.id ?? null);
253
+ setActive(list.at(idx % list.length)?.id ?? null);
231
254
  e.preventDefault();
232
255
  }
233
256
  if (e.key === "Enter") {
234
- const selected = items.find((item) => item.id === active);
257
+ const selected = list.find((item) => item.id === getActive());
235
258
  if (selected) onSelect(selected);
236
259
  e.preventDefault();
237
260
  }
@@ -252,46 +275,81 @@ function SearchDialogList({ items = null, Empty = () => /* @__PURE__ */ jsx("div
252
275
  content.removeEventListener("keydown", onKey);
253
276
  };
254
277
  }, []);
255
- useOnChange(items, () => {
256
- setActive(items?.[0]?.id ?? null);
257
- });
258
- useEffect(() => {
259
- const input = ref.current?.closest("[role=\"dialog\"]")?.querySelector("[role=\"combobox\"]");
260
- if (!input) return;
261
- input.setAttribute("aria-expanded", String(active !== null));
262
- if (active !== null) input.setAttribute("aria-activedescendant", `fd-search-option-${active}`);
263
- else input.removeAttribute("aria-activedescendant");
264
- }, [active]);
265
278
  return /* @__PURE__ */ jsx("div", {
266
279
  ...props,
267
280
  ref,
268
281
  "data-empty": items === null,
269
- className: cn("overflow-hidden h-(--fd-animated-height) transition-[height]", props.className),
270
- children: /* @__PURE__ */ jsx("div", {
282
+ className: cn("overflow-hidden h-(--fd-animated-height) transition-[height] [&_::highlight(fd-search)]:text-fd-primary [&_::highlight(fd-search)]:underline", props.className),
283
+ children: /* @__PURE__ */ jsxs("div", {
271
284
  id: "fd-search-list",
272
285
  role: items?.length ? "listbox" : void 0,
273
286
  "aria-label": items?.length ? t("Search") : void 0,
274
287
  className: cn("w-full flex flex-col overflow-y-auto max-h-[460px] p-1", !items && "hidden"),
275
- children: /* @__PURE__ */ jsxs(ListContext, {
276
- value: useMemo(() => ({
277
- active,
278
- setActive
279
- }), [active]),
280
- children: [items?.length === 0 && Empty(), items?.map((item) => /* @__PURE__ */ jsx(Fragment$1, { children: Item({
281
- item,
282
- onClick: () => onSelect(item)
283
- }) }, item.id))]
284
- })
288
+ children: [items?.length === 0 && Empty(), items?.map((item) => /* @__PURE__ */ jsx(Fragment$1, { children: item.type === "table" ? Table({
289
+ table: item,
290
+ Item,
291
+ onSelect
292
+ }) : Item({
293
+ item,
294
+ onClick: () => onSelect(item)
295
+ }) }, item.id))]
285
296
  })
286
297
  });
287
298
  }
288
- function SearchDialogListItem({ item, className, children, renderMarkdown = (s) => /* @__PURE__ */ jsx(mdRenderer.Markdown, {
289
- components: mdComponents,
290
- children: s
291
- }), renderHighlights: _, ...props }) {
292
- const { active: activeId, setActive } = useSearchList();
293
- const active = item.id === activeId;
294
- if (item.type === "action") children ??= item.node;
299
+ function flattenItems(items) {
300
+ const out = [];
301
+ for (const item of items) if (item.type === "table") out.push(...item.rows);
302
+ else out.push(item);
303
+ return out;
304
+ }
305
+ function SearchDialogListTable({ table, Item, onSelect }) {
306
+ const { query } = useSearch();
307
+ const highlightRef = useHighlightQuery(query);
308
+ const headerId = `fd-search-table-${table.id}`;
309
+ const header = useMemo(() => table.header && toJsxRuntime(table.header, tableRenderOptions), [table.header]);
310
+ return /* @__PURE__ */ jsx("div", {
311
+ role: table.header ? "group" : void 0,
312
+ "aria-labelledby": table.header ? headerId : void 0,
313
+ className: "ms-3 shrink-0 border-s py-2 ps-3.25 pe-2.5",
314
+ children: /* @__PURE__ */ jsxs("div", {
315
+ className: "grid gap-x-3 overflow-hidden rounded-lg border bg-fd-card text-sm",
316
+ style: { gridTemplateColumns: `${"fit-content(30%) ".repeat(table.columns - 1)}minmax(30%, 1fr)` },
317
+ children: [header && /* @__PURE__ */ jsx("div", {
318
+ id: headerId,
319
+ ref: highlightRef,
320
+ "aria-hidden": true,
321
+ className: "col-span-full grid grid-cols-subgrid bg-fd-secondary px-3 py-1 text-xs text-fd-muted-foreground",
322
+ children: header
323
+ }), table.rows.map((row) => /* @__PURE__ */ jsx(Fragment$1, { children: Item({
324
+ item: row,
325
+ table,
326
+ onClick: () => onSelect(row)
327
+ }) }, row.id))]
328
+ })
329
+ });
330
+ }
331
+ function SearchDialogListItem({ item, table, className, children, renderMarkdown, ...props }) {
332
+ const { query, getActive, setActive, subscribeActive } = useSearch();
333
+ const isActive = () => getActive() === item.id;
334
+ const active = useSyncExternalStore(subscribeActive, isActive, isActive);
335
+ const highlightRef = useHighlightQuery(query);
336
+ const content = useMemo(() => {
337
+ if (item.type === "action") return item.node;
338
+ if (renderMarkdown && typeof item.content === "string") return renderMarkdown(item.content);
339
+ if (!("hastContent" in item)) return item.content;
340
+ if (!table) return toJsxRuntime(item.hastContent, renderOptions);
341
+ return toJsxRuntime(item.hastContent, table.header ? rowRenderOptions : tableRenderOptions);
342
+ }, [
343
+ item,
344
+ renderMarkdown,
345
+ table
346
+ ]);
347
+ if (item.type === "action") children ??= content;
348
+ else if (table) children ??= /* @__PURE__ */ jsx("div", {
349
+ ref: highlightRef,
350
+ className: cn("contents", !table.header && "*:first:font-medium *:first:text-fd-primary *:nth-2:text-xs *:not-first:text-fd-muted-foreground"),
351
+ children: content
352
+ });
295
353
  else children ??= /* @__PURE__ */ jsxs(Fragment, { children: [
296
354
  /* @__PURE__ */ jsx("div", {
297
355
  className: "inline-flex items-center text-fd-muted-foreground text-xs empty:hidden",
@@ -303,24 +361,23 @@ function SearchDialogListItem({ item, className, children, renderMarkdown = (s)
303
361
  }),
304
362
  item.type === "heading" && /* @__PURE__ */ jsx(Hash, { className: "absolute inset-s-6 top-2.5 size-4 text-fd-muted-foreground" }),
305
363
  /* @__PURE__ */ jsx("div", {
306
- className: cn("min-w-0", item.type === "text" && "ps-4", item.type === "heading" && "ps-8", item.type === "page" || item.type === "heading" ? "font-medium" : "text-fd-popover-foreground/80"),
307
- children: typeof item.content === "string" ? renderMarkdown(item.content) : item.content
364
+ ref: highlightRef,
365
+ className: cn("prose prose-sm leading-5 min-w-0", item.type === "text" && "ps-4", item.type === "heading" && "ps-8", item.type === "page" || item.type === "heading" ? "font-medium text-inherit" : "text-fd-popover-foreground/80"),
366
+ children: content
308
367
  })
309
368
  ] });
310
- return /* @__PURE__ */ jsx("button", {
311
- type: "button",
369
+ return /* @__PURE__ */ jsx("div", {
312
370
  id: `fd-search-option-${item.id}`,
313
371
  role: "option",
314
- tabIndex: -1,
315
372
  ref: useCallback((element) => {
316
373
  if (active && element) scrollIntoView(element, {
317
374
  scrollMode: "if-needed",
318
375
  block: "nearest",
319
- boundary: element.parentElement
376
+ boundary: element.closest("[role=\"listbox\"]")
320
377
  });
321
378
  }, [active]),
322
379
  "aria-selected": active,
323
- className: cn("relative select-none shrink-0 px-2.5 py-2 text-start text-sm overflow-hidden rounded-lg", active && "bg-fd-accent text-fd-accent-foreground", className),
380
+ className: cn("cursor-default", table ? "col-span-full grid grid-cols-subgrid items-baseline px-3 py-1.5 not-first:border-t" : "relative shrink-0 px-2.5 py-2 text-sm overflow-hidden rounded-lg", active && "bg-fd-accent text-fd-accent-foreground", className),
324
381
  onPointerMove: () => setActive(item.id),
325
382
  ...props,
326
383
  children
@@ -376,10 +433,5 @@ function useTagsList() {
376
433
  if (!ctx) throw new Error("Missing <TagsList />");
377
434
  return ctx;
378
435
  }
379
- function useSearchList() {
380
- const ctx = use(ListContext);
381
- if (!ctx) throw new Error("Missing <SearchDialogList />");
382
- return ctx;
383
- }
384
436
  //#endregion
385
- export { SearchDialog, SearchDialogClose, SearchDialogContent, SearchDialogFooter, SearchDialogHeader, SearchDialogIcon, SearchDialogInput, SearchDialogList, SearchDialogListItem, SearchDialogOverlay, TagsList, TagsListItem, useSearch, useSearchList, useTagsList };
437
+ export { SearchDialog, SearchDialogClose, SearchDialogContent, SearchDialogFooter, SearchDialogHeader, SearchDialogIcon, SearchDialogInput, SearchDialogList, SearchDialogListItem, SearchDialogListTable, SearchDialogOverlay, TagsList, TagsListItem, useSearch, useTagsList };
@@ -30,11 +30,17 @@ export interface SidebarProviderProps {
30
30
  * Prefetch links, default behaviour depends on your React.js framework.
31
31
  */
32
32
  prefetch?: boolean;
33
+ /**
34
+ * Support collapsing the sidebar on desktop mode
35
+ *
36
+ * @defaultValue true
37
+ */
38
+ collapsible?: boolean;
33
39
  children?: ReactNode;
34
40
  }
35
41
  type Mode = 'drawer' | 'full';
36
42
  declare const SidebarContext: import("react").Context<SidebarContext | null>;
37
- export declare function SidebarProvider({ defaultOpenLevel, prefetch, children }: SidebarProviderProps): import("react").JSX.Element;
43
+ export declare function SidebarProvider({ defaultOpenLevel, prefetch, collapsible, children }: SidebarProviderProps): import("react").JSX.Element;
38
44
  export declare function useSidebar(): SidebarContext;
39
45
  export declare function useFolder(): {
40
46
  open: boolean;
@@ -2,6 +2,7 @@
2
2
  import { __exportAll } from "../../_virtual/_rolldown/runtime.js";
3
3
  import { cn } from "../../utils/cn.js";
4
4
  import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "../ui/collapsible.js";
5
+ import { useHotKey } from "../../utils/hotkey.js";
5
6
  import Link from "fumadocs-core/link";
6
7
  import { usePathname } from "fumadocs-core/framework";
7
8
  import { jsx, jsxs } from "react/jsx-runtime";
@@ -35,7 +36,7 @@ var base_exports = /* @__PURE__ */ __exportAll({
35
36
  });
36
37
  const SidebarContext = createContext(null);
37
38
  const FolderContext = createContext(null);
38
- function SidebarProvider({ defaultOpenLevel = 0, prefetch, children }) {
39
+ function SidebarProvider({ defaultOpenLevel = 0, prefetch, collapsible = true, children }) {
39
40
  const closeOnRedirect = useRef(true);
40
41
  const [open, setOpen] = useState(false);
41
42
  const [collapsed, setCollapsed] = useState(false);
@@ -45,6 +46,14 @@ function SidebarProvider({ defaultOpenLevel = 0, prefetch, children }) {
45
46
  if (closeOnRedirect.current) setOpen(false);
46
47
  closeOnRedirect.current = true;
47
48
  });
49
+ useHotKey((e) => {
50
+ if (!(e.metaKey || e.ctrlKey) || e.shiftKey || e.altKey || e.key !== "b") return;
51
+ e.preventDefault();
52
+ setCollapsed((prev) => !prev);
53
+ }, {
54
+ enabled: collapsible && mode === "full",
55
+ ignoreTyping: true
56
+ });
48
57
  return /* @__PURE__ */ jsx(SidebarContext, {
49
58
  value: useMemo(() => ({
50
59
  open,
@@ -2,7 +2,7 @@
2
2
  import { cn } from "../utils/cn.js";
3
3
  import { Tabs as Tabs$1, TabsContent as TabsContent$1, TabsList as TabsList$1, TabsTrigger as TabsTrigger$1 } from "./ui/tabs.js";
4
4
  import { jsx, jsxs } from "react/jsx-runtime";
5
- import { createContext, useContext, useEffect, useId, useMemo, useState } from "react";
5
+ import { createContext, useContext, useEffect, useId, useMemo } from "react";
6
6
  //#region src/components/tabs.tsx
7
7
  const TabsContext = createContext(null);
8
8
  function useTabContext() {
@@ -23,16 +23,11 @@ function TabsTrigger({ className, ...props }) {
23
23
  });
24
24
  }
25
25
  function Tabs({ ref, className, items, label, defaultIndex = 0, defaultValue = items ? escapeValue(items[defaultIndex]) : void 0, ...props }) {
26
- const [value, setValue] = useState(defaultValue);
27
26
  const collection = useMemo(() => [], []);
28
27
  return /* @__PURE__ */ jsxs(Tabs$1, {
29
28
  ref,
30
29
  className: (s) => cn("flex flex-col overflow-hidden rounded-xl border bg-fd-secondary my-4", typeof className === "function" ? className(s) : className),
31
- value,
32
- onValueChange: (v) => {
33
- if (items && !items.some((item) => escapeValue(item) === v)) return;
34
- setValue(v);
35
- },
30
+ defaultValue,
36
31
  ...props,
37
32
  children: [items && /* @__PURE__ */ jsxs(TabsList, { children: [label && /* @__PURE__ */ jsx("span", {
38
33
  className: "text-sm font-medium my-auto me-auto",
@@ -19,7 +19,7 @@ export interface TabsProps extends ComponentProps<typeof Tabs$1.Root> {
19
19
  onValueChange?: (value: string) => void;
20
20
  }
21
21
  export declare const TabsList: import("react").ForwardRefExoticComponent<Omit<import("@base-ui/react").TabsListProps, "ref"> & import("react").RefAttributes<HTMLDivElement>>;
22
- export declare const TabsTrigger: import("react").ForwardRefExoticComponent<Omit<import("@base-ui/react").TabsTabProps, "ref"> & import("react").RefAttributes<HTMLElement>>;
22
+ export declare function TabsTrigger({ value, ref, ...props }: ComponentProps<typeof Tabs$1.Tab>): import("react").JSX.Element;
23
23
  export declare function Tabs({ ref, groupId, persist, updateAnchor, defaultValue, value: _value, onValueChange: _onValueChange, ...props }: TabsProps): import("react").JSX.Element;
24
24
  export declare function TabsContent({ value, ref, ...props }: ComponentProps<typeof Tabs$1.Panel>): import("react").JSX.Element;
25
25
  //#endregion
@@ -12,46 +12,60 @@ function useTabContext() {
12
12
  return ctx;
13
13
  }
14
14
  const TabsList = Tabs$1.List;
15
- const TabsTrigger = Tabs$1.Tab;
15
+ function TabsTrigger({ value, ref, ...props }) {
16
+ const { triggers } = useTabContext();
17
+ return /* @__PURE__ */ jsx(Tabs$1.Tab, {
18
+ ref: mergeRefs(ref, (element) => {
19
+ if (element) triggers.add(value);
20
+ else triggers.delete(value);
21
+ }),
22
+ value,
23
+ ...props
24
+ });
25
+ }
16
26
  function Tabs({ ref, groupId, persist = false, updateAnchor = false, defaultValue, value: _value, onValueChange: _onValueChange, ...props }) {
17
27
  const tabsRef = useRef(null);
18
28
  const valueToIdMap = useMemo(() => /* @__PURE__ */ new Map(), []);
19
29
  const panels = useMemo(() => /* @__PURE__ */ new Map(), []);
20
- const [value, setValue] = _value === void 0 ? useState(defaultValue) : [_value, useEffectEvent((v) => _onValueChange?.(v))];
30
+ const triggers = useMemo(() => /* @__PURE__ */ new Set(), []);
31
+ const [value, setValue] = _value === void 0 ? useState(defaultValue) : [_value, (v) => _onValueChange?.(v)];
32
+ const onChange = useEffectEvent((v) => {
33
+ if (triggers.has(v)) setValue(v);
34
+ });
35
+ const openFromHash = useEffectEvent(() => {
36
+ const hash = window.location.hash.slice(1);
37
+ if (!hash) return;
38
+ for (const [value, id] of valueToIdMap.entries()) if (id === hash) {
39
+ setValue(value);
40
+ tabsRef.current?.scrollIntoView();
41
+ return;
42
+ }
43
+ const target = document.getElementById(hash);
44
+ if (!target) return;
45
+ for (const [value, panel] of panels.entries()) {
46
+ if (!panel.contains(target)) continue;
47
+ setValue(value);
48
+ requestAnimationFrame(() => target.scrollIntoView());
49
+ return;
50
+ }
51
+ });
21
52
  useLayoutEffect(() => {
22
53
  if (!groupId) return;
23
54
  let previous = sessionStorage.getItem(groupId);
24
55
  if (persist) previous ??= localStorage.getItem(groupId);
25
- if (previous) setValue(previous);
56
+ if (previous) onChange(previous);
26
57
  const groupListeners = listeners.get(groupId) ?? /* @__PURE__ */ new Set();
27
- groupListeners.add(setValue);
58
+ groupListeners.add(onChange);
28
59
  listeners.set(groupId, groupListeners);
29
60
  return () => {
30
- groupListeners.delete(setValue);
61
+ groupListeners.delete(onChange);
31
62
  };
32
63
  }, [groupId, persist]);
33
64
  useLayoutEffect(() => {
34
- const openFromHash = () => {
35
- const hash = window.location.hash.slice(1);
36
- if (!hash) return;
37
- for (const [value, id] of valueToIdMap.entries()) if (id === hash) {
38
- setValue(value);
39
- tabsRef.current?.scrollIntoView();
40
- return;
41
- }
42
- const target = document.getElementById(hash);
43
- if (!target) return;
44
- for (const [value, panel] of panels.entries()) {
45
- if (!panel.contains(target)) continue;
46
- setValue(value);
47
- requestAnimationFrame(() => target.scrollIntoView());
48
- return;
49
- }
50
- };
51
65
  openFromHash();
52
66
  window.addEventListener("hashchange", openFromHash);
53
67
  return () => window.removeEventListener("hashchange", openFromHash);
54
- }, [valueToIdMap, panels]);
68
+ }, []);
55
69
  return /* @__PURE__ */ jsx(Tabs$1.Root, {
56
70
  ref: mergeRefs(ref, tabsRef),
57
71
  value,
@@ -71,8 +85,13 @@ function Tabs({ ref, groupId, persist = false, updateAnchor = false, defaultValu
71
85
  children: /* @__PURE__ */ jsx(TabsContext, {
72
86
  value: useMemo(() => ({
73
87
  valueToIdMap,
74
- panels
75
- }), [valueToIdMap, panels]),
88
+ panels,
89
+ triggers
90
+ }), [
91
+ valueToIdMap,
92
+ panels,
93
+ triggers
94
+ ]),
76
95
  children: props.children
77
96
  })
78
97
  });
@@ -1,6 +1,7 @@
1
1
  "use client";
2
+ import { useHotKey } from "../utils/hotkey.js";
2
3
  import { jsx, jsxs } from "react/jsx-runtime";
3
- import { Suspense, createContext, lazy, use, useEffect, useEffectEvent, useMemo, useState, useSyncExternalStore } from "react";
4
+ import { Suspense, createContext, lazy, use, useMemo, useState, useSyncExternalStore } from "react";
4
5
  import { Dialog } from "@base-ui/react/dialog";
5
6
  //#region src/contexts/search.tsx
6
7
  /** built-in Base UI Dialog handle */
@@ -29,18 +30,12 @@ const DEFAULT_HOT_KEYS = [{
29
30
  const DefaultSearchDialog = lazy(() => import("../components/dialog/search-default.js"));
30
31
  function SearchProvider({ SearchDialog = DefaultSearchDialog, children, options, hotKey = DEFAULT_HOT_KEYS, links }) {
31
32
  const [isOpen, setIsOpen] = useState(false);
32
- const onKeyDown = useEffectEvent((e) => {
33
+ useHotKey((e) => {
33
34
  if (hotKey.every((v) => typeof v.key === "string" ? e.key === v.key : v.key(e))) {
34
35
  setIsOpen((open) => !open);
35
36
  e.preventDefault();
36
37
  }
37
38
  });
38
- useEffect(() => {
39
- window.addEventListener("keydown", onKeyDown);
40
- return () => {
41
- window.removeEventListener("keydown", onKeyDown);
42
- };
43
- }, []);
44
39
  return /* @__PURE__ */ jsxs(SearchContext, {
45
40
  value: useMemo(() => ({
46
41
  enabled: true,
@@ -48,7 +48,7 @@ function useTabsGroups(tabs) {
48
48
  const out = [];
49
49
  const last = path[path.length - 1];
50
50
  const page = last?.type === "page" ? last : void 0;
51
- let scope = tree.fallback && !tree.children.includes(path[0]) ? tree.fallback : tree;
51
+ let scope = tree.fallback ? [...tree.children, ...tree.fallback.children] : tree.children;
52
52
  for (const node of path) {
53
53
  if (node.type !== "folder" || !node.root) continue;
54
54
  const group = {
@@ -57,7 +57,7 @@ function useTabsGroups(tabs) {
57
57
  };
58
58
  collectTabs(scope, node, page, tabs, group.options);
59
59
  if (group.options.length > 0) out.push(group);
60
- scope = node;
60
+ scope = node.children;
61
61
  }
62
62
  const custom = tabs.filter((tab) => !tab.$folder);
63
63
  if (custom.length > 0) {
@@ -74,7 +74,7 @@ function useTabsGroups(tabs) {
74
74
  }
75
75
  /** collect the tabs of root folders with the same type as `active` within a scope */
76
76
  function collectTabs(scope, active, page, tabs, out) {
77
- for (const node of scope.children) {
77
+ for (const node of scope) {
78
78
  if (node.type !== "folder") continue;
79
79
  if (node.root === active.root) {
80
80
  const tab = tabs.find((tab) => tab.$folder && (tab.$folder === node || tab.$folder.$id === node.$id));
@@ -84,7 +84,7 @@ function collectTabs(scope, active, page, tabs, out) {
84
84
  ...tab,
85
85
  url: projection.url
86
86
  } : tab);
87
- } else if (!node.root) collectTabs(node, active, page, tabs, out);
87
+ } else if (!node.root) collectTabs(node.children, active, page, tabs, out);
88
88
  }
89
89
  }
90
90
  //#endregion
@@ -58,6 +58,7 @@ function LayoutBody(props) {
58
58
  children: /* @__PURE__ */ jsx(slots.sidebar.provider, {
59
59
  defaultOpenLevel,
60
60
  prefetch,
61
+ collapsible: sidebarEnabled && sidebarProps.collapsible,
61
62
  children: /* @__PURE__ */ jsxs(slots.container, {
62
63
  ...containerProps,
63
64
  children: [
@@ -7,7 +7,7 @@ export interface SidebarProps extends ComponentProps<'aside'> {
7
7
  banner?: ReactNode;
8
8
  footer?: ReactNode;
9
9
  }
10
- export type SidebarProviderProps = SidebarProviderProps$1;
10
+ export type SidebarProviderProps = Omit<SidebarProviderProps$1, 'collapsible'>;
11
11
  export declare const useSidebar: typeof useSidebar$1;
12
12
  export declare function SidebarProvider(props: SidebarProviderProps): import("react").JSX.Element;
13
13
  export declare function Sidebar({ footer, banner, components, ...rest }: SidebarProps): import("react").JSX.Element;