@nswds/ui 4.3.0 → 4.4.1
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/README.md +23 -4
- 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
package/README.md
CHANGED
|
@@ -28,6 +28,29 @@ export default function Demo() {
|
|
|
28
28
|
|
|
29
29
|
`@nswds/ui/styles.css` ships the full token foundation (NSW palette, masterbrand theme, semantic tokens) plus all component styles, so components render correctly with no extra wiring.
|
|
30
30
|
|
|
31
|
+
### Import it last
|
|
32
|
+
|
|
33
|
+
If your app also imports the `@nswds/tokens` Tailwind bridges — you only need them when your **own** Tailwind build has to know the NSW scales, so `bg-primary-800` or `text-text-muted` written in your code resolves — put `@nswds/ui/styles.css` **after** them:
|
|
34
|
+
|
|
35
|
+
```css
|
|
36
|
+
/* Bridges first: they teach your Tailwind build the NSW scales. */
|
|
37
|
+
@import '@nswds/tokens/tailwind/colors/global/oklch.css';
|
|
38
|
+
@import '@nswds/tokens/tailwind/colors/semantic/oklch.css';
|
|
39
|
+
|
|
40
|
+
/* @nswds/ui last: it is the only one of these that carries dark mode. */
|
|
41
|
+
@import '@nswds/ui/styles.css';
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The order matters because both stylesheets emit an unlayered `:root` block of light-mode values, and only `@nswds/ui/styles.css` also ships the `[data-theme='dark'], .dark` block. `:root` and `.dark` have identical specificity, so the block that appears **last** wins. Import the bridge afterwards and its light values land last: every `bg-*` utility still flips to dark (those are compiled classes, not tokens) while the semantic role tokens stay light — so backgrounds go dark and text stays dark on top of them. Nothing errors; it just looks broken.
|
|
45
|
+
|
|
46
|
+
### Dark mode
|
|
47
|
+
|
|
48
|
+
Toggle `class="dark"` or `data-theme="dark"` on a root element (for example with [`next-themes`](https://github.com/pacocoursey/next-themes)). Semantic tokens and `dark:` utilities both key off either marker, and both match the element carrying it as well as its descendants — so a scoped `<div class="dark">` darkens just that subtree.
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
<html lang='en' className='dark'>
|
|
52
|
+
```
|
|
53
|
+
|
|
31
54
|
### Icons
|
|
32
55
|
|
|
33
56
|
Icons are tree-shakeable from the barrel, or importable individually:
|
|
@@ -46,10 +69,6 @@ export function Toolbar() {
|
|
|
46
69
|
}
|
|
47
70
|
```
|
|
48
71
|
|
|
49
|
-
### Dark mode
|
|
50
|
-
|
|
51
|
-
Toggle the `dark` class on a root element (for example with [`next-themes`](https://github.com/pacocoursey/next-themes)). The semantic tokens remap automatically.
|
|
52
|
-
|
|
53
72
|
## Components
|
|
54
73
|
|
|
55
74
|
Badge, Button, ButtonLink, Card, DescriptionList, Field, Input, Label, LabeledSeparator, Link, Logo (NSW Government logo), Separator, Spinner — plus the icon set under `@nswds/ui/icons`.
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import * as class_variance_authority_types from 'class-variance-authority/types';
|
|
2
|
+
import { VariantProps } from 'class-variance-authority';
|
|
3
|
+
import React__default from 'react';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Surface colours the search can be themed with — the footer's thirteen-name
|
|
7
|
+
* vocabulary (see `footerColors` in footer.tsx) plus `default`, the nswds-app
|
|
8
|
+
* grey-100 chip that headers embed on a white surface.
|
|
9
|
+
*
|
|
10
|
+
* Every derived colour resolves from a single `--search-ink` token, so a
|
|
11
|
+
* variant only declares its surface utilities and that one value. This map
|
|
12
|
+
* replaces the FOUR parallel 14-entry cva tables in the nswds-app source
|
|
13
|
+
* (surface / collapsed / button / focus): each of those tables was a manual
|
|
14
|
+
* projection of the same underlying ink, so they are collapsed into the
|
|
15
|
+
* derivations below.
|
|
16
|
+
*
|
|
17
|
+
* The derivations are written literally here (once) rather than per-variant:
|
|
18
|
+
* Tailwind scans source text for class names, so a template-built arbitrary
|
|
19
|
+
* property would never be emitted. Mirrors footer.tsx (`--footer-*`) and
|
|
20
|
+
* link.tsx (`--link-halo`).
|
|
21
|
+
*
|
|
22
|
+
* Expanded text/surface pairs are the ones verified for footer.tsx: every
|
|
23
|
+
* light pair clears WCAG 2.2 AA (1.4.3, 4.5:1) — the worst are `primary-600`
|
|
24
|
+
* (4.57:1) and `accent-600` (5.18:1) — and in dark mode all clear AAA (worst
|
|
25
|
+
* 13.6:1). The collapsed chip renders no text, only the icon, which paints in
|
|
26
|
+
* the same ink.
|
|
27
|
+
*/
|
|
28
|
+
declare const expandableSearchVariants: (props?: ({
|
|
29
|
+
variant?: "default" | "white" | "primary-800" | "primary-600" | "primary-400" | "primary-200" | "grey-800" | "grey-600" | "grey-400" | "grey-200" | "accent-800" | "accent-600" | "accent-400" | "accent-200" | null | undefined;
|
|
30
|
+
} & class_variance_authority_types.ClassProp) | undefined) => string;
|
|
31
|
+
type ExpandableSearchVariant = NonNullable<VariantProps<typeof expandableSearchVariants>['variant']>;
|
|
32
|
+
type ExpandableSearchProps = React__default.ComponentPropsWithoutRef<'form'> & VariantProps<typeof expandableSearchVariants> & {
|
|
33
|
+
/**
|
|
34
|
+
* Called with the current query when the form submits (Enter in the field
|
|
35
|
+
* or a click on the search button). Not called if a supplied `onSubmit`
|
|
36
|
+
* prevented the event's default.
|
|
37
|
+
*/
|
|
38
|
+
onAction?: (value: string) => void;
|
|
39
|
+
/** Initial query. A non-empty value renders the field expanded. */
|
|
40
|
+
defaultValue?: string;
|
|
41
|
+
ref?: React__default.Ref<HTMLFormElement>;
|
|
42
|
+
};
|
|
43
|
+
type ExpandableSearchButtonProps = Omit<React__default.ComponentPropsWithoutRef<'button'>, 'type'> & {
|
|
44
|
+
[key: `data-${string}`]: string | number | boolean | undefined;
|
|
45
|
+
};
|
|
46
|
+
type ExpandableSearchFieldProps = Omit<React__default.ComponentPropsWithoutRef<'input'>, 'type' | 'value' | 'defaultValue'> & {
|
|
47
|
+
/**
|
|
48
|
+
* Accessible name for the input, rendered as a visually-hidden `<label>`
|
|
49
|
+
* and mirrored as `aria-label`. Defaults to `'Search'`.
|
|
50
|
+
*/
|
|
51
|
+
label?: string;
|
|
52
|
+
/** Accessible name for the submit button. Defaults to `'Search'`. */
|
|
53
|
+
buttonLabel?: string;
|
|
54
|
+
/** Extra props for the submit button (its `type` is pinned to `submit`). */
|
|
55
|
+
buttonProps?: ExpandableSearchButtonProps;
|
|
56
|
+
ref?: React__default.Ref<HTMLInputElement>;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Site-search disclosure: a 48px search chip that expands into a text field
|
|
60
|
+
* whenever it holds focus or a value, and submits the query to `onAction`.
|
|
61
|
+
* Compose it as `<ExpandableSearch>` (the form root, owning the value and the
|
|
62
|
+
* focus-within state) around one `<ExpandableSearchField>` (the input and its
|
|
63
|
+
* submit button) — typically inside `HeaderActions`.
|
|
64
|
+
*
|
|
65
|
+
* Ported from nswds-app's `ExpandableSearch`. The mechanic is kept verbatim —
|
|
66
|
+
* the collapsed control IS the text input, styled as a chip, so that the very
|
|
67
|
+
* first focus or tap both reveals and focuses the field with no extra step.
|
|
68
|
+
* Expansion is pure CSS keyed on the active state; no Base UI primitive is
|
|
69
|
+
* used because none models this pattern (it is a styled form, not a popup —
|
|
70
|
+
* focus never moves, nothing is portalled, nothing needs ARIA beyond the
|
|
71
|
+
* label).
|
|
72
|
+
*
|
|
73
|
+
* Accessibility contract:
|
|
74
|
+
* - The input carries a visually-hidden `<label>` and an `aria-label`
|
|
75
|
+
* (default "Search"), and is `type='search'` with `enterKeyHint='search'`,
|
|
76
|
+
* so its role and purpose are announced while it still looks like a button
|
|
77
|
+
* (WCAG 2.2, 4.1.2; the source input was an unlabelled-by-type text input).
|
|
78
|
+
* - Keyboard focus shows a 2px ink outline drawn inside the chip — the
|
|
79
|
+
* source's ring was its own surface colour against a hardcoded white offset
|
|
80
|
+
* and could vanish entirely (2.4.7, 2.4.13). See `searchFocusClassName`.
|
|
81
|
+
* - Enter submits; Escape clears (native `type='search'` behaviour); an empty
|
|
82
|
+
* blurred field collapses back to the chip.
|
|
83
|
+
* - Expansion and halo transitions honour `prefers-reduced-motion` (2.3.3).
|
|
84
|
+
*
|
|
85
|
+
* Departures from the nswds-app source, beyond those above:
|
|
86
|
+
* - React 19 `ref` prop instead of `forwardRef`.
|
|
87
|
+
* - Four parallel 14-entry variant tables replaced by one ink-driven map —
|
|
88
|
+
* see `expandableSearchVariants`.
|
|
89
|
+
* - Expanded width `w-96` → `w-[min(24rem,80vw)]` so the field can never
|
|
90
|
+
* overflow a small viewport (1.4.10 Reflow).
|
|
91
|
+
* - The expanded state no longer sets `outline-none` (it would have killed
|
|
92
|
+
* the new focus indicator; the source could afford it because its
|
|
93
|
+
* indicator was a box-shadow ring).
|
|
94
|
+
* - Expanded text selection uses `select-text`, not the source's
|
|
95
|
+
* `select-auto` — `auto` computes from the root's `select-none` parent, so
|
|
96
|
+
* the source's expanded field was unselectable.
|
|
97
|
+
* - A consumer `onChange` on the field is chained after the context update
|
|
98
|
+
* instead of replacing it (the source's spread order let it clobber the
|
|
99
|
+
* controlled handler).
|
|
100
|
+
*
|
|
101
|
+
* The root deliberately does not claim `role='search'`: where the component
|
|
102
|
+
* is the page's site search, add it yourself (one search landmark per page).
|
|
103
|
+
*/
|
|
104
|
+
declare function ExpandableSearch({ className, onAction, defaultValue, onSubmit, onFocusCapture, onBlurCapture, variant, ref, ...props }: ExpandableSearchProps): React__default.JSX.Element;
|
|
105
|
+
/**
|
|
106
|
+
* The input and its submit button. Must be rendered inside `ExpandableSearch`,
|
|
107
|
+
* which owns the value and the focus-within state.
|
|
108
|
+
*
|
|
109
|
+
* The submit button is `pointer-events-none` while collapsed — deliberately,
|
|
110
|
+
* per the source: collapsed, the input overlays the exact same 48px square and
|
|
111
|
+
* OWNS the click, so a tap lands on the input, focuses it, and expands the
|
|
112
|
+
* field. Making the button clickable there would submit an empty query
|
|
113
|
+
* instead of opening the field. It also never traps keyboard focus while
|
|
114
|
+
* collapsed: the input precedes it in tab order, and focusing the input
|
|
115
|
+
* expands the field before the button is reached.
|
|
116
|
+
*/
|
|
117
|
+
declare function ExpandableSearchField({ className, id, label, 'aria-label': ariaLabel, autoComplete, enterKeyHint, onChange, buttonLabel, buttonProps, ref, ...props }: ExpandableSearchFieldProps): React__default.JSX.Element;
|
|
118
|
+
|
|
119
|
+
export { ExpandableSearch, type ExpandableSearchButtonProps, ExpandableSearchField, type ExpandableSearchFieldProps, type ExpandableSearchProps, type ExpandableSearchVariant, expandableSearchVariants };
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
|
+
import { cva } from 'class-variance-authority';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
import { cn } from '../lib/utils.js';
|
|
6
|
+
import { IconSearch } from '../icons/search.js';
|
|
7
|
+
|
|
8
|
+
const expandableSearchVariants = cva(
|
|
9
|
+
[
|
|
10
|
+
// `group/search` is named so the submit button's group-focus-within hook
|
|
11
|
+
// can't collide with an ancestor's bare `group` (e.g. inside a card).
|
|
12
|
+
"group/search relative inline-block rounded-sm select-none",
|
|
13
|
+
// Halo for the submit button's hover / the form's focus-within state, a
|
|
14
|
+
// heavier press halo, and the placeholder colour — all mixed down from
|
|
15
|
+
// the ink. 70% ink keeps the placeholder related to but visibly lighter
|
|
16
|
+
// than the entered text (the source used grey-700 / current at 70%
|
|
17
|
+
// opacity). Dilution costs contrast, though, so the percentage routes
|
|
18
|
+
// through --search-placeholder-pct and the surfaces whose 70% composite
|
|
19
|
+
// fails 1.4.3 raise it to 100% in the variant map below. Verified by
|
|
20
|
+
// alpha-compositing the mix over each surface (light mode): at 70%
|
|
21
|
+
// primary-600 is 3.0:1, accent-600 3.1:1 and accent-400 4.48:1 — all
|
|
22
|
+
// AA failures, cured by full ink (4.57 / 5.18 / 8.34:1). The eleven
|
|
23
|
+
// surfaces that keep 70% all clear 4.5:1, the closest being
|
|
24
|
+
// primary-400 (4.57:1) and grey-400 (4.58:1); dark mode's worst 70%
|
|
25
|
+
// composite is 7.0:1.
|
|
26
|
+
"[--search-halo:color-mix(in_oklch,var(--search-ink)_10%,transparent)]",
|
|
27
|
+
"[--search-halo-active:color-mix(in_oklch,var(--search-ink)_18%,transparent)]",
|
|
28
|
+
"[--search-placeholder:color-mix(in_oklch,var(--search-ink)_var(--search-placeholder-pct,70%),transparent)]"
|
|
29
|
+
],
|
|
30
|
+
{
|
|
31
|
+
variants: {
|
|
32
|
+
// Ink values use the RAW masterbrand tokens (--primary-800, --grey-800,
|
|
33
|
+
// --accent-800) rather than Tailwind's --color-* bridge aliases. Tailwind
|
|
34
|
+
// v4 tree-shakes an unreferenced @theme key, and referencing one from
|
|
35
|
+
// inside an arbitrary property is not a usage signal — the raw tokens are
|
|
36
|
+
// plain :root declarations from @nswds/tokens and always resolve.
|
|
37
|
+
// (--color-white is safe: `text-white` below is a real utility, so that
|
|
38
|
+
// key is always emitted.) Same reasoning as footer.tsx and header.tsx.
|
|
39
|
+
//
|
|
40
|
+
// Dark mode deepens every surface onto the same family's dark steps —
|
|
41
|
+
// -800→-950, -600→-900, -400→-850, -200→-800, white→grey-900 — per
|
|
42
|
+
// footer.tsx's family mapping, and `default` follows header.tsx's
|
|
43
|
+
// `light` surface: grey-100→grey-850. Ink goes white on all fourteen.
|
|
44
|
+
//
|
|
45
|
+
// primary-600, accent-600 and accent-400 set --search-placeholder-pct
|
|
46
|
+
// to 100%: their 70% placeholder composite fails AA (3.0 / 3.1 /
|
|
47
|
+
// 4.48:1 — see the derivation comment above), so the placeholder
|
|
48
|
+
// renders full ink there. The override is deliberately not
|
|
49
|
+
// dark-scoped: every dark 70% composite clears 7:1, so 100% in dark
|
|
50
|
+
// is merely a slightly stronger placeholder, not worth a second
|
|
51
|
+
// declaration.
|
|
52
|
+
//
|
|
53
|
+
// `default` is the one variant whose ink is NOT its text colour: the
|
|
54
|
+
// nswds-app chip pairs grey-800 entered text with a primary-blue icon
|
|
55
|
+
// and primary-800/10 halos. Declaring ink as primary-800 reproduces the
|
|
56
|
+
// icon and halos exactly; the text colour is a surface utility, so it
|
|
57
|
+
// stays grey-800. (Side effect, documented: the placeholder mixes from
|
|
58
|
+
// primary-800 instead of the source's grey-700 — a subtle blue shift.)
|
|
59
|
+
variant: {
|
|
60
|
+
default: "bg-grey-100 text-grey-800 [--search-ink:var(--primary-800)] dark:bg-grey-850 dark:text-white dark:[--search-ink:var(--color-white)]",
|
|
61
|
+
"primary-800": "bg-primary-800 text-white [--search-ink:var(--color-white)] dark:bg-primary-950",
|
|
62
|
+
"primary-600": "bg-primary-600 text-white [--search-ink:var(--color-white)] [--search-placeholder-pct:100%] dark:bg-primary-900",
|
|
63
|
+
"primary-400": "bg-primary-400 text-primary-800 [--search-ink:var(--primary-800)] dark:bg-primary-850 dark:text-white dark:[--search-ink:var(--color-white)]",
|
|
64
|
+
"primary-200": "bg-primary-200 text-primary-800 [--search-ink:var(--primary-800)] dark:bg-primary-800 dark:text-white dark:[--search-ink:var(--color-white)]",
|
|
65
|
+
"grey-800": "bg-grey-800 text-white [--search-ink:var(--color-white)] dark:bg-grey-950",
|
|
66
|
+
"grey-600": "bg-grey-600 text-white [--search-ink:var(--color-white)] dark:bg-grey-900",
|
|
67
|
+
"grey-400": "bg-grey-400 text-grey-800 [--search-ink:var(--grey-800)] dark:bg-grey-850 dark:text-white dark:[--search-ink:var(--color-white)]",
|
|
68
|
+
"grey-200": "bg-grey-200 text-grey-800 [--search-ink:var(--grey-800)] dark:bg-grey-800 dark:text-white dark:[--search-ink:var(--color-white)]",
|
|
69
|
+
"accent-800": "bg-accent-800 text-white [--search-ink:var(--color-white)] dark:bg-accent-950",
|
|
70
|
+
"accent-600": "bg-accent-600 text-white [--search-ink:var(--color-white)] [--search-placeholder-pct:100%] dark:bg-accent-900",
|
|
71
|
+
"accent-400": "bg-accent-400 text-accent-800 [--search-ink:var(--accent-800)] [--search-placeholder-pct:100%] dark:bg-accent-850 dark:text-white dark:[--search-ink:var(--color-white)]",
|
|
72
|
+
"accent-200": "bg-accent-200 text-accent-800 [--search-ink:var(--accent-800)] dark:bg-accent-800 dark:text-white dark:[--search-ink:var(--color-white)]",
|
|
73
|
+
white: "bg-white text-grey-800 [--search-ink:var(--grey-800)] dark:bg-grey-900 dark:text-white dark:[--search-ink:var(--color-white)]"
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
defaultVariants: {
|
|
77
|
+
variant: "default"
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
const searchFocusClassName = "focus-visible:outline focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-(--search-ink)";
|
|
82
|
+
const ExpandableSearchContext = React.createContext(null);
|
|
83
|
+
function warnIfFieldUnlabelled(label, ariaLabel) {
|
|
84
|
+
if (process.env.NODE_ENV === "production") {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
if (!label && !ariaLabel) {
|
|
88
|
+
console.warn(
|
|
89
|
+
"[nswds/ui] ExpandableSearchField has no label \u2014 pass `label` or `aria-label` so the search input has an accessible name."
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function ExpandableSearch({
|
|
94
|
+
className,
|
|
95
|
+
onAction,
|
|
96
|
+
defaultValue = "",
|
|
97
|
+
onSubmit,
|
|
98
|
+
onFocusCapture,
|
|
99
|
+
onBlurCapture,
|
|
100
|
+
variant = "default",
|
|
101
|
+
ref,
|
|
102
|
+
...props
|
|
103
|
+
}) {
|
|
104
|
+
const [value, setValue] = React.useState(defaultValue);
|
|
105
|
+
const [formHasFocusWithin, setFormHasFocusWithin] = React.useState(false);
|
|
106
|
+
const resolvedVariant = variant ?? "default";
|
|
107
|
+
const isActive = value.length > 0 || formHasFocusWithin;
|
|
108
|
+
const contextValue = React.useMemo(
|
|
109
|
+
() => ({ value, setValue, formHasFocusWithin, variant: resolvedVariant }),
|
|
110
|
+
[value, formHasFocusWithin, resolvedVariant]
|
|
111
|
+
);
|
|
112
|
+
const handleSubmit = (e) => {
|
|
113
|
+
onSubmit?.(e);
|
|
114
|
+
if (e.defaultPrevented) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
e.preventDefault();
|
|
118
|
+
onAction?.(value);
|
|
119
|
+
};
|
|
120
|
+
const handleFocusCapture = (e) => {
|
|
121
|
+
setFormHasFocusWithin(true);
|
|
122
|
+
onFocusCapture?.(e);
|
|
123
|
+
};
|
|
124
|
+
const handleBlurCapture = (e) => {
|
|
125
|
+
const nextFocusedElement = e.relatedTarget;
|
|
126
|
+
if (!e.currentTarget.contains(nextFocusedElement)) {
|
|
127
|
+
setFormHasFocusWithin(false);
|
|
128
|
+
}
|
|
129
|
+
onBlurCapture?.(e);
|
|
130
|
+
};
|
|
131
|
+
return /* @__PURE__ */ jsx(ExpandableSearchContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(
|
|
132
|
+
"form",
|
|
133
|
+
{
|
|
134
|
+
"data-slot": "expandable-search",
|
|
135
|
+
"data-variant": resolvedVariant,
|
|
136
|
+
"data-expanded": isActive || void 0,
|
|
137
|
+
...props,
|
|
138
|
+
className: cn(expandableSearchVariants({ variant }), className),
|
|
139
|
+
onBlurCapture: handleBlurCapture,
|
|
140
|
+
onFocusCapture: handleFocusCapture,
|
|
141
|
+
onSubmit: handleSubmit,
|
|
142
|
+
ref
|
|
143
|
+
}
|
|
144
|
+
) });
|
|
145
|
+
}
|
|
146
|
+
function ExpandableSearchField({
|
|
147
|
+
className,
|
|
148
|
+
id,
|
|
149
|
+
label = "Search",
|
|
150
|
+
"aria-label": ariaLabel,
|
|
151
|
+
autoComplete = "off",
|
|
152
|
+
enterKeyHint = "search",
|
|
153
|
+
onChange,
|
|
154
|
+
buttonLabel = "Search",
|
|
155
|
+
buttonProps,
|
|
156
|
+
ref,
|
|
157
|
+
...props
|
|
158
|
+
}) {
|
|
159
|
+
const context = React.useContext(ExpandableSearchContext);
|
|
160
|
+
if (!context) throw new Error("ExpandableSearchField must be used within ExpandableSearch");
|
|
161
|
+
const generatedId = React.useId();
|
|
162
|
+
const {
|
|
163
|
+
className: buttonClassName,
|
|
164
|
+
children: buttonChildren,
|
|
165
|
+
"aria-label": buttonAriaLabel,
|
|
166
|
+
...restButtonProps
|
|
167
|
+
} = buttonProps ?? {};
|
|
168
|
+
const isActive = context.value.length > 0 || context.formHasFocusWithin;
|
|
169
|
+
const inputId = id ?? generatedId;
|
|
170
|
+
warnIfFieldUnlabelled(label, ariaLabel);
|
|
171
|
+
const handleChange = (e) => {
|
|
172
|
+
context.setValue(e.target.value);
|
|
173
|
+
onChange?.(e);
|
|
174
|
+
};
|
|
175
|
+
return /* @__PURE__ */ jsxs("div", { "data-slot": "expandable-search-field", className: "relative flex-1", children: [
|
|
176
|
+
label ? (
|
|
177
|
+
// aria-label (below) wins for the accessible name where both exist;
|
|
178
|
+
// the label element is kept for source parity and so a click on any
|
|
179
|
+
// future visible rendering of it focuses the input.
|
|
180
|
+
/* @__PURE__ */ jsx("label", { className: "sr-only", htmlFor: inputId, children: label })
|
|
181
|
+
) : null,
|
|
182
|
+
/* @__PURE__ */ jsx(
|
|
183
|
+
"input",
|
|
184
|
+
{
|
|
185
|
+
"data-slot": "expandable-search-input",
|
|
186
|
+
"aria-label": ariaLabel ?? (label || void 0),
|
|
187
|
+
autoComplete,
|
|
188
|
+
enterKeyHint,
|
|
189
|
+
...props,
|
|
190
|
+
type: "search",
|
|
191
|
+
id: inputId,
|
|
192
|
+
value: context.value,
|
|
193
|
+
onChange: handleChange,
|
|
194
|
+
className: cn(
|
|
195
|
+
// Collapsed, the input is a 48px chip: transparent text and
|
|
196
|
+
// placeholder so nothing peeks out, cursor-pointer because it acts
|
|
197
|
+
// as the "open search" affordance. The background comes from the
|
|
198
|
+
// form root, which shrink-wraps this element.
|
|
199
|
+
"flex h-12 w-12 cursor-pointer overflow-hidden rounded-sm border-0 bg-transparent text-transparent",
|
|
200
|
+
"placeholder:text-transparent placeholder:opacity-0",
|
|
201
|
+
"transition-[width,box-shadow,background-color] duration-300 ease-in-out motion-reduce:transition-none",
|
|
202
|
+
// Hide WebKit's native clear affordance: the source field (type
|
|
203
|
+
// text) had none, and Escape already clears a search input.
|
|
204
|
+
"[&::-webkit-search-cancel-button]:appearance-none",
|
|
205
|
+
searchFocusClassName,
|
|
206
|
+
isActive ? (
|
|
207
|
+
// Expanded: capped at 80vw so the field cannot overflow small
|
|
208
|
+
// viewports (the source's fixed w-96 could). pr-12 reserves the
|
|
209
|
+
// submit button's square. select-text (not select-auto — see
|
|
210
|
+
// the component JSDoc) re-enables selection under the root's
|
|
211
|
+
// select-none.
|
|
212
|
+
"w-[min(24rem,80vw)] cursor-auto pt-0 pr-12 pb-0 pl-4 text-inherit select-text placeholder:text-(--search-placeholder) placeholder:opacity-100"
|
|
213
|
+
) : (
|
|
214
|
+
// Collapsed: the chip halos on hover exactly like the button
|
|
215
|
+
// does — one visual whether the pointer technically rests on
|
|
216
|
+
// the input overlay or not.
|
|
217
|
+
"p-0 hover:bg-(--search-halo)"
|
|
218
|
+
),
|
|
219
|
+
className
|
|
220
|
+
),
|
|
221
|
+
ref
|
|
222
|
+
}
|
|
223
|
+
),
|
|
224
|
+
/* @__PURE__ */ jsx(
|
|
225
|
+
"button",
|
|
226
|
+
{
|
|
227
|
+
"data-slot": "expandable-search-button",
|
|
228
|
+
"aria-label": buttonAriaLabel ?? buttonLabel,
|
|
229
|
+
...restButtonProps,
|
|
230
|
+
type: "submit",
|
|
231
|
+
className: cn(
|
|
232
|
+
"absolute top-0 right-0 z-10 flex h-12 w-12 cursor-pointer rounded-sm",
|
|
233
|
+
"transition-[background-color,box-shadow] duration-200 motion-reduce:transition-none",
|
|
234
|
+
// Icon and halos all derive from the ink; the whole-form
|
|
235
|
+
// focus-within halo marks the button as the pending action while
|
|
236
|
+
// the user types.
|
|
237
|
+
"text-(--search-ink)",
|
|
238
|
+
"group-focus-within/search:bg-(--search-halo) hover:bg-(--search-halo) active:bg-(--search-halo-active)",
|
|
239
|
+
searchFocusClassName,
|
|
240
|
+
// Collapsed, the input overlay owns the click — see the JSDoc.
|
|
241
|
+
isActive ? "pointer-events-auto" : "pointer-events-none",
|
|
242
|
+
buttonClassName
|
|
243
|
+
),
|
|
244
|
+
children: buttonChildren ?? /* @__PURE__ */ jsx(IconSearch, { "aria-hidden": "true", className: "m-auto block size-8 shrink-0 fill-current" })
|
|
245
|
+
}
|
|
246
|
+
)
|
|
247
|
+
] });
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export { ExpandableSearch, ExpandableSearchField, expandableSearchVariants };
|
|
251
|
+
//# sourceMappingURL=expandable-search.js.map
|
|
252
|
+
//# sourceMappingURL=expandable-search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/components/expandable-search.tsx"],"names":[],"mappings":";;;;;;AAgCA,MAAM,wBAAA,GAA2B,GAAA;AAAA,EAC/B;AAAA;AAAA;AAAA,IAGE,2DAAA;AAAA;AAAA;AAAA;AAAA;A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;AAAA,MAAC,QAAA;AAAA,MAAA;AAAA,QACC,WAAA,EAAU,0BAAA;AAAA,QACV,cAAY,eAAA,IAAmB,WAAA;AAAA,QAC9B,GAAG,eAAA;AAAA,QACJ,IAAA,EAAK,QAAA;AAAA,QACL,SAAA,EAAW,EAAA;AAAA,UACT,sEAAA;AAAA,UACA,qFAAA;AAAA;AAAA;AAAA;AAAA,UAIA,qBAAA;AAAA,UACA,wGAAA;AAAA,UACA,oBAAA;AAAA;AAAA,UAEA,WAAW,qBAAA,GAAwB,qBAAA;AAAA,UACnC;AAAA,SACF;AAAA,QAEC,4CACC,GAAA,CAAC,UAAA,EAAA,EAAW,aAAA,EAAY,MAAA,EAAO,WAAU,2CAAA,EAA4C;AAAA;AAAA;AAEzF,GAAA,EACF,CAAA;AAEJ","file":"expandable-search.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 { IconSearch } from '../icons/search.js'\n\n/**\n * Surface colours the search can be themed with — the footer's thirteen-name\n * vocabulary (see `footerColors` in footer.tsx) plus `default`, the nswds-app\n * grey-100 chip that headers embed on a white surface.\n *\n * Every derived colour resolves from a single `--search-ink` token, so a\n * variant only declares its surface utilities and that one value. This map\n * replaces the FOUR parallel 14-entry cva tables in the nswds-app source\n * (surface / collapsed / button / focus): each of those tables was a manual\n * projection of the same underlying ink, so they are collapsed into the\n * derivations below.\n *\n * The derivations are written literally here (once) rather than per-variant:\n * Tailwind scans source text for class names, so a template-built arbitrary\n * property would never be emitted. Mirrors footer.tsx (`--footer-*`) and\n * link.tsx (`--link-halo`).\n *\n * Expanded text/surface pairs are the ones verified for footer.tsx: every\n * light pair clears WCAG 2.2 AA (1.4.3, 4.5:1) — the worst are `primary-600`\n * (4.57:1) and `accent-600` (5.18:1) — and in dark mode all clear AAA (worst\n * 13.6:1). The collapsed chip renders no text, only the icon, which paints in\n * the same ink.\n */\nconst expandableSearchVariants = cva(\n [\n // `group/search` is named so the submit button's group-focus-within hook\n // can't collide with an ancestor's bare `group` (e.g. inside a card).\n 'group/search relative inline-block rounded-sm select-none',\n // Halo for the submit button's hover / the form's focus-within state, a\n // heavier press halo, and the placeholder colour — all mixed down from\n // the ink. 70% ink keeps the placeholder related to but visibly lighter\n // than the entered text (the source used grey-700 / current at 70%\n // opacity). Dilution costs contrast, though, so the percentage routes\n // through --search-placeholder-pct and the surfaces whose 70% composite\n // fails 1.4.3 raise it to 100% in the variant map below. Verified by\n // alpha-compositing the mix over each surface (light mode): at 70%\n // primary-600 is 3.0:1, accent-600 3.1:1 and accent-400 4.48:1 — all\n // AA failures, cured by full ink (4.57 / 5.18 / 8.34:1). The eleven\n // surfaces that keep 70% all clear 4.5:1, the closest being\n // primary-400 (4.57:1) and grey-400 (4.58:1); dark mode's worst 70%\n // composite is 7.0:1.\n '[--search-halo:color-mix(in_oklch,var(--search-ink)_10%,transparent)]',\n '[--search-halo-active:color-mix(in_oklch,var(--search-ink)_18%,transparent)]',\n '[--search-placeholder:color-mix(in_oklch,var(--search-ink)_var(--search-placeholder-pct,70%),transparent)]',\n ],\n {\n variants: {\n // Ink values use the RAW masterbrand tokens (--primary-800, --grey-800,\n // --accent-800) rather than Tailwind's --color-* bridge aliases. Tailwind\n // v4 tree-shakes an unreferenced @theme key, and referencing one from\n // inside an arbitrary property is not a usage signal — the raw tokens are\n // plain :root declarations from @nswds/tokens and always resolve.\n // (--color-white is safe: `text-white` below is a real utility, so that\n // key is always emitted.) Same reasoning as footer.tsx and header.tsx.\n //\n // Dark mode deepens every surface onto the same family's dark steps —\n // -800→-950, -600→-900, -400→-850, -200→-800, white→grey-900 — per\n // footer.tsx's family mapping, and `default` follows header.tsx's\n // `light` surface: grey-100→grey-850. Ink goes white on all fourteen.\n //\n // primary-600, accent-600 and accent-400 set --search-placeholder-pct\n // to 100%: their 70% placeholder composite fails AA (3.0 / 3.1 /\n // 4.48:1 — see the derivation comment above), so the placeholder\n // renders full ink there. The override is deliberately not\n // dark-scoped: every dark 70% composite clears 7:1, so 100% in dark\n // is merely a slightly stronger placeholder, not worth a second\n // declaration.\n //\n // `default` is the one variant whose ink is NOT its text colour: the\n // nswds-app chip pairs grey-800 entered text with a primary-blue icon\n // and primary-800/10 halos. Declaring ink as primary-800 reproduces the\n // icon and halos exactly; the text colour is a surface utility, so it\n // stays grey-800. (Side effect, documented: the placeholder mixes from\n // primary-800 instead of the source's grey-700 — a subtle blue shift.)\n variant: {\n default:\n 'bg-grey-100 text-grey-800 [--search-ink:var(--primary-800)] dark:bg-grey-850 dark:text-white dark:[--search-ink:var(--color-white)]',\n 'primary-800':\n 'bg-primary-800 text-white [--search-ink:var(--color-white)] dark:bg-primary-950',\n 'primary-600':\n 'bg-primary-600 text-white [--search-ink:var(--color-white)] [--search-placeholder-pct:100%] dark:bg-primary-900',\n 'primary-400':\n 'bg-primary-400 text-primary-800 [--search-ink:var(--primary-800)] dark:bg-primary-850 dark:text-white dark:[--search-ink:var(--color-white)]',\n 'primary-200':\n 'bg-primary-200 text-primary-800 [--search-ink:var(--primary-800)] dark:bg-primary-800 dark:text-white dark:[--search-ink:var(--color-white)]',\n 'grey-800': 'bg-grey-800 text-white [--search-ink:var(--color-white)] dark:bg-grey-950',\n 'grey-600': 'bg-grey-600 text-white [--search-ink:var(--color-white)] dark:bg-grey-900',\n 'grey-400':\n 'bg-grey-400 text-grey-800 [--search-ink:var(--grey-800)] dark:bg-grey-850 dark:text-white dark:[--search-ink:var(--color-white)]',\n 'grey-200':\n 'bg-grey-200 text-grey-800 [--search-ink:var(--grey-800)] dark:bg-grey-800 dark:text-white dark:[--search-ink:var(--color-white)]',\n 'accent-800':\n 'bg-accent-800 text-white [--search-ink:var(--color-white)] dark:bg-accent-950',\n 'accent-600':\n 'bg-accent-600 text-white [--search-ink:var(--color-white)] [--search-placeholder-pct:100%] dark:bg-accent-900',\n 'accent-400':\n 'bg-accent-400 text-accent-800 [--search-ink:var(--accent-800)] [--search-placeholder-pct:100%] dark:bg-accent-850 dark:text-white dark:[--search-ink:var(--color-white)]',\n 'accent-200':\n 'bg-accent-200 text-accent-800 [--search-ink:var(--accent-800)] dark:bg-accent-800 dark:text-white dark:[--search-ink:var(--color-white)]',\n white:\n 'bg-white text-grey-800 [--search-ink:var(--grey-800)] dark:bg-grey-900 dark:text-white dark:[--search-ink:var(--color-white)]',\n },\n },\n defaultVariants: {\n variant: 'default',\n },\n },\n)\n\ntype ExpandableSearchVariant = NonNullable<VariantProps<typeof expandableSearchVariants>['variant']>\n\n// One focus treatment for the input and the submit button, INSET rather than\n// the source's offset ring. The source ringed each variant in its own surface\n// colour with a hardcoded white offset — on the default chip that was\n// ring-grey-100 around a grey-100 chip on a white page: effectively invisible\n// (a 2.4.7 Focus Visible / 2.4.13 Focus Appearance failure). An outward\n// indicator can only guarantee contrast against the page, which this component\n// cannot know; the ink is guaranteed to contrast with the surface (that is its\n// whole job), so the indicator is drawn 2px INSIDE the chip in ink, where the\n// contrast maths always holds.\nconst searchFocusClassName =\n 'focus-visible:outline focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-(--search-ink)'\n\ntype ExpandableSearchContextValue = {\n value: string\n setValue: (value: string) => void\n formHasFocusWithin: boolean\n variant: ExpandableSearchVariant\n}\n\nconst ExpandableSearchContext = React.createContext<ExpandableSearchContextValue | null>(null)\n\ntype ExpandableSearchProps = React.ComponentPropsWithoutRef<'form'> &\n VariantProps<typeof expandableSearchVariants> & {\n /**\n * Called with the current query when the form submits (Enter in the field\n * or a click on the search button). Not called if a supplied `onSubmit`\n * prevented the event's default.\n */\n onAction?: (value: string) => void\n /** Initial query. A non-empty value renders the field expanded. */\n defaultValue?: string\n ref?: React.Ref<HTMLFormElement>\n }\n\ntype ExpandableSearchButtonProps = Omit<React.ComponentPropsWithoutRef<'button'>, 'type'> & {\n [key: `data-${string}`]: string | number | boolean | undefined\n}\n\ntype ExpandableSearchFieldProps = Omit<\n React.ComponentPropsWithoutRef<'input'>,\n // `type` is pinned to `search`; `value`/`defaultValue` are owned by the\n // `ExpandableSearch` context (seed it via the root's `defaultValue` prop).\n 'type' | 'value' | 'defaultValue'\n> & {\n /**\n * Accessible name for the input, rendered as a visually-hidden `<label>`\n * and mirrored as `aria-label`. Defaults to `'Search'`.\n */\n label?: string\n /** Accessible name for the submit button. Defaults to `'Search'`. */\n buttonLabel?: string\n /** Extra props for the submit button (its `type` is pinned to `submit`). */\n buttonProps?: ExpandableSearchButtonProps\n ref?: React.Ref<HTMLInputElement>\n}\n\n/**\n * Dev-only guard, mirroring the icon-only Button check in button.tsx: with\n * `label=''` and no `aria-label` the input reaches assistive tech unnamed\n * (WCAG 2.2, 4.1.2 Name, Role, Value) — and this control leans on its name\n * more than most, since collapsed it looks like a plain icon button. No-op in\n * production.\n */\nfunction warnIfFieldUnlabelled(label: string | undefined, ariaLabel: string | undefined) {\n if (process.env.NODE_ENV === 'production') {\n return\n }\n if (!label && !ariaLabel) {\n console.warn(\n '[nswds/ui] ExpandableSearchField has no label — pass `label` or `aria-label` so the search input has an accessible name.',\n )\n }\n}\n\n/**\n * Site-search disclosure: a 48px search chip that expands into a text field\n * whenever it holds focus or a value, and submits the query to `onAction`.\n * Compose it as `<ExpandableSearch>` (the form root, owning the value and the\n * focus-within state) around one `<ExpandableSearchField>` (the input and its\n * submit button) — typically inside `HeaderActions`.\n *\n * Ported from nswds-app's `ExpandableSearch`. The mechanic is kept verbatim —\n * the collapsed control IS the text input, styled as a chip, so that the very\n * first focus or tap both reveals and focuses the field with no extra step.\n * Expansion is pure CSS keyed on the active state; no Base UI primitive is\n * used because none models this pattern (it is a styled form, not a popup —\n * focus never moves, nothing is portalled, nothing needs ARIA beyond the\n * label).\n *\n * Accessibility contract:\n * - The input carries a visually-hidden `<label>` and an `aria-label`\n * (default \"Search\"), and is `type='search'` with `enterKeyHint='search'`,\n * so its role and purpose are announced while it still looks like a button\n * (WCAG 2.2, 4.1.2; the source input was an unlabelled-by-type text input).\n * - Keyboard focus shows a 2px ink outline drawn inside the chip — the\n * source's ring was its own surface colour against a hardcoded white offset\n * and could vanish entirely (2.4.7, 2.4.13). See `searchFocusClassName`.\n * - Enter submits; Escape clears (native `type='search'` behaviour); an empty\n * blurred field collapses back to the chip.\n * - Expansion and halo transitions honour `prefers-reduced-motion` (2.3.3).\n *\n * Departures from the nswds-app source, beyond those above:\n * - React 19 `ref` prop instead of `forwardRef`.\n * - Four parallel 14-entry variant tables replaced by one ink-driven map —\n * see `expandableSearchVariants`.\n * - Expanded width `w-96` → `w-[min(24rem,80vw)]` so the field can never\n * overflow a small viewport (1.4.10 Reflow).\n * - The expanded state no longer sets `outline-none` (it would have killed\n * the new focus indicator; the source could afford it because its\n * indicator was a box-shadow ring).\n * - Expanded text selection uses `select-text`, not the source's\n * `select-auto` — `auto` computes from the root's `select-none` parent, so\n * the source's expanded field was unselectable.\n * - A consumer `onChange` on the field is chained after the context update\n * instead of replacing it (the source's spread order let it clobber the\n * controlled handler).\n *\n * The root deliberately does not claim `role='search'`: where the component\n * is the page's site search, add it yourself (one search landmark per page).\n */\nfunction ExpandableSearch({\n className,\n onAction,\n defaultValue = '',\n onSubmit,\n onFocusCapture,\n onBlurCapture,\n variant = 'default',\n ref,\n ...props\n}: ExpandableSearchProps) {\n const [value, setValue] = React.useState(defaultValue)\n const [formHasFocusWithin, setFormHasFocusWithin] = React.useState(false)\n const resolvedVariant: ExpandableSearchVariant = variant ?? 'default'\n const isActive = value.length > 0 || formHasFocusWithin\n\n const contextValue = React.useMemo(\n () => ({ value, setValue, formHasFocusWithin, variant: resolvedVariant }),\n [value, formHasFocusWithin, resolvedVariant],\n )\n\n const handleSubmit = (e: React.SubmitEvent<HTMLFormElement>) => {\n onSubmit?.(e)\n\n if (e.defaultPrevented) {\n return\n }\n\n e.preventDefault()\n onAction?.(value)\n }\n\n const handleFocusCapture = (e: React.FocusEvent<HTMLFormElement>) => {\n setFormHasFocusWithin(true)\n onFocusCapture?.(e)\n }\n\n const handleBlurCapture = (e: React.FocusEvent<HTMLFormElement>) => {\n const nextFocusedElement = e.relatedTarget as Node | null\n\n if (!e.currentTarget.contains(nextFocusedElement)) {\n setFormHasFocusWithin(false)\n }\n\n onBlurCapture?.(e)\n }\n\n return (\n <ExpandableSearchContext.Provider value={contextValue}>\n <form\n data-slot='expandable-search'\n data-variant={resolvedVariant}\n // Present-or-absent rather than \"true\"/\"false\", so it reads like Base\n // UI's data-* attributes and Tailwind's bare `data-expanded:` variant\n // works. Same convention as Header's data-scrolled.\n data-expanded={isActive || undefined}\n {...props}\n // The surface (and its ink) lives on the root so BOTH children — the\n // input and the absolutely-positioned submit button — inherit the\n // custom properties. The root shrink-wraps the input, so painting the\n // background here is visually identical to painting it on the input.\n className={cn(expandableSearchVariants({ variant }), className)}\n onBlurCapture={handleBlurCapture}\n onFocusCapture={handleFocusCapture}\n onSubmit={handleSubmit}\n ref={ref}\n />\n </ExpandableSearchContext.Provider>\n )\n}\n\n/**\n * The input and its submit button. Must be rendered inside `ExpandableSearch`,\n * which owns the value and the focus-within state.\n *\n * The submit button is `pointer-events-none` while collapsed — deliberately,\n * per the source: collapsed, the input overlays the exact same 48px square and\n * OWNS the click, so a tap lands on the input, focuses it, and expands the\n * field. Making the button clickable there would submit an empty query\n * instead of opening the field. It also never traps keyboard focus while\n * collapsed: the input precedes it in tab order, and focusing the input\n * expands the field before the button is reached.\n */\nfunction ExpandableSearchField({\n className,\n id,\n label = 'Search',\n 'aria-label': ariaLabel,\n autoComplete = 'off',\n enterKeyHint = 'search',\n onChange,\n buttonLabel = 'Search',\n buttonProps,\n ref,\n ...props\n}: ExpandableSearchFieldProps) {\n const context = React.useContext(ExpandableSearchContext)\n if (!context) throw new Error('ExpandableSearchField must be used within ExpandableSearch')\n\n const generatedId = React.useId()\n const {\n className: buttonClassName,\n children: buttonChildren,\n 'aria-label': buttonAriaLabel,\n ...restButtonProps\n } = buttonProps ?? {}\n\n const isActive = context.value.length > 0 || context.formHasFocusWithin\n const inputId = id ?? generatedId\n\n warnIfFieldUnlabelled(label, ariaLabel)\n\n const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {\n context.setValue(e.target.value)\n onChange?.(e)\n }\n\n return (\n <div data-slot='expandable-search-field' className='relative flex-1'>\n {label ? (\n // aria-label (below) wins for the accessible name where both exist;\n // the label element is kept for source parity and so a click on any\n // future visible rendering of it focuses the input.\n <label className='sr-only' htmlFor={inputId}>\n {label}\n </label>\n ) : null}\n\n <input\n data-slot='expandable-search-input'\n aria-label={ariaLabel ?? (label || undefined)}\n autoComplete={autoComplete}\n enterKeyHint={enterKeyHint}\n {...props}\n type='search'\n id={inputId}\n value={context.value}\n onChange={handleChange}\n className={cn(\n // Collapsed, the input is a 48px chip: transparent text and\n // placeholder so nothing peeks out, cursor-pointer because it acts\n // as the \"open search\" affordance. The background comes from the\n // form root, which shrink-wraps this element.\n 'flex h-12 w-12 cursor-pointer overflow-hidden rounded-sm border-0 bg-transparent text-transparent',\n 'placeholder:text-transparent placeholder:opacity-0',\n 'transition-[width,box-shadow,background-color] duration-300 ease-in-out motion-reduce:transition-none',\n // Hide WebKit's native clear affordance: the source field (type\n // text) had none, and Escape already clears a search input.\n '[&::-webkit-search-cancel-button]:appearance-none',\n searchFocusClassName,\n isActive\n ? // Expanded: capped at 80vw so the field cannot overflow small\n // viewports (the source's fixed w-96 could). pr-12 reserves the\n // submit button's square. select-text (not select-auto — see\n // the component JSDoc) re-enables selection under the root's\n // select-none.\n 'w-[min(24rem,80vw)] cursor-auto pt-0 pr-12 pb-0 pl-4 text-inherit select-text placeholder:text-(--search-placeholder) placeholder:opacity-100'\n : // Collapsed: the chip halos on hover exactly like the button\n // does — one visual whether the pointer technically rests on\n // the input overlay or not.\n 'p-0 hover:bg-(--search-halo)',\n className,\n )}\n ref={ref}\n />\n\n <button\n data-slot='expandable-search-button'\n aria-label={buttonAriaLabel ?? buttonLabel}\n {...restButtonProps}\n type='submit'\n className={cn(\n 'absolute top-0 right-0 z-10 flex h-12 w-12 cursor-pointer rounded-sm',\n 'transition-[background-color,box-shadow] duration-200 motion-reduce:transition-none',\n // Icon and halos all derive from the ink; the whole-form\n // focus-within halo marks the button as the pending action while\n // the user types.\n 'text-(--search-ink)',\n 'group-focus-within/search:bg-(--search-halo) hover:bg-(--search-halo) active:bg-(--search-halo-active)',\n searchFocusClassName,\n // Collapsed, the input overlay owns the click — see the JSDoc.\n isActive ? 'pointer-events-auto' : 'pointer-events-none',\n buttonClassName,\n )}\n >\n {buttonChildren ?? (\n <IconSearch aria-hidden='true' className='m-auto block size-8 shrink-0 fill-current' />\n )}\n </button>\n </div>\n )\n}\n\nexport { ExpandableSearch, ExpandableSearchField, expandableSearchVariants }\nexport type {\n ExpandableSearchButtonProps,\n ExpandableSearchFieldProps,\n ExpandableSearchProps,\n ExpandableSearchVariant,\n}\n"]}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import * as class_variance_authority_types from 'class-variance-authority/types';
|
|
2
|
+
import { VariantProps } from 'class-variance-authority';
|
|
3
|
+
import React__default from 'react';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Surface colours the main navigation can be themed with — exactly the
|
|
7
|
+
* Footer's thirteen-name vocabulary, so a service that themes its footer
|
|
8
|
+
* `primary-800` writes the same word here. Each name is the LIGHT-mode
|
|
9
|
+
* surface; dark mode deepens it onto the same family's dark steps (see the
|
|
10
|
+
* `color` variants below), so `color` expresses tonal weight rather than a
|
|
11
|
+
* literal colour.
|
|
12
|
+
*
|
|
13
|
+
* The bar's text/background pairs are identical to the Footer's, so its
|
|
14
|
+
* contrast conclusions carry over verbatim: every light pair clears WCAG 2.2
|
|
15
|
+
* AA (1.4.3, 4.5:1) for the bar's text and 11 of the 13 also clear AAA
|
|
16
|
+
* (1.4.6, 7:1). The two AA-only pairs are `primary-600` (4.57:1) and
|
|
17
|
+
* `accent-600` (5.18:1), both with white ink; prefer the `-800` steps when a
|
|
18
|
+
* service is held to AAA. In dark mode all thirteen clear AAA (worst 13.6:1).
|
|
19
|
+
*
|
|
20
|
+
* The mega panels always render on the house popup surface (`bg-popover`),
|
|
21
|
+
* matching the nswds-app source where every dropdown was white regardless of
|
|
22
|
+
* bar colour — only the panel's accent ink follows the colour family.
|
|
23
|
+
*/
|
|
24
|
+
declare const mainNavColors: readonly ["primary-800", "primary-600", "primary-400", "primary-200", "grey-800", "grey-600", "grey-400", "grey-200", "accent-800", "accent-600", "accent-400", "accent-200", "white"];
|
|
25
|
+
type MainNavColor = (typeof mainNavColors)[number];
|
|
26
|
+
declare const mainNavVariants: (props?: ({
|
|
27
|
+
color?: "white" | "primary-800" | "primary-600" | "primary-400" | "primary-200" | "grey-800" | "grey-600" | "grey-400" | "grey-200" | "accent-800" | "accent-600" | "accent-400" | "accent-200" | null | undefined;
|
|
28
|
+
border?: "none" | "both" | "top" | "bottom" | null | undefined;
|
|
29
|
+
} & class_variance_authority_types.ClassProp) | undefined) => string;
|
|
30
|
+
declare const mainNavContainerVariants: (props?: ({
|
|
31
|
+
container?: "fluid" | "contained" | null | undefined;
|
|
32
|
+
} & class_variance_authority_types.ClassProp) | undefined) => string;
|
|
33
|
+
/**
|
|
34
|
+
* Mega-panel accent ink, keyed by the same thirteen colour names as the bar.
|
|
35
|
+
* The panel itself is always the house popup surface (`bg-popover`, white in
|
|
36
|
+
* light mode) — parity with the app source, whose dropdown was white for
|
|
37
|
+
* every variant — so the family only tints the featured lead link, hover ink
|
|
38
|
+
* and cell borders. Declared on the Content element (not inherited from the
|
|
39
|
+
* bar) because Base UI portals the panel to `document.body`: a custom
|
|
40
|
+
* property set on the `<nav>` can never reach it.
|
|
41
|
+
*
|
|
42
|
+
* Light inks are the family's -800 step on white — grey-800 on white is
|
|
43
|
+
* 15.1:1 and white on primary-800 is 14.4:1 (both AAA; contrast is symmetric,
|
|
44
|
+
* ratios from header.tsx); accent-800 on white clears AAA per the Footer's
|
|
45
|
+
* verified pairs. Dark inks step to the family's -200 — the same dark-mode
|
|
46
|
+
* interactive ink link.tsx and navigation-menu.tsx already use on this
|
|
47
|
+
* popover surface. The featured lead is text-xl bold, so even the softest
|
|
48
|
+
* pair is far above the large-text AA floor (1.4.3, 3:1).
|
|
49
|
+
*/
|
|
50
|
+
declare const mainNavPanelVariants: (props?: ({
|
|
51
|
+
color?: "white" | "primary-800" | "primary-600" | "primary-400" | "primary-200" | "grey-800" | "grey-600" | "grey-400" | "grey-200" | "accent-800" | "accent-600" | "accent-400" | "accent-200" | null | undefined;
|
|
52
|
+
} & class_variance_authority_types.ClassProp) | undefined) => string;
|
|
53
|
+
/** One link inside a mega panel. */
|
|
54
|
+
type MainNavLinkItem = {
|
|
55
|
+
/** Visible link text, e.g. "Find support near you". */
|
|
56
|
+
title: string;
|
|
57
|
+
href: string;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* One top-level navigation item. With `links` it renders as a trigger opening
|
|
61
|
+
* a mega panel (with `href` becoming the panel's featured lead link; without
|
|
62
|
+
* an `href` the lead is a plain heading row — dev builds warn); without
|
|
63
|
+
* `links` it renders as a plain link styled like the triggers. Recursion is
|
|
64
|
+
* deliberately one level deep — mega panels list flat links, per the app
|
|
65
|
+
* source's NavigationSection shape.
|
|
66
|
+
*/
|
|
67
|
+
type MainNavItem = {
|
|
68
|
+
title: string;
|
|
69
|
+
href?: string;
|
|
70
|
+
links?: MainNavLinkItem[];
|
|
71
|
+
};
|
|
72
|
+
type MainNavProps = Omit<React__default.ComponentPropsWithoutRef<'nav'>, 'children'> & VariantProps<typeof mainNavVariants> & VariantProps<typeof mainNavContainerVariants> & {
|
|
73
|
+
/** Top-level items; items with `links` open mega panels. */
|
|
74
|
+
navigation: MainNavItem[];
|
|
75
|
+
/**
|
|
76
|
+
* The current page's href. The exact matching link gets
|
|
77
|
+
* `aria-current="page"`, and the top-level item it belongs to (a plain
|
|
78
|
+
* link, or the trigger whose panel contains it) gets the current-item
|
|
79
|
+
* underline. The app source read this from next/navigation's
|
|
80
|
+
* `usePathname` — a framework API this package must not import; compare
|
|
81
|
+
* in the consumer and pass the result down.
|
|
82
|
+
*/
|
|
83
|
+
currentHref?: string;
|
|
84
|
+
/**
|
|
85
|
+
* Stick to the top of the viewport as the page scrolls. Defaults to
|
|
86
|
+
* `false`. When the nav renders below a sticky `Header`, set
|
|
87
|
+
* `--main-nav-top` to the header's height so the two stack instead of
|
|
88
|
+
* overlapping — this custom property replaces the app source's
|
|
89
|
+
* `useSelectorHeight` DOM-measuring hook (the app measured `#nsw-header`
|
|
90
|
+
* on every resize; a consumer that needs a live value can still measure
|
|
91
|
+
* its own header and write the property, see the Sticky story).
|
|
92
|
+
*/
|
|
93
|
+
sticky?: boolean;
|
|
94
|
+
/** Drop shadow under the bar. Defaults to `true`. */
|
|
95
|
+
shadow?: boolean;
|
|
96
|
+
/** Classes applied to the inner width-constraining wrapper. */
|
|
97
|
+
containerClassName?: string;
|
|
98
|
+
ref?: React__default.Ref<HTMLElement>;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Full-width NSW site navigation bar with mega-menu panels — the data-driven
|
|
102
|
+
* consolidation of the nswds-app `NavigationMenuMainNavigation` /
|
|
103
|
+
* `MainNavigation` components, rebuilt on this package's `NavigationMenu`
|
|
104
|
+
* (Base UI underneath).
|
|
105
|
+
*
|
|
106
|
+
* - `navigation` drives everything: items with `links` render a trigger whose
|
|
107
|
+
* mega panel shows a featured lead link (the section `href`; with no `href`
|
|
108
|
+
* the lead demotes to a plain heading row — dev builds warn) above a
|
|
109
|
+
* responsive 1/2/3-column grid of bordered section links; items without
|
|
110
|
+
* `links` render plain links styled like the triggers. All anchors render
|
|
111
|
+
* through `Link`, so a framework link injected via `LinkProvider`
|
|
112
|
+
* (e.g. next/link) is used automatically.
|
|
113
|
+
* - `color` themes the bar with the Footer's thirteen-name vocabulary and its
|
|
114
|
+
* exact dark-mode deepening; every derived colour (halos, borders, focus
|
|
115
|
+
* outlines) resolves from a single `--main-nav-ink` per variant. Contrast
|
|
116
|
+
* conclusions are the Footer's, verbatim (see `mainNavColors`).
|
|
117
|
+
* - Panels span the nav container's full width: a ResizeObserver on the inner
|
|
118
|
+
* container feeds each panel's inline width, and an `alignOffset` function
|
|
119
|
+
* shifts Base UI's trigger-anchored positioner back to the container's left
|
|
120
|
+
* edge (measured at position time from the open trigger, so it is exact for
|
|
121
|
+
* every trigger — this replaces the app source's panelMetrics state).
|
|
122
|
+
* Assumes LTR; see the class comments.
|
|
123
|
+
* - `container`, `border`, `shadow`, `sticky` mirror the house chrome
|
|
124
|
+
* components; the app's deprecated `showTopBorder`/`showBottomBorder` and
|
|
125
|
+
* `responsive` props are not ported (use `border` and
|
|
126
|
+
* `className="hidden lg:block"`).
|
|
127
|
+
*
|
|
128
|
+
* Accessibility: the outer `<nav>` is the landmark (default label "Main
|
|
129
|
+
* navigation"; Base UI's own root is demoted to a `<div>` via `render` so the
|
|
130
|
+
* page doesn't gain a nested nav landmark). Everything interactive is
|
|
131
|
+
* inherited from Base UI via the sibling: triggers are real buttons with
|
|
132
|
+
* `aria-expanded`/`aria-controls`, ArrowDown/Enter opens, arrow keys rove,
|
|
133
|
+
* Escape and focus-out close, and the current page announces
|
|
134
|
+
* `aria-current="page"` (WCAG 2.4.8). Focus indicators are 2px ink-derived
|
|
135
|
+
* outlines that contrast on all thirteen surfaces and on the panel surface
|
|
136
|
+
* (2.4.7, 2.4.13). The 220ms `closeDelay` reproduces the app's hover-out
|
|
137
|
+
* grace timer through Base UI instead of a hand-rolled setTimeout.
|
|
138
|
+
*
|
|
139
|
+
* The default `id="nsw-main-navigation"` is kept for compatibility with
|
|
140
|
+
* existing shells that target it (override via the `id` prop).
|
|
141
|
+
*/
|
|
142
|
+
declare function MainNav({ className, containerClassName, color, container, border, navigation, currentHref, sticky, shadow, 'aria-label': ariaLabel, ref, ...props }: MainNavProps): React__default.JSX.Element;
|
|
143
|
+
|
|
144
|
+
export { MainNav, type MainNavColor, type MainNavItem, type MainNavLinkItem, type MainNavProps, mainNavColors, mainNavContainerVariants, mainNavPanelVariants, mainNavVariants };
|