@zonsh/ui 0.2.0 → 0.5.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/LICENSE +22 -0
- package/README.md +19 -13
- package/dist/components/accordion.js +1 -1
- package/dist/components/alert-dialog.js +1 -1
- package/dist/components/ascii-field.d.ts +1 -1
- package/dist/components/ascii-field.js +2 -1
- package/dist/components/bottom-nav.d.ts +86 -0
- package/dist/components/bottom-nav.js +175 -0
- package/dist/components/breadcrumb.d.ts +9 -0
- package/dist/components/breadcrumb.js +6 -1
- package/dist/components/button-group.d.ts +1 -1
- package/dist/components/button-group.js +2 -2
- package/dist/components/button.js +1 -1
- package/dist/components/calendar.d.ts +1 -1
- package/dist/components/calendar.js +18 -3
- package/dist/components/carousel.d.ts +22 -3
- package/dist/components/carousel.js +25 -7
- package/dist/components/chart.d.ts +20 -0
- package/dist/components/chart.js +52 -3
- package/dist/components/code-block.d.ts +20 -10
- package/dist/components/code-block.js +54 -12
- package/dist/components/collapsible.js +3 -3
- package/dist/components/color-picker.d.ts +30 -8
- package/dist/components/color-picker.js +48 -27
- package/dist/components/combobox.d.ts +21 -3
- package/dist/components/combobox.js +79 -7
- package/dist/components/command.d.ts +23 -3
- package/dist/components/command.js +63 -18
- package/dist/components/conversation.d.ts +10 -1
- package/dist/components/conversation.js +24 -7
- package/dist/components/copy-button.d.ts +15 -6
- package/dist/components/copy-button.js +27 -10
- package/dist/components/data-table.d.ts +66 -6
- package/dist/components/data-table.js +376 -23
- package/dist/components/date-picker.d.ts +73 -4
- package/dist/components/date-picker.js +138 -7
- package/dist/components/dialog.d.ts +10 -1
- package/dist/components/dialog.js +6 -3
- package/dist/components/drawer.d.ts +10 -1
- package/dist/components/drawer.js +6 -3
- package/dist/components/feature-list.js +2 -2
- package/dist/components/field.d.ts +27 -8
- package/dist/components/field.js +49 -4
- package/dist/components/file-drop.d.ts +26 -4
- package/dist/components/file-drop.js +125 -64
- package/dist/components/filter-bar.d.ts +50 -3
- package/dist/components/filter-bar.js +71 -33
- package/dist/components/form.d.ts +4 -2
- package/dist/components/form.js +24 -2
- package/dist/components/graph.d.ts +41 -0
- package/dist/components/graph.js +106 -50
- package/dist/components/illustration.js +51 -4
- package/dist/components/input-group.d.ts +18 -7
- package/dist/components/input-group.js +41 -19
- package/dist/components/input.d.ts +2 -2
- package/dist/components/input.js +3 -3
- package/dist/components/kbd.d.ts +17 -0
- package/dist/components/kbd.js +8 -1
- package/dist/components/layout.d.ts +2 -1
- package/dist/components/layout.js +55 -11
- package/dist/components/lightbox.d.ts +100 -0
- package/dist/components/lightbox.js +457 -0
- package/dist/components/locale.d.ts +44 -0
- package/dist/components/locale.js +37 -0
- package/dist/components/logo.js +1 -1
- package/dist/components/menu.js +3 -1
- package/dist/components/menubar.js +60 -13
- package/dist/components/message.d.ts +3 -3
- package/dist/components/message.js +12 -8
- package/dist/components/meter.js +3 -1
- package/dist/components/money-input.d.ts +80 -0
- package/dist/components/money-input.js +253 -0
- package/dist/components/navigation-menu.js +4 -2
- package/dist/components/number-field.d.ts +21 -2
- package/dist/components/number-field.js +53 -28
- package/dist/components/otp-field.d.ts +14 -1
- package/dist/components/otp-field.js +10 -2
- package/dist/components/pagination.d.ts +13 -7
- package/dist/components/pagination.js +18 -7
- package/dist/components/pdf-viewer.d.ts +67 -2
- package/dist/components/pdf-viewer.js +88 -21
- package/dist/components/phone-input.d.ts +79 -0
- package/dist/components/phone-input.js +282 -0
- package/dist/components/popover.js +1 -1
- package/dist/components/progress.js +3 -1
- package/dist/components/prompt-input.d.ts +34 -9
- package/dist/components/prompt-input.js +37 -17
- package/dist/components/radio-group.js +7 -2
- package/dist/components/rating.d.ts +73 -0
- package/dist/components/rating.js +124 -0
- package/dist/components/reasoning.d.ts +16 -1
- package/dist/components/reasoning.js +10 -7
- package/dist/components/resizable.d.ts +10 -1
- package/dist/components/resizable.js +4 -1
- package/dist/components/response.d.ts +17 -5
- package/dist/components/response.js +58 -18
- package/dist/components/scroll-area.d.ts +1 -1
- package/dist/components/select.js +14 -3
- package/dist/components/separator.js +1 -1
- package/dist/components/sidebar.d.ts +20 -4
- package/dist/components/sidebar.js +70 -35
- package/dist/components/site-header.d.ts +46 -12
- package/dist/components/site-header.js +78 -19
- package/dist/components/skeleton.d.ts +1 -1
- package/dist/components/skeleton.js +1 -1
- package/dist/components/slider.js +3 -1
- package/dist/components/sortable.d.ts +178 -0
- package/dist/components/sortable.js +1282 -0
- package/dist/components/sources.d.ts +17 -2
- package/dist/components/sources.js +11 -4
- package/dist/components/spinner.d.ts +13 -1
- package/dist/components/spinner.js +5 -2
- package/dist/components/stat.d.ts +91 -0
- package/dist/components/stat.js +141 -0
- package/dist/components/stepper.d.ts +71 -0
- package/dist/components/stepper.js +101 -0
- package/dist/components/switch.d.ts +2 -2
- package/dist/components/switch.js +2 -2
- package/dist/components/table.d.ts +13 -3
- package/dist/components/table.js +231 -41
- package/dist/components/tabs.js +8 -4
- package/dist/components/tags-input.d.ts +18 -2
- package/dist/components/tags-input.js +66 -34
- package/dist/components/theme.d.ts +16 -2
- package/dist/components/theme.js +34 -8
- package/dist/components/time-field.d.ts +24 -4
- package/dist/components/time-field.js +32 -9
- package/dist/components/timeline.d.ts +18 -2
- package/dist/components/timeline.js +13 -4
- package/dist/components/tippy.d.ts +48 -0
- package/dist/components/tippy.js +648 -0
- package/dist/components/toast.d.ts +18 -2
- package/dist/components/toast.js +12 -3
- package/dist/components/toggle-group.js +1 -0
- package/dist/components/tool-call.d.ts +19 -8
- package/dist/components/tool-call.js +23 -7
- package/dist/components/toolbar.d.ts +1 -1
- package/dist/components/toolbar.js +10 -2
- package/dist/components/tooltip.d.ts +4 -2
- package/dist/components/tooltip.js +10 -4
- package/dist/components/tour.d.ts +127 -0
- package/dist/components/tour.js +673 -0
- package/dist/components/tree.js +45 -9
- package/dist/components/video-player.d.ts +10 -2
- package/dist/components/video-player.js +134 -38
- package/dist/illustration.d.ts +2 -0
- package/dist/illustration.js +7 -0
- package/dist/index.d.ts +11 -3
- package/dist/index.js +11 -3
- package/dist/lib/code-highlight.d.ts +3 -16
- package/dist/lib/code-highlight.js +160 -10
- package/dist/lib/code-theme.js +3 -3
- package/dist/lib/digit-roll.js +8 -8
- package/dist/lib/field-errors.d.ts +28 -0
- package/dist/lib/field-errors.js +24 -0
- package/dist/lib/field-value.d.ts +13 -0
- package/dist/lib/field-value.js +44 -0
- package/dist/lib/glide.d.ts +5 -4
- package/dist/lib/glide.js +5 -4
- package/dist/lib/graph-layout.js +11 -2
- package/dist/lib/money.d.ts +73 -0
- package/dist/lib/money.js +343 -0
- package/dist/lib/phone-countries.d.ts +1 -0
- package/dist/lib/phone-countries.js +281 -0
- package/dist/lib/phone.d.ts +90 -0
- package/dist/lib/phone.js +286 -0
- package/dist/lib/platform.d.ts +8 -3
- package/dist/lib/platform.js +13 -6
- package/dist/lib/row-index.d.ts +15 -0
- package/dist/lib/row-index.js +136 -0
- package/dist/lib/table-fit.d.ts +3 -2
- package/dist/lib/table-fit.js +52 -23
- package/dist/lib/theme-script.d.ts +15 -2
- package/dist/lib/theme-script.js +23 -3
- package/dist/lib/tippy/pixels.d.ts +47 -0
- package/dist/lib/tippy/pixels.js +187 -0
- package/dist/lib/tippy/sprite.d.ts +86 -0
- package/dist/lib/tippy/sprite.js +753 -0
- package/dist/messages/de.d.ts +12 -0
- package/dist/messages/de.js +326 -0
- package/dist/styles/expressive.css +60 -26
- package/dist/styles/index.css +11 -2
- package/dist/styles/material.css +47 -23
- package/dist/styles/parts/ai.css +4 -2
- package/dist/styles/parts/controls.css +85 -32
- package/dist/styles/parts/data.css +33 -5
- package/dist/styles/parts/feedback.css +110 -13
- package/dist/styles/parts/foundations.css +1 -1
- package/dist/styles/parts/navigation.css +230 -5
- package/dist/styles/parts/overlays.css +98 -11
- package/dist/styles/parts/pdf.css +13 -7
- package/dist/styles/tokens.css +28 -4
- package/dist/vite.d.ts +0 -50
- package/dist/vite.js +0 -100
- package/package.json +28 -33
- package/dist/components/code-highlight.d.ts +0 -13
- package/dist/components/code-highlight.js +0 -107
- package/dist/components/whiteboard.d.ts +0 -80
- package/dist/components/whiteboard.js +0 -389
- package/dist/styles/parts/whiteboard.css +0 -855
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { DirectionProvider } from "@base-ui/react/direction-provider";
|
|
3
4
|
import { Field as BaseField } from "@base-ui/react/field";
|
|
4
5
|
import { CheckIcon, ChevronsUpDownIcon } from "lucide-react";
|
|
5
6
|
import * as React from "react";
|
|
6
7
|
import { cn, sx } from "../lib/cn.js";
|
|
7
8
|
import { Input } from "./input.js";
|
|
9
|
+
import { useMessages } from "./locale.js";
|
|
8
10
|
import { Popover, PopoverContent, PopoverTrigger } from "./popover.js";
|
|
9
11
|
import { Slider } from "./slider.js";
|
|
12
|
+
const formatNames = { hex: "Hex", rgb: "RGB", hsl: "HSL", oklch: "OKLCH" };
|
|
13
|
+
const defaultMessages = {
|
|
14
|
+
area: "Saturation and brightness",
|
|
15
|
+
areaRole: "2D slider",
|
|
16
|
+
areaValue: (saturation, brightness) => `Saturation ${saturation}%, brightness ${brightness}%`,
|
|
17
|
+
hue: "Hue",
|
|
18
|
+
alpha: "Opacity",
|
|
19
|
+
value: (format) => `${formatNames[format]} value`,
|
|
20
|
+
presets: "Presets",
|
|
21
|
+
placeholder: "Pick a colour",
|
|
22
|
+
};
|
|
10
23
|
const clamp = (v, min = 0, max = 1) => Math.min(max, Math.max(min, v));
|
|
11
24
|
const round = (v, digits) => Number(v.toFixed(digits));
|
|
12
25
|
function hsvToRgb({ h, s, v }) {
|
|
@@ -187,7 +200,7 @@ function useColorValue({ value, defaultValue, onValueChange, format, }) {
|
|
|
187
200
|
* (Shift, Page Up/Down, Home and End for bigger steps). One focusable thumb with `role="slider"`
|
|
188
201
|
* carries both values in its text.
|
|
189
202
|
*/
|
|
190
|
-
function ColorArea({ color, onChange, disabled, label }) {
|
|
203
|
+
function ColorArea({ color, onChange, disabled, label, text }) {
|
|
191
204
|
const thumb = React.useRef(null);
|
|
192
205
|
const move = (event) => {
|
|
193
206
|
const box = event.currentTarget.getBoundingClientRect();
|
|
@@ -231,7 +244,7 @@ function ColorArea({ color, onChange, disabled, label }) {
|
|
|
231
244
|
}, onPointerMove: (event) => {
|
|
232
245
|
if (event.currentTarget.hasPointerCapture(event.pointerId))
|
|
233
246
|
move(event);
|
|
234
|
-
}, children: [_jsx("span", { "aria-hidden": true, className: "pointer-events-none absolute inset-0 rounded-item border border-control-edge shadow-field" }), _jsx("div", { ref: thumb, role: "slider", tabIndex: disabled ? -1 : 0, "aria-label": label, "aria-roledescription":
|
|
247
|
+
}, children: [_jsx("span", { "aria-hidden": true, className: "pointer-events-none absolute inset-0 rounded-item border border-control-edge shadow-field" }), _jsx("div", { ref: thumb, role: "slider", tabIndex: disabled ? -1 : 0, "aria-label": label, "aria-roledescription": text.areaRole, "aria-valuemin": 0, "aria-valuemax": 100, "aria-valuenow": saturation, "aria-valuetext": text.areaValue(saturation, brightness), "aria-disabled": disabled || undefined, "data-slot": "color-picker-area-thumb", onKeyDown: disabled ? undefined : onKeyDown, className: cn("absolute size-[18px] -translate-x-1/2 -translate-y-1/2 cursor-grab rounded-full border-2 border-white shadow-thumb forced-color-adjust-auto active:cursor-grabbing", "focus-visible:outline-2 focus-visible:outline-offset-0 focus-visible:outline-control-focus", disabled && "cursor-not-allowed"), style: {
|
|
235
248
|
// Rounded: read from oklch(), the server's maths and the browser's can differ in the
|
|
236
249
|
// last digits, and the page would not hydrate.
|
|
237
250
|
left: `${round(color.s * 100, 2)}%`,
|
|
@@ -252,12 +265,6 @@ const checker = "repeating-conic-gradient(var(--field-edge) 0 25%, var(--field-s
|
|
|
252
265
|
const paintedSlider = cn("[&_[data-slot=slider-control]]:py-0.5", "[&_[data-slot=slider-indicator]]:hidden", "[&_[data-slot=slider-track]]:h-3 [&_[data-slot=slider-track]]:border-control-edge [&_[data-slot=slider-track]]:[background:var(--color-picker-track)]!",
|
|
253
266
|
// The colours are the content, also in a contrast theme; the thumb keeps the forced colours.
|
|
254
267
|
"[&_[data-slot=slider-thumb]]:forced-color-adjust-auto [&_[data-slot=slider-track]]:forced-color-adjust-none", "[&_[data-slot=slider-thumb]]:border-2 [&_[data-slot=slider-thumb]]:border-white [&_[data-slot=slider-thumb]]:[background:var(--color-picker-thumb)]!");
|
|
255
|
-
const inputLabels = {
|
|
256
|
-
hex: "Hex value",
|
|
257
|
-
rgb: "RGB value",
|
|
258
|
-
hsl: "HSL value",
|
|
259
|
-
oklch: "OKLCH value",
|
|
260
|
-
};
|
|
261
268
|
/**
|
|
262
269
|
* A Field of its own around each of the panel's controls. Base UI's slider and text field join the
|
|
263
270
|
* Field around them: inside a picker's Field they would take the picker's place as its control (its
|
|
@@ -267,6 +274,7 @@ function OwnField({ children }) {
|
|
|
267
274
|
return _jsx(BaseField.Root, { className: "contents", children: children });
|
|
268
275
|
}
|
|
269
276
|
function PanelBody({ color, text, onColorChange, format = "hex", alpha = false, presets, disabled, labels, className, ...props }) {
|
|
277
|
+
const messages = useMessages("colorPicker", defaultMessages, labels);
|
|
270
278
|
const [draft, setDraft] = React.useState(text);
|
|
271
279
|
const [invalid, setInvalid] = React.useState(false);
|
|
272
280
|
React.useEffect(() => {
|
|
@@ -287,13 +295,27 @@ function PanelBody({ color, text, onColorChange, format = "hex", alpha = false,
|
|
|
287
295
|
};
|
|
288
296
|
const opaque = cssColor({ ...color, a: 1 });
|
|
289
297
|
const hue = Math.round(color.h) % 360;
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
298
|
+
// One swatch per colour: a preset that picks the same colour as an earlier one (`#fff` after
|
|
299
|
+
// `rgb(255 255 255)`, or a translucent one in a picker without `alpha`) is left out, keeping the
|
|
300
|
+
// first one's label. The colour a preset picks, as hex, is its key.
|
|
301
|
+
const swatches = [];
|
|
302
|
+
for (const preset of presets ?? []) {
|
|
303
|
+
const { value, label } = typeof preset === "string" ? { value: preset, label: preset } : preset;
|
|
304
|
+
const parsed = parseColor(value, color);
|
|
305
|
+
if (!parsed)
|
|
306
|
+
continue;
|
|
307
|
+
const picks = alpha ? parsed : { ...parsed, a: 1 };
|
|
308
|
+
const hex = formatColor(picks, "hex");
|
|
309
|
+
if (!swatches.some((s) => s.hex === hex))
|
|
310
|
+
swatches.push({ hex, label, picks });
|
|
311
|
+
}
|
|
312
|
+
return (_jsxs("div", { "data-slot": "color-picker-panel", className: cn("flex w-full min-w-0 flex-col gap-3", className), ...props, children: [_jsx(ColorArea, { color: color, onChange: onColorChange, disabled: disabled, label: messages.area, text: messages }), _jsx(DirectionProvider, { direction: "ltr", children: _jsxs("div", { dir: "ltr", className: "flex flex-col gap-2", children: [_jsx(OwnField, { children: _jsx(Slider, { "data-slot": "color-picker-hue", min: 0, max: 360, step: 1, thumbAlignment: "edge", value: Math.round(color.h), onValueChange: (h) => onColorChange({ ...color, h }), thumbLabels: [messages.hue], disabled: disabled, className: paintedSlider, style: {
|
|
313
|
+
"--color-picker-track": hueTrack,
|
|
314
|
+
"--color-picker-thumb": `hsl(${hue} 100% 50%)`,
|
|
315
|
+
} }) }), alpha && (_jsx(OwnField, { children: _jsx(Slider, { "data-slot": "color-picker-alpha", min: 0, max: 1, step: 0.01, thumbAlignment: "edge", value: color.a, onValueChange: (a) => onColorChange({ ...color, a }), format: { style: "percent" }, thumbLabels: [messages.alpha], disabled: disabled, className: paintedSlider, style: {
|
|
316
|
+
"--color-picker-track": `linear-gradient(to right, transparent, ${opaque}), ${checker}`,
|
|
317
|
+
"--color-picker-thumb": `linear-gradient(${cssColor(color)}, ${cssColor(color)}), ${checker}`,
|
|
318
|
+
} }) }))] }) }), _jsx(OwnField, { children: _jsx(Input, { size: "sm", "data-slot": "color-picker-input", "aria-label": labels?.input ?? messages.value(format), "aria-invalid": invalid || undefined, spellCheck: false, autoComplete: "off", disabled: disabled, value: draft, onChange: (event) => {
|
|
297
319
|
setDraft(event.target.value);
|
|
298
320
|
setInvalid(false);
|
|
299
321
|
}, onBlur: commit, onKeyDown: (event) => {
|
|
@@ -312,15 +334,13 @@ function PanelBody({ color, text, onColorChange, format = "hex", alpha = false,
|
|
|
312
334
|
setDraft(text);
|
|
313
335
|
setInvalid(false);
|
|
314
336
|
}
|
|
315
|
-
},
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
const current = formatColor(parsed, "hex") === formatColor(color, "hex");
|
|
321
|
-
return (_jsx("button", { type: "button", "aria-label": label, "aria-pressed": current, disabled: disabled, "data-slot": "color-picker-preset", "data-current": current ? "" : undefined, onClick: () => onColorChange(alpha ? parsed : { ...parsed, a: 1 }), className: "grid size-(--control-inset-sm) cursor-pointer place-items-center rounded-checkbox border border-control-edge focus-ring forced-color-adjust-none disabled:cursor-not-allowed disabled:opacity-55 forced-colors:focus-visible:outline-[CanvasText]", style: { background: cssColor(parsed) }, children: current && (_jsx(CheckIcon, { "aria-hidden": true, className: "size-3.5",
|
|
337
|
+
},
|
|
338
|
+
// 16px on a phone, so focusing it doesn't zoom the page (iOS); 12px from `sm` up.
|
|
339
|
+
className: "font-mono text-base tabular-nums sm:text-xs" }) }), swatches.length > 0 && (_jsx("div", { role: "group", "aria-label": messages.presets, "data-slot": "color-picker-presets", className: "flex flex-wrap gap-1", children: swatches.map(({ hex, label, picks }) => {
|
|
340
|
+
const current = hex === formatColor(color, "hex");
|
|
341
|
+
return (_jsx("button", { type: "button", "aria-label": label, "aria-pressed": current, disabled: disabled, "data-slot": "color-picker-preset", "data-current": current ? "" : undefined, onClick: () => onColorChange(picks), className: "grid size-(--control-inset-sm) cursor-pointer place-items-center rounded-item border border-control-edge focus-ring forced-color-adjust-none disabled:cursor-not-allowed disabled:opacity-55 forced-colors:focus-visible:outline-[CanvasText]", style: { background: cssColor(picks) }, children: current && (_jsx(CheckIcon, { "aria-hidden": true, className: "size-3.5",
|
|
322
342
|
// Colour is the content here: the tick takes whichever ink reads on it.
|
|
323
|
-
style: { color: isLight(
|
|
343
|
+
style: { color: isLight(picks) ? "oklch(0.2 0 0)" : "oklch(0.98 0 0)" } })) }, hex));
|
|
324
344
|
}) }))] }));
|
|
325
345
|
}
|
|
326
346
|
/**
|
|
@@ -338,12 +358,12 @@ export function ColorPickerPanel({ value, defaultValue, onValueChange, format =
|
|
|
338
358
|
* checkerboard so transparency shows. Decorative (`aria-hidden`): say the colour in text beside it.
|
|
339
359
|
*/
|
|
340
360
|
export function ColorSwatch({ color, className, style, ...props }) {
|
|
341
|
-
return (_jsx("span", { "aria-hidden": true, "data-slot": "color-swatch", className: cn("relative inline-block size-4 shrink-0 overflow-hidden rounded-checkbox border border-field-edge forced-color-adjust-none", className), style: { background: checker, ...style }, ...props, children: _jsx("span", { className: "absolute inset-0", style: { background: color } }) }));
|
|
361
|
+
return (_jsx("span", { "aria-hidden": true, "data-slot": "color-swatch", className: cn("relative inline-block size-4 shrink-0 overflow-hidden rounded-checkbox border border-field-edge forced-color-adjust-none", className), style: { background: checker, ...style }, ...props, children: _jsx("span", { "data-slot": "color-swatch-fill", className: "absolute inset-0", style: { background: color } }) }));
|
|
342
362
|
}
|
|
343
363
|
/* ── ColorPicker ────────────────────────────────────────────────────────────── */
|
|
344
364
|
const triggerSizes = {
|
|
345
365
|
sm: "min-h-(--control-h-sm) gap-2 px-2.5 text-xs",
|
|
346
|
-
default: "min-h-(--control-h) gap-2.5
|
|
366
|
+
default: "min-h-(--control-h) gap-2.5 px-2.5 text-[13px]",
|
|
347
367
|
};
|
|
348
368
|
/**
|
|
349
369
|
* ColorPicker — a recessed trigger like a `Select` trigger, with a swatch and the value in Licht
|
|
@@ -355,8 +375,9 @@ const triggerSizes = {
|
|
|
355
375
|
* break: a picker for a choice among a few named colours. Those are a `RadioGroup` or a
|
|
356
376
|
* `ToggleGroup` of tones, with names.
|
|
357
377
|
*/
|
|
358
|
-
export function ColorPicker({ value, defaultValue, onValueChange, format = "hex", size = "default", name, required, placeholder
|
|
378
|
+
export function ColorPicker({ value, defaultValue, onValueChange, format = "hex", size = "default", name, required, placeholder, open, defaultOpen, onOpenChange, className, popover, disabled, id, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, ...panel }) {
|
|
359
379
|
const state = useColorValue({ value, defaultValue, onValueChange, format });
|
|
380
|
+
const text = useMessages("colorPicker", defaultMessages, panel.labels);
|
|
360
381
|
const empty = state.text === "";
|
|
361
382
|
const trigger = React.useRef(null);
|
|
362
383
|
const valueId = React.useId();
|
|
@@ -397,7 +418,7 @@ export function ColorPicker({ value, defaultValue, onValueChange, format = "hex"
|
|
|
397
418
|
"aria-label": ariaLabel,
|
|
398
419
|
"aria-labelledby": ariaLabelledBy,
|
|
399
420
|
"aria-describedby": ariaDescribedBy,
|
|
400
|
-
}).filter(([, v]) => v !== undefined)), className: cn("group/trigger inline-flex w-full min-w-40 material-field cursor-pointer items-center rounded-control text-start font-mono leading-none whitespace-nowrap tabular-nums outline-none select-none", "disabled:cursor-not-allowed disabled:opacity-55", triggerSizes[size], className), children: [_jsx(ColorSwatch, { color: empty ? "transparent" : cssColor(state.color), className: size === "sm" ? "size-4" : "size-5" }), _jsx("span", { id: valueId, "data-slot": "color-picker-value", className: cn("flex-1 truncate", empty && "text-control-muted"), children: empty ? placeholder : state.text }), _jsx(ChevronsUpDownIcon, { "aria-hidden": true, className: "size-4 shrink-0 text-muted-foreground transition-colors duration-(--duration) ease-brand group-hover/trigger:not-group-data-disabled/trigger:text-foreground" })] }), _jsx(PopoverContent, { align: "start", ...popover, "data-slot": "color-picker-content", className: sx("p-3", popover?.className), onBlur: (event) => {
|
|
421
|
+
}).filter(([, v]) => v !== undefined)), className: cn("group/trigger inline-flex w-full min-w-40 material-field cursor-pointer items-center rounded-control text-start font-mono leading-none whitespace-nowrap tabular-nums outline-none select-none", "disabled:cursor-not-allowed disabled:opacity-55", triggerSizes[size], className), children: [_jsx(ColorSwatch, { color: empty ? "transparent" : cssColor(state.color), className: size === "sm" ? "size-4" : "size-5" }), _jsx("span", { id: valueId, "data-slot": "color-picker-value", className: cn("flex-1 truncate", empty && "text-control-muted"), children: empty ? (placeholder ?? text.placeholder) : state.text }), _jsx(ChevronsUpDownIcon, { "aria-hidden": true, className: "size-4 shrink-0 text-muted-foreground transition-colors duration-(--duration) ease-brand group-hover/trigger:not-group-data-disabled/trigger:text-foreground" })] }), _jsx(PopoverContent, { align: "start", ...popover, "data-slot": "color-picker-content", className: sx("p-3", popover?.className), onBlur: (event) => {
|
|
401
422
|
popover?.onBlur?.(event);
|
|
402
423
|
// Out of the panel, and not back to the trigger: the field is left.
|
|
403
424
|
const to = event.relatedTarget;
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import { Combobox as BaseCombobox } from "@base-ui/react/combobox";
|
|
2
2
|
import * as React from "react";
|
|
3
|
+
export interface ComboboxMessages {
|
|
4
|
+
/** ComboboxInput's clear button. */
|
|
5
|
+
clear: string;
|
|
6
|
+
/** ComboboxInput's chevron. */
|
|
7
|
+
showOptions: string;
|
|
8
|
+
/** A chip's remove button, by the chip's text when it is a string. */
|
|
9
|
+
remove: (label?: string) => string;
|
|
10
|
+
}
|
|
11
|
+
declare module "./locale" {
|
|
12
|
+
interface Messages {
|
|
13
|
+
combobox: ComboboxMessages;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
3
16
|
/**
|
|
4
17
|
* Combobox — a `Select` you can type into: the input filters a fixed list of options.
|
|
5
18
|
*
|
|
@@ -8,7 +21,7 @@ import * as React from "react";
|
|
|
8
21
|
* is long enough that scrolling hurts (roughly 15+ options). The rule people break: using it for
|
|
9
22
|
* free-text search — a combobox only accepts listed values.
|
|
10
23
|
*/
|
|
11
|
-
export declare function Combobox<Value, Multiple extends boolean | undefined = false, Item = Value>({ inputRef, ...props }: BaseCombobox.Root.Props<Value, Multiple, Item>): React.JSX.Element;
|
|
24
|
+
export declare function Combobox<Value, Multiple extends boolean | undefined = false, Item = Value>({ inputRef, onItemHighlighted, ...props }: BaseCombobox.Root.Props<Value, Multiple, Item>): React.JSX.Element;
|
|
12
25
|
declare const groupSizes: {
|
|
13
26
|
readonly sm: "min-h-(--control-h-sm) gap-0.5 py-0.5 ps-2.5 pe-0.5 text-[13px]";
|
|
14
27
|
readonly default: "min-h-(--control-h) gap-0.5 py-[3px] ps-3.5 pe-[3px] text-base sm:text-[15px]";
|
|
@@ -22,12 +35,14 @@ export interface ComboboxInputProps extends Omit<BaseCombobox.Input.Props, "size
|
|
|
22
35
|
showClear?: boolean;
|
|
23
36
|
/** Classes for the visible field (the input group), not the bare `<input>`. */
|
|
24
37
|
className?: string;
|
|
38
|
+
/** The buttons' names (`clear`, `showOptions`); a LocaleProvider sets them for every Combobox. */
|
|
39
|
+
labels?: Partial<ComboboxMessages>;
|
|
25
40
|
}
|
|
26
41
|
/**
|
|
27
42
|
* ComboboxInput — the recessed field the user types into, with an optional clear button and
|
|
28
43
|
* a chevron trigger. Inside `<ComboboxChips>` it renders as the bare text input after the chips.
|
|
29
44
|
*/
|
|
30
|
-
export declare function ComboboxInput({ size, showTrigger, showClear, className, ...props }: ComboboxInputProps): React.JSX.Element;
|
|
45
|
+
export declare function ComboboxInput({ size, showTrigger, showClear, className, labels, ...props }: ComboboxInputProps): React.JSX.Element;
|
|
31
46
|
export interface ComboboxChipsProps<Value = any> extends Omit<BaseCombobox.Chips.Props, "children"> {
|
|
32
47
|
/**
|
|
33
48
|
* The chips and a trailing `<ComboboxInput />`. Pass a function to receive the selected
|
|
@@ -41,7 +56,10 @@ export interface ComboboxChipsProps<Value = any> extends Omit<BaseCombobox.Chips
|
|
|
41
56
|
*/
|
|
42
57
|
export declare function ComboboxChips<Value = any>({ className, children, ...props }: ComboboxChipsProps<Value>): React.JSX.Element;
|
|
43
58
|
export interface ComboboxChipProps extends BaseCombobox.Chip.Props {
|
|
44
|
-
/**
|
|
59
|
+
/**
|
|
60
|
+
* Accessible name for the remove button. Defaults to "Remove {label}" for string children
|
|
61
|
+
* (`messages.combobox.remove`).
|
|
62
|
+
*/
|
|
45
63
|
removeLabel?: string;
|
|
46
64
|
}
|
|
47
65
|
/** ComboboxChip — one selected value, with a remove button. */
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import { Combobox as BaseCombobox } from "@base-ui/react/combobox";
|
|
4
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
4
5
|
import { CheckIcon, ChevronsUpDownIcon, XIcon } from "lucide-react";
|
|
5
6
|
import * as React from "react";
|
|
6
7
|
import { cn, sx } from "../lib/cn.js";
|
|
7
8
|
import { useFormReset } from "../lib/form-reset.js";
|
|
8
9
|
import { mergeRefs } from "../lib/merge-refs.js";
|
|
9
10
|
import { usePopupDir } from "./_overlay.js";
|
|
11
|
+
import { useMessages } from "./locale.js";
|
|
10
12
|
import { ScrollArea } from "./scroll-area.js";
|
|
13
|
+
const defaultMessages = {
|
|
14
|
+
clear: "Clear selection",
|
|
15
|
+
showOptions: "Show options",
|
|
16
|
+
remove: (label) => (label ? `Remove ${label}` : "Remove"),
|
|
17
|
+
};
|
|
11
18
|
/**
|
|
12
19
|
* Combobox — a `Select` you can type into: the input filters a fixed list of options.
|
|
13
20
|
*
|
|
@@ -16,12 +23,36 @@ import { ScrollArea } from "./scroll-area.js";
|
|
|
16
23
|
* is long enough that scrolling hurts (roughly 15+ options). The rule people break: using it for
|
|
17
24
|
* free-text search — a combobox only accepts listed values.
|
|
18
25
|
*/
|
|
19
|
-
export function Combobox({ inputRef, ...props }) {
|
|
26
|
+
export function Combobox({ inputRef, onItemHighlighted, ...props }) {
|
|
20
27
|
// A form's reset remounts it from its defaults (lib/form-reset); its hidden input finds the form.
|
|
21
28
|
const [resetKey, formRef] = useFormReset();
|
|
22
29
|
const refs = React.useMemo(() => mergeRefs(formRef, inputRef), [formRef, inputRef]);
|
|
23
|
-
|
|
30
|
+
// A long flat list renders only the options in view (ComboboxList), unless it says otherwise.
|
|
31
|
+
const items = props.items;
|
|
32
|
+
const virtual = props.virtualized === undefined &&
|
|
33
|
+
!!items &&
|
|
34
|
+
items.length > VIRTUAL_FROM &&
|
|
35
|
+
!(items[0] !== null &&
|
|
36
|
+
typeof items[0] === "object" &&
|
|
37
|
+
Array.isArray(items[0].items));
|
|
38
|
+
const virtualizerRef = React.useRef(null);
|
|
39
|
+
const highlighted = (item, details) => {
|
|
40
|
+
onItemHighlighted?.(item, details);
|
|
41
|
+
const virtualizer = virtualizerRef.current;
|
|
42
|
+
if (!virtual || !virtualizer || item === undefined)
|
|
43
|
+
return;
|
|
44
|
+
// The highlighted option may not be rendered: the first or last by key, or the selected one.
|
|
45
|
+
const { reason, index } = details;
|
|
46
|
+
const edge = index === 0 || index === virtualizer.options.count - 1;
|
|
47
|
+
if (reason === "none" || (reason === "keyboard" && edge))
|
|
48
|
+
queueMicrotask(() => virtualizer.scrollToIndex(index, { align: edge && index > 0 ? "start" : "auto" }));
|
|
49
|
+
};
|
|
50
|
+
return (_jsx(VirtualContext.Provider, { value: virtual ? virtualizerRef : null, children: _jsx(BaseCombobox.Root, { inputRef: refs, virtualized: virtual || undefined, onItemHighlighted: highlighted, ...props }, resetKey) }));
|
|
24
51
|
}
|
|
52
|
+
/** Flat lists of more options than this render only those in view. */
|
|
53
|
+
const VIRTUAL_FROM = 200;
|
|
54
|
+
/** Set by `Combobox` when its list is virtual; `ComboboxList` hands it the virtualizer. */
|
|
55
|
+
const VirtualContext = React.createContext(null);
|
|
25
56
|
/** True inside `<ComboboxChips>`: `ComboboxInput` then renders bare, inside the chips field. */
|
|
26
57
|
const ChipsContext = React.createContext(false);
|
|
27
58
|
const fieldGroup = "flex w-full items-center rounded-control material-field font-body";
|
|
@@ -38,12 +69,13 @@ const iconButton = "inline-flex shrink-0 cursor-pointer items-center justify-cen
|
|
|
38
69
|
* ComboboxInput — the recessed field the user types into, with an optional clear button and
|
|
39
70
|
* a chevron trigger. Inside `<ComboboxChips>` it renders as the bare text input after the chips.
|
|
40
71
|
*/
|
|
41
|
-
export function ComboboxInput({ size = "default", showTrigger = true, showClear = false, className, ...props }) {
|
|
72
|
+
export function ComboboxInput({ size = "default", showTrigger = true, showClear = false, className, labels, ...props }) {
|
|
42
73
|
const inChips = React.useContext(ChipsContext);
|
|
74
|
+
const text = useMessages("combobox", defaultMessages, labels);
|
|
43
75
|
if (inChips) {
|
|
44
76
|
return (_jsx(BaseCombobox.Input, { "data-slot": "combobox-input", className: cn("min-h-(--control-h-sm) min-w-16 flex-1 bg-transparent px-1.5 text-base outline-none placeholder:text-control-muted sm:text-[15px]", className), ...props }));
|
|
45
77
|
}
|
|
46
|
-
return (_jsxs(BaseCombobox.InputGroup, { "data-slot": "combobox-input-group", "data-size": size, className: cn(fieldGroup, groupSizes[size], className), children: [_jsx(BaseCombobox.Input, { "data-slot": "combobox-input", className: "min-w-0 flex-1 self-stretch bg-transparent outline-none placeholder:text-control-muted", ...props }), showClear && (_jsx(BaseCombobox.Clear, { "data-slot": "combobox-clear", "aria-label":
|
|
78
|
+
return (_jsxs(BaseCombobox.InputGroup, { "data-slot": "combobox-input-group", "data-size": size, className: cn(fieldGroup, groupSizes[size], className), children: [_jsx(BaseCombobox.Input, { "data-slot": "combobox-input", className: "min-w-0 flex-1 self-stretch bg-transparent outline-none placeholder:text-control-muted", ...props }), showClear && (_jsx(BaseCombobox.Clear, { "data-slot": "combobox-clear", "aria-label": text.clear, className: cn(iconButton, iconButtonSizes[size]), children: _jsx(XIcon, {}) })), showTrigger && (_jsx(BaseCombobox.Trigger, { "data-slot": "combobox-trigger", "aria-label": text.showOptions, "aria-labelledby": undefined, className: cn(iconButton, iconButtonSizes[size]), children: _jsx(ChevronsUpDownIcon, {}) }))] }));
|
|
47
79
|
}
|
|
48
80
|
/**
|
|
49
81
|
* ComboboxChips — the field for a `multiple` combobox: selected values as raised 30px chips,
|
|
@@ -54,7 +86,8 @@ export function ComboboxChips({ className, children, ...props }) {
|
|
|
54
86
|
}
|
|
55
87
|
/** ComboboxChip — one selected value, with a remove button. */
|
|
56
88
|
export function ComboboxChip({ className, children, removeLabel, ...props }) {
|
|
57
|
-
const
|
|
89
|
+
const text = useMessages("combobox", defaultMessages);
|
|
90
|
+
const label = removeLabel ?? text.remove(typeof children === "string" ? children : undefined);
|
|
58
91
|
return (_jsxs(BaseCombobox.Chip, { "data-slot": "combobox-chip", className: sx([
|
|
59
92
|
"inline-flex min-h-(--control-h-sm) max-w-full cursor-default items-center gap-1 rounded-item border border-transparent ps-2.5 pe-[3px] text-[13px] leading-none font-medium text-foreground",
|
|
60
93
|
"bg-control-surface bg-linear-to-b from-control-inner-edge to-transparent shadow-raised",
|
|
@@ -69,12 +102,51 @@ export function ComboboxContent({ className, children, positioner, dir, ...props
|
|
|
69
102
|
return (_jsx(BaseCombobox.Portal, { children: _jsx(BaseCombobox.Positioner, { sideOffset: 6, dir: popupDir, ...positioner, className: "z-50 outline-none", children: _jsx(BaseCombobox.Popup, { "data-slot": "combobox-content", className: sx("w-(--anchor-width) max-w-(--available-width) popup-motion overflow-hidden rounded-button material-menu p-1 font-body outline-none", className), ...props, children: children }) }) }));
|
|
70
103
|
}
|
|
71
104
|
export function ComboboxList({ className, children, ...props }) {
|
|
72
|
-
|
|
105
|
+
const virtual = React.useContext(VirtualContext);
|
|
106
|
+
const [viewport, setViewport] = React.useState(null);
|
|
107
|
+
return (_jsx(ScrollArea, { className: "max-h-[min(23rem,calc(var(--available-height)-0.5rem))] has-[[data-slot=combobox-list][data-empty]]:hidden", viewportRender: _jsx(BaseCombobox.List, { "data-slot": "combobox-list", ref: virtual ? setViewport : undefined, role: "listbox", className: cn("scroll-py-1 outline-none data-has-overflow-y:pe-3", className), ...props }), children: typeof children === "function" ? (virtual ? (_jsx(VirtualOptions, { virtualizerRef: virtual, scroller: viewport, children: children })) : (_jsx(BaseCombobox.Collection, { children: children }))) : (children) }));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* The filtered options in view and a few beyond, over the space of all of them. Each option takes
|
|
111
|
+
* its index in the filtered list, which a virtual combobox navigates by.
|
|
112
|
+
*/
|
|
113
|
+
function VirtualOptions({ children, scroller, virtualizerRef, }) {
|
|
114
|
+
const items = BaseCombobox.useFilteredItems();
|
|
115
|
+
// Options are the control height (a long one wraps, and is measured).
|
|
116
|
+
const rowHeight = React.useMemo(() => (scroller ? parseFloat(getComputedStyle(scroller).getPropertyValue("--control-h")) || 38 : 38), [scroller]);
|
|
117
|
+
const virtualizer = useVirtualizer({
|
|
118
|
+
count: items.length,
|
|
119
|
+
getScrollElement: () => scroller,
|
|
120
|
+
estimateSize: () => rowHeight,
|
|
121
|
+
overscan: 10,
|
|
122
|
+
});
|
|
123
|
+
virtualizerRef.current = virtualizer;
|
|
124
|
+
React.useEffect(() => () => {
|
|
125
|
+
virtualizerRef.current = null;
|
|
126
|
+
}, [virtualizerRef]);
|
|
127
|
+
return (_jsx("div", { role: "presentation", className: "relative w-full", style: { height: virtualizer.getTotalSize() }, children: virtualizer.getVirtualItems().map((row) => {
|
|
128
|
+
const option = children(items[row.index], row.index);
|
|
129
|
+
if (!React.isValidElement(option))
|
|
130
|
+
return option;
|
|
131
|
+
return React.cloneElement(option, {
|
|
132
|
+
key: row.key,
|
|
133
|
+
index: row.index,
|
|
134
|
+
"data-index": row.index,
|
|
135
|
+
ref: virtualizer.measureElement,
|
|
136
|
+
style: {
|
|
137
|
+
...option.props.style,
|
|
138
|
+
position: "absolute",
|
|
139
|
+
top: 0,
|
|
140
|
+
insetInline: 0,
|
|
141
|
+
translate: `0 ${row.start}px`,
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
}) }));
|
|
73
145
|
}
|
|
74
146
|
/** ComboboxItem — a 38px option with a 7px radius and a check when selected, like `SelectItem`. */
|
|
75
147
|
export function ComboboxItem({ className, children, ...props }) {
|
|
76
148
|
return (_jsxs(BaseCombobox.Item, { "data-slot": "combobox-item", className: sx([
|
|
77
|
-
"grid min-h-(--control-h) cursor-pointer grid-cols-[1fr_1rem] items-center gap-3 rounded-item border border-transparent
|
|
149
|
+
"grid min-h-(--control-h) cursor-pointer grid-cols-[1fr_1rem] items-center gap-3 rounded-item border border-transparent px-2.5 py-[clamp(0px,calc((var(--control-h)-22px)/2),6px)] text-sm text-foreground-soft outline-none select-none",
|
|
78
150
|
"data-highlighted:border-control-inner-edge data-highlighted:bg-control-hover data-highlighted:text-foreground data-highlighted:shadow-highlight",
|
|
79
151
|
"data-disabled:pointer-events-none data-disabled:opacity-50 data-selected:text-foreground",
|
|
80
152
|
], className), ...props, children: [_jsx("span", { className: "col-start-1 line-clamp-2 [overflow-wrap:anywhere]", children: children }), _jsx(BaseCombobox.ItemIndicator, { "data-slot": "combobox-item-indicator", className: "col-start-2 flex", children: _jsx(CheckIcon, { className: "size-4" }) })] }));
|
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
import { Autocomplete as BaseAutocomplete } from "@base-ui/react/autocomplete";
|
|
2
2
|
import { Dialog as BaseDialog } from "@base-ui/react/dialog";
|
|
3
3
|
import * as React from "react";
|
|
4
|
+
export interface CommandMessages {
|
|
5
|
+
/** The palette's name. */
|
|
6
|
+
label: string;
|
|
7
|
+
placeholder: string;
|
|
8
|
+
/** Shown when nothing matches. */
|
|
9
|
+
empty: string;
|
|
10
|
+
/** The `Esc` key that closes the palette. */
|
|
11
|
+
close: string;
|
|
12
|
+
/** The footer's hints, after ↑ ↓ and ↵. */
|
|
13
|
+
navigate: string;
|
|
14
|
+
open: string;
|
|
15
|
+
}
|
|
16
|
+
declare module "./locale" {
|
|
17
|
+
interface Messages {
|
|
18
|
+
command: CommandMessages;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
4
21
|
/** One entry in the palette. `value` must be unique; `keywords` widen the match. */
|
|
5
22
|
export interface CommandEntry {
|
|
6
23
|
value: string;
|
|
@@ -39,11 +56,14 @@ export interface CommandDialogProps extends Pick<BaseDialog.Root.Props, "open" |
|
|
|
39
56
|
items: readonly CommandEntry[];
|
|
40
57
|
/** Custom matcher. Replaces the ranking: matching entries keep their original order. */
|
|
41
58
|
filter?: (entry: CommandEntry, query: string) => boolean;
|
|
42
|
-
/** Accessible name of the palette. */
|
|
59
|
+
/** Accessible name of the palette. Default `labels.label`. */
|
|
43
60
|
label?: string;
|
|
61
|
+
/** Default `labels.placeholder`. */
|
|
44
62
|
placeholder?: string;
|
|
45
|
-
/** Shown when nothing matches. */
|
|
63
|
+
/** Shown when nothing matches. Default `labels.empty`. */
|
|
46
64
|
emptyMessage?: React.ReactNode;
|
|
65
|
+
/** The palette's words, for its parts too; a LocaleProvider sets them for every palette. */
|
|
66
|
+
labels?: Partial<CommandMessages>;
|
|
47
67
|
/** An element that opens the palette, e.g. `<Button variant="secondary" />`. */
|
|
48
68
|
trigger?: React.ReactElement;
|
|
49
69
|
/** Replace the default input / list / footer composition with your own command parts. */
|
|
@@ -59,7 +79,7 @@ export interface CommandDialogProps extends Pick<BaseDialog.Root.Props, "open" |
|
|
|
59
79
|
* The rule people break: making the palette the only way to reach something. It is a shortcut;
|
|
60
80
|
* every entry must also be reachable from the visible navigation.
|
|
61
81
|
*/
|
|
62
|
-
export declare function CommandDialog({ items, filter, label, placeholder, emptyMessage, trigger, children, className, onOpenChangeComplete, ...rootProps }: CommandDialogProps): React.JSX.Element;
|
|
82
|
+
export declare function CommandDialog({ items, filter, label, placeholder, emptyMessage, labels, trigger, children, className, onOpenChangeComplete, ...rootProps }: CommandDialogProps): React.JSX.Element;
|
|
63
83
|
/**
|
|
64
84
|
* The search field: borderless, 46px, a search icon before it and an `Esc` hint after it.
|
|
65
85
|
* The hint is the palette's close button, so touch and screen-reader users have one too.
|
|
@@ -9,6 +9,15 @@ import { modPressed } from "../lib/platform.js";
|
|
|
9
9
|
import { ScrollArea } from "./scroll-area.js";
|
|
10
10
|
import { Kbd, kbdClassName } from "./kbd.js";
|
|
11
11
|
import { overlayBackdrop, overlayCard, usePopupDir } from "./_overlay.js";
|
|
12
|
+
import { useMessages } from "./locale.js";
|
|
13
|
+
const defaultMessages = {
|
|
14
|
+
label: "Search",
|
|
15
|
+
placeholder: "Search pages and actions…",
|
|
16
|
+
empty: "Nothing matches that search.",
|
|
17
|
+
close: "Close search",
|
|
18
|
+
navigate: "navigate",
|
|
19
|
+
open: "open",
|
|
20
|
+
};
|
|
12
21
|
/** Group a flat list by `group`, keeping first-seen order. */
|
|
13
22
|
function groupEntries(entries) {
|
|
14
23
|
const groups = new Map();
|
|
@@ -27,6 +36,26 @@ const normalize = (text) => text
|
|
|
27
36
|
.replace(/\p{Diacritic}/gu, "")
|
|
28
37
|
.toLowerCase();
|
|
29
38
|
const wordsOf = (text) => text.split(/[^\p{L}\p{N}]+/u).filter(Boolean);
|
|
39
|
+
/** An entry's text, normalised once: a palette of thousands is searched again at every key. */
|
|
40
|
+
const searchText = new WeakMap();
|
|
41
|
+
function readSearchable(entry) {
|
|
42
|
+
const label = normalize(entry.label);
|
|
43
|
+
const description = normalize(entry.description ?? "");
|
|
44
|
+
return {
|
|
45
|
+
label,
|
|
46
|
+
labelWords: wordsOf(label),
|
|
47
|
+
keywords: (entry.keywords ?? []).map(normalize),
|
|
48
|
+
group: normalize(entry.group ?? ""),
|
|
49
|
+
description,
|
|
50
|
+
descriptionWords: wordsOf(description),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function searchable(entry) {
|
|
54
|
+
let text = searchText.get(entry);
|
|
55
|
+
if (!text)
|
|
56
|
+
searchText.set(entry, (text = readSearchable(entry)));
|
|
57
|
+
return text;
|
|
58
|
+
}
|
|
30
59
|
/**
|
|
31
60
|
* How well an entry matches the query; 0 is no match. Every query word has to match somewhere,
|
|
32
61
|
* and where it matches decides the weight: the label first (whole label, its start, the start of
|
|
@@ -38,12 +67,7 @@ export function scoreCommandEntry(entry, query) {
|
|
|
38
67
|
const words = q.split(/\s+/).filter(Boolean);
|
|
39
68
|
if (words.length === 0)
|
|
40
69
|
return 1;
|
|
41
|
-
const label =
|
|
42
|
-
const labelWords = wordsOf(label);
|
|
43
|
-
const keywords = (entry.keywords ?? []).map(normalize);
|
|
44
|
-
const group = normalize(entry.group ?? "");
|
|
45
|
-
const description = normalize(entry.description ?? "");
|
|
46
|
-
const descriptionWords = wordsOf(description);
|
|
70
|
+
const { label, labelWords, keywords, group, description, descriptionWords } = searchable(entry);
|
|
47
71
|
let total = 0;
|
|
48
72
|
for (const word of words) {
|
|
49
73
|
let best = 0;
|
|
@@ -91,18 +115,36 @@ export function scoreCommandEntry(entry, query) {
|
|
|
91
115
|
*/
|
|
92
116
|
function rankEntries(entries, query, filter) {
|
|
93
117
|
if (!query.trim())
|
|
94
|
-
return groupEntries(entries);
|
|
118
|
+
return groupEntries(entries.slice(0, MAX_RESULTS));
|
|
95
119
|
// A custom matcher filters only; it keeps the original order.
|
|
96
|
-
if (filter)
|
|
97
|
-
|
|
120
|
+
if (filter) {
|
|
121
|
+
const matches = [];
|
|
122
|
+
for (const entry of entries) {
|
|
123
|
+
if (filter(entry, query))
|
|
124
|
+
matches.push(entry);
|
|
125
|
+
if (matches.length === MAX_RESULTS)
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
return groupEntries(matches);
|
|
129
|
+
}
|
|
98
130
|
const ranked = entries
|
|
99
131
|
.map((entry, index) => ({ entry, index, score: scoreCommandEntry(entry, query) }))
|
|
100
132
|
.filter((r) => r.score > 0)
|
|
101
133
|
.sort((a, b) => b.score - a.score || a.entry.label.length - b.entry.label.length || a.index - b.index)
|
|
134
|
+
.slice(0, MAX_RESULTS)
|
|
102
135
|
.map((r) => r.entry);
|
|
103
136
|
return groupEntries(ranked);
|
|
104
137
|
}
|
|
105
|
-
|
|
138
|
+
/**
|
|
139
|
+
* The most results the palette lists: the best matches, or the first entries before anything is
|
|
140
|
+
* typed. Past a few hundred, a list is scrolled through instead of narrowed, and each row costs
|
|
141
|
+
* time to draw.
|
|
142
|
+
*/
|
|
143
|
+
const MAX_RESULTS = 200;
|
|
144
|
+
const CommandContext = React.createContext({
|
|
145
|
+
close: () => { },
|
|
146
|
+
});
|
|
147
|
+
const useCommandText = () => useMessages("command", defaultMessages, React.useContext(CommandContext).labels);
|
|
106
148
|
/**
|
|
107
149
|
* CommandDialog — the ⌘K command palette: a dialog holding a search field and a filtered list of
|
|
108
150
|
* entries (pages, actions, settings). Use it as the global search of a site or app; open it with
|
|
@@ -112,14 +154,15 @@ const CommandContext = React.createContext({ close: () => { } });
|
|
|
112
154
|
* The rule people break: making the palette the only way to reach something. It is a shortcut;
|
|
113
155
|
* every entry must also be reachable from the visible navigation.
|
|
114
156
|
*/
|
|
115
|
-
export function CommandDialog({ items, filter, label
|
|
157
|
+
export function CommandDialog({ items, filter, label, placeholder, emptyMessage, labels, trigger, children, className, onOpenChangeComplete, ...rootProps }) {
|
|
158
|
+
const text = useMessages("command", defaultMessages, labels);
|
|
116
159
|
const actionsRef = React.useRef(null);
|
|
117
160
|
const [query, setQuery] = React.useState("");
|
|
118
161
|
const groups = React.useMemo(() => groupEntries(items), [items]);
|
|
119
162
|
const results = React.useMemo(() => rankEntries(items, query, filter), [items, query, filter]);
|
|
120
163
|
// Closing through Base UI's imperative action keeps controlled and uncontrolled use working:
|
|
121
164
|
// onOpenChange fires with reason "imperative-action".
|
|
122
|
-
const context = React.useMemo(() => ({ close: () => actionsRef.current?.close() }), []);
|
|
165
|
+
const context = React.useMemo(() => ({ close: () => actionsRef.current?.close(), labels }), [labels]);
|
|
123
166
|
const popupDir = usePopupDir();
|
|
124
167
|
return (_jsxs(BaseDialog.Root, { actionsRef: actionsRef, onOpenChangeComplete: (open) => {
|
|
125
168
|
// Every opening starts from an empty search. Cleared once the palette has closed — also
|
|
@@ -128,14 +171,15 @@ export function CommandDialog({ items, filter, label = "Search", placeholder = "
|
|
|
128
171
|
if (!open)
|
|
129
172
|
setQuery("");
|
|
130
173
|
onOpenChangeComplete?.(open);
|
|
131
|
-
}, ...rootProps, children: [trigger && _jsx(BaseDialog.Trigger, {
|
|
174
|
+
}, ...rootProps, children: [trigger && _jsx(BaseDialog.Trigger, { render: trigger }), _jsxs(BaseDialog.Portal, { children: [_jsx(BaseDialog.Backdrop, { "data-slot": "command-backdrop", className: cn(overlayBackdrop, "transition-none") }), _jsx(BaseDialog.Viewport, { "data-slot": "command-viewport", className: "fixed inset-0 z-50 flex items-start justify-center px-4 pt-[12dvh] pb-4 sm:pt-[16dvh]", children: _jsx(BaseDialog.Popup, { "data-slot": "command-dialog", "aria-label": label ?? text.label, dir: popupDir, className: cn("relative flex max-h-[min(34rem,calc(84dvh-1rem))] w-full max-w-[40rem] flex-col overflow-hidden rounded-card font-body outline-none", overlayCard, className), children: _jsx(CommandContext.Provider, { value: context, children: _jsx(BaseAutocomplete.Root, { open: true, inline: true, items: groups, filteredItems: results, value: query, onValueChange: setQuery, itemToStringValue: (item) => item.label, autoHighlight: "always", keepHighlight: true, children: children ?? (_jsxs(_Fragment, { children: [_jsx(CommandInput, { placeholder: placeholder ?? text.placeholder }), _jsx(CommandEmpty, { children: emptyMessage === undefined ? text.empty : emptyMessage }), _jsx(CommandList, { children: (group) => (_jsx(CommandGroup, { items: group.items, label: group.value, children: (entry) => _jsx(CommandItem, { value: entry }, entry.value) }, group.value)) }), _jsx(CommandFooter, {})] })) }) }) }) })] })] }));
|
|
132
175
|
}
|
|
133
176
|
/**
|
|
134
177
|
* The search field: borderless, 46px, a search icon before it and an `Esc` hint after it.
|
|
135
178
|
* The hint is the palette's close button, so touch and screen-reader users have one too.
|
|
136
179
|
*/
|
|
137
180
|
export function CommandInput({ className, ...props }) {
|
|
138
|
-
|
|
181
|
+
const text = useCommandText();
|
|
182
|
+
return (_jsxs(BaseAutocomplete.InputGroup, { "data-slot": "command-input-group", className: "flex min-h-(--control-h-lg) shrink-0 cursor-text items-center gap-3 border-b border-border px-4", children: [_jsx(SearchIcon, { "aria-hidden": true, className: "size-4 shrink-0 text-muted-foreground" }), _jsx(BaseAutocomplete.Input, { "data-slot": "command-input", className: sx("min-h-(--control-h-lg) w-full min-w-0 flex-1 border-0 bg-transparent text-base text-foreground outline-none placeholder:text-control-muted sm:text-[15px]", className), ...props }), _jsxs(BaseDialog.Close, { "data-slot": "command-close", className: cn(kbdClassName, "cursor-pointer focus-ring"), children: ["Esc", _jsxs("span", { className: "sr-only", children: [", ", text.close] })] })] }));
|
|
139
183
|
}
|
|
140
184
|
export function CommandList({ className, children, ...props }) {
|
|
141
185
|
return (_jsx(ScrollArea
|
|
@@ -147,7 +191,7 @@ export function CommandList({ className, children, ...props }) {
|
|
|
147
191
|
className: "flex min-h-0 flex-1 flex-col has-[[data-slot=command-list][data-empty]]:hidden", viewportClassName: "h-auto min-h-0 flex-1",
|
|
148
192
|
// Rows are as wide as the palette, so a long label truncates. Base UI sizes the content to
|
|
149
193
|
// fit its widest line, which would push the row, its hint and its edge out of view.
|
|
150
|
-
contentClassName: "min-w-0!", viewportRender: _jsx(BaseAutocomplete.List, { "data-slot": "command-list", role: "listbox", className: cn("scroll-py-2 p-2 data-has-overflow-y:
|
|
194
|
+
contentClassName: "min-w-0!", viewportRender: _jsx(BaseAutocomplete.List, { "data-slot": "command-list", role: "listbox", className: cn("scroll-py-2 p-2 data-has-overflow-y:pe-3", className), ...props }), children: typeof children === "function" ? (_jsx(BaseAutocomplete.Collection, { children: children })) : (children) }));
|
|
151
195
|
}
|
|
152
196
|
/** A labelled group of entries inside `CommandList`. */
|
|
153
197
|
export function CommandGroup({ items, label, children, className, ...props }) {
|
|
@@ -172,8 +216,8 @@ export function CommandItem({ value, onItemSelect, closeOnSelect = true, classNa
|
|
|
172
216
|
}, className: sx([
|
|
173
217
|
"flex min-h-(--control-h) cursor-pointer scroll-my-2 items-center gap-3 rounded-item border border-transparent px-2.5 text-sm text-foreground-soft outline-none select-none",
|
|
174
218
|
// The raised treatment, on the highlight only: the pointer moves the highlight, so a CSS
|
|
175
|
-
// :hover lift (as in material-row) would show two raised rows while typing.
|
|
176
|
-
|
|
219
|
+
// :hover lift (as in material-row) would show two raised rows while typing. No transition:
|
|
220
|
+
// the highlight follows the arrow keys at once, as in a menu or a select.
|
|
177
221
|
"data-highlighted:bg-control-hover data-highlighted:bg-[linear-gradient(180deg,var(--control-inner-edge),transparent)] data-highlighted:text-foreground data-highlighted:shadow-raised",
|
|
178
222
|
"data-disabled:pointer-events-none data-disabled:opacity-50",
|
|
179
223
|
"[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&>svg]:text-muted-foreground data-highlighted:[&>svg]:text-foreground",
|
|
@@ -186,7 +230,8 @@ export function CommandEmpty({ className, ...props }) {
|
|
|
186
230
|
}
|
|
187
231
|
/** Keyboard hints along the bottom edge: "↑↓ navigate · ↵ open". */
|
|
188
232
|
export function CommandFooter({ className, children, ...props }) {
|
|
189
|
-
|
|
233
|
+
const text = useCommandText();
|
|
234
|
+
return (_jsx("div", { "data-slot": "command-footer", className: cn("flex min-h-(--control-h) shrink-0 items-center gap-4 border-t border-border px-4 font-mono text-[11px] text-muted-foreground select-none", className), ...props, children: children ?? (_jsxs(_Fragment, { children: [_jsxs("span", { className: "inline-flex items-center gap-1.5", children: [_jsx(Kbd, { children: "\u2191" }), _jsx(Kbd, { children: "\u2193" }), text.navigate] }), _jsx("span", { "aria-hidden": true, children: "\u00B7" }), _jsxs("span", { className: "inline-flex items-center gap-1.5", children: [_jsx(Kbd, { children: "\u21B5" }), text.open] })] })) }));
|
|
190
235
|
}
|
|
191
236
|
export function useCommandShortcut(keyOrHandler, maybeHandler) {
|
|
192
237
|
const key = (typeof keyOrHandler === "string" ? keyOrHandler : "k").toLowerCase();
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { type ButtonProps } from "./button.js";
|
|
3
|
+
export interface ConversationMessages {
|
|
4
|
+
/** ConversationScrollButton's name. */
|
|
5
|
+
scrollToLatest: string;
|
|
6
|
+
}
|
|
7
|
+
declare module "./locale" {
|
|
8
|
+
interface Messages {
|
|
9
|
+
conversation: ConversationMessages;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
3
12
|
export interface ConversationProps extends React.ComponentProps<"div"> {
|
|
4
13
|
}
|
|
5
14
|
/**
|
|
@@ -16,7 +25,7 @@ export declare function Conversation({ className, style, children, ref, onScroll
|
|
|
16
25
|
/** The column of messages: 24px apart, padded, at a reading width. */
|
|
17
26
|
export declare function ConversationContent({ className, ...props }: React.ComponentProps<"div">): React.JSX.Element;
|
|
18
27
|
export interface ConversationScrollButtonProps extends Omit<ButtonProps, "children"> {
|
|
19
|
-
/** Accessible name. Default `Scroll to the latest message`. */
|
|
28
|
+
/** Accessible name. Default `Scroll to the latest message`, or `messages.conversation.scrollToLatest`. */
|
|
20
29
|
label?: string;
|
|
21
30
|
}
|
|
22
31
|
/**
|