@nswds/ui 4.2.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/header.d.ts +147 -0
- package/dist/components/header.js +221 -0
- package/dist/components/header.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 +8 -0
- package/dist/index.js +8 -0
- package/dist/styles.css +1 -1
- package/package.json +6 -5
|
@@ -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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAcA,uEAAA;AAAA,IACA,8EAAA;AAAA,IACA;AAAA,GACF;AAAA,EACA;AAAA,IACE,QAAA,EAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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A,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,147 @@
|
|
|
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
|
+
import { Link } from './link.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Surface colours the header can be themed with, sharing the Masthead's and
|
|
8
|
+
* SkipLink's four-name vocabulary (`dark`, `light`, `white`, `grey`) — the three
|
|
9
|
+
* components stack at the top of the page and a service picks one word for the
|
|
10
|
+
* whole chrome.
|
|
11
|
+
*
|
|
12
|
+
* Every pair is WCAG 2.2 AAA (1.4.6, 7:1), computed from the masterbrand oklch
|
|
13
|
+
* tokens. Light mode: grey-800 on white 15.1:1, grey-800 on grey-100 13.8:1,
|
|
14
|
+
* white on primary-800 14.4:1, white on grey-800 15.1:1. Dark mode deepens each
|
|
15
|
+
* surface onto the same family's dark step and clears AAA more comfortably
|
|
16
|
+
* still (17.2:1–20.2:1).
|
|
17
|
+
*
|
|
18
|
+
* Unlike the Masthead — a brand strip whose light/white/grey surfaces are frozen
|
|
19
|
+
* across themes — the header is the page's own surface, so all four follow the
|
|
20
|
+
* theme. Pair `Header color="dark"` with `Masthead color="dark"` when a service
|
|
21
|
+
* wants the two to match in both modes.
|
|
22
|
+
*/
|
|
23
|
+
declare const headerColors: {
|
|
24
|
+
dark: string;
|
|
25
|
+
light: string;
|
|
26
|
+
white: string;
|
|
27
|
+
grey: string;
|
|
28
|
+
};
|
|
29
|
+
type HeaderColor = keyof typeof headerColors;
|
|
30
|
+
declare const headerVariants: (props?: ({
|
|
31
|
+
color?: "light" | "white" | "grey" | "dark" | null | undefined;
|
|
32
|
+
} & class_variance_authority_types.ClassProp) | undefined) => string;
|
|
33
|
+
declare const headerContainerVariants: (props?: ({
|
|
34
|
+
container?: "fluid" | "contained" | null | undefined;
|
|
35
|
+
} & class_variance_authority_types.ClassProp) | undefined) => string;
|
|
36
|
+
type HeaderBrandProps = React__default.ComponentPropsWithoutRef<'div'> & {
|
|
37
|
+
/** Home-page target for the brand link. Defaults to `/`. */
|
|
38
|
+
href?: React__default.ComponentPropsWithoutRef<typeof Link>['href'];
|
|
39
|
+
/** Service or site name shown beside the logo. */
|
|
40
|
+
sitename?: React__default.ReactNode;
|
|
41
|
+
/**
|
|
42
|
+
* Heading level for `sitename`. Omit (the default) to render a `<span>`,
|
|
43
|
+
* which is usually right: the page's own `<h1>` belongs to its main content,
|
|
44
|
+
* and a site name repeated in the header on every page is not the heading
|
|
45
|
+
* that describes this page. Supply a level only when a service deliberately
|
|
46
|
+
* places the site name in the document outline.
|
|
47
|
+
*
|
|
48
|
+
* `1` is excluded for the reason above; `6` is the deepest heading HTML
|
|
49
|
+
* defines.
|
|
50
|
+
*/
|
|
51
|
+
headingLevel?: 2 | 3 | 4 | 5 | 6;
|
|
52
|
+
/**
|
|
53
|
+
* Version string shown in a Badge beside the brand. Sits outside the link,
|
|
54
|
+
* so it never becomes part of the home link's accessible name.
|
|
55
|
+
*/
|
|
56
|
+
version?: React__default.ReactNode;
|
|
57
|
+
/**
|
|
58
|
+
* Visually-hidden prefix announced before `version`, giving the bare number
|
|
59
|
+
* context for screen readers ("2.1.0" → "Version 2.1.0"). Pass an empty
|
|
60
|
+
* string to suppress.
|
|
61
|
+
*/
|
|
62
|
+
versionLabel?: string;
|
|
63
|
+
/**
|
|
64
|
+
* `true` (default) renders the NSW Government waratah, `false` omits it, and
|
|
65
|
+
* a node replaces it — pass an agency lockup here.
|
|
66
|
+
*/
|
|
67
|
+
logo?: boolean | React__default.ReactNode;
|
|
68
|
+
/**
|
|
69
|
+
* Accessible name for the brand link. Left unset by default: the link's name
|
|
70
|
+
* is then its visible content — "NSW Government" (from `Logo`'s
|
|
71
|
+
* visually-hidden text) plus `sitename` — which keeps the visible label
|
|
72
|
+
* inside the accessible name (WCAG 2.2, 2.5.3 Label in Name). Overriding it
|
|
73
|
+
* with something like "Home page" would break that, so set it only when the
|
|
74
|
+
* brand renders no text at all.
|
|
75
|
+
*/
|
|
76
|
+
label?: string;
|
|
77
|
+
ref?: React__default.Ref<HTMLDivElement>;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Logo, site name and optional version badge, linked to the home page. Sits at
|
|
81
|
+
* the start of the `Header` row.
|
|
82
|
+
*
|
|
83
|
+
* The link renders through `Link`, so it picks up the framework link component
|
|
84
|
+
* from `LinkProvider` (next/link et al) rather than hardcoding one.
|
|
85
|
+
*/
|
|
86
|
+
declare function HeaderBrand({ className, href, sitename, headingLevel, version, versionLabel, logo, label, children, ref, ...props }: HeaderBrandProps): React__default.JSX.Element;
|
|
87
|
+
type HeaderActionsProps = React__default.ComponentPropsWithoutRef<'div'> & {
|
|
88
|
+
ref?: React__default.Ref<HTMLDivElement>;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Trailing cluster of header controls — search, theme switcher, sign-in, and
|
|
92
|
+
* the like. Deliberately unopinionated about what goes in it: site search and
|
|
93
|
+
* theme switching are app concerns (they need routing and a theme provider),
|
|
94
|
+
* so the design system supplies the slot and its spacing rather than the
|
|
95
|
+
* widgets.
|
|
96
|
+
*
|
|
97
|
+
* Not a `nav` landmark: a row of controls is not a set of navigation links, and
|
|
98
|
+
* an extra landmark on every page buries the ones that matter. Wrap genuine
|
|
99
|
+
* navigation in your own `<nav>` inside `Header`.
|
|
100
|
+
*/
|
|
101
|
+
declare function HeaderActions({ className, ref, ...props }: HeaderActionsProps): React__default.JSX.Element;
|
|
102
|
+
type HeaderProps = React__default.ComponentPropsWithoutRef<'header'> & VariantProps<typeof headerVariants> & VariantProps<typeof headerContainerVariants> & {
|
|
103
|
+
/** Stick to the top of the viewport as the page scrolls. Defaults to `true`. */
|
|
104
|
+
sticky?: boolean;
|
|
105
|
+
/** Hairline rule along the bottom edge. Defaults to `true`. */
|
|
106
|
+
border?: boolean;
|
|
107
|
+
/** Raise the header with a shadow once the page is scrolled. Defaults to `true`. */
|
|
108
|
+
shadow?: boolean;
|
|
109
|
+
/** Classes applied to the inner width-constraining wrapper. */
|
|
110
|
+
containerClassName?: string;
|
|
111
|
+
ref?: React__default.Ref<HTMLElement>;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Top-of-page `banner` landmark: brand, site name and header controls. Render
|
|
115
|
+
* it once in a shared layout, below `SkipLinks` and `Masthead`.
|
|
116
|
+
*
|
|
117
|
+
* - `color` themes the surface; all four pairs are WCAG 2.2 AAA and follow the
|
|
118
|
+
* theme in dark mode (see `headerColors`). The bottom rule derives from the
|
|
119
|
+
* surface's ink, so it follows automatically.
|
|
120
|
+
* - `container` selects the inner wrapper layout: `fluid` (full-bleed,
|
|
121
|
+
* nswds-app parity) or `contained` (centred 1200px column, legacy
|
|
122
|
+
* `nsw-container` parity). Fine-tune either with the `--header-max-width`
|
|
123
|
+
* and `--header-padding-x` custom properties.
|
|
124
|
+
* - Scroll position is tracked and exposed as `data-scrolled`, which is what
|
|
125
|
+
* the `shadow` treatment keys off. Style your own scrolled state with it.
|
|
126
|
+
*
|
|
127
|
+
* Compose the row from `HeaderBrand` and `HeaderActions`:
|
|
128
|
+
*
|
|
129
|
+
* ```tsx
|
|
130
|
+
* <Header>
|
|
131
|
+
* <HeaderBrand sitename="Design System" version="2.1.0" />
|
|
132
|
+
* <HeaderActions>
|
|
133
|
+
* <ThemeSwitcher />
|
|
134
|
+
* </HeaderActions>
|
|
135
|
+
* </Header>
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* The `banner` landmark comes from the native `<header>` element, which only
|
|
139
|
+
* carries it outside `article`, `aside`, `main`, `nav` and `section` — wrapping
|
|
140
|
+
* `div`s are fine. Keep it out of `<main>`.
|
|
141
|
+
*
|
|
142
|
+
* The default `id="nsw-header"` is kept for compatibility with existing shells
|
|
143
|
+
* that target it (override via the `id` prop).
|
|
144
|
+
*/
|
|
145
|
+
declare function Header({ className, color, container, containerClassName, sticky, border, shadow, children, ref, ...props }: HeaderProps): React__default.JSX.Element;
|
|
146
|
+
|
|
147
|
+
export { Header, HeaderActions, type HeaderActionsProps, HeaderBrand, type HeaderBrandProps, type HeaderColor, type HeaderProps, headerColors, headerContainerVariants, headerVariants };
|