@brdgapp/ui 1.10.0 → 1.12.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.
@@ -0,0 +1,77 @@
1
+ import * as React from "react";
2
+ /** A date as "YYYY-MM-DD", the way the browser's date field and a database write it. */
3
+ type DateString = string;
4
+ /** A from–to range. Either end is `null` until it's picked. */
5
+ type DateRangeValue = {
6
+ start: DateString | null;
7
+ end: DateString | null;
8
+ };
9
+ /** A limit: "YYYY-MM-DD", or "today". */
10
+ type DateLimit = DateString | "today";
11
+ type CalendarSingleProps = {
12
+ range?: false;
13
+ /** The picked day, "YYYY-MM-DD". */
14
+ value?: DateString | null;
15
+ /** Called with the day that was picked. */
16
+ onValueChange?: (value: DateString) => void;
17
+ };
18
+ type CalendarRangeProps = {
19
+ /** Pick a start day, then an end day. */
20
+ range: true;
21
+ value?: DateRangeValue;
22
+ /** Called after each pick: first `{ start, end: null }`, then both. */
23
+ onValueChange?: (value: DateRangeValue) => void;
24
+ };
25
+ type CalendarProps = (CalendarSingleProps | CalendarRangeProps) & {
26
+ /** The earliest day that can be picked: "YYYY-MM-DD" or "today". */
27
+ min?: DateLimit;
28
+ /** The latest day that can be picked: "YYYY-MM-DD" or "today" (no future dates). */
29
+ max?: DateLimit;
30
+ /** Focus the picked day (or today) when it appears, as it does when DatePicker opens it. */
31
+ autoFocus?: boolean;
32
+ /** Layout only. */
33
+ className?: string;
34
+ };
35
+ /**
36
+ * The calendar on its own, for a place that has no field: a table cell's popover. In a form, use
37
+ * DatePicker, which opens this from its calendar button. Weeks start on Monday.
38
+ */
39
+ declare function Calendar(props: CalendarProps): React.JSX.Element;
40
+ type FieldBits = {
41
+ /** The earliest date allowed: "YYYY-MM-DD" or "today". Earlier days are greyed out, and a typed one is an error. */
42
+ min?: DateLimit;
43
+ /** The latest date allowed: "YYYY-MM-DD" or "today" (no future dates). */
44
+ max?: DateLimit;
45
+ /** Can't be typed in or opened. */
46
+ disabled?: boolean;
47
+ /** Sends the value with a form as "YYYY-MM-DD". A range sends two: `name` + "Start" and `name` + "End". */
48
+ name?: string;
49
+ /** Only outside a Field. Inside one, the FieldLabel names it. */
50
+ "aria-label"?: string;
51
+ /** Layout only: `max-w-60`. */
52
+ className?: string;
53
+ };
54
+ type SingleDatePickerProps = FieldBits & {
55
+ range?: false;
56
+ /** The date, "YYYY-MM-DD", or `null` for none. */
57
+ value?: DateString | null;
58
+ defaultValue?: DateString | null;
59
+ /** Called with "YYYY-MM-DD" once a date is picked or typed (when the field is left or Enter is pressed), or `null` when it's emptied or can't be read. */
60
+ onValueChange?: (value: DateString | null) => void;
61
+ };
62
+ type RangeDatePickerProps = FieldBits & {
63
+ /** A from–to range: two dates in one box, one calendar. */
64
+ range: true;
65
+ value?: DateRangeValue;
66
+ defaultValue?: DateRangeValue;
67
+ /** Called with `{ start, end }`, each "YYYY-MM-DD" or `null`. */
68
+ onValueChange?: (value: DateRangeValue) => void;
69
+ };
70
+ type DatePickerProps = SingleDatePickerProps | RangeDatePickerProps;
71
+ /**
72
+ * A date field: type a date ("Mar 4", "3/4/2026", "today") or pick it from the calendar button at its
73
+ * end. `range` makes it a from–to pair in one box. Put it in a Field with a FieldLabel.
74
+ */
75
+ declare function DatePicker(props: DatePickerProps): React.JSX.Element;
76
+ export { Calendar, DatePicker };
77
+ export type { CalendarProps, DateLimit, DatePickerProps, DateRangeValue, DateString, RangeDatePickerProps, SingleDatePickerProps };
@@ -0,0 +1,367 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
+ import * as React from "react";
4
+ import { Input as InputPrimitive } from "@base-ui/react/input";
5
+ import { DayPicker } from "react-day-picker";
6
+ import { AlertCircle, CalendarDays, ChevronDown, ChevronLeft, ChevronRight } from "lucide-react";
7
+ import { IconButton } from "@brdgapp/ui/components/icon-button";
8
+ import { Popover } from "@brdgapp/ui/components/popover";
9
+ import { cn } from "@brdgapp/ui/lib/utils";
10
+ const FULL_MONTHS = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
11
+ const SHORT_MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
12
+ const SHOWN = new Intl.DateTimeFormat("en-US", { month: "short", day: "numeric", year: "numeric" });
13
+ const CAPTION = new Intl.DateTimeFormat("en-US", { month: "long", year: "numeric" });
14
+ const pad = (n) => String(n).padStart(2, "0");
15
+ const toIso = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
16
+ const startOfToday = () => {
17
+ const d = new Date();
18
+ return new Date(d.getFullYear(), d.getMonth(), d.getDate());
19
+ };
20
+ const startOfMonth = (d) => new Date(d.getFullYear(), d.getMonth(), 1);
21
+ const addMonths = (d, n) => new Date(d.getFullYear(), d.getMonth() + n, 1);
22
+ /** A real day at local midnight, or undefined (Feb 30 isn't one). */
23
+ function makeDate(y, m, d) {
24
+ if (y === undefined || m < 1 || m > 12 || d < 1 || d > 31)
25
+ return undefined;
26
+ const date = new Date(y, m - 1, d);
27
+ return date.getMonth() === m - 1 ? date : undefined;
28
+ }
29
+ function fromIso(s) {
30
+ const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(s ?? "");
31
+ return m ? makeDate(Number(m[1]), Number(m[2]), Number(m[3])) : undefined;
32
+ }
33
+ const resolveLimit = (limit) => (limit === "today" ? startOfToday() : fromIso(limit));
34
+ const show = (d) => SHOWN.format(d);
35
+ /** Two-digit years land within ten years from now, or the century before: "31" is 2031, "85" is 1985. */
36
+ function fullYear(y, now) {
37
+ if (!y)
38
+ return now.getFullYear();
39
+ if (y.length === 4)
40
+ return Number(y);
41
+ const n = Number(y);
42
+ return n <= (now.getFullYear() % 100) + 10 ? 2000 + n : 1900 + n;
43
+ }
44
+ /** "mar", "march", "sept" → 3, 3, 9. At least three letters. */
45
+ function monthFromWord(w) {
46
+ if (w.length < 3)
47
+ return undefined;
48
+ const i = FULL_MONTHS.findIndex((m) => m.startsWith(w));
49
+ return i === -1 ? undefined : i + 1;
50
+ }
51
+ /**
52
+ * Reads what people type: "Mar 4, 2026", "March 4", "4 Mar 2026", "3/4/2026" (month first, the US way),
53
+ * "3/4/26", "2026-03-04", "today", "yesterday", "tomorrow". A date without a year is this year.
54
+ */
55
+ function parseDate(text, now = new Date()) {
56
+ const t = text
57
+ .trim()
58
+ .toLowerCase()
59
+ .replace(/(\d)(st|nd|rd|th)\b/g, "$1")
60
+ .replace(/[,.]/g, " ")
61
+ .replace(/\s+/g, " ")
62
+ .trim();
63
+ const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
64
+ if (t === "today")
65
+ return today;
66
+ if (t === "yesterday")
67
+ return new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1);
68
+ if (t === "tomorrow")
69
+ return new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);
70
+ let m = /^(\d{4})[-/ ](\d{1,2})[-/ ](\d{1,2})$/.exec(t);
71
+ if (m)
72
+ return makeDate(Number(m[1]), Number(m[2]), Number(m[3]));
73
+ m = /^(\d{1,2})[-/](\d{1,2})(?:[-/](\d{2}|\d{4}))?$/.exec(t);
74
+ if (m)
75
+ return makeDate(fullYear(m[3], now), Number(m[1]), Number(m[2]));
76
+ m = /^([a-z]+) (\d{1,2})(?: (\d{2}|\d{4}))?$/.exec(t);
77
+ if (m) {
78
+ const month = monthFromWord(m[1]);
79
+ return month ? makeDate(fullYear(m[3], now), month, Number(m[2])) : undefined;
80
+ }
81
+ m = /^(\d{1,2}) ([a-z]+)(?: (\d{2}|\d{4}))?$/.exec(t);
82
+ if (m) {
83
+ const month = monthFromWord(m[2]);
84
+ return month ? makeDate(fullYear(m[3], now), month, Number(m[1])) : undefined;
85
+ }
86
+ return undefined;
87
+ }
88
+ const headerButton = cn("inline-flex h-8 items-center gap-1 rounded-lg px-2 text-14 font-semibold text-[var(--text-primary)] outline-none", "transition-colors duration-150 ease-out hover:bg-[var(--surface-secondary)] focus-visible:ring-2 focus-visible:ring-[var(--primary)]");
89
+ // Days are 40px (44px on phones, where the calendar is a bottom sheet); the month and year grids fill the
90
+ // same height as the day grid, so the popover keeps its size.
91
+ const cellButton = cn("flex w-full items-center justify-center rounded-lg text-14 text-[var(--text-primary)] outline-none", "transition-colors duration-150 ease-out hover:bg-[var(--surface-secondary)] focus-visible:ring-2 focus-visible:ring-[var(--primary)]", "disabled:cursor-not-allowed disabled:text-[var(--text-disabled)] disabled:hover:bg-transparent", "data-[current]:bg-[var(--primary-solid)] data-[current]:font-semibold data-[current]:text-white data-[current]:hover:bg-[var(--primary-solid-hover)]");
92
+ /** Arrow keys move between the 12 months or years, three to a row. */
93
+ function gridKeys(e) {
94
+ const step = { ArrowLeft: -1, ArrowRight: 1, ArrowUp: -3, ArrowDown: 3 }[e.key];
95
+ if (!step)
96
+ return;
97
+ const buttons = Array.from(e.currentTarget.querySelectorAll("button:not(:disabled)"));
98
+ const at = buttons.indexOf(document.activeElement);
99
+ const next = buttons[Math.min(buttons.length - 1, Math.max(0, at + step))];
100
+ if (next) {
101
+ e.preventDefault();
102
+ next.focus();
103
+ }
104
+ }
105
+ function initialMonth(props) {
106
+ const picked = props.range ? fromIso(props.value?.start) ?? fromIso(props.value?.end) : fromIso(props.value);
107
+ const max = resolveLimit(props.max);
108
+ const min = resolveLimit(props.min);
109
+ let d = picked ?? startOfToday();
110
+ if (max && d > max)
111
+ d = max;
112
+ if (min && d < min)
113
+ d = min;
114
+ return startOfMonth(d);
115
+ }
116
+ /**
117
+ * The calendar on its own, for a place that has no field: a table cell's popover. In a form, use
118
+ * DatePicker, which opens this from its calendar button. Weeks start on Monday.
119
+ */
120
+ function Calendar(props) {
121
+ const { min, max, autoFocus, className } = props;
122
+ const [view, setView] = React.useState("days");
123
+ const [month, setMonth] = React.useState(() => initialMonth(props));
124
+ const [focusDays, setFocusDays] = React.useState(Boolean(autoFocus));
125
+ const gridRef = React.useRef(null);
126
+ const minDate = resolveLimit(min);
127
+ const maxDate = resolveLimit(max);
128
+ const year = month.getFullYear();
129
+ const firstYear = year - (((year % 12) + 12) % 12);
130
+ // Moving into the month or year view puts focus on the month or year you came from.
131
+ const [moved, setMoved] = React.useState(false);
132
+ React.useEffect(() => {
133
+ if (moved && view !== "days")
134
+ gridRef.current?.querySelector("[data-current]")?.focus();
135
+ }, [moved, view]);
136
+ const go = (next) => {
137
+ setMoved(true);
138
+ setFocusDays(next === "days");
139
+ setView(next);
140
+ };
141
+ const monthOut = (y, m) => Boolean((minDate && new Date(y, m + 1, 0) < minDate) || (maxDate && new Date(y, m, 1) > maxDate));
142
+ const yearOut = (y) => Boolean((minDate && y < minDate.getFullYear()) || (maxDate && y > maxDate.getFullYear()));
143
+ let caption;
144
+ let prev;
145
+ let next;
146
+ if (view === "days") {
147
+ caption = (_jsxs("button", { type: "button", className: headerButton, "aria-label": `${CAPTION.format(month)}, choose a month`, onClick: () => go("months"), children: [CAPTION.format(month), _jsx(ChevronDown, { "aria-hidden": "true", className: "size-4 text-[var(--text-muted)]" })] }));
148
+ prev = { label: "Previous month", to: () => setMonth(addMonths(month, -1)), disabled: Boolean(minDate && addMonths(month, -1) < startOfMonth(minDate)) };
149
+ next = { label: "Next month", to: () => setMonth(addMonths(month, 1)), disabled: Boolean(maxDate && addMonths(month, 1) > maxDate) };
150
+ }
151
+ else if (view === "months") {
152
+ caption = (_jsxs("button", { type: "button", className: headerButton, "aria-label": `${year}, choose a year`, onClick: () => go("years"), children: [year, _jsx(ChevronDown, { "aria-hidden": "true", className: "size-4 text-[var(--text-muted)]" })] }));
153
+ prev = { label: "Previous year", to: () => setMonth(new Date(year - 1, month.getMonth(), 1)), disabled: yearOut(year - 1) };
154
+ next = { label: "Next year", to: () => setMonth(new Date(year + 1, month.getMonth(), 1)), disabled: yearOut(year + 1) };
155
+ }
156
+ else {
157
+ caption = (_jsxs("span", { className: "px-2 text-14 font-semibold text-[var(--text-primary)] tabular-nums", children: [firstYear, " \u2013 ", firstYear + 11] }));
158
+ prev = { label: "Earlier years", to: () => setMonth(new Date(year - 12, month.getMonth(), 1)), disabled: yearOut(firstYear - 1) };
159
+ next = { label: "Later years", to: () => setMonth(new Date(year + 12, month.getMonth(), 1)), disabled: yearOut(firstYear + 12) };
160
+ }
161
+ const disabled = [...(minDate ? [{ before: minDate }] : []), ...(maxDate ? [{ after: maxDate }] : [])];
162
+ const shared = {
163
+ month,
164
+ onMonthChange: (m) => setMonth(startOfMonth(m)),
165
+ weekStartsOn: 1,
166
+ showOutsideDays: true,
167
+ fixedWeeks: true,
168
+ hideNavigation: true,
169
+ autoFocus: focusDays,
170
+ // Days out of range are greyed out and skipped by the arrow keys. The months around them stay drawn, so the
171
+ // grid keeps its shape (react-day-picker's startMonth/endMonth would blank next month's first days).
172
+ disabled,
173
+ classNames: {
174
+ root: "w-full",
175
+ months: "w-full",
176
+ month: "w-full",
177
+ // The visible caption is the button above; this one stays for screen readers, which hear the month change.
178
+ month_caption: "sr-only",
179
+ month_grid: "w-full border-separate border-spacing-y-0.5",
180
+ weekdays: "",
181
+ weekday: "h-8 text-12 font-medium text-[var(--text-muted)]",
182
+ week: "",
183
+ day: "p-0 text-center",
184
+ day_button: cn(cellButton, "h-11 md:h-10 tabular-nums"),
185
+ today: "[&>button]:font-bold [&:not([data-selected])>button]:text-[var(--primary)]",
186
+ outside: "[&:not([data-selected])>button]:text-[var(--text-muted)]",
187
+ disabled: "[&>button]:cursor-not-allowed [&>button]:!text-[var(--text-disabled)] [&>button]:hover:bg-transparent",
188
+ selected: "[&>button]:bg-[var(--primary-solid)] [&>button]:font-semibold [&>button]:text-white [&>button]:hover:bg-[var(--primary-solid-hover)]",
189
+ range_start: "rounded-l-lg bg-[var(--info-bg)]",
190
+ range_middle: "bg-[var(--info-bg)] [&>button]:!bg-transparent [&>button]:!font-normal [&>button]:!text-[var(--text-primary)] [&>button]:hover:!bg-[var(--surface-secondary)]",
191
+ range_end: "rounded-r-lg bg-[var(--info-bg)]",
192
+ hidden: "invisible",
193
+ },
194
+ };
195
+ return (_jsxs("div", { "data-slot": "calendar", className: cn("w-full min-w-[280px]", className), children: [_jsxs("div", { className: "mb-1 flex items-center justify-between gap-2", children: [caption, _jsxs("div", { className: "flex items-center gap-1", children: [_jsx(IconButton, { type: "button", size: "sm", "aria-label": prev.label, disabled: prev.disabled, onClick: prev.to, children: _jsx(ChevronLeft, {}) }), _jsx(IconButton, { type: "button", size: "sm", "aria-label": next.label, disabled: next.disabled, onClick: next.to, children: _jsx(ChevronRight, {}) })] })] }), view === "days" ? (props.range ? (_jsx(DayPicker, { ...shared, mode: "range", selected: { from: fromIso(props.value?.start), to: fromIso(props.value?.end) }, onSelect: (_range, day) => {
196
+ const start = props.value?.start ?? null;
197
+ const startDate = fromIso(start);
198
+ const iso = toIso(day);
199
+ // The first pick is the start; the next is the end, unless it's earlier, which starts again.
200
+ if (!startDate || props.value?.end || day < startDate)
201
+ props.onValueChange?.({ start: iso, end: null });
202
+ else
203
+ props.onValueChange?.({ start, end: iso });
204
+ } })) : (_jsx(DayPicker, { ...shared, mode: "single", required: true, selected: fromIso(props.value), onSelect: (_date, day) => props.onValueChange?.(toIso(day)) }))) : view === "months" ? (_jsx("div", { ref: gridRef, role: "group", "aria-label": `Months of ${year}`, className: "grid h-[312px] grid-cols-3 grid-rows-4 gap-1 pt-1 md:h-[288px]", onKeyDown: gridKeys, children: SHORT_MONTHS.map((label, m) => (_jsx("button", { type: "button", className: cn(cellButton, "h-full"), "aria-label": `${FULL_MONTHS[m][0].toUpperCase()}${FULL_MONTHS[m].slice(1)} ${year}`, "data-current": m === month.getMonth() ? "" : undefined, "aria-current": m === month.getMonth() ? "date" : undefined, disabled: monthOut(year, m), onClick: () => {
205
+ setMonth(new Date(year, m, 1));
206
+ go("days");
207
+ }, children: label }, label))) })) : (_jsx("div", { ref: gridRef, role: "group", "aria-label": `Years ${firstYear} to ${firstYear + 11}`, className: "grid h-[312px] grid-cols-3 grid-rows-4 gap-1 pt-1 md:h-[288px]", onKeyDown: gridKeys, children: Array.from({ length: 12 }, (_, i) => firstYear + i).map((y) => (_jsx("button", { type: "button", className: cn(cellButton, "h-full tabular-nums"), "data-current": y === year ? "" : undefined, "aria-current": y === year ? "date" : undefined, disabled: yearOut(y), onClick: () => {
208
+ setMonth(new Date(y, month.getMonth(), 1));
209
+ go("months");
210
+ }, children: y }, y))) }))] }));
211
+ }
212
+ // --- The field ----------------------------------------------------------------------------------
213
+ // The same box as Input with an icon: drawn around the typed text and the calendar button together.
214
+ const boxClasses = cn("flex h-11 md:h-10 w-full min-w-0 items-center gap-2 rounded-lg border border-[var(--border-field)] bg-[var(--surface-primary)] pr-1.5 pl-3.5", "transition-colors duration-150 ease-out hover:border-[var(--text-muted)]", "has-[input:focus-visible]:border-[var(--primary)] has-[input:focus-visible]:ring-2 has-[input:focus-visible]:ring-[var(--primary)]/20",
215
+ // An error stays red on hover and on focus.
216
+ "has-[[aria-invalid=true]]:border-[var(--error)] has-[[data-invalid]]:border-[var(--error)]", "has-[[aria-invalid=true]]:hover:border-[var(--error)] has-[[data-invalid]]:hover:border-[var(--error)]", "has-[[aria-invalid=true]:focus-visible]:border-[var(--error)] has-[[data-invalid]:focus-visible]:border-[var(--error)]", "has-[[aria-invalid=true]:focus-visible]:ring-[var(--error)]/20 has-[[data-invalid]:focus-visible]:ring-[var(--error)]/20", "has-[:disabled]:cursor-not-allowed has-[:disabled]:opacity-50");
217
+ const typed = cn("h-full w-full min-w-0 flex-1 bg-transparent outline-none tabular-nums", "text-16 md:text-14 text-[var(--text-primary)] placeholder:text-[var(--text-muted)]", "disabled:cursor-not-allowed");
218
+ /** The example in an empty field: a date this year, in the form it will show. */
219
+ const placeholder = () => `Mar 4, ${new Date().getFullYear()}`;
220
+ /** Why a date can't be used, in words that say how to fix it, or null. */
221
+ function problemWith(d, minDate, maxDate, min, max) {
222
+ if (minDate && d < minDate)
223
+ return `Pick a date on or after ${min === "today" ? "today" : show(minDate)}.`;
224
+ if (maxDate && d > maxDate)
225
+ return `Pick a date on or before ${max === "today" ? "today" : show(maxDate)}.`;
226
+ return null;
227
+ }
228
+ /**
229
+ * The typed half of the field: its own text while it's typed, read when it's left or Enter is pressed.
230
+ * A value set from outside (a reset, a loaded record) replaces the text; the value this field just sent doesn't.
231
+ */
232
+ function useTypedDate(value, send, check) {
233
+ const [text, setText] = React.useState(() => (fromIso(value) ? show(fromIso(value)) : ""));
234
+ const [error, setError] = React.useState(null);
235
+ const [synced, setSynced] = React.useState(value);
236
+ if (value !== synced) {
237
+ setSynced(value);
238
+ const d = fromIso(value);
239
+ setText(d ? show(d) : "");
240
+ setError(null);
241
+ }
242
+ const emit = (next) => {
243
+ setSynced(next);
244
+ if (next !== value)
245
+ send(next);
246
+ };
247
+ const read = () => {
248
+ if (!text.trim()) {
249
+ setError(null);
250
+ setText("");
251
+ return emit(null);
252
+ }
253
+ const d = parseDate(text);
254
+ if (!d) {
255
+ setError(`Enter a date like ${placeholder()}.`);
256
+ return emit(null);
257
+ }
258
+ const problem = check(d);
259
+ if (problem) {
260
+ setError(problem);
261
+ return emit(null);
262
+ }
263
+ setError(null);
264
+ setText(show(d));
265
+ emit(toIso(d));
266
+ };
267
+ return { text, setText, error, setError, read };
268
+ }
269
+ const DateError = ({ id, children }) => (_jsxs("p", { id: id, "data-slot": "date-picker-error", "aria-live": "polite", className: "flex items-start gap-1.5 text-12 leading-[1.625] text-[var(--danger-text)]", children: [_jsx(AlertCircle, { "aria-hidden": "true", className: "mt-px size-3.5 shrink-0" }), _jsx("span", { children: children })] }));
270
+ /**
271
+ * A date field: type a date ("Mar 4", "3/4/2026", "today") or pick it from the calendar button at its
272
+ * end. `range` makes it a from–to pair in one box. Put it in a Field with a FieldLabel.
273
+ */
274
+ function DatePicker(props) {
275
+ return props.range ? _jsx(RangePicker, { ...props }) : _jsx(SinglePicker, { ...props });
276
+ }
277
+ function useControlled(value, defaultValue, onChange) {
278
+ const [own, setOwn] = React.useState(defaultValue);
279
+ const current = value !== undefined ? value : own;
280
+ const set = (v) => {
281
+ if (value === undefined)
282
+ setOwn(v);
283
+ onChange?.(v);
284
+ };
285
+ return [current, set];
286
+ }
287
+ function CalendarButton({ label, open, setOpen, disabled, children }) {
288
+ return (_jsx(Popover, { title: label, hideTitle: true, align: "end", open: open, onOpenChange: setOpen, trigger: _jsx(IconButton, { type: "button", size: "sm", "aria-label": label, disabled: disabled, "data-slot": "date-picker-button", className: "ml-auto", children: _jsx(CalendarDays, {}) }), children: children }));
289
+ }
290
+ function SinglePicker({ value, defaultValue = null, onValueChange, min, max, disabled, name, className, "aria-label": ariaLabel, ...rest }) {
291
+ const [current, setCurrent] = useControlled(value, defaultValue, onValueChange);
292
+ const [open, setOpen] = React.useState(false);
293
+ const minDate = resolveLimit(min);
294
+ const maxDate = resolveLimit(max);
295
+ const field = useTypedDate(current ?? null, setCurrent, (d) => problemWith(d, minDate, maxDate, min, max));
296
+ const errorId = React.useId();
297
+ const forced = rest["data-force-state"];
298
+ return (_jsxs("div", { "data-slot": "date-picker", className: cn("flex w-full min-w-0 flex-col gap-1.5", className), children: [_jsxs("span", { "data-slot": "date-picker-box", "data-force-state": forced, className: boxClasses, children: [_jsx(InputPrimitive, { "data-slot": "date-picker-input", "data-force-state": forced, type: "text", autoComplete: "off", "aria-label": ariaLabel, placeholder: placeholder(), disabled: disabled, value: field.text, "aria-invalid": field.error ? true : undefined, "aria-describedby": field.error ? errorId : undefined, className: typed, onValueChange: (text) => {
299
+ field.setText(text);
300
+ if (field.error)
301
+ field.setError(null);
302
+ }, onBlur: field.read, onKeyDown: (e) => {
303
+ if (e.key === "Enter")
304
+ field.read();
305
+ } }), _jsx(CalendarButton, { label: "Choose a date", open: open, setOpen: setOpen, disabled: disabled, children: _jsx(Calendar, { value: current, min: min, max: max, autoFocus: true, onValueChange: (iso) => {
306
+ field.setError(null);
307
+ setCurrent(iso);
308
+ setOpen(false);
309
+ } }) })] }), name ? _jsx("input", { type: "hidden", name: name, value: current ?? "" }) : null, field.error ? _jsx(DateError, { id: errorId, children: field.error }) : null] }));
310
+ }
311
+ function RangePicker({ value, defaultValue = { start: null, end: null }, onValueChange, min, max, disabled, name, className, "aria-label": ariaLabel, ...rest }) {
312
+ const [current, setCurrent] = useControlled(value, defaultValue, onValueChange);
313
+ const [open, setOpen] = React.useState(false);
314
+ const minDate = resolveLimit(min);
315
+ const maxDate = resolveLimit(max);
316
+ const start = current?.start ?? null;
317
+ const end = current?.end ?? null;
318
+ const startField = useTypedDate(start, (s) => setCurrent({ start: s, end }), (d) => {
319
+ const endDate = fromIso(end);
320
+ if (endDate && d > endDate)
321
+ return `Pick a start date on or before ${show(endDate)}.`;
322
+ return problemWith(d, minDate, maxDate, min, max);
323
+ });
324
+ const endField = useTypedDate(end, (e) => setCurrent({ start, end: e }), (d) => {
325
+ const startDate = fromIso(start);
326
+ if (startDate && d < startDate)
327
+ return `Pick an end date on or after ${show(startDate)}.`;
328
+ return problemWith(d, minDate, maxDate, min, max);
329
+ });
330
+ const errorId = React.useId();
331
+ const error = startField.error ?? endField.error;
332
+ const forced = rest["data-force-state"];
333
+ const box = (f, label, primitive) => {
334
+ const common = {
335
+ "data-force-state": forced,
336
+ type: "text",
337
+ autoComplete: "off",
338
+ placeholder: label === "Start date" ? placeholder() : `Mar 11, ${new Date().getFullYear()}`,
339
+ disabled,
340
+ value: f.text,
341
+ "aria-invalid": f.error ? true : undefined,
342
+ "aria-describedby": f.error ? errorId : undefined,
343
+ // Sized to a date ("Sep 30, 2026"), so the two halves read as one range, not two far-apart fields.
344
+ className: cn(typed, "w-28 flex-none"),
345
+ onBlur: f.read,
346
+ onKeyDown: (e) => {
347
+ if (e.key === "Enter")
348
+ f.read();
349
+ },
350
+ };
351
+ const onText = (text) => {
352
+ f.setText(text);
353
+ if (f.error)
354
+ f.setError(null);
355
+ };
356
+ // The start is the Field's control, so a FieldLabel and FieldDescription reach it; each half says which end it is.
357
+ return primitive ? (_jsx(InputPrimitive, { "data-slot": "date-picker-input", ...common, "aria-label": ariaLabel ? `${ariaLabel}, ${label.toLowerCase()}` : label, onValueChange: onText })) : (_jsx("input", { "data-slot": "date-picker-input", ...common, "aria-label": ariaLabel ? `${ariaLabel}, ${label.toLowerCase()}` : label, onChange: (e) => onText(e.target.value) }));
358
+ };
359
+ return (_jsxs("div", { "data-slot": "date-picker", className: cn("flex w-full min-w-0 flex-col gap-1.5", className), children: [_jsxs("span", { "data-slot": "date-picker-box", "data-force-state": forced, role: "group", "aria-label": ariaLabel, className: boxClasses, children: [box(startField, "Start date", true), _jsx("span", { "aria-hidden": "true", className: "shrink-0 text-[var(--text-muted)]", children: "\u2013" }), box(endField, "End date", false), _jsx(CalendarButton, { label: "Choose dates", open: open, setOpen: setOpen, disabled: disabled, children: _jsx(Calendar, { range: true, value: { start, end }, min: min, max: max, autoFocus: true, onValueChange: (next) => {
360
+ startField.setError(null);
361
+ endField.setError(null);
362
+ setCurrent(next);
363
+ if (next.start && next.end)
364
+ setOpen(false);
365
+ } }) })] }), name ? (_jsxs(_Fragment, { children: [_jsx("input", { type: "hidden", name: `${name}Start`, value: start ?? "" }), _jsx("input", { type: "hidden", name: `${name}End`, value: end ?? "" })] })) : null, error ? _jsx(DateError, { id: errorId, children: error }) : null] }));
366
+ }
367
+ export { Calendar, DatePicker };
@@ -8,6 +8,42 @@ type FilterPillOption<T extends string> = {
8
8
  /** Search variant only: shown before the label, like an Avatar size="xs". */
9
9
  icon?: React.ReactNode;
10
10
  };
