@nswds/ui 4.3.0 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/expandable-search.d.ts +119 -0
- package/dist/components/expandable-search.js +252 -0
- package/dist/components/expandable-search.js.map +1 -0
- package/dist/components/main-nav.d.ts +144 -0
- package/dist/components/main-nav.js +366 -0
- package/dist/components/main-nav.js.map +1 -0
- package/dist/components/navigation-menu.d.ts +148 -0
- package/dist/components/navigation-menu.js +205 -0
- package/dist/components/navigation-menu.js.map +1 -0
- package/dist/components/push-menu.d.ts +149 -0
- package/dist/components/push-menu.js +357 -0
- package/dist/components/push-menu.js.map +1 -0
- package/dist/components/side-nav.d.ts +116 -0
- package/dist/components/side-nav.js +180 -0
- package/dist/components/side-nav.js.map +1 -0
- package/dist/components/site-search.d.ts +115 -0
- package/dist/components/site-search.js +204 -0
- package/dist/components/site-search.js.map +1 -0
- package/dist/components/step-indicator.d.ts +160 -0
- package/dist/components/step-indicator.js +260 -0
- package/dist/components/step-indicator.js.map +1 -0
- package/dist/components/theme-switcher.d.ts +70 -0
- package/dist/components/theme-switcher.js +52 -0
- package/dist/components/theme-switcher.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +7 -0
- package/dist/styles.css +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
3
|
+
import { cva } from 'class-variance-authority';
|
|
4
|
+
import { cn } from '../lib/utils.js';
|
|
5
|
+
import { Collapsible, CollapsibleTrigger, CollapsibleContent } from '../components/collapsible.js';
|
|
6
|
+
import { Link } from '../components/link.js';
|
|
7
|
+
import { IconChevronRight } from '../icons/chevron-right.js';
|
|
8
|
+
|
|
9
|
+
const sideNavRowVariants = cva(
|
|
10
|
+
[
|
|
11
|
+
"w-full cursor-pointer rounded-r-sm border-l border-transparent py-1 pr-2 pl-4 text-left text-base/8 sm:text-sm/6",
|
|
12
|
+
"transition-colors motion-reduce:transition-none",
|
|
13
|
+
"hover:border-grey-950 hover:bg-primary-800/10 hover:font-semibold hover:text-grey-950",
|
|
14
|
+
"dark:text-grey-400 dark:hover:border-grey-400 dark:hover:text-white",
|
|
15
|
+
"focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-current"
|
|
16
|
+
],
|
|
17
|
+
{
|
|
18
|
+
variants: {
|
|
19
|
+
current: {
|
|
20
|
+
true: "border-primary-800 bg-primary-800/10 font-bold text-primary-800 dark:border-white dark:bg-white/20 dark:text-white",
|
|
21
|
+
false: ""
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
defaultVariants: {
|
|
25
|
+
current: false
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
);
|
|
29
|
+
function isItemOrDescendantCurrent(item, currentHref) {
|
|
30
|
+
if (currentHref === void 0) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
if (item.href === currentHref) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return item.links?.some((child) => isItemOrDescendantCurrent(child, currentHref)) ?? false;
|
|
37
|
+
}
|
|
38
|
+
function warnIfItemMisshapen(item) {
|
|
39
|
+
if (process.env.NODE_ENV === "production") {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const hasChildren = (item.links?.length ?? 0) > 0;
|
|
43
|
+
if (!hasChildren && item.href === void 0) {
|
|
44
|
+
console.warn(
|
|
45
|
+
`[nswds/ui] SideNav item "${item.title}" has neither href nor links \u2014 it renders as inert text. Give it a destination or children.`
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
if (hasChildren && item.href !== void 0) {
|
|
49
|
+
console.warn(
|
|
50
|
+
`[nswds/ui] SideNav item "${item.title}" has both href and links \u2014 branch rows render as collapsible triggers, so its href is unreachable. Add a separate leaf item for the destination.`
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function SideNavList({ items, currentHref, onNavigate, nested = false }) {
|
|
55
|
+
return /* @__PURE__ */ jsx(
|
|
56
|
+
"ul",
|
|
57
|
+
{
|
|
58
|
+
role: "list",
|
|
59
|
+
"data-slot": "side-nav-list",
|
|
60
|
+
className: cn(
|
|
61
|
+
"flex flex-col border-l border-grey-400 dark:border-grey-200/15",
|
|
62
|
+
nested ? "mt-2 ml-5 gap-1" : "mt-2 gap-2 lg:mt-4"
|
|
63
|
+
),
|
|
64
|
+
children: items.map((item) => /* @__PURE__ */ jsx(
|
|
65
|
+
SideNavRow,
|
|
66
|
+
{
|
|
67
|
+
item,
|
|
68
|
+
currentHref,
|
|
69
|
+
onNavigate
|
|
70
|
+
},
|
|
71
|
+
item.href ?? item.title
|
|
72
|
+
))
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
function SideNavRow({ item, currentHref, onNavigate }) {
|
|
77
|
+
const hasChildren = (item.links?.length ?? 0) > 0;
|
|
78
|
+
const isCurrent = currentHref !== void 0 && item.href === currentHref;
|
|
79
|
+
warnIfItemMisshapen(item);
|
|
80
|
+
if (hasChildren) {
|
|
81
|
+
return /* @__PURE__ */ jsx("li", { "data-slot": "side-nav-item", className: "-ml-px flex w-full flex-col items-start gap-1", children: /* @__PURE__ */ jsxs(Collapsible, { className: "w-full", defaultOpen: isItemOrDescendantCurrent(item, currentHref), children: [
|
|
82
|
+
/* @__PURE__ */ jsxs(
|
|
83
|
+
CollapsibleTrigger,
|
|
84
|
+
{
|
|
85
|
+
"data-slot": "side-nav-trigger",
|
|
86
|
+
className: cn(sideNavRowVariants(), "group flex items-center justify-between gap-1"),
|
|
87
|
+
children: [
|
|
88
|
+
/* @__PURE__ */ jsx("span", { children: item.title }),
|
|
89
|
+
/* @__PURE__ */ jsx(
|
|
90
|
+
IconChevronRight,
|
|
91
|
+
{
|
|
92
|
+
"aria-hidden": "true",
|
|
93
|
+
className: "ml-2 size-5 shrink-0 transition-transform duration-200 group-data-panel-open:rotate-90 motion-reduce:transition-none"
|
|
94
|
+
}
|
|
95
|
+
)
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
),
|
|
99
|
+
/* @__PURE__ */ jsx(CollapsibleContent, { "data-slot": "side-nav-panel", children: /* @__PURE__ */ jsx(
|
|
100
|
+
SideNavList,
|
|
101
|
+
{
|
|
102
|
+
items: item.links ?? [],
|
|
103
|
+
currentHref,
|
|
104
|
+
onNavigate,
|
|
105
|
+
nested: true
|
|
106
|
+
}
|
|
107
|
+
) })
|
|
108
|
+
] }) });
|
|
109
|
+
}
|
|
110
|
+
if (item.href === void 0) {
|
|
111
|
+
return /* @__PURE__ */ jsx("li", { "data-slot": "side-nav-item", className: "-ml-px flex w-full flex-col items-start gap-1", children: /* @__PURE__ */ jsx(
|
|
112
|
+
"span",
|
|
113
|
+
{
|
|
114
|
+
"data-slot": "side-nav-text",
|
|
115
|
+
className: cn(sideNavRowVariants(), "inline-block cursor-default"),
|
|
116
|
+
children: item.title
|
|
117
|
+
}
|
|
118
|
+
) });
|
|
119
|
+
}
|
|
120
|
+
return /* @__PURE__ */ jsx("li", { "data-slot": "side-nav-item", className: "-ml-px flex w-full flex-col items-start gap-1", children: /* @__PURE__ */ jsx(
|
|
121
|
+
Link,
|
|
122
|
+
{
|
|
123
|
+
variant: "unstyled",
|
|
124
|
+
"data-slot": "side-nav-link",
|
|
125
|
+
href: item.href,
|
|
126
|
+
onClick: onNavigate,
|
|
127
|
+
"aria-current": isCurrent ? "page" : void 0,
|
|
128
|
+
className: cn(sideNavRowVariants({ current: isCurrent }), "inline-block"),
|
|
129
|
+
children: item.title
|
|
130
|
+
}
|
|
131
|
+
) });
|
|
132
|
+
}
|
|
133
|
+
function SideNav({
|
|
134
|
+
className,
|
|
135
|
+
sections,
|
|
136
|
+
currentHref,
|
|
137
|
+
onNavigate,
|
|
138
|
+
headingLevel = 2,
|
|
139
|
+
"aria-label": ariaLabel = "Section navigation",
|
|
140
|
+
ref,
|
|
141
|
+
...props
|
|
142
|
+
}) {
|
|
143
|
+
const Heading = `h${headingLevel}`;
|
|
144
|
+
return /* @__PURE__ */ jsx(
|
|
145
|
+
"nav",
|
|
146
|
+
{
|
|
147
|
+
"data-slot": "side-nav",
|
|
148
|
+
...props,
|
|
149
|
+
"aria-label": ariaLabel,
|
|
150
|
+
className: cn("text-base text-grey-800 lg:text-sm dark:text-grey-400", className),
|
|
151
|
+
ref,
|
|
152
|
+
children: /* @__PURE__ */ jsx("ul", { role: "list", "data-slot": "side-nav-sections", className: "flex flex-col gap-9", children: sections.map((section) => /* @__PURE__ */ jsx("li", { "data-slot": "side-nav-section", children: section.links ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
153
|
+
/* @__PURE__ */ jsx(
|
|
154
|
+
Heading,
|
|
155
|
+
{
|
|
156
|
+
"data-slot": "side-nav-heading",
|
|
157
|
+
className: "font-display font-medium text-grey-800 dark:text-white",
|
|
158
|
+
children: section.title
|
|
159
|
+
}
|
|
160
|
+
),
|
|
161
|
+
/* @__PURE__ */ jsx(
|
|
162
|
+
SideNavList,
|
|
163
|
+
{
|
|
164
|
+
items: section.links,
|
|
165
|
+
currentHref,
|
|
166
|
+
onNavigate
|
|
167
|
+
}
|
|
168
|
+
)
|
|
169
|
+
] }) : (
|
|
170
|
+
// A link-less section is a single rail link (Navigation.tsx
|
|
171
|
+
// parity) — reuse the rail so the border/offset stay identical.
|
|
172
|
+
/* @__PURE__ */ jsx(SideNavList, { items: [section], currentHref, onNavigate })
|
|
173
|
+
) }, section.href ?? section.title)) })
|
|
174
|
+
}
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export { SideNav, sideNavRowVariants };
|
|
179
|
+
//# sourceMappingURL=side-nav.js.map
|
|
180
|
+
//# sourceMappingURL=side-nav.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/side-nav.tsx"],"names":[],"mappings":";;;;;;;AA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AAY,KAAA,EAAO,CAAC,OAAO,CAAA,EAAG,aAA0B,UAAA,EAAwB;AAAA,OAAA,EAAA,EArB5E,OAAA,CAAQ,IAAA,IAAQ,OAAA,CAAQ,KAuBjC,CACD,CAAA,EACH;AAAA;AAAA,GACF;AAEJ","file":"side-nav.js","sourcesContent":["'use client'\n\nimport { cva, type VariantProps } from 'class-variance-authority'\nimport React from 'react'\n\nimport { cn } from '../lib/utils.js'\n\nimport { Collapsible, CollapsibleContent, CollapsibleTrigger } from '../components/collapsible.js'\nimport { Link } from '../components/link.js'\nimport { IconChevronRight } from '../icons/chevron-right.js'\n\n/**\n * One entry in a `SideNav` tree.\n *\n * - `href` and no `links` — a leaf link.\n * - `links` — a branch. At the top level it renders as a section heading over an\n * always-visible list; at any deeper level it renders as a collapsible\n * trigger. A branch's own `href` is IGNORED (the row is a disclosure button,\n * not a link) — a dev-only warning flags it, mirroring the nswds-app\n * `SidebarNavigation` behaviour where branch rows only toggled.\n * - Neither — a dead entry; rendered as inert text and flagged with a dev-only\n * warning rather than a decoy `<a>` with no destination.\n */\ntype SideNavItem = {\n /** Visible text for the link, section heading, or branch trigger. */\n title: string\n href?: string\n links?: SideNavItem[]\n}\n\n/**\n * Row treatment shared by leaf links and branch triggers — the nswds-app\n * left-rail language: each row carries its own left border sitting exactly on\n * the list's rail (the `-ml-px` on the `<li>`), so hover and the current-page\n * state recolour the rail segment beside the row.\n *\n * Departures from the source, both deliberate:\n * - The idle border is `border-transparent` rather than the bare `border-l`\n * default: idle rows show the rail through their transparent border, so the\n * idle rail colour is defined once (on the list) instead of twice.\n * - The current row adds `dark:text-white`. The source left dark active text\n * to fall through to the idle `dark:text-grey-400`, which on the\n * `dark:bg-white/20` highlight is both low-contrast and cascade-order\n * dependent; bold white ink clears WCAG 2.2 AA (1.4.3) on that overlay.\n *\n * The hover font-weight change (`hover:font-semibold`) is source parity; it\n * reflows the row's own text only, never the rail.\n *\n * Focus uses the house visible-focus pattern (outline-current, offset 2), so\n * the indicator always contrasts with whatever text colour the row currently\n * has (WCAG 2.2, 2.4.13 Focus Appearance).\n */\nconst sideNavRowVariants = cva(\n [\n 'w-full cursor-pointer rounded-r-sm border-l border-transparent py-1 pr-2 pl-4 text-left text-base/8 sm:text-sm/6',\n 'transition-colors motion-reduce:transition-none',\n 'hover:border-grey-950 hover:bg-primary-800/10 hover:font-semibold hover:text-grey-950',\n 'dark:text-grey-400 dark:hover:border-grey-400 dark:hover:text-white',\n 'focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-current',\n ],\n {\n variants: {\n current: {\n true: 'border-primary-800 bg-primary-800/10 font-bold text-primary-800 dark:border-white dark:bg-white/20 dark:text-white',\n false: '',\n },\n },\n defaultVariants: {\n current: false,\n },\n },\n)\n\n/**\n * Port of nswds-app's `isLinkOrDescendantActive`: true when the item itself,\n * or any item anywhere beneath it, is the current page. Drives `defaultOpen`\n * on branch collapsibles so the rail opens onto the reader's location.\n */\nfunction isItemOrDescendantCurrent(item: SideNavItem, currentHref: string | undefined): boolean {\n if (currentHref === undefined) {\n return false\n }\n if (item.href === currentHref) {\n return true\n }\n return item.links?.some((child) => isItemOrDescendantCurrent(child, currentHref)) ?? false\n}\n\n/**\n * Dev-only guard, mirroring the icon-only Button check in button.tsx: an item\n * with neither destination nor children renders as inert text (a decoy link\n * would violate WCAG 2.2, 4.1.2 Name, Role, Value expectations), and a branch's\n * `href` is silently unreachable because the row renders as a disclosure\n * button. Both are almost certainly data mistakes. No-op in production.\n */\nfunction warnIfItemMisshapen(item: SideNavItem) {\n if (process.env.NODE_ENV === 'production') {\n return\n }\n const hasChildren = (item.links?.length ?? 0) > 0\n if (!hasChildren && item.href === undefined) {\n console.warn(\n `[nswds/ui] SideNav item \"${item.title}\" has neither href nor links — it renders as inert text. Give it a destination or children.`,\n )\n }\n if (hasChildren && item.href !== undefined) {\n console.warn(\n `[nswds/ui] SideNav item \"${item.title}\" has both href and links — branch rows render as collapsible triggers, so its href is unreachable. Add a separate leaf item for the destination.`,\n )\n }\n}\n\ntype SideNavListProps = {\n items: SideNavItem[]\n currentHref?: string\n onNavigate?: React.MouseEventHandler<HTMLAnchorElement>\n /** Deeper than the section rail — tighter gap, indented, per the source. */\n nested?: boolean\n}\n\n/**\n * A rail of rows: the bordered `<ul>` every level of the tree renders into.\n * `role='list'` is kept from the source — Safari/VoiceOver strips list\n * semantics from lists whose `list-style` is removed, and the explicit role\n * restores them (this is the one sanctioned hand-written role in the file).\n */\nfunction SideNavList({ items, currentHref, onNavigate, nested = false }: SideNavListProps) {\n return (\n <ul\n role='list'\n data-slot='side-nav-list'\n className={cn(\n 'flex flex-col border-l border-grey-400 dark:border-grey-200/15',\n nested ? 'mt-2 ml-5 gap-1' : 'mt-2 gap-2 lg:mt-4',\n )}\n >\n {items.map((item) => (\n <SideNavRow\n key={item.href ?? item.title}\n item={item}\n currentHref={currentHref}\n onNavigate={onNavigate}\n />\n ))}\n </ul>\n )\n}\n\ntype SideNavRowProps = {\n item: SideNavItem\n currentHref?: string\n onNavigate?: React.MouseEventHandler<HTMLAnchorElement>\n}\n\n/**\n * One row of the rail — a leaf link, a collapsible branch, or (data mistake)\n * inert text. Recurses through `SideNavList` for branch children.\n */\nfunction SideNavRow({ item, currentHref, onNavigate }: SideNavRowProps) {\n const hasChildren = (item.links?.length ?? 0) > 0\n const isCurrent = currentHref !== undefined && item.href === currentHref\n\n warnIfItemMisshapen(item)\n\n if (hasChildren) {\n return (\n <li data-slot='side-nav-item' className='-ml-px flex w-full flex-col items-start gap-1'>\n {/* defaultOpen (not controlled `open`) so the branch holding the\n current page starts expanded but the reader can still fold it —\n the source's auto-expansion behaviour exactly. */}\n <Collapsible className='w-full' defaultOpen={isItemOrDescendantCurrent(item, currentHref)}>\n {/* Base UI's Trigger owns the disclosure semantics (aria-expanded,\n aria-controls, keyboard activation) — nothing hand-rolled here.\n `group` scopes the chevron's rotation to THIS trigger: nested\n triggers are never ancestors of each other's icons, and the\n Collapsible root carries data-open (not data-panel-open), so an\n open outer branch cannot rotate descendant chevrons. */}\n <CollapsibleTrigger\n data-slot='side-nav-trigger'\n className={cn(sideNavRowVariants(), 'group flex items-center justify-between gap-1')}\n >\n <span>{item.title}</span>\n <IconChevronRight\n aria-hidden='true'\n className='ml-2 size-5 shrink-0 transition-transform duration-200 group-data-panel-open:rotate-90 motion-reduce:transition-none'\n />\n </CollapsibleTrigger>\n <CollapsibleContent data-slot='side-nav-panel'>\n <SideNavList\n items={item.links ?? []}\n currentHref={currentHref}\n onNavigate={onNavigate}\n nested\n />\n </CollapsibleContent>\n </Collapsible>\n </li>\n )\n }\n\n if (item.href === undefined) {\n // No destination, no children: inert text, not a decoy anchor. The\n // dev-only warning above names the item.\n return (\n <li data-slot='side-nav-item' className='-ml-px flex w-full flex-col items-start gap-1'>\n <span\n data-slot='side-nav-text'\n className={cn(sideNavRowVariants(), 'inline-block cursor-default')}\n >\n {item.title}\n </span>\n </li>\n )\n }\n\n return (\n <li data-slot='side-nav-item' className='-ml-px flex w-full flex-col items-start gap-1'>\n {/* variant='unstyled' — the rail supplies the complete row treatment;\n Link's underline/colour variants would fight it. Rendering through\n Link still picks up the framework link component from LinkProvider\n (next/link et al). */}\n <Link\n variant='unstyled'\n data-slot='side-nav-link'\n href={item.href}\n onClick={onNavigate}\n aria-current={isCurrent ? 'page' : undefined}\n className={cn(sideNavRowVariants({ current: isCurrent }), 'inline-block')}\n >\n {item.title}\n </Link>\n </li>\n )\n}\n\ntype SideNavProps = Omit<React.ComponentPropsWithoutRef<'nav'>, 'children'> & {\n /**\n * The navigation tree. A top-level item with `links` renders as a section\n * heading over an always-visible rail; one without renders as a plain rail\n * link — mirroring nswds-app `Navigation`'s section shape. Deeper items with\n * `links` render as collapsible branches (nswds-app `SidebarNavigation`).\n */\n sections: SideNavItem[]\n /**\n * The current page's href. The matching leaf gets `aria-current='page'` and\n * the active rail treatment, and every branch on the path to it starts\n * expanded — at MOUNT only. Branches are `defaultOpen`, not controlled: a\n * later `currentHref` change (SPA navigation) re-highlights the leaf but\n * never re-expands a branch the reader has folded, so the highlight can\n * land inside a collapsed branch. Branch expansion is seeded from\n * `currentHref` on mount; pass a `key` (e.g. `key={currentHref}`) to\n * remount and re-open the path on navigation — the same contract as\n * PushMenu's level stack. Frameworkless replacement for the source's\n * `usePathname()` — pass your router's pathname in.\n */\n currentHref?: string\n /**\n * Fired from every leaf link (never from branch triggers). The mobile-drawer\n * close hook, as in the source's `onLinkClick`.\n */\n onNavigate?: React.MouseEventHandler<HTMLAnchorElement>\n /**\n * Heading level for section titles. Defaults to `2` — correct when the nav\n * sits at the top level of the document outline. Step it down when the page\n * nests the nav under another heading (WCAG 1.3.1). `1` is excluded: a\n * section-nav heading is never the page's own title. Same contract as\n * `FooterNavColumn`.\n */\n headingLevel?: 2 | 3 | 4 | 5 | 6\n ref?: React.Ref<HTMLElement>\n}\n\n/**\n * Left-rail section navigation — the consolidation of nswds-app's flat\n * `Navigation` and recursive `SidebarNavigation` into one tree-shaped\n * component. Renders a `<nav>` landmark of headed sections whose links may\n * nest to any depth; nested branches collapse.\n *\n * Accessibility contract:\n * - The landmark is named (`aria-label`, default \"Section navigation\") so it is\n * distinguishable from the page's other navigation landmarks (WCAG 1.3.1).\n * - Disclosure semantics — `aria-expanded`, `aria-controls`, Enter/Space\n * activation — come entirely from Base UI's Collapsible; nothing hand-rolled.\n * - The current page is marked with `aria-current='page'`, and every branch on\n * the path to it starts expanded ON MOUNT, so the highlighted location is\n * visible on arrival (ported `isLinkOrDescendantActive`). Expansion is\n * `defaultOpen`, so it does not re-run when `currentHref` changes — see the\n * `currentHref` prop for the `key={currentHref}` remount workaround.\n * - Focus is always visible via the house `outline-current` pattern (2.4.13).\n *\n * Departures from the nswds-app source (see also `sideNavRowVariants` and\n * `SideNavItem`):\n * - `usePathname()` → the `currentHref` prop; links render through `Link`, so\n * frameworks inject their own anchor via `LinkProvider`.\n * - The Radix-based Collapsible → the Base UI primitive (this repo's\n * `collapsible.tsx`), whose open state is `data-panel-open` on the trigger\n * rather than Radix's `data-state='open'`.\n * - The source rendered a stray empty `<ul>` after link-less sections and sent\n * `href='#'` placeholders through the tree; both are dropped.\n */\nfunction SideNav({\n className,\n sections,\n currentHref,\n onNavigate,\n headingLevel = 2,\n 'aria-label': ariaLabel = 'Section navigation',\n ref,\n ...props\n}: SideNavProps) {\n const Heading = `h${headingLevel}` as const\n\n return (\n <nav\n data-slot='side-nav'\n {...props}\n aria-label={ariaLabel}\n // Base ink declared once here (the source inherited the page body's\n // colour in light mode; a design-system component states it).\n className={cn('text-base text-grey-800 lg:text-sm dark:text-grey-400', className)}\n ref={ref}\n >\n <ul role='list' data-slot='side-nav-sections' className='flex flex-col gap-9'>\n {sections.map((section) => (\n <li key={section.href ?? section.title} data-slot='side-nav-section'>\n {section.links ? (\n <>\n {/* font-display resolves through @nswds/tokens' Tailwind\n preset (--font-display, Public Sans stack) — the same\n utility the source used. */}\n <Heading\n data-slot='side-nav-heading'\n className='font-display font-medium text-grey-800 dark:text-white'\n >\n {section.title}\n </Heading>\n <SideNavList\n items={section.links}\n currentHref={currentHref}\n onNavigate={onNavigate}\n />\n </>\n ) : (\n // A link-less section is a single rail link (Navigation.tsx\n // parity) — reuse the rail so the border/offset stay identical.\n <SideNavList items={[section]} currentHref={currentHref} onNavigate={onNavigate} />\n )}\n </li>\n ))}\n </ul>\n </nav>\n )\n}\n\nexport { SideNav, sideNavRowVariants }\nexport type { SideNavItem, SideNavProps }\nexport type SideNavRowVariantProps = VariantProps<typeof sideNavRowVariants>\n"]}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import React__default from 'react';
|
|
2
|
+
|
|
3
|
+
/** A single searchable destination. `keywords` extend matching beyond the title. */
|
|
4
|
+
type SiteSearchItem = {
|
|
5
|
+
title: string;
|
|
6
|
+
href: string;
|
|
7
|
+
keywords?: string[];
|
|
8
|
+
};
|
|
9
|
+
/** A titled section of results, rendered with a muted group heading. */
|
|
10
|
+
type SiteSearchGroup = {
|
|
11
|
+
title: string;
|
|
12
|
+
items: SiteSearchItem[];
|
|
13
|
+
};
|
|
14
|
+
type SiteSearchProps = {
|
|
15
|
+
/** The searchable site map, as titled groups of items. */
|
|
16
|
+
groups: SiteSearchGroup[];
|
|
17
|
+
/**
|
|
18
|
+
* Called with the chosen item on click or Enter, after the palette closes.
|
|
19
|
+
* Navigation is the APP's job — call your router here (e.g. next/navigation's
|
|
20
|
+
* `router.push(item.href)`). The design system ships no framework code, so it
|
|
21
|
+
* never navigates itself; this mirrors the DS-wide framework-free rule
|
|
22
|
+
* (see `Link` / `LinkProvider`).
|
|
23
|
+
*/
|
|
24
|
+
onSelect: (item: SiteSearchItem) => void;
|
|
25
|
+
/** Whether the palette is open. Use when controlled. */
|
|
26
|
+
open?: boolean;
|
|
27
|
+
/** Called whenever the palette asks to open or close. */
|
|
28
|
+
onOpenChange?: (open: boolean) => void;
|
|
29
|
+
/** Whether the palette is initially open (uncontrolled). */
|
|
30
|
+
defaultOpen?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Wire the global Cmd/Ctrl-K toggle on `document`, with cleanup on unmount.
|
|
33
|
+
* Defaults to true. Only one shortcut-enabled SiteSearch should be mounted
|
|
34
|
+
* per page, or each press toggles all of them.
|
|
35
|
+
*/
|
|
36
|
+
shortcut?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Accessible name for the palette, applied as `aria-label` to the dialog
|
|
39
|
+
* panel, the search input and the default trigger. Defaults to
|
|
40
|
+
* `'Search site'`. Mirrors ExpandableSearch's `label` prop — localise it
|
|
41
|
+
* rather than hardcoding English into consumers' pages.
|
|
42
|
+
*/
|
|
43
|
+
label?: string;
|
|
44
|
+
/** Placeholder for the search input. */
|
|
45
|
+
placeholder?: string;
|
|
46
|
+
/** Message shown when no items match the query. */
|
|
47
|
+
emptyMessage?: string;
|
|
48
|
+
/**
|
|
49
|
+
* The element that opens the palette. Defaults to a ghost icon `Button`
|
|
50
|
+
* named by `label`. Pass an element to replace it — it is composed via Base
|
|
51
|
+
* UI's `render` prop, so it inherits the trigger behaviour and ARIA — or
|
|
52
|
+
* `null` to render no trigger at all (open via `open` or the shortcut).
|
|
53
|
+
* A custom trigger owns its own accessible name — `label` is applied as
|
|
54
|
+
* `aria-label` to the default icon button only, since overriding a visible
|
|
55
|
+
* label that way fails WCAG 2.2, 2.5.3 Label in Name.
|
|
56
|
+
*
|
|
57
|
+
* Conditional triggers must resolve to `null`, not `false`: write
|
|
58
|
+
* `cond ? <Button/> : null`, since `cond && <Button/>` yields `false`, which
|
|
59
|
+
* Base UI's `render` prop rejects with an invalid-element error. TypeScript
|
|
60
|
+
* catches it (`false` is not assignable here); plain-JS consumers see the
|
|
61
|
+
* error at render.
|
|
62
|
+
*/
|
|
63
|
+
trigger?: React__default.ReactElement | null;
|
|
64
|
+
/**
|
|
65
|
+
* Extra content rendered at the foot of the panel, below the results —
|
|
66
|
+
* e.g. shortcut hints or a "browse all" link.
|
|
67
|
+
*/
|
|
68
|
+
children?: React__default.ReactNode;
|
|
69
|
+
/** Extra classes for the centred panel. */
|
|
70
|
+
className?: string;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Cmd/Ctrl-K command-palette site search: a centred modal panel with a
|
|
74
|
+
* filter-as-you-type input over a grouped list of destinations.
|
|
75
|
+
*
|
|
76
|
+
* Ported from nswds-app's `MobileSearch`, rebuilt framework-free: the app
|
|
77
|
+
* version composed cmdk's `Command*` widgets with next/navigation's router;
|
|
78
|
+
* this version composes Base UI's Autocomplete (the combobox/listbox pattern)
|
|
79
|
+
* inside Base UI's Dialog, and hands the chosen item to `onSelect` instead of
|
|
80
|
+
* navigating. The Autocomplete renders in `inline` mode — its input and list
|
|
81
|
+
* sit statically inside the dialog panel with no popup of their own — which is
|
|
82
|
+
* Base UI's documented composition for palettes (it avoids any portal /
|
|
83
|
+
* focus-trap interplay between the two primitives; the always-`open` inline
|
|
84
|
+
* root unmounts with the dialog, so the query and highlight reset on close).
|
|
85
|
+
*
|
|
86
|
+
* Accessibility contract (inherited, not hand-rolled):
|
|
87
|
+
* - Dialog provides the modal behaviour: focus trap, scroll lock, Escape and
|
|
88
|
+
* backdrop-press dismissal, and focus restoration to the trigger on close.
|
|
89
|
+
* The panel is named via `aria-label` (WCAG 2.2, 4.1.2).
|
|
90
|
+
* - Autocomplete provides the combobox pattern: the input is announced as a
|
|
91
|
+
* combobox controlling a listbox, arrow keys move `aria-activedescendant`
|
|
92
|
+
* highlight while DOM focus stays in the input, Enter activates the
|
|
93
|
+
* highlighted item, and the empty state is announced politely.
|
|
94
|
+
* - The trigger (default or custom) renders through `Dialog.Trigger`, so it
|
|
95
|
+
* carries `aria-haspopup="dialog"` / `aria-expanded` automatically.
|
|
96
|
+
* - Result rows are at least 44px tall (2.5.8 Target Size, AAA-sized).
|
|
97
|
+
* - Panel and backdrop transitions honour `prefers-reduced-motion` (2.3.3).
|
|
98
|
+
*
|
|
99
|
+
* Departures from the nswds-app source, beyond the rebuild above:
|
|
100
|
+
* - `groups`/`onSelect` replace the app's `NavigationItem[]` + router: the
|
|
101
|
+
* data shape is explicit (`title`/`href`/`keywords`) instead of the app's
|
|
102
|
+
* nav config, and untitled/unlinked entries can't exist by construction
|
|
103
|
+
* (the source filtered them at render time).
|
|
104
|
+
* - Filtering matches `title` AND `keywords`, case-insensitively, via the
|
|
105
|
+
* Autocomplete `filter` prop. Base UI's `useFilter` helper only matches one
|
|
106
|
+
* string per item, so a custom predicate is the documented escape hatch for
|
|
107
|
+
* multi-field matching; groups with no matching items are dropped by the
|
|
108
|
+
* primitive, headings included.
|
|
109
|
+
* - The Cmd/Ctrl-K listener can be disabled (`shortcut={false}`) — required
|
|
110
|
+
* for pages that mount more than one instance. It still toggles, per the
|
|
111
|
+
* source.
|
|
112
|
+
*/
|
|
113
|
+
declare function SiteSearch({ groups, onSelect, open: openProp, onOpenChange, defaultOpen, shortcut, label, placeholder, emptyMessage, trigger, children, className, }: SiteSearchProps): React__default.JSX.Element;
|
|
114
|
+
|
|
115
|
+
export { SiteSearch, type SiteSearchGroup, type SiteSearchItem, type SiteSearchProps };
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
3
|
+
import { Autocomplete } from '@base-ui/react/autocomplete';
|
|
4
|
+
import { Dialog } from '@base-ui/react/dialog';
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import { IconSearch } from '../icons/search.js';
|
|
7
|
+
import { cn } from '../lib/utils.js';
|
|
8
|
+
import { Button } from '../components/button.js';
|
|
9
|
+
|
|
10
|
+
function SiteSearch({
|
|
11
|
+
groups,
|
|
12
|
+
onSelect,
|
|
13
|
+
open: openProp,
|
|
14
|
+
onOpenChange,
|
|
15
|
+
defaultOpen = false,
|
|
16
|
+
shortcut = true,
|
|
17
|
+
label = "Search site",
|
|
18
|
+
placeholder = "Type to search across the site...",
|
|
19
|
+
emptyMessage = "No results found.",
|
|
20
|
+
trigger,
|
|
21
|
+
children,
|
|
22
|
+
className
|
|
23
|
+
}) {
|
|
24
|
+
const [uncontrolledOpen, setUncontrolledOpen] = React.useState(defaultOpen);
|
|
25
|
+
const isControlled = openProp !== void 0;
|
|
26
|
+
const open = isControlled ? openProp : uncontrolledOpen;
|
|
27
|
+
const inputRef = React.useRef(null);
|
|
28
|
+
const openRef = React.useRef(open);
|
|
29
|
+
const requestOpenChangeRef = React.useRef(() => {
|
|
30
|
+
});
|
|
31
|
+
const requestOpenChange = (next) => {
|
|
32
|
+
if (isControlled) {
|
|
33
|
+
if (open === next) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
onOpenChange?.(next);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (openRef.current === next) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
openRef.current = next;
|
|
43
|
+
setUncontrolledOpen(next);
|
|
44
|
+
onOpenChange?.(next);
|
|
45
|
+
};
|
|
46
|
+
React.useEffect(() => {
|
|
47
|
+
openRef.current = open;
|
|
48
|
+
requestOpenChangeRef.current = requestOpenChange;
|
|
49
|
+
});
|
|
50
|
+
React.useEffect(() => {
|
|
51
|
+
if (!shortcut) {
|
|
52
|
+
return void 0;
|
|
53
|
+
}
|
|
54
|
+
const handleKeyDown = (event) => {
|
|
55
|
+
if (event.key.toLowerCase() === "k" && (event.metaKey || event.ctrlKey) && !event.altKey && !event.defaultPrevented) {
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
requestOpenChangeRef.current(!openRef.current);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
61
|
+
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
62
|
+
}, [shortcut]);
|
|
63
|
+
const handleSelect = (item) => {
|
|
64
|
+
requestOpenChange(false);
|
|
65
|
+
onSelect(item);
|
|
66
|
+
};
|
|
67
|
+
const filter = React.useCallback((item, query) => {
|
|
68
|
+
const q = query.trim().toLowerCase();
|
|
69
|
+
if (q === "") {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
if (item.title.toLowerCase().includes(q)) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
return (item.keywords ?? []).some((keyword) => keyword.toLowerCase().includes(q));
|
|
76
|
+
}, []);
|
|
77
|
+
const defaultTrigger = /* @__PURE__ */ jsx(
|
|
78
|
+
Button,
|
|
79
|
+
{
|
|
80
|
+
variant: "ghost",
|
|
81
|
+
color: "grey",
|
|
82
|
+
size: "icon",
|
|
83
|
+
"aria-label": label,
|
|
84
|
+
leadingVisual: IconSearch
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
return /* @__PURE__ */ jsxs(Dialog.Root, { open, onOpenChange: (next) => requestOpenChange(next), children: [
|
|
88
|
+
trigger === null ? null : /* @__PURE__ */ jsx(Dialog.Trigger, { "data-slot": "site-search-trigger", render: trigger ?? defaultTrigger }),
|
|
89
|
+
/* @__PURE__ */ jsxs(Dialog.Portal, { children: [
|
|
90
|
+
/* @__PURE__ */ jsx(
|
|
91
|
+
Dialog.Backdrop,
|
|
92
|
+
{
|
|
93
|
+
"data-slot": "site-search-overlay",
|
|
94
|
+
className: "fixed inset-0 z-50 bg-black/80 transition-opacity duration-150 data-ending-style:opacity-0 data-starting-style:opacity-0 supports-backdrop-filter:backdrop-blur-xs motion-reduce:transition-none"
|
|
95
|
+
}
|
|
96
|
+
),
|
|
97
|
+
/* @__PURE__ */ jsxs(
|
|
98
|
+
Dialog.Popup,
|
|
99
|
+
{
|
|
100
|
+
"data-slot": "site-search-panel",
|
|
101
|
+
"aria-label": label,
|
|
102
|
+
initialFocus: inputRef,
|
|
103
|
+
className: cn(
|
|
104
|
+
// Centred panel on the house popup surface (popover.tsx / sheet.tsx).
|
|
105
|
+
"fixed top-[max(--spacing(4),15vh)] left-1/2 z-50 w-[calc(100vw-2rem)] max-w-2xl -translate-x-1/2",
|
|
106
|
+
"rounded-lg bg-popover text-popover-foreground shadow-md ring-1 ring-foreground/10 outline-hidden",
|
|
107
|
+
"transition duration-150 ease-out motion-reduce:transition-none",
|
|
108
|
+
"data-ending-style:scale-95 data-ending-style:opacity-0 data-starting-style:scale-95 data-starting-style:opacity-0",
|
|
109
|
+
className
|
|
110
|
+
),
|
|
111
|
+
children: [
|
|
112
|
+
/* @__PURE__ */ jsxs(
|
|
113
|
+
Autocomplete.Root,
|
|
114
|
+
{
|
|
115
|
+
items: groups,
|
|
116
|
+
filter,
|
|
117
|
+
itemToStringValue: (item) => item.title,
|
|
118
|
+
autoHighlight: true,
|
|
119
|
+
inline: true,
|
|
120
|
+
open: true,
|
|
121
|
+
onOpenChange: (nextOpen, eventDetails) => {
|
|
122
|
+
if (!nextOpen && (eventDetails.reason === "escape-key" || eventDetails.reason === "item-press")) {
|
|
123
|
+
requestOpenChange(false);
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
children: [
|
|
127
|
+
/* @__PURE__ */ jsxs(
|
|
128
|
+
"div",
|
|
129
|
+
{
|
|
130
|
+
"data-slot": "site-search-input-row",
|
|
131
|
+
className: "flex items-center gap-3 border-b border-foreground/10 px-4",
|
|
132
|
+
children: [
|
|
133
|
+
/* @__PURE__ */ jsx(IconSearch, { "aria-hidden": "true", className: "size-5 shrink-0 text-muted-foreground" }),
|
|
134
|
+
/* @__PURE__ */ jsx(
|
|
135
|
+
Autocomplete.Input,
|
|
136
|
+
{
|
|
137
|
+
"data-slot": "site-search-input",
|
|
138
|
+
"aria-label": label,
|
|
139
|
+
placeholder,
|
|
140
|
+
className: "h-12 w-full bg-transparent text-base text-foreground outline-hidden placeholder:text-muted-foreground",
|
|
141
|
+
ref: inputRef
|
|
142
|
+
}
|
|
143
|
+
)
|
|
144
|
+
]
|
|
145
|
+
}
|
|
146
|
+
),
|
|
147
|
+
/* @__PURE__ */ jsx(Autocomplete.Empty, { "data-slot": "site-search-empty", children: /* @__PURE__ */ jsx("div", { className: "px-4 py-6 text-center text-sm text-muted-foreground", children: emptyMessage }) }),
|
|
148
|
+
/* @__PURE__ */ jsx(
|
|
149
|
+
Autocomplete.List,
|
|
150
|
+
{
|
|
151
|
+
"data-slot": "site-search-list",
|
|
152
|
+
className: "max-h-[min(--spacing(96),60vh)] overflow-y-auto overscroll-contain p-2 empty:hidden",
|
|
153
|
+
children: (group) => /* @__PURE__ */ jsxs(
|
|
154
|
+
Autocomplete.Group,
|
|
155
|
+
{
|
|
156
|
+
items: group.items,
|
|
157
|
+
"data-slot": "site-search-group",
|
|
158
|
+
children: [
|
|
159
|
+
/* @__PURE__ */ jsx(
|
|
160
|
+
Autocomplete.GroupLabel,
|
|
161
|
+
{
|
|
162
|
+
"data-slot": "site-search-group-label",
|
|
163
|
+
className: "px-3 pt-3 pb-1 text-xs font-semibold text-muted-foreground",
|
|
164
|
+
children: group.title
|
|
165
|
+
}
|
|
166
|
+
),
|
|
167
|
+
/* @__PURE__ */ jsx(Autocomplete.Collection, { children: (item) => /* @__PURE__ */ jsx(
|
|
168
|
+
Autocomplete.Item,
|
|
169
|
+
{
|
|
170
|
+
value: item,
|
|
171
|
+
"data-slot": "site-search-item",
|
|
172
|
+
onClick: () => handleSelect(item),
|
|
173
|
+
className: "flex min-h-11 cursor-default items-center gap-2 rounded-sm px-3 py-2 text-sm text-foreground outline-hidden select-none data-highlighted:bg-primary-800/10 data-highlighted:text-accent-foreground dark:data-highlighted:bg-primary-200/10",
|
|
174
|
+
children: item.title
|
|
175
|
+
},
|
|
176
|
+
item.href
|
|
177
|
+
) })
|
|
178
|
+
]
|
|
179
|
+
},
|
|
180
|
+
group.title
|
|
181
|
+
)
|
|
182
|
+
}
|
|
183
|
+
)
|
|
184
|
+
]
|
|
185
|
+
}
|
|
186
|
+
),
|
|
187
|
+
children != null ? /* @__PURE__ */ jsx(
|
|
188
|
+
"div",
|
|
189
|
+
{
|
|
190
|
+
"data-slot": "site-search-footer",
|
|
191
|
+
className: "border-t border-foreground/10 px-4 py-2 text-xs text-muted-foreground",
|
|
192
|
+
children
|
|
193
|
+
}
|
|
194
|
+
) : null
|
|
195
|
+
]
|
|
196
|
+
}
|
|
197
|
+
)
|
|
198
|
+
] })
|
|
199
|
+
] });
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export { SiteSearch };
|
|
203
|
+
//# sourceMappingURL=site-search.js.map
|
|
204
|
+
//# sourceMappingURL=site-search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/site-search.tsx"],"names":[],"mappings":";;;;;;;;AA4HA,SAAS,UAAA,CAAW;AAAA,EAClB,MAAA;AAAA,EACA,QAAA;AAAA,EACA,IAAA,EAAM,QAAA;AAAA,EACN,YAAA;AAAA,EACA,WAAA,GAAc,KAAA;AAAA,EACd,QAAA,GAAW,IAAA;AAAA,EACX,KAAA,GAAQ,aAAA;AAAA,EACR,WAAA,GAAc,mCAAA;AAAA,EACd,YAAA,GAAe,mBAAA;AAAA,EACf,OAAA;AAAA,EACA,QAAA;AAAA,EACA;AACF,CAAA,EA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,4DAAA;AAAA,gCAET,QAAA,EAAA,KAAA,CAAM;AAAA;AAAA,6BACT;AAAA,4CACA,GAAA,CAAC,YAAA,CAAa,UAAA,EAAb,EACE,WAAC,IAAA,qBACA,GAAA;AAAA,8BAAC,YAAA,CAAa,IAAA;AAAA,8BAAb;AAAA,gCAEC,KAAA,EAAO,IAAA;AAAA,gCACP,WAAA,EAAU,kBAAA;AAAA,gCACV,OAAA,EAAS,MAAM,YAAA,CAAa,IAAI,CAAA;AAAA,gCAGhC,SAAA,EAAU,4OAAA;AAAA,gCAET,QAAA,EAAA,IAAA,CAAK;AAAA,+BAAA;AAAA,8BARD,IAAA,CAAK;AAAA,6BASZ,EAEJ;AAAA;AAAA,yBAAA;AAAA,wBAxBK,KAAA,CAAM;AAAA;AAyBb;AAAA;AAEJ;AAAA;AAAA,aACF;AAAA,YAEC,YAAY,IAAA,mBACX,GAAA;AAAA,cAAC,KAAA;AAAA,cAAA;AAAA,gBACC,WAAA,EAAU,oBAAA;AAAA,gBACV,SAAA,EAAU,uEAAA;AAAA,gBAET;AAAA;AAAA,aACH,GACE;AAAA;AAAA;AAAA;AACN,KAAA,EACF;AAAA,GAAA,EACF,CAAA;AAEJ","file":"site-search.js","sourcesContent":["'use client'\n\nimport { Autocomplete } from '@base-ui/react/autocomplete'\nimport { Dialog } from '@base-ui/react/dialog'\nimport React from 'react'\n\nimport { IconSearch } from '../icons/search.js'\nimport { cn } from '../lib/utils.js'\n\nimport { Button } from '../components/button.js'\n\n/** A single searchable destination. `keywords` extend matching beyond the title. */\ntype SiteSearchItem = {\n title: string\n href: string\n keywords?: string[]\n}\n\n/** A titled section of results, rendered with a muted group heading. */\ntype SiteSearchGroup = {\n title: string\n items: SiteSearchItem[]\n}\n\ntype SiteSearchProps = {\n /** The searchable site map, as titled groups of items. */\n groups: SiteSearchGroup[]\n /**\n * Called with the chosen item on click or Enter, after the palette closes.\n * Navigation is the APP's job — call your router here (e.g. next/navigation's\n * `router.push(item.href)`). The design system ships no framework code, so it\n * never navigates itself; this mirrors the DS-wide framework-free rule\n * (see `Link` / `LinkProvider`).\n */\n onSelect: (item: SiteSearchItem) => void\n /** Whether the palette is open. Use when controlled. */\n open?: boolean\n /** Called whenever the palette asks to open or close. */\n onOpenChange?: (open: boolean) => void\n /** Whether the palette is initially open (uncontrolled). */\n defaultOpen?: boolean\n /**\n * Wire the global Cmd/Ctrl-K toggle on `document`, with cleanup on unmount.\n * Defaults to true. Only one shortcut-enabled SiteSearch should be mounted\n * per page, or each press toggles all of them.\n */\n shortcut?: boolean\n /**\n * Accessible name for the palette, applied as `aria-label` to the dialog\n * panel, the search input and the default trigger. Defaults to\n * `'Search site'`. Mirrors ExpandableSearch's `label` prop — localise it\n * rather than hardcoding English into consumers' pages.\n */\n label?: string\n /** Placeholder for the search input. */\n placeholder?: string\n /** Message shown when no items match the query. */\n emptyMessage?: string\n /**\n * The element that opens the palette. Defaults to a ghost icon `Button`\n * named by `label`. Pass an element to replace it — it is composed via Base\n * UI's `render` prop, so it inherits the trigger behaviour and ARIA — or\n * `null` to render no trigger at all (open via `open` or the shortcut).\n * A custom trigger owns its own accessible name — `label` is applied as\n * `aria-label` to the default icon button only, since overriding a visible\n * label that way fails WCAG 2.2, 2.5.3 Label in Name.\n *\n * Conditional triggers must resolve to `null`, not `false`: write\n * `cond ? <Button/> : null`, since `cond && <Button/>` yields `false`, which\n * Base UI's `render` prop rejects with an invalid-element error. TypeScript\n * catches it (`false` is not assignable here); plain-JS consumers see the\n * error at render.\n */\n trigger?: React.ReactElement | null\n /**\n * Extra content rendered at the foot of the panel, below the results —\n * e.g. shortcut hints or a \"browse all\" link.\n */\n children?: React.ReactNode\n /** Extra classes for the centred panel. */\n className?: string\n}\n\n/**\n * Cmd/Ctrl-K command-palette site search: a centred modal panel with a\n * filter-as-you-type input over a grouped list of destinations.\n *\n * Ported from nswds-app's `MobileSearch`, rebuilt framework-free: the app\n * version composed cmdk's `Command*` widgets with next/navigation's router;\n * this version composes Base UI's Autocomplete (the combobox/listbox pattern)\n * inside Base UI's Dialog, and hands the chosen item to `onSelect` instead of\n * navigating. The Autocomplete renders in `inline` mode — its input and list\n * sit statically inside the dialog panel with no popup of their own — which is\n * Base UI's documented composition for palettes (it avoids any portal /\n * focus-trap interplay between the two primitives; the always-`open` inline\n * root unmounts with the dialog, so the query and highlight reset on close).\n *\n * Accessibility contract (inherited, not hand-rolled):\n * - Dialog provides the modal behaviour: focus trap, scroll lock, Escape and\n * backdrop-press dismissal, and focus restoration to the trigger on close.\n * The panel is named via `aria-label` (WCAG 2.2, 4.1.2).\n * - Autocomplete provides the combobox pattern: the input is announced as a\n * combobox controlling a listbox, arrow keys move `aria-activedescendant`\n * highlight while DOM focus stays in the input, Enter activates the\n * highlighted item, and the empty state is announced politely.\n * - The trigger (default or custom) renders through `Dialog.Trigger`, so it\n * carries `aria-haspopup=\"dialog\"` / `aria-expanded` automatically.\n * - Result rows are at least 44px tall (2.5.8 Target Size, AAA-sized).\n * - Panel and backdrop transitions honour `prefers-reduced-motion` (2.3.3).\n *\n * Departures from the nswds-app source, beyond the rebuild above:\n * - `groups`/`onSelect` replace the app's `NavigationItem[]` + router: the\n * data shape is explicit (`title`/`href`/`keywords`) instead of the app's\n * nav config, and untitled/unlinked entries can't exist by construction\n * (the source filtered them at render time).\n * - Filtering matches `title` AND `keywords`, case-insensitively, via the\n * Autocomplete `filter` prop. Base UI's `useFilter` helper only matches one\n * string per item, so a custom predicate is the documented escape hatch for\n * multi-field matching; groups with no matching items are dropped by the\n * primitive, headings included.\n * - The Cmd/Ctrl-K listener can be disabled (`shortcut={false}`) — required\n * for pages that mount more than one instance. It still toggles, per the\n * source.\n */\nfunction SiteSearch({\n groups,\n onSelect,\n open: openProp,\n onOpenChange,\n defaultOpen = false,\n shortcut = true,\n label = 'Search site',\n placeholder = 'Type to search across the site...',\n emptyMessage = 'No results found.',\n trigger,\n children,\n className,\n}: SiteSearchProps) {\n const [uncontrolledOpen, setUncontrolledOpen] = React.useState(defaultOpen)\n const isControlled = openProp !== undefined\n const open = isControlled ? openProp : uncontrolledOpen\n\n const inputRef = React.useRef<HTMLInputElement>(null)\n\n // Latest-value refs so the document-level shortcut listener (bound once per\n // `shortcut` value) never acts on a stale open state or stale callbacks.\n // They are written in a passive effect (not during render, per\n // react-hooks/refs) — safe because they are only read from event listeners,\n // which always fire after the commit that synced them.\n const openRef = React.useRef(open)\n const requestOpenChangeRef = React.useRef<(next: boolean) => void>(() => {})\n\n const requestOpenChange = (next: boolean) => {\n if (isControlled) {\n // Dedupe against the render-authoritative prop only — never write\n // openRef optimistically here. An optimistic write would let a parent\n // that vetoes a request (ignores it, so no re-render runs the resync\n // effect) permanently swallow every later identical request. Accepted\n // trade-off: a same-tick duplicate close (Escape reaching both the\n // Autocomplete and the Dialog) may reach `onOpenChange` twice with the\n // same value — idempotent for a setState parent, and correctness in\n // the veto case beats dedupe cosmetics.\n if (open === next) {\n return\n }\n onOpenChange?.(next)\n return\n }\n // Uncontrolled, the component owns the state, so the optimistic write is\n // safe (the commit below confirms it) and dedupes the double-report case\n // (both primitives announcing the same close) down to one callback.\n if (openRef.current === next) {\n return\n }\n openRef.current = next\n setUncontrolledOpen(next)\n onOpenChange?.(next)\n }\n\n React.useEffect(() => {\n // `open` (not `next` from an event) is authoritative here: in controlled\n // mode this effect is the ref's ONLY writer, so the shortcut listener\n // always toggles from the last value the parent actually rendered.\n openRef.current = open\n requestOpenChangeRef.current = requestOpenChange\n })\n\n React.useEffect(() => {\n if (!shortcut) {\n return undefined\n }\n const handleKeyDown = (event: KeyboardEvent) => {\n if (\n event.key.toLowerCase() === 'k' &&\n (event.metaKey || event.ctrlKey) &&\n !event.altKey &&\n !event.defaultPrevented\n ) {\n event.preventDefault()\n // Toggle, per the nswds-app source: the same chord opens and closes.\n requestOpenChangeRef.current(!openRef.current)\n }\n }\n document.addEventListener('keydown', handleKeyDown)\n return () => document.removeEventListener('keydown', handleKeyDown)\n }, [shortcut])\n\n const handleSelect = (item: SiteSearchItem) => {\n // Close first, then hand over — mirrors the source's `runCommand`, so the\n // app's navigation starts from a closed palette.\n requestOpenChange(false)\n onSelect(item)\n }\n\n const filter = React.useCallback((item: SiteSearchItem, query: string) => {\n const q = query.trim().toLowerCase()\n if (q === '') {\n return true\n }\n if (item.title.toLowerCase().includes(q)) {\n return true\n }\n return (item.keywords ?? []).some((keyword) => keyword.toLowerCase().includes(q))\n }, [])\n\n const defaultTrigger = (\n <Button\n variant='ghost'\n color='grey'\n size='icon'\n aria-label={label}\n leadingVisual={IconSearch}\n />\n )\n\n return (\n <Dialog.Root open={open} onOpenChange={(next) => requestOpenChange(next)}>\n {trigger === null ? null : (\n <Dialog.Trigger data-slot='site-search-trigger' render={trigger ?? defaultTrigger} />\n )}\n\n <Dialog.Portal>\n <Dialog.Backdrop\n data-slot='site-search-overlay'\n className='fixed inset-0 z-50 bg-black/80 transition-opacity duration-150 data-ending-style:opacity-0 data-starting-style:opacity-0 supports-backdrop-filter:backdrop-blur-xs motion-reduce:transition-none'\n />\n <Dialog.Popup\n data-slot='site-search-panel'\n aria-label={label}\n // The default focus target on touch is the panel itself (to keep the\n // virtual keyboard closed) — wrong for a search palette, whose whole\n // purpose is typing. Always focus the input.\n initialFocus={inputRef}\n className={cn(\n // Centred panel on the house popup surface (popover.tsx / sheet.tsx).\n 'fixed top-[max(--spacing(4),15vh)] left-1/2 z-50 w-[calc(100vw-2rem)] max-w-2xl -translate-x-1/2',\n 'rounded-lg bg-popover text-popover-foreground shadow-md ring-1 ring-foreground/10 outline-hidden',\n 'transition duration-150 ease-out motion-reduce:transition-none',\n 'data-ending-style:scale-95 data-ending-style:opacity-0 data-starting-style:scale-95 data-starting-style:opacity-0',\n className,\n )}\n >\n <Autocomplete.Root\n items={groups}\n filter={filter}\n itemToStringValue={(item: SiteSearchItem) => item.title}\n autoHighlight\n // Inline mode: the list renders statically inside the dialog panel\n // (no Autocomplete portal/positioner), with `open` pinned per the\n // Base UI docs. Opening/closing belongs to the Dialog; the whole\n // Autocomplete unmounts with it, resetting query and highlight.\n inline\n open\n onOpenChange={(nextOpen, eventDetails) => {\n // The pinned-open inline Autocomplete still *requests* closes;\n // forward the ones that should dismiss the palette. Other\n // reasons (input-clear, focus-out) must not close the dialog —\n // clearing the query or tabbing within the trap isn't dismissal.\n if (\n !nextOpen &&\n (eventDetails.reason === 'escape-key' || eventDetails.reason === 'item-press')\n ) {\n requestOpenChange(false)\n }\n }}\n >\n <div\n data-slot='site-search-input-row'\n className='flex items-center gap-3 border-b border-foreground/10 px-4'\n >\n <IconSearch aria-hidden='true' className='size-5 shrink-0 text-muted-foreground' />\n <Autocomplete.Input\n data-slot='site-search-input'\n aria-label={label}\n placeholder={placeholder}\n className='h-12 w-full bg-transparent text-base text-foreground outline-hidden placeholder:text-muted-foreground'\n ref={inputRef}\n />\n </div>\n\n {/* Base UI renders Empty's children only while the list is empty;\n the padding lives on the inner div so the (always-mounted, for\n polite announcements) outer element paints nothing otherwise. */}\n <Autocomplete.Empty data-slot='site-search-empty'>\n <div className='px-4 py-6 text-center text-sm text-muted-foreground'>\n {emptyMessage}\n </div>\n </Autocomplete.Empty>\n\n <Autocomplete.List\n data-slot='site-search-list'\n className='max-h-[min(--spacing(96),60vh)] overflow-y-auto overscroll-contain p-2 empty:hidden'\n >\n {(group: SiteSearchGroup) => (\n <Autocomplete.Group\n key={group.title}\n items={group.items}\n data-slot='site-search-group'\n >\n <Autocomplete.GroupLabel\n data-slot='site-search-group-label'\n className='px-3 pt-3 pb-1 text-xs font-semibold text-muted-foreground'\n >\n {group.title}\n </Autocomplete.GroupLabel>\n <Autocomplete.Collection>\n {(item: SiteSearchItem) => (\n <Autocomplete.Item\n key={item.href}\n value={item}\n data-slot='site-search-item'\n onClick={() => handleSelect(item)}\n // Highlight treatment mirrors menubar.tsx: keyboard and\n // hover unify as data-highlighted, painted as 10% ink.\n className='flex min-h-11 cursor-default items-center gap-2 rounded-sm px-3 py-2 text-sm text-foreground outline-hidden select-none data-highlighted:bg-primary-800/10 data-highlighted:text-accent-foreground dark:data-highlighted:bg-primary-200/10'\n >\n {item.title}\n </Autocomplete.Item>\n )}\n </Autocomplete.Collection>\n </Autocomplete.Group>\n )}\n </Autocomplete.List>\n </Autocomplete.Root>\n\n {children != null ? (\n <div\n data-slot='site-search-footer'\n className='border-t border-foreground/10 px-4 py-2 text-xs text-muted-foreground'\n >\n {children}\n </div>\n ) : null}\n </Dialog.Popup>\n </Dialog.Portal>\n </Dialog.Root>\n )\n}\n\nexport { SiteSearch }\nexport type { SiteSearchGroup, SiteSearchItem, SiteSearchProps }\n"]}
|