@cbrl/design 0.9.0 → 0.10.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.
@@ -6,8 +6,26 @@ export type SettingsDialogSection<K extends string = string> = {
6
6
  icon?: React.ComponentType<{
7
7
  className?: string;
8
8
  }>;
9
- /** Shown under the title in the header while this section is active. */
9
+ /** Shown on top of the content while this section is active. */
10
10
  description?: React.ReactNode;
11
+ /** Extra terms the search matches (what the section holds, synonyms). */
12
+ keywords?: readonly string[];
13
+ /** Sections sharing a group sit together; a divider separates groups. */
14
+ group?: string;
15
+ };
16
+ export type SettingsDialogLabels = {
17
+ /** Title of the modal. */
18
+ title: string;
19
+ /** Accessible name of the section nav. */
20
+ nav: string;
21
+ /** Accessible name of the search box. */
22
+ search: string;
23
+ /** Short placeholder of the search box (the nav column is narrow). */
24
+ searchPlaceholder: string;
25
+ /** Accessible name of the search's clear button. */
26
+ clearSearch: string;
27
+ /** Shown in the nav when no section matches. */
28
+ noResults: string;
11
29
  };
12
30
  export type SettingsDialogProps<K extends string = string> = {
13
31
  open?: boolean;
@@ -19,18 +37,55 @@ export type SettingsDialogProps<K extends string = string> = {
19
37
  /** Initial section when uncontrolled. Defaults to the first section. */
20
38
  defaultValue?: K;
21
39
  onValueChange?: (key: K) => void;
22
- /** Accessible name of the section nav. */
23
- navLabel?: string;
24
- /** Overrides the header title (defaults to the active section's label). */
25
- title?: React.ReactNode;
40
+ labels?: Partial<SettingsDialogLabels>;
26
41
  /** Content of the active section; apps switch on the value. */
27
42
  children?: React.ReactNode;
28
43
  className?: string;
29
44
  };
30
45
  /**
31
- * Settings modal shell: section nav (a column from `sm`, a sideways-scrolling
32
- * strip on phones that keeps the active section in view), a header with the
33
- * active section's label/description, and a scrolling content area. Which
34
- * sections a user may see and how each loads its data stay in the app.
46
+ * Sections where every word of the query starts some word of the label or
47
+ * keywords (accents and plurals ignored).
48
+ */
49
+ export declare function matchSections<K extends string>(sections: readonly SettingsDialogSection<K>[], query: string): readonly SettingsDialogSection<K>[];
50
+ /**
51
+ * Settings modal: a title bar, then the section nav (a column from `sm`, a
52
+ * row on phones) under a search box, beside the active section's content.
53
+ * Typing filters the nav by label and keywords; Enter opens the first match;
54
+ * Escape (or the clear button) empties the search before closing.
55
+ */
56
+ export declare function SettingsDialog<K extends string>({ open, defaultOpen, onOpenChange, sections, value, defaultValue, onValueChange, labels: labelsProp, children, className }: SettingsDialogProps<K>): React.JSX.Element;
57
+ /** A titled block of settings rows ("Perfil", "Geral"…). */
58
+ export declare function SettingsGroup({ title, action, listLabel, children, className }: {
59
+ title?: React.ReactNode;
60
+ /** Right of the title (e.g. a small button acting on the whole group). */
61
+ action?: React.ReactNode;
62
+ /**
63
+ * Makes the rows a list (`ul`, named by this label) — for a variable set of
64
+ * items (accounts, units, rules). Render each row as `SettingsItem as="li"`.
65
+ */
66
+ listLabel?: string;
67
+ children?: React.ReactNode;
68
+ className?: string;
69
+ }): React.JSX.Element;
70
+ /** Muted text standing for a group with nothing to list ("Nenhuma regra ainda."). */
71
+ export declare function SettingsEmpty({ children }: {
72
+ children: React.ReactNode;
73
+ }): React.JSX.Element;
74
+ /**
75
+ * One setting: label (and description) on the left, its control on the right
76
+ * — or, with `layout="stacked"`, a large control (a textarea) under them.
77
+ * Inputs and select triggers in the control get the standard compact width.
35
78
  */
36
- export declare function SettingsDialog<K extends string>({ open, defaultOpen, onOpenChange, sections, value, defaultValue, onValueChange, navLabel, title, children, className }: SettingsDialogProps<K>): React.JSX.Element;
79
+ export declare function SettingsItem({ label, description, leading, htmlFor, layout, as: Row, children, className }: {
80
+ label: React.ReactNode;
81
+ description?: React.ReactNode;
82
+ /** Before the label: an icon or avatar identifying the row (e.g. a business). */
83
+ leading?: React.ReactNode;
84
+ /** Id of the control: the label becomes its `<label>`. */
85
+ htmlFor?: string;
86
+ layout?: 'inline' | 'stacked';
87
+ /** `li` inside a `SettingsGroup listLabel`. */
88
+ as?: 'div' | 'li';
89
+ children?: React.ReactNode;
90
+ className?: string;
91
+ }): React.JSX.Element;
@@ -1,22 +1,66 @@
1
1
  "use client";
2
+ import { isDirtyField } from "../hooks/use-autosave-form.js";
2
3
  import { useScrollStrip } from "../hooks/use-scroll-strip.js";
3
4
  import { cn } from "../lib/cn.js";
4
- import { fadeMask } from "../lib/scroll-target.js";
5
5
  import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "./dialog.js";
6
+ import { InputGroup, InputGroupAddon, InputGroupInput } from "./input-group.js";
6
7
  import { ScrollArea } from "./scroll-area.js";
7
8
  import * as React from "react";
8
9
  import { jsx, jsxs } from "react/jsx-runtime";
10
+ import { Search, X } from "lucide-react";
9
11
  // src/components/settings-dialog.tsx
12
+ const DEFAULT_LABELS = {
13
+ title: "Configurações",
14
+ nav: "Seções de configuração",
15
+ search: "Buscar configurações",
16
+ searchPlaceholder: "Procurar",
17
+ clearSearch: "Limpar busca",
18
+ noResults: "Nada encontrado"
19
+ };
20
+ /** Fades only the edges of the phone strip that hide sections (as in the DS). */
21
+ function fadeMask({ start, end }) {
22
+ if (!start && !end) return void 0;
23
+ return `linear-gradient(to right, ${start ? "transparent 0, black 1.5rem" : "black 0"}, ${end ? "black calc(100% - 1.5rem), transparent 100%" : "black 100%"})`;
24
+ }
25
+ /** Lowercase without diacritics, so "aparencia" finds "Aparência". */
26
+ function normalizeSearch(text) {
27
+ return text.normalize("NFD").replace(/\p{Diacritic}/gu, "").toLowerCase().trim();
28
+ }
29
+ /** Light plural folding, so "conexão" finds "Conexões" and "regra" "Regras". */
30
+ function stem(word) {
31
+ return word.replace(/(oes|aes)$/, "ao").replace(/(?<=.{3})s$/, "");
32
+ }
33
+ /** Each word normalized, plus its plural-folded form. */
34
+ const words = (text) => normalizeSearch(text).split(/[\s\-/]+/).filter(Boolean).flatMap((word) => [word, stem(word)]);
35
+ /**
36
+ * Sections where every word of the query starts some word of the label or
37
+ * keywords (accents and plurals ignored).
38
+ */
39
+ function matchSections(sections, query) {
40
+ const terms = normalizeSearch(query).split(/\s+/).filter(Boolean);
41
+ if (terms.length === 0) return sections;
42
+ return sections.filter((section) => {
43
+ const haystack = words([section.label, ...section.keywords ?? []].join(" "));
44
+ return terms.every((term) => [term, stem(term)].some((form) => haystack.some((word) => word.startsWith(form))));
45
+ });
46
+ }
10
47
  /**
11
- * Settings modal shell: section nav (a column from `sm`, a sideways-scrolling
12
- * strip on phones that keeps the active section in view), a header with the
13
- * active section's label/description, and a scrolling content area. Which
14
- * sections a user may see and how each loads its data stay in the app.
48
+ * Settings modal: a title bar, then the section nav (a column from `sm`, a
49
+ * row on phones) under a search box, beside the active section's content.
50
+ * Typing filters the nav by label and keywords; Enter opens the first match;
51
+ * Escape (or the clear button) empties the search before closing.
15
52
  */
16
- function SettingsDialog({ open, defaultOpen, onOpenChange, sections, value, defaultValue, onValueChange, navLabel = "Settings sections", title, children, className }) {
53
+ function SettingsDialog({ open, defaultOpen, onOpenChange, sections, value, defaultValue, onValueChange, labels: labelsProp, children, className }) {
54
+ const labels = {
55
+ ...DEFAULT_LABELS,
56
+ ...labelsProp
57
+ };
17
58
  const [inner, setInner] = React.useState(defaultValue);
59
+ const [query, setQuery] = React.useState("");
60
+ const searchRef = React.useRef(null);
18
61
  const current = value ?? inner ?? sections[0]?.key;
19
62
  const active = sections.find((s) => s.key === current);
63
+ const visible = matchSections(sections, query);
20
64
  const select = (key) => {
21
65
  if (value === void 0) setInner(key);
22
66
  onValueChange?.(key);
@@ -27,55 +71,192 @@ function SettingsDialog({ open, defaultOpen, onOpenChange, sections, value, defa
27
71
  onOpenChange,
28
72
  children: /* @__PURE__ */ jsxs(DialogContent, {
29
73
  "data-slot": "settings-dialog",
30
- className: cn("grid h-[min(85vh,40rem)] max-w-3xl grid-rows-[auto_1fr] gap-0 overflow-hidden p-0 sm:max-w-3xl sm:grid-cols-[12rem_minmax(0,1fr)] sm:grid-rows-1", className),
74
+ className: cn("flex h-[min(85vh,44rem)] max-w-4xl flex-col gap-0 overflow-hidden p-0 sm:max-w-4xl", className),
31
75
  onOpenAutoFocus: (event) => event.preventDefault(),
32
- children: [/* @__PURE__ */ jsx(SettingsDialogNav, {
33
- label: navLabel,
34
- sections,
35
- current,
36
- onSelect: select
76
+ onEscapeKeyDown: (event) => {
77
+ if (isDirtyField(document.activeElement)) {
78
+ event.preventDefault();
79
+ return;
80
+ }
81
+ if (query === "") return;
82
+ event.preventDefault();
83
+ setQuery("");
84
+ },
85
+ children: [/* @__PURE__ */ jsxs(DialogHeader, {
86
+ className: "border-b px-4 py-3 pr-12 text-left",
87
+ children: [/* @__PURE__ */ jsx(DialogTitle, {
88
+ className: "text-base",
89
+ children: labels.title
90
+ }), /* @__PURE__ */ jsx(DialogDescription, {
91
+ className: "sr-only",
92
+ children: active?.label
93
+ })]
37
94
  }), /* @__PURE__ */ jsxs("div", {
38
- className: "flex min-h-0 min-w-0 flex-col",
39
- children: [/* @__PURE__ */ jsxs(DialogHeader, {
40
- className: "gap-1 border-b p-6 pr-14",
41
- children: [/* @__PURE__ */ jsx(DialogTitle, { children: title ?? active?.label }), active?.description ? /* @__PURE__ */ jsx(DialogDescription, { children: active.description }) : /* @__PURE__ */ jsx(DialogDescription, {
42
- className: "sr-only",
43
- children: active?.label
95
+ className: "grid min-h-0 flex-1 grid-rows-[auto_1fr] sm:grid-cols-[14rem_minmax(0,1fr)] sm:grid-rows-1",
96
+ children: [/* @__PURE__ */ jsxs("div", {
97
+ className: "flex min-w-0 flex-col gap-2 border-b p-3 sm:border-r sm:border-b-0",
98
+ children: [/* @__PURE__ */ jsxs(InputGroup, {
99
+ className: "bg-muted/70 dark:bg-muted/50 h-8 border-transparent shadow-none",
100
+ children: [
101
+ /* @__PURE__ */ jsx(InputGroupAddon, {
102
+ align: "inline-start",
103
+ children: /* @__PURE__ */ jsx(Search, { "aria-hidden": true })
104
+ }),
105
+ /* @__PURE__ */ jsx(InputGroupInput, {
106
+ ref: searchRef,
107
+ type: "search",
108
+ value: query,
109
+ placeholder: labels.searchPlaceholder,
110
+ "aria-label": labels.search,
111
+ className: "[&::-webkit-search-cancel-button]:appearance-none",
112
+ onChange: (event) => setQuery(event.target.value),
113
+ onKeyDown: (event) => {
114
+ const first = visible[0];
115
+ if (event.key === "Enter" && first !== void 0) {
116
+ event.preventDefault();
117
+ select(first.key);
118
+ }
119
+ }
120
+ }),
121
+ query === "" ? null : /* @__PURE__ */ jsx(InputGroupAddon, {
122
+ align: "inline-end",
123
+ children: /* @__PURE__ */ jsx("button", {
124
+ type: "button",
125
+ "aria-label": labels.clearSearch,
126
+ onClick: () => {
127
+ setQuery("");
128
+ searchRef.current?.focus();
129
+ },
130
+ className: "text-muted-foreground hover:text-foreground focus-visible:ring-ring/50 flex size-5 cursor-pointer items-center justify-center rounded-sm outline-none focus-visible:ring-2",
131
+ children: /* @__PURE__ */ jsx(X, {
132
+ "aria-hidden": true,
133
+ className: "size-3.5"
134
+ })
135
+ })
136
+ })
137
+ ]
138
+ }), /* @__PURE__ */ jsx(SettingsNav, {
139
+ label: labels.nav,
140
+ noResults: labels.noResults,
141
+ sections: visible,
142
+ current,
143
+ onSelect: select
44
144
  })]
45
145
  }), /* @__PURE__ */ jsx(ScrollArea, {
46
- className: "min-h-0 flex-1 [&_[data-slot=scroll-area-viewport]>div]:!block",
47
- children: /* @__PURE__ */ jsx("div", {
146
+ className: "min-h-0 [&_[data-slot=scroll-area-viewport]>div]:!block",
147
+ children: /* @__PURE__ */ jsxs("div", {
148
+ role: "region",
149
+ "aria-label": active?.label,
48
150
  "data-slot": "settings-dialog-content",
49
- className: "p-6",
50
- children
151
+ className: "grid gap-8 px-6 py-5",
152
+ children: [active?.description ? /* @__PURE__ */ jsx("p", {
153
+ className: "text-muted-foreground -mb-4 text-sm",
154
+ children: active.description
155
+ }) : null, children]
51
156
  })
52
157
  })]
53
158
  })]
54
159
  })
55
160
  });
56
161
  }
57
- function SettingsDialogNav({ label, sections, current, onSelect }) {
162
+ function SettingsNav({ label, noResults, sections, current, onSelect }) {
58
163
  const ref = React.useRef(null);
59
164
  const edges = useScrollStrip(ref, "[aria-current=\"page\"]");
60
- return /* @__PURE__ */ jsx("nav", {
165
+ return /* @__PURE__ */ jsxs("nav", {
61
166
  ref,
62
167
  "aria-label": label,
63
168
  "data-slot": "settings-dialog-nav",
64
169
  "data-fade-start": edges.start || void 0,
65
170
  "data-fade-end": edges.end || void 0,
66
171
  style: { maskImage: fadeMask(edges) },
67
- className: "bg-muted/40 flex [scrollbar-width:none] gap-0.5 overflow-x-auto border-b p-2 sm:flex-col sm:overflow-x-visible sm:border-r sm:border-b-0 [&::-webkit-scrollbar]:hidden",
68
- children: sections.map(({ key, label: itemLabel, icon: Icon }) => /* @__PURE__ */ jsxs("button", {
172
+ className: "flex [scrollbar-width:none] gap-0.5 overflow-x-auto sm:flex-col sm:overflow-x-visible [&::-webkit-scrollbar]:hidden",
173
+ children: [sections.map(({ key, label: itemLabel, icon: Icon, group }, index) => /* @__PURE__ */ jsxs(React.Fragment, { children: [index > 0 && group !== sections[index - 1]?.group ? /* @__PURE__ */ jsx("span", {
174
+ role: "separator",
175
+ className: "bg-border mx-2.5 my-2 hidden h-px sm:block"
176
+ }) : null, /* @__PURE__ */ jsxs("button", {
69
177
  type: "button",
70
178
  onClick: () => onSelect(key),
71
179
  "aria-current": key === current ? "page" : void 0,
72
- className: "text-muted-foreground hover:bg-background/60 aria-[current=page]:bg-background aria-[current=page]:text-foreground focus-visible:ring-ring/50 flex shrink-0 cursor-pointer items-center gap-2 rounded-md px-3 py-2 text-left text-sm font-normal whitespace-nowrap transition-colors outline-none focus-visible:ring-3 aria-[current=page]:font-medium",
180
+ className: "text-foreground/80 hover:bg-muted aria-[current=page]:bg-muted aria-[current=page]:text-foreground focus-visible:ring-ring/50 flex shrink-0 cursor-pointer items-center gap-2.5 rounded-md px-2.5 py-1.5 text-left text-sm whitespace-nowrap transition-colors outline-none focus-visible:ring-3",
73
181
  children: [Icon ? /* @__PURE__ */ jsx("span", {
74
182
  "aria-hidden": true,
75
183
  className: "flex shrink-0",
76
184
  children: /* @__PURE__ */ jsx(Icon, { className: "size-4" })
77
185
  }) : null, itemLabel]
78
- }, key))
186
+ })] }, key)), sections.length === 0 ? /* @__PURE__ */ jsx("p", {
187
+ role: "status",
188
+ className: "text-muted-foreground px-2.5 py-1.5 text-sm",
189
+ children: noResults
190
+ }) : null]
191
+ });
192
+ }
193
+ /** A titled block of settings rows ("Perfil", "Geral"…). */
194
+ function SettingsGroup({ title, action, listLabel, children, className }) {
195
+ return /* @__PURE__ */ jsxs("section", {
196
+ "data-slot": "settings-group",
197
+ className: cn("grid gap-1", className),
198
+ children: [title || action ? /* @__PURE__ */ jsxs("div", {
199
+ className: "flex min-h-8 items-center justify-between gap-4",
200
+ children: [title ? /* @__PURE__ */ jsx("h3", {
201
+ className: "text-sm font-medium",
202
+ children: title
203
+ }) : /* @__PURE__ */ jsx("span", {}), action ? /* @__PURE__ */ jsx("div", {
204
+ className: "flex items-center gap-2",
205
+ children: action
206
+ }) : null]
207
+ }) : null, listLabel === void 0 ? /* @__PURE__ */ jsx("div", {
208
+ className: "grid",
209
+ children
210
+ }) : /* @__PURE__ */ jsx("ul", {
211
+ "aria-label": listLabel,
212
+ className: "grid",
213
+ children
214
+ })]
215
+ });
216
+ }
217
+ /** Muted text standing for a group with nothing to list ("Nenhuma regra ainda."). */
218
+ function SettingsEmpty({ children }) {
219
+ return /* @__PURE__ */ jsx("p", {
220
+ "data-slot": "settings-empty",
221
+ className: "text-muted-foreground py-2.5 text-sm",
222
+ children
223
+ });
224
+ }
225
+ /**
226
+ * One setting: label (and description) on the left, its control on the right
227
+ * — or, with `layout="stacked"`, a large control (a textarea) under them.
228
+ * Inputs and select triggers in the control get the standard compact width.
229
+ */
230
+ function SettingsItem({ label, description, leading, htmlFor, layout = "inline", as: Row = "div", children, className }) {
231
+ const stacked = layout === "stacked";
232
+ return /* @__PURE__ */ jsxs(Row, {
233
+ "data-slot": "settings-item",
234
+ "data-layout": layout,
235
+ className: cn("flex min-h-12 gap-6 border-b py-2.5 last:border-b-0", stacked ? "flex-col gap-2" : "items-center justify-between", className),
236
+ children: [/* @__PURE__ */ jsxs("div", {
237
+ className: "flex min-w-0 items-center gap-3",
238
+ children: [leading ? /* @__PURE__ */ jsx("span", {
239
+ className: "flex shrink-0",
240
+ children: leading
241
+ }) : null, /* @__PURE__ */ jsxs("div", {
242
+ className: "grid min-w-0 gap-0.5",
243
+ children: [htmlFor ? /* @__PURE__ */ jsx("label", {
244
+ htmlFor,
245
+ className: "text-sm",
246
+ children: label
247
+ }) : /* @__PURE__ */ jsx("div", {
248
+ className: "text-sm",
249
+ children: label
250
+ }), description ? /* @__PURE__ */ jsx("p", {
251
+ className: "text-muted-foreground text-sm leading-snug",
252
+ children: description
253
+ }) : null]
254
+ })]
255
+ }), children === void 0 ? null : /* @__PURE__ */ jsx("div", {
256
+ "data-slot": "settings-item-control",
257
+ className: cn(stacked ? "grid" : "flex shrink-0 items-center gap-2 [&_[data-slot=input]]:w-56 [&_[data-slot=select-trigger]]:w-56"),
258
+ children
259
+ })]
79
260
  });
80
261
  }
81
- export { SettingsDialog };
262
+ export { SettingsDialog, SettingsEmpty, SettingsGroup, SettingsItem, matchSections, normalizeSearch };
@@ -12,7 +12,7 @@ import { PanelLeftIcon } from "lucide-react";
12
12
  // src/components/sidebar-core.tsx
13
13
  const SIDEBAR_COOKIE_NAME = "sidebar_state";
14
14
  const SIDEBAR_COOKIE_MAX_AGE = 604800;
15
- const SIDEBAR_WIDTH = "14rem";
15
+ const SIDEBAR_WIDTH = "20rem";
16
16
  const SIDEBAR_WIDTH_MOBILE = "18rem";
17
17
  const SIDEBAR_WIDTH_ICON = "3rem";
18
18
  const SIDEBAR_KEYBOARD_SHORTCUT = "b";
@@ -107,7 +107,7 @@ function Sidebar({ side = "left", variant = "sidebar", collapsible = "offcanvas"
107
107
  side,
108
108
  children: [/* @__PURE__ */ jsxs(SheetHeader, {
109
109
  className: "sr-only",
110
- children: [/* @__PURE__ */ jsx(SheetTitle, { children: "Sidebar" }), /* @__PURE__ */ jsx(SheetDescription, { children: "Displays the mobile sidebar." })]
110
+ children: [/* @__PURE__ */ jsx(SheetTitle, { children: "Barra lateral" }), /* @__PURE__ */ jsx(SheetDescription, { children: "Navegação do aplicativo." })]
111
111
  }), /* @__PURE__ */ jsx("div", {
112
112
  className: "flex h-full w-full flex-col",
113
113
  children
@@ -153,7 +153,7 @@ function SidebarTrigger({ className, onClick, ...props }) {
153
153
  ...props,
154
154
  children: [/* @__PURE__ */ jsx(PanelLeftIcon, {}), /* @__PURE__ */ jsx("span", {
155
155
  className: "sr-only",
156
- children: "Toggle Sidebar"
156
+ children: "Alternar barra lateral"
157
157
  })]
158
158
  });
159
159
  }
@@ -162,10 +162,10 @@ function SidebarRail({ className, ...props }) {
162
162
  return /* @__PURE__ */ jsx("button", {
163
163
  "data-sidebar": "rail",
164
164
  "data-slot": "sidebar-rail",
165
- "aria-label": "Toggle Sidebar",
165
+ "aria-label": "Alternar barra lateral",
166
166
  tabIndex: -1,
167
167
  onClick: toggleSidebar,
168
- title: "Toggle Sidebar",
168
+ title: "Alternar barra lateral",
169
169
  className: cn("hover:after:bg-sidebar-border absolute inset-y-0 z-20 hidden w-4 transition-all ease-linear group-data-[side=left]:-right-4 group-data-[side=right]:left-0 after:absolute after:inset-y-0 after:start-1/2 after:w-[2px] sm:flex ltr:-translate-x-1/2 rtl:-translate-x-1/2", "in-data-[side=left]:cursor-w-resize in-data-[side=right]:cursor-e-resize", "[[data-side=left][data-state=collapsed]_&]:cursor-e-resize [[data-side=right][data-state=collapsed]_&]:cursor-w-resize", "hover:group-data-[collapsible=offcanvas]:bg-sidebar group-data-[collapsible=offcanvas]:translate-x-0 group-data-[collapsible=offcanvas]:after:left-full", "[[data-side=left][data-collapsible=offcanvas]_&]:-right-2", "[[data-side=right][data-collapsible=offcanvas]_&]:-left-2", className),
170
170
  ...props
171
171
  });
@@ -0,0 +1,40 @@
1
+ import { Sidebar } from "./sidebar-core.js";
2
+ import "./sidebar.js";
3
+ import * as React from "react";
4
+ /** Whether the collapsed sidebar is peeking over the content right now. */
5
+ export declare function useSidebarPeek(): {
6
+ peeking: boolean;
7
+ };
8
+ /** Goes inside `SidebarProvider`, around `PeekSidebar` and `AppToolbar`. */
9
+ export declare function SidebarPeekProvider({ children }: {
10
+ children: React.ReactNode;
11
+ }): React.JSX.Element;
12
+ /**
13
+ * The DS `Sidebar`, off-canvas when collapsed, that slides in over the
14
+ * content while peeking. Its rail starts a peek from the window's left edge.
15
+ */
16
+ export declare function PeekSidebar({ className, children, ...props }: React.ComponentProps<typeof Sidebar>): React.JSX.Element;
17
+ export type AppToolbarLabels = {
18
+ show: string;
19
+ hide: string;
20
+ back: string;
21
+ forward: string;
22
+ /** Shortcut hint in the toggle's tooltip. */
23
+ shortcut: string;
24
+ };
25
+ /** Space the fixed toolbar takes on top of the sidebar and the content (md+). */
26
+ export declare const APP_TOOLBAR_HEIGHT = "h-12";
27
+ /**
28
+ * Fixed on the top left (md+): the sidebar toggle (hover peeks, click pins)
29
+ * and the history back/forward buttons. Leave `APP_TOOLBAR_HEIGHT` free at the
30
+ * top of the sidebar and of the content.
31
+ */
32
+ export declare function AppToolbar({ labels: labelsProp, onBack, onForward, className }: {
33
+ labels?: Partial<AppToolbarLabels>;
34
+ /** Replaces the browser's history back (the button then stays enabled). */
35
+ onBack?: () => void;
36
+ /** Replaces the browser's history forward (the button then stays enabled). */
37
+ onForward?: () => void;
38
+ /** E.g. `h-14` to line up with a 56px header (default `h-12`). */
39
+ className?: string;
40
+ }): React.JSX.Element;
@@ -0,0 +1,204 @@
1
+ "use client";
2
+ import { cn } from "../lib/cn.js";
3
+ import { Button } from "./button.js";
4
+ import { Sidebar, SidebarRail, useSidebar } from "./sidebar-core.js";
5
+ import { Tooltip, TooltipContent, TooltipTrigger } from "./tooltip.js";
6
+ import * as React from "react";
7
+ import { jsx, jsxs } from "react/jsx-runtime";
8
+ import { ChevronLeft, ChevronRight, PanelLeft } from "lucide-react";
9
+ // src/components/sidebar-peek.tsx
10
+ /** Pointer grace before a peek closes (crossing gaps, small overshoots). */
11
+ const PEEK_CLOSE_DELAY = 250;
12
+ /** A menu (or popover) opened from the sidebar is on screen: its trigger is open. */
13
+ function hasOpenPopup(container) {
14
+ return container?.querySelector("[data-state=\"open\"]") != null;
15
+ }
16
+ /** A click inside the sidebar or inside a popup it opened (portaled content). */
17
+ function isInsidePeek(target, container) {
18
+ if (!(target instanceof Element)) return false;
19
+ return (container?.contains(target) ?? false) || target.closest("[data-radix-popper-content-wrapper]") !== null;
20
+ }
21
+ const PeekContext = React.createContext(null);
22
+ function usePeek() {
23
+ const peek = React.useContext(PeekContext);
24
+ if (peek === null) throw new Error("usePeek must be used within <SidebarPeekProvider>");
25
+ return peek;
26
+ }
27
+ /** Whether the collapsed sidebar is peeking over the content right now. */
28
+ function useSidebarPeek() {
29
+ return { peeking: usePeek().peeking };
30
+ }
31
+ /** Goes inside `SidebarProvider`, around `PeekSidebar` and `AppToolbar`. */
32
+ function SidebarPeekProvider({ children }) {
33
+ const { open, isMobile } = useSidebar();
34
+ const [peeking, setPeeking] = React.useState(false);
35
+ const timer = React.useRef(null);
36
+ const containerRef = React.useRef(null);
37
+ const outsideClick = React.useRef(null);
38
+ const stopHolding = () => {
39
+ if (outsideClick.current !== null) document.removeEventListener("pointerdown", outsideClick.current, true);
40
+ outsideClick.current = null;
41
+ };
42
+ const clear = () => {
43
+ if (timer.current !== null) clearTimeout(timer.current);
44
+ timer.current = null;
45
+ stopHolding();
46
+ };
47
+ const holdUntilOutsideClick = () => {
48
+ stopHolding();
49
+ const listener = (event) => {
50
+ if (isInsidePeek(event.target, containerRef.current)) return;
51
+ stopHolding();
52
+ setPeeking(false);
53
+ };
54
+ outsideClick.current = listener;
55
+ document.addEventListener("pointerdown", listener, true);
56
+ };
57
+ const value = React.useMemo(() => ({
58
+ peeking: peeking && !open && !isMobile,
59
+ show: () => {
60
+ clear();
61
+ setPeeking(true);
62
+ },
63
+ hideSoon: () => {
64
+ clear();
65
+ timer.current = setTimeout(() => {
66
+ timer.current = null;
67
+ if (hasOpenPopup(containerRef.current)) holdUntilOutsideClick();
68
+ else setPeeking(false);
69
+ }, PEEK_CLOSE_DELAY);
70
+ },
71
+ hideNow: () => {
72
+ clear();
73
+ setPeeking(false);
74
+ },
75
+ containerRef
76
+ }), [
77
+ peeking,
78
+ open,
79
+ isMobile
80
+ ]);
81
+ React.useEffect(() => clear, []);
82
+ return /* @__PURE__ */ jsx(PeekContext.Provider, {
83
+ value,
84
+ children
85
+ });
86
+ }
87
+ /**
88
+ * The DS `Sidebar`, off-canvas when collapsed, that slides in over the
89
+ * content while peeking. Its rail starts a peek from the window's left edge.
90
+ */
91
+ function PeekSidebar({ className, children, ...props }) {
92
+ const { open } = useSidebar();
93
+ const { peeking, show, hideSoon, containerRef } = usePeek();
94
+ return /* @__PURE__ */ jsxs(Sidebar, {
95
+ ref: containerRef,
96
+ collapsible: "offcanvas",
97
+ "data-peeking": peeking || void 0,
98
+ onMouseEnter: peeking ? show : void 0,
99
+ onMouseLeave: peeking ? hideSoon : void 0,
100
+ className: cn(peeking && "!left-0 z-40 shadow-xl", className),
101
+ ...props,
102
+ children: [children, /* @__PURE__ */ jsx(SidebarRail, { onMouseEnter: open ? void 0 : show })]
103
+ });
104
+ }
105
+ const DEFAULT_TOOLBAR_LABELS = {
106
+ show: "Mostrar barra lateral",
107
+ hide: "Ocultar barra lateral",
108
+ back: "Voltar",
109
+ forward: "Avançar",
110
+ shortcut: "Ctrl+B"
111
+ };
112
+ const browserNavigation = () => window.navigation;
113
+ const subscribeHistory = (onChange) => {
114
+ const navigation = browserNavigation();
115
+ navigation?.addEventListener("currententrychange", onChange);
116
+ return () => navigation?.removeEventListener("currententrychange", onChange);
117
+ };
118
+ const historySnapshot = () => {
119
+ const navigation = browserNavigation();
120
+ if (navigation === void 0) return "?";
121
+ return `${navigation.canGoBack ? 1 : 0}|${navigation.canGoForward ? 1 : 0}`;
122
+ };
123
+ /**
124
+ * Whether the browser history can go back / forward, from the Navigation API
125
+ * (`navigation.canGoBack` / `canGoForward`, updated on every entry change —
126
+ * router pushes included). Router-agnostic: it reads the browser, not the app.
127
+ */
128
+ function useHistoryMoves() {
129
+ const snapshot = React.useSyncExternalStore(subscribeHistory, historySnapshot, () => "?");
130
+ if (snapshot === "?") return {
131
+ back: true,
132
+ forward: true
133
+ };
134
+ return {
135
+ back: snapshot.startsWith("1"),
136
+ forward: snapshot.endsWith("1")
137
+ };
138
+ }
139
+ /** Space the fixed toolbar takes on top of the sidebar and the content (md+). */
140
+ const APP_TOOLBAR_HEIGHT = "h-12";
141
+ /**
142
+ * Fixed on the top left (md+): the sidebar toggle (hover peeks, click pins)
143
+ * and the history back/forward buttons. Leave `APP_TOOLBAR_HEIGHT` free at the
144
+ * top of the sidebar and of the content.
145
+ */
146
+ function AppToolbar({ labels: labelsProp, onBack, onForward, className }) {
147
+ const labels = {
148
+ ...DEFAULT_TOOLBAR_LABELS,
149
+ ...labelsProp
150
+ };
151
+ const { open, toggleSidebar } = useSidebar();
152
+ const { show, hideSoon, hideNow } = usePeek();
153
+ const toggleLabel = open ? labels.hide : labels.show;
154
+ const moves = useHistoryMoves();
155
+ return /* @__PURE__ */ jsxs("div", {
156
+ "data-slot": "app-toolbar",
157
+ className: cn("fixed top-0 left-0 z-50 hidden items-center gap-0.5 px-2 md:flex", APP_TOOLBAR_HEIGHT, className),
158
+ children: [
159
+ /* @__PURE__ */ jsxs(Tooltip, { children: [/* @__PURE__ */ jsx(TooltipTrigger, {
160
+ asChild: true,
161
+ children: /* @__PURE__ */ jsx(Button, {
162
+ variant: "ghost",
163
+ size: "icon-sm",
164
+ "aria-label": toggleLabel,
165
+ "aria-expanded": open,
166
+ onMouseEnter: open ? void 0 : show,
167
+ onMouseLeave: open ? void 0 : hideSoon,
168
+ onClick: () => {
169
+ hideNow();
170
+ toggleSidebar();
171
+ },
172
+ children: /* @__PURE__ */ jsx(PanelLeft, { "aria-hidden": true })
173
+ })
174
+ }), /* @__PURE__ */ jsxs(TooltipContent, {
175
+ side: "right",
176
+ children: [
177
+ toggleLabel,
178
+ " ",
179
+ /* @__PURE__ */ jsx("kbd", {
180
+ className: "ml-1 opacity-70",
181
+ children: labels.shortcut
182
+ })
183
+ ]
184
+ })] }),
185
+ /* @__PURE__ */ jsx(Button, {
186
+ variant: "ghost",
187
+ size: "icon-sm",
188
+ "aria-label": labels.back,
189
+ disabled: onBack === void 0 && !moves.back,
190
+ onClick: onBack ?? (() => window.history.back()),
191
+ children: /* @__PURE__ */ jsx(ChevronLeft, { "aria-hidden": true })
192
+ }),
193
+ /* @__PURE__ */ jsx(Button, {
194
+ variant: "ghost",
195
+ size: "icon-sm",
196
+ "aria-label": labels.forward,
197
+ disabled: onForward === void 0 && !moves.forward,
198
+ onClick: onForward ?? (() => window.history.forward()),
199
+ children: /* @__PURE__ */ jsx(ChevronRight, { "aria-hidden": true })
200
+ })
201
+ ]
202
+ });
203
+ }
204
+ export { APP_TOOLBAR_HEIGHT, AppToolbar, PeekSidebar, SidebarPeekProvider, hasOpenPopup, isInsidePeek, useSidebarPeek };