11
+ /** Two names, then how many more: "London, Berlin +1". `spoken` is the same for screen readers. */
12
+ declare function summarize(labels: string[]): {
13
+ text: string;
14
+ spoken: string;
15
+ };
16
+ /**
17
+ * The picked options, kept with their labels: the app's search results change as people type, and a value
18
+ * picked from an earlier search must still show its name.
19
+ */
20
+ declare function usePicked<T extends string>(value: T[], options: FilterPillOption<T>[]): {
21
+ picked: FilterPillOption<T>[];
22
+ remember: (o: FilterPillOption<T>) => void;
23
+ };
24
+ /** A row's tick box, drawn like Checkbox. The row is the option; the box is only its look. */
25
+ declare function TickBox({ on }: {
26
+ on: boolean;
27
+ }): React.JSX.Element;
28
+ type MultiPanelProps<T extends string> = {
29
+ value: T[];
30
+ options: FilterPillOption<T>[];
31
+ onChange: (value: T[]) => void;
32
+ /** What's filtered: "Location". Names the list. */
33
+ name: string;
34
+ search?: true | FilterPillSearch;
35
+ searchPlaceholder?: string;
36
+ picked: FilterPillOption<T>[];
37
+ remember: (o: FilterPillOption<T>) => void;
38
+ phone?: boolean;
39
+ };
40
+ /** Several values to filter by: an optional search field, tick rows, and Reset. Each tick filters at once. */
41
+ declare function MultiPanel<T extends string>({ value, options, onChange, name, search, searchPlaceholder, picked, remember, phone }: MultiPanelProps<T>): React.JSX.Element;
42
+ /** Reset beside a phone sheet's title, while something is picked. */
43
+ declare function SheetReset({ show, onReset }: {
44
+ show: boolean;
45
+ onReset: () => void;
46
+ }): React.JSX.Element | null;
11
47
  type FilterPillSearch = {
12
48
  /** What's typed. */
13
49
  value: string;
@@ -38,11 +74,12 @@ declare function FilterSheet<T extends string>({ open, onClose, title, value, op
38
74
  options: FilterPillOption<T>[];
39
75
  onChange: (value: T) => void;
40
76
  }): React.JSX.Element;
41
- type FilterPillProps<T extends string> = ElementProps<HTMLButtonElement> & {
77
+ type SingleFilterPillProps<T extends string> = ElementProps<HTMLButtonElement> & {
78
+ multiple?: false;
42
79
  value: T;
43
80
  /** The first option is the resting "All" state. */
44
81
  options: FilterPillOption<T>[];
45
- onChange: (value: T) => void;
82
+ onChange: (value: NoInfer<T>) => void;
46
83
  /** What is being filtered: "Degree", "Status". Read as "Degree: 1st". */
47
84
  ariaLabel?: string;
48
85
  /**
@@ -52,6 +89,24 @@ type FilterPillProps<T extends string> = ElementProps<HTMLButtonElement> & {
52
89
  search?: FilterPillSearch;
53
90
  className?: string;
54
91
  };
55
- declare function FilterPill<T extends string>({ value, options, onChange, ariaLabel, search, className, ...rest }: FilterPillProps<T>): React.JSX.Element;
56
- export { FilterPill, FilterMenuPopup, FilterSheet };
57
- export type { FilterPillOption, FilterPillProps, FilterPillSearch };
92
+ type MultipleFilterPillProps<T extends string> = ElementProps<HTMLButtonElement> & {
93
+ /** Several values: tick rows, each tick filters at once, Reset clears them. */
94
+ multiple: true;
95
+ /** The picked values; empty is the resting state ("Location"). */
96
+ value: T[];
97
+ /** The values to pick from. No "All" row: an empty pick is all. */
98
+ options: FilterPillOption<T>[];
99
+ onChange: (value: NoInfer<T>[]) => void;
100
+ /** What's filtered: "Location". The resting label, then "Location: London, Berlin +1". */
101
+ ariaLabel: string;
102
+ /** A search field on top. `true` filters `options`; an object means the app searches and `options` are its results. */
103
+ search?: true | FilterPillSearch;
104
+ /** The search field's placeholder. Default "Search". */
105
+ searchPlaceholder?: string;
106
+ className?: string;
107
+ };
108
+ type FilterPillProps<T extends string> = SingleFilterPillProps<T> | MultipleFilterPillProps<T>;
109
+ declare function FilterPill<T extends string>(props: MultipleFilterPillProps<T>): React.JSX.Element;
110
+ declare function FilterPill<T extends string>(props: SingleFilterPillProps<T>): React.JSX.Element;
111
+ export { FilterPill, FilterMenuPopup, FilterSheet, MultiPanel, SheetReset, summarize, TickBox, usePicked };
112
+ export type { FilterPillOption, FilterPillProps, FilterPillSearch, MultipleFilterPillProps, SingleFilterPillProps };