@brdgapp/ui 1.9.0 → 1.11.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/CHANGELOG.md +54 -1
- package/README.md +6 -1
- package/components/action-menu.d.ts +18 -6
- package/components/action-menu.js +28 -9
- package/components/app-shell.js +1 -1
- package/components/button.d.ts +6 -1
- package/components/button.js +16 -4
- package/components/column-filter-menu.d.ts +20 -4
- package/components/column-filter-menu.js +26 -4
- package/components/copy-field.d.ts +14 -7
- package/components/copy-field.js +25 -22
- package/components/data-table.d.ts +1 -1
- package/components/data-table.js +2 -2
- package/components/date-picker.d.ts +77 -0
- package/components/date-picker.js +367 -0
- package/components/filter-pill.d.ts +58 -4
- package/components/filter-pill.js +115 -10
- package/components/icon-button.d.ts +7 -1
- package/components/icon-button.js +14 -4
- package/components/input.d.ts +27 -3
- package/components/input.js +96 -3
- package/components/search-input.js +2 -2
- package/components/select.d.ts +41 -6
- package/components/select.js +167 -2
- package/components/spinner.d.ts +19 -0
- package/components/spinner.js +20 -0
- package/hooks/use-copy.d.ts +10 -0
- package/hooks/use-copy.js +27 -0
- package/hooks/use-theme.js +3 -3
- package/lib/theme.d.ts +2 -2
- package/lib/theme.js +7 -7
- package/package.json +4 -3
|
@@ -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,7 +74,8 @@ 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
|
|
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>[];
|
|
@@ -52,6 +89,23 @@ type FilterPillProps<T extends string> = ElementProps<HTMLButtonElement> & {
|
|
|
52
89
|
search?: FilterPillSearch;
|
|
53
90
|
className?: string;
|
|
54
91
|
};
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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: 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: FilterPillProps<T>): React.JSX.Element;
|
|
110
|
+
export { FilterPill, FilterMenuPopup, FilterSheet, MultiPanel, SheetReset, summarize, TickBox, usePicked };
|
|
111
|
+
export type { FilterPillOption, FilterPillProps, FilterPillSearch, MultipleFilterPillProps, SingleFilterPillProps };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { jsx as _jsx,
|
|
2
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
import { useState } from "react";
|
|
5
5
|
import { Menu } from "@base-ui/react/menu";
|
|
@@ -9,6 +9,92 @@ import { BottomSheet, BottomSheetOption, BottomSheetOptions } from "@brdgapp/ui/
|
|
|
9
9
|
import { SearchInput } from "@brdgapp/ui/components/search-input";
|
|
10
10
|
import { useIsMobile } from "@brdgapp/ui/hooks/use-is-mobile";
|
|
11
11
|
import { cn } from "@brdgapp/ui/lib/utils";
|
|
12
|
+
/** Two names, then how many more: "London, Berlin +1". `spoken` is the same for screen readers. */
|
|
13
|
+
function summarize(labels) {
|
|
14
|
+
const shown = labels.slice(0, 2).join(", ");
|
|
15
|
+
const more = labels.length - 2;
|
|
16
|
+
return { text: more > 0 ? `${shown} +${more}` : shown, spoken: more > 0 ? `${shown} and ${more} more` : shown };
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The picked options, kept with their labels: the app's search results change as people type, and a value
|
|
20
|
+
* picked from an earlier search must still show its name.
|
|
21
|
+
*/
|
|
22
|
+
function usePicked(value, options) {
|
|
23
|
+
const [known, setKnown] = useState({});
|
|
24
|
+
const all = { ...known };
|
|
25
|
+
for (const o of options)
|
|
26
|
+
all[o.value] = o;
|
|
27
|
+
const picked = value.map((v) => all[v] ?? { value: v, label: v });
|
|
28
|
+
const remember = (o) => setKnown((k) => ({ ...k, [o.value]: o }));
|
|
29
|
+
return { picked, remember };
|
|
30
|
+
}
|
|
31
|
+
const boxBase = "relative inline-flex size-4 shrink-0 items-center justify-center rounded-[4px] border text-white transition-colors";
|
|
32
|
+
/** A row's tick box, drawn like Checkbox. The row is the option; the box is only its look. */
|
|
33
|
+
function TickBox({ on }) {
|
|
34
|
+
return (_jsx("span", { "aria-hidden": "true", className: cn(boxBase, on ? "border-[var(--primary-solid)] bg-[var(--primary-solid)]" : "border-[var(--text-muted)] bg-[var(--surface-primary)]"), children: on ? _jsx(Check, { className: "size-3", strokeWidth: 3 }) : null }));
|
|
35
|
+
}
|
|
36
|
+
/** Several values to filter by: an optional search field, tick rows, and Reset. Each tick filters at once. */
|
|
37
|
+
function MultiPanel({ value, options, onChange, name, search, searchPlaceholder = "Search", picked, remember, phone = false }) {
|
|
38
|
+
const listId = React.useId();
|
|
39
|
+
const panelRef = React.useRef(null);
|
|
40
|
+
const server = typeof search === "object" ? search : undefined;
|
|
41
|
+
const [own, setOwn] = useState("");
|
|
42
|
+
const query = server ? server.value : own;
|
|
43
|
+
const setQuery = server ? server.onChange : setOwn;
|
|
44
|
+
const [at, setAt] = useState(0);
|
|
45
|
+
const q = query.trim().toLowerCase();
|
|
46
|
+
const min = server ? (server.minLength ?? 2) : 0;
|
|
47
|
+
const short = q.length < min;
|
|
48
|
+
// Before the app has something to search, its list shows what's picked, so it can be unticked.
|
|
49
|
+
const rows = server
|
|
50
|
+
? short
|
|
51
|
+
? picked
|
|
52
|
+
: options
|
|
53
|
+
: q
|
|
54
|
+
? options.filter((o) => o.label.toLowerCase().includes(q) || (o.description?.toLowerCase().includes(q) ?? false))
|
|
55
|
+
: options;
|
|
56
|
+
const cur = at < rows.length ? at : 0;
|
|
57
|
+
const note = short && !picked.length ? `Type at least ${min} letters.` : !short && !server?.loading && !rows.length ? (server?.emptyText ?? "No results.") : null;
|
|
58
|
+
const toggle = (o) => {
|
|
59
|
+
remember(o);
|
|
60
|
+
onChange(value.includes(o.value) ? value.filter((v) => v !== o.value) : [...value, o.value]);
|
|
61
|
+
};
|
|
62
|
+
const keys = (e, typing) => {
|
|
63
|
+
const n = rows.length;
|
|
64
|
+
if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
65
|
+
e.preventDefault();
|
|
66
|
+
if (n)
|
|
67
|
+
setAt(e.key === "ArrowDown" ? (cur + 1) % n : (cur - 1 + n) % n);
|
|
68
|
+
}
|
|
69
|
+
else if ((e.key === "Enter" || (!typing && e.key === " ")) && rows[cur]) {
|
|
70
|
+
e.preventDefault();
|
|
71
|
+
toggle(rows[cur]);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
const searchField = search ? (_jsx("div", { className: phone ? "px-5 pb-2" : "p-2 pb-1", children: _jsx(SearchInput, { autoFocus: !phone, role: phone ? undefined : "combobox", "aria-expanded": phone ? undefined : "true", "aria-controls": phone ? undefined : listId, "aria-autocomplete": phone ? undefined : "list", "aria-activedescendant": !phone && rows[cur] ? `${listId}-${cur}` : undefined, placeholder: searchPlaceholder, value: query, loading: server?.loading, onChange: (e) => {
|
|
75
|
+
setAt(0);
|
|
76
|
+
setQuery(e.target.value);
|
|
77
|
+
}, onClear: () => setQuery(""), onKeyDown: phone ? undefined : (e) => keys(e, true) }) })) : null;
|
|
78
|
+
const noteLine = note ? (_jsx("p", { role: "status", className: cn("text-[var(--text-muted)]", phone ? "px-5 pb-4 text-14" : "px-3 pt-1 pb-3 text-13"), children: note })) : null;
|
|
79
|
+
// Phones: the sheet's own tick rows (44px, real checkboxes); Reset sits beside the title.
|
|
80
|
+
if (phone) {
|
|
81
|
+
return (_jsxs(_Fragment, { children: [searchField, _jsx(BottomSheetOptions, { multiple: true, children: rows.map((o) => (_jsx(BottomSheetOption, { selected: value.includes(o.value), onSelect: () => toggle(o), children: o.label }, o.value))) }), noteLine] }));
|
|
82
|
+
}
|
|
83
|
+
return (_jsxs("div", { ref: panelRef, "data-slot": "filter-multi", className: "flex flex-col", children: [searchField, _jsx("div", { id: listId, role: "listbox", "aria-multiselectable": "true", "aria-label": name,
|
|
84
|
+
// With no search field, the list itself takes focus and the arrow keys.
|
|
85
|
+
tabIndex: search ? -1 : 0, autoFocus: !search, "aria-activedescendant": !search && rows[cur] ? `${listId}-${cur}` : undefined, onKeyDown: search ? undefined : (e) => keys(e, false), className: "max-h-[280px] overflow-y-auto py-1 outline-none", children: rows.map((o, i) => {
|
|
86
|
+
const on = value.includes(o.value);
|
|
87
|
+
return (_jsxs("div", { id: `${listId}-${i}`, role: "option", "aria-selected": on, "data-slot": "filter-multi-option", "data-highlighted": i === cur || undefined, onMouseDown: (e) => e.preventDefault(), onMouseMove: () => setAt(i), onClick: () => toggle(o), className: "flex w-full cursor-default items-center gap-3 px-3 py-2 text-left text-13 leading-normal text-[var(--text-body)] transition-colors data-[highlighted]:bg-[var(--surface-secondary)]", children: [_jsx(TickBox, { on: on }), o.icon ? _jsx("span", { className: "flex shrink-0", children: o.icon }) : null, _jsxs("span", { className: "min-w-0 flex-1", children: [_jsx("span", { className: cn("block truncate", on && "text-[var(--text-primary)]"), children: o.label }), o.description ? _jsx("span", { className: "block truncate text-12 text-[var(--text-muted)]", children: o.description }) : null] })] }, o.value));
|
|
88
|
+
}) }), noteLine, value.length ? (_jsx("div", { className: "border-t border-[var(--border-divider)] p-1", children: _jsx("button", { type: "button", "data-slot": "filter-reset", onClick: () => {
|
|
89
|
+
onChange([]);
|
|
90
|
+
// Reset goes away with nothing picked: focus goes back to the search field, or the list.
|
|
91
|
+
panelRef.current?.querySelector("input, [role=listbox]")?.focus();
|
|
92
|
+
}, className: "w-full rounded-lg px-3 py-2 text-left text-13 font-semibold text-[var(--primary)] outline-none transition-colors hover:bg-[var(--surface-secondary)] focus-visible:ring-2 focus-visible:ring-[var(--primary)]", children: "Reset" }) })) : null] }));
|
|
93
|
+
}
|
|
94
|
+
/** Reset beside a phone sheet's title, while something is picked. */
|
|
95
|
+
function SheetReset({ show, onReset }) {
|
|
96
|
+
return show ? (_jsx("button", { type: "button", onClick: onReset, className: "rounded-lg px-2 py-1 text-14 font-semibold text-[var(--primary)] outline-none focus-visible:ring-2 focus-visible:ring-[var(--primary)]", children: "Reset" })) : null;
|
|
97
|
+
}
|
|
12
98
|
/** The menu body shared by FilterPill and ColumnFilterMenu. */
|
|
13
99
|
function FilterMenuPopup({ value, options, onChange, width, align, }) {
|
|
14
100
|
return (_jsx(Menu.Portal, { children: _jsx(Menu.Positioner, { side: "bottom", align: align, sideOffset: 6, collisionPadding: 8, className: "z-[95]", children: _jsx(Menu.Popup, { "data-slot": "filter-menu", style: { minWidth: width }, className: "max-h-[320px] overflow-y-auto rounded-xl border border-[var(--border-default)] bg-[var(--surface-elevated)] py-1 shadow-[var(--shadow-menu)] outline-none transition-opacity duration-200 ease-out data-[ending-style]:duration-150 data-[starting-style]:opacity-0 data-[ending-style]:opacity-0", children: _jsx(Menu.RadioGroup, { value: value, onValueChange: (v) => onChange(v), children: options.map((option) => (_jsxs(Menu.RadioItem, { value: option.value, closeOnClick: true, "data-slot": "filter-menu-option", className: "group flex w-full cursor-default items-center justify-between gap-3 px-3 py-2 text-left text-13 leading-normal text-[var(--text-body)] outline-none transition-colors data-[checked]:font-semibold data-[checked]:text-[var(--text-primary)] data-[highlighted]:bg-[var(--surface-secondary)] hover:bg-[var(--surface-secondary)]", children: [_jsx("span", { className: "truncate", children: option.label }), _jsx(Menu.RadioItemIndicator, { className: "inline-flex shrink-0", children: _jsx(Check, { "aria-hidden": "true", className: "size-4 text-[var(--primary)]" }) })] }, option.value))) }) }) }) }));
|
|
@@ -53,16 +139,35 @@ function SearchPanel({ value, options, search, onPick, phone = false, }) {
|
|
|
53
139
|
return (_jsxs("div", { id: `${listId}-${i}`, role: "option", "aria-selected": selected, "data-slot": "filter-search-option", "data-highlighted": i === cur || undefined, onMouseDown: (e) => e.preventDefault(), onMouseMove: () => setAt(i), onClick: () => onPick(option), className: cn("flex w-full cursor-default items-center gap-3 text-left leading-normal transition-colors data-[highlighted]:bg-[var(--surface-secondary)]", phone ? "min-h-11 px-5 py-2 text-14" : "px-3 py-2 text-13", selected ? "font-semibold text-[var(--text-primary)]" : "text-[var(--text-body)]"), children: [option.icon ? _jsx("span", { className: "flex shrink-0", children: option.icon }) : null, _jsxs("span", { className: "min-w-0 flex-1", children: [_jsx("span", { className: "block truncate", children: option.label }), option.description ? _jsx("span", { className: "block truncate text-12 font-normal text-[var(--text-muted)]", children: option.description }) : null] }), selected ? _jsx(Check, { "aria-hidden": "true", className: "size-4 shrink-0 text-[var(--primary)]" }) : null] }, option.value));
|
|
54
140
|
}) }), note ? _jsx("p", { role: "status", className: cn("text-[var(--text-muted)]", phone ? "px-5 pb-4 text-14" : "px-3 pt-1 pb-3 text-13"), children: note }) : null] }));
|
|
55
141
|
}
|
|
56
|
-
function FilterPill(
|
|
142
|
+
function FilterPill(props) {
|
|
143
|
+
return props.multiple ? _jsx(MultipleFilterPill, { ...props }) : _jsx(SingleFilterPill, { ...props });
|
|
144
|
+
}
|
|
145
|
+
const pillClass = (active, className) => cn("inline-flex shrink-0 items-center gap-1.5 rounded-full border px-3 py-1.5 text-13 leading-normal font-semibold outline-none transition-colors duration-150", "focus-visible:ring-2 focus-visible:ring-[var(--primary)]", active
|
|
146
|
+
? "border-[var(--text-muted)] bg-[var(--surface-tertiary)] text-[var(--text-primary)] hover:border-[var(--text-secondary)]"
|
|
147
|
+
: "border-[var(--text-disabled)] bg-transparent text-[var(--text-secondary)] hover:border-[var(--text-muted)] hover:text-[var(--text-primary)]", className);
|
|
148
|
+
function MultipleFilterPill({ multiple, value, options, onChange, ariaLabel, search, searchPlaceholder, className, ...rest }) {
|
|
149
|
+
const [open, setOpen] = useState(false);
|
|
150
|
+
const mobile = useIsMobile();
|
|
151
|
+
const { picked, remember } = usePicked(value, options);
|
|
152
|
+
const active = value.length > 0;
|
|
153
|
+
const sum = summarize(picked.map((o) => o.label));
|
|
154
|
+
const label = active ? `${ariaLabel}: ${sum.text}` : ariaLabel;
|
|
155
|
+
const name = active ? `${ariaLabel}: ${sum.spoken}` : ariaLabel;
|
|
156
|
+
const inner = (_jsxs(_Fragment, { children: [_jsx("span", { className: "max-w-[260px] truncate", children: label }), _jsx(ChevronDown, { "aria-hidden": "true", className: cn("size-3.5 shrink-0 text-[var(--text-muted)] transition-transform", open && "rotate-180") })] }));
|
|
157
|
+
const panel = (phone) => (_jsx(MultiPanel, { value: value, options: options, onChange: onChange, name: ariaLabel, search: search, searchPlaceholder: searchPlaceholder, picked: picked, remember: remember, phone: phone }));
|
|
158
|
+
if (mobile) {
|
|
159
|
+
return (_jsxs(_Fragment, { children: [_jsx("button", { ...rest, type: "button", "data-slot": "filter-pill", "data-multiple": multiple ? "" : undefined, "aria-haspopup": "dialog", "aria-label": name, className: pillClass(active, className), onClick: () => setOpen(true), children: inner }), _jsx(BottomSheet, { open: open, onClose: () => setOpen(false), title: ariaLabel, action: _jsx(SheetReset, { show: active, onReset: () => onChange([]) }), children: panel(true) })] }));
|
|
160
|
+
}
|
|
161
|
+
return (_jsxs(PopoverPrimitive.Root, { open: open, onOpenChange: setOpen, children: [_jsx(PopoverPrimitive.Trigger, { ...rest, "data-slot": "filter-pill", "data-multiple": multiple ? "" : undefined, "aria-label": name, className: pillClass(active, className), children: inner }), _jsx(PopoverPrimitive.Portal, { children: _jsx(PopoverPrimitive.Positioner, { side: "bottom", align: "end", sideOffset: 6, collisionPadding: 8, className: "z-[95]", children: _jsx(PopoverPrimitive.Popup, { "data-slot": "filter-menu", "aria-label": ariaLabel, className: "w-[280px] max-w-[calc(100vw-16px)] rounded-xl border border-[var(--border-default)] bg-[var(--surface-elevated)] shadow-[var(--shadow-menu)] outline-none transition-opacity duration-200 ease-out data-[ending-style]:duration-150 data-[starting-style]:opacity-0 data-[ending-style]:opacity-0", children: panel(false) }) }) })] }));
|
|
162
|
+
}
|
|
163
|
+
function SingleFilterPill({ multiple, value, options, onChange, ariaLabel = "Filter", search, className, ...rest }) {
|
|
57
164
|
const [open, setOpen] = useState(false);
|
|
58
165
|
const mobile = useIsMobile();
|
|
59
166
|
const active = value !== options[0]?.value;
|
|
60
167
|
// The search variant's results change as people type, so the chosen label is kept here.
|
|
61
168
|
const [picked, setPicked] = useState();
|
|
62
169
|
const label = options.find((o) => o.value === value)?.label ?? (active ? picked : undefined) ?? options[0]?.label ?? "";
|
|
63
|
-
const triggerClass =
|
|
64
|
-
? "border-[var(--text-muted)] bg-[var(--surface-tertiary)] text-[var(--text-primary)] hover:border-[var(--text-secondary)]"
|
|
65
|
-
: "border-[var(--text-disabled)] bg-transparent text-[var(--text-secondary)] hover:border-[var(--text-muted)] hover:text-[var(--text-primary)]", className);
|
|
170
|
+
const triggerClass = pillClass(active, className);
|
|
66
171
|
const inner = (_jsxs(_Fragment, { children: [label, _jsx(ChevronDown, { "aria-hidden": "true", className: cn("size-3.5 text-[var(--text-muted)] transition-transform", open && "rotate-180") })] }));
|
|
67
172
|
if (search) {
|
|
68
173
|
const pick = (option) => {
|
|
@@ -72,14 +177,14 @@ function FilterPill({ value, options, onChange, ariaLabel = "Filter", search, cl
|
|
|
72
177
|
setOpen(false);
|
|
73
178
|
};
|
|
74
179
|
if (mobile) {
|
|
75
|
-
return (_jsxs(_Fragment, { children: [_jsx("button", { ...rest, type: "button", "data-slot": "filter-pill", "aria-haspopup": "dialog", "aria-label": `${ariaLabel}: ${label}`, className: triggerClass, onClick: () => setOpen(true), children: inner }), _jsx(BottomSheet, { open: open, onClose: () => setOpen(false), title: ariaLabel, children: _jsx(SearchPanel, { value: value, options: options, search: search, onPick: pick, phone: true }) })] }));
|
|
180
|
+
return (_jsxs(_Fragment, { children: [_jsx("button", { ...rest, type: "button", "data-slot": "filter-pill", "data-multiple": multiple ? "" : undefined, "aria-haspopup": "dialog", "aria-label": `${ariaLabel}: ${label}`, className: triggerClass, onClick: () => setOpen(true), children: inner }), _jsx(BottomSheet, { open: open, onClose: () => setOpen(false), title: ariaLabel, children: _jsx(SearchPanel, { value: value, options: options, search: search, onPick: pick, phone: true }) })] }));
|
|
76
181
|
}
|
|
77
|
-
return (_jsxs(PopoverPrimitive.Root, { open: open, onOpenChange: setOpen, children: [_jsx(PopoverPrimitive.Trigger, { ...rest, "data-slot": "filter-pill", "aria-label": `${ariaLabel}: ${label}`, className: triggerClass, children: inner }), _jsx(PopoverPrimitive.Portal, { children: _jsx(PopoverPrimitive.Positioner, { side: "bottom", align: "end", sideOffset: 6, collisionPadding: 8, className: "z-[95]", children: _jsx(PopoverPrimitive.Popup, { "data-slot": "filter-menu", "aria-label": ariaLabel, className: "w-[300px] max-w-[calc(100vw-16px)] rounded-xl border border-[var(--border-default)] bg-[var(--surface-elevated)] shadow-[var(--shadow-menu)] outline-none transition-opacity duration-200 ease-out data-[ending-style]:duration-150 data-[starting-style]:opacity-0 data-[ending-style]:opacity-0", children: _jsx(SearchPanel, { value: value, options: options, search: search, onPick: pick }) }) }) })] }));
|
|
182
|
+
return (_jsxs(PopoverPrimitive.Root, { open: open, onOpenChange: setOpen, children: [_jsx(PopoverPrimitive.Trigger, { ...rest, "data-slot": "filter-pill", "data-multiple": multiple ? "" : undefined, "aria-label": `${ariaLabel}: ${label}`, className: triggerClass, children: inner }), _jsx(PopoverPrimitive.Portal, { children: _jsx(PopoverPrimitive.Positioner, { side: "bottom", align: "end", sideOffset: 6, collisionPadding: 8, className: "z-[95]", children: _jsx(PopoverPrimitive.Popup, { "data-slot": "filter-menu", "aria-label": ariaLabel, className: "w-[300px] max-w-[calc(100vw-16px)] rounded-xl border border-[var(--border-default)] bg-[var(--surface-elevated)] shadow-[var(--shadow-menu)] outline-none transition-opacity duration-200 ease-out data-[ending-style]:duration-150 data-[starting-style]:opacity-0 data-[ending-style]:opacity-0", children: _jsx(SearchPanel, { value: value, options: options, search: search, onPick: pick }) }) }) })] }));
|
|
78
183
|
}
|
|
79
184
|
// Phones: the choices slide up from the bottom, near the thumb, in 44px rows.
|
|
80
185
|
if (mobile) {
|
|
81
|
-
return (_jsxs(_Fragment, { children: [_jsx("button", { ...rest, type: "button", "data-slot": "filter-pill", "aria-haspopup": "dialog", "aria-label": `${ariaLabel}: ${label}`, className: triggerClass, onClick: () => setOpen(true), children: inner }), _jsx(FilterSheet, { open: open, onClose: () => setOpen(false), title: ariaLabel, value: value, options: options, onChange: onChange })] }));
|
|
186
|
+
return (_jsxs(_Fragment, { children: [_jsx("button", { ...rest, type: "button", "data-slot": "filter-pill", "data-multiple": multiple ? "" : undefined, "aria-haspopup": "dialog", "aria-label": `${ariaLabel}: ${label}`, className: triggerClass, onClick: () => setOpen(true), children: inner }), _jsx(FilterSheet, { open: open, onClose: () => setOpen(false), title: ariaLabel, value: value, options: options, onChange: onChange })] }));
|
|
82
187
|
}
|
|
83
|
-
return (_jsxs(Menu.Root, { open: open, onOpenChange: setOpen, children: [_jsx(Menu.Trigger, { ...rest, "data-slot": "filter-pill", "aria-label": `${ariaLabel}: ${label}`, className: triggerClass, children: inner }), _jsx(FilterMenuPopup, { value: value, options: options, onChange: onChange, width: 200, align: "end" })] }));
|
|
188
|
+
return (_jsxs(Menu.Root, { open: open, onOpenChange: setOpen, children: [_jsx(Menu.Trigger, { ...rest, "data-slot": "filter-pill", "data-multiple": multiple ? "" : undefined, "aria-label": `${ariaLabel}: ${label}`, className: triggerClass, children: inner }), _jsx(FilterMenuPopup, { value: value, options: options, onChange: onChange, width: 200, align: "end" })] }));
|
|
84
189
|
}
|
|
85
|
-
export { FilterPill, FilterMenuPopup, FilterSheet };
|
|
190
|
+
export { FilterPill, FilterMenuPopup, FilterSheet, MultiPanel, SheetReset, summarize, TickBox, usePicked };
|