@zonsh/ui 0.2.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +19 -13
- package/dist/components/alert-dialog.js +1 -1
- package/dist/components/ascii-field.d.ts +1 -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/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 +9 -0
- package/dist/components/chart.js +40 -3
- package/dist/components/code-block.d.ts +20 -10
- package/dist/components/code-block.js +54 -12
- package/dist/components/collapsible.js +2 -2
- 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 +61 -16
- package/dist/components/conversation.d.ts +10 -1
- package/dist/components/conversation.js +14 -4
- package/dist/components/copy-button.d.ts +15 -6
- package/dist/components/copy-button.js +20 -5
- 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 +84 -45
- package/dist/components/illustration.js +1 -1
- package/dist/components/input-group.d.ts +16 -5
- package/dist/components/input-group.js +35 -15
- 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/lightbox.d.ts +100 -0
- package/dist/components/lightbox.js +439 -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/message.d.ts +3 -3
- package/dist/components/message.js +10 -7
- 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/number-field.d.ts +21 -2
- package/dist/components/number-field.js +27 -16
- 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 +28 -16
- 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 +14 -3
- package/dist/components/response.js +36 -8
- 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 +34 -18
- package/dist/components/site-header.d.ts +46 -12
- package/dist/components/site-header.js +73 -19
- package/dist/components/slider.js +3 -1
- package/dist/components/sortable.d.ts +178 -0
- package/dist/components/sortable.js +1258 -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 +12 -2
- package/dist/components/table.js +176 -39
- package/dist/components/tags-input.d.ts +18 -2
- package/dist/components/tags-input.js +65 -33
- package/dist/components/theme.d.ts +16 -2
- package/dist/components/theme.js +15 -7
- package/dist/components/time-field.d.ts +22 -3
- package/dist/components/time-field.js +19 -6
- 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/tool-call.d.ts +19 -8
- package/dist/components/tool-call.js +18 -7
- package/dist/components/toolbar.js +6 -1
- package/dist/components/tour.d.ts +127 -0
- package/dist/components/tour.js +648 -0
- package/dist/components/tree.js +17 -4
- package/dist/components/video-player.d.ts +10 -2
- package/dist/components/video-player.js +73 -33
- 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/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.js +23 -4
- 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 +20 -5
- package/dist/styles/index.css +3 -2
- package/dist/styles/material.css +7 -3
- package/dist/styles/parts/ai.css +2 -1
- package/dist/styles/parts/controls.css +62 -13
- package/dist/styles/parts/feedback.css +68 -1
- package/dist/styles/parts/foundations.css +1 -1
- package/dist/styles/parts/navigation.css +194 -2
- package/dist/styles/parts/overlays.css +7 -7
- package/dist/styles/tokens.css +12 -3
- 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,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: overlayBackdrop }), _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, "popup-motion", 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 }) {
|
|
@@ -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
|
/**
|
|
@@ -5,6 +5,8 @@ import * as React from "react";
|
|
|
5
5
|
import { cn, sx } from "../lib/cn.js";
|
|
6
6
|
import { mergeRefs } from "../lib/merge-refs.js";
|
|
7
7
|
import { Button } from "./button.js";
|
|
8
|
+
import { useMessages } from "./locale.js";
|
|
9
|
+
const defaultMessages = { scrollToLatest: "Scroll to the latest message" };
|
|
8
10
|
/** How close to the end (px) still counts as "at the latest message". */
|
|
9
11
|
const STICK_PX = 32;
|
|
10
12
|
const ConversationContext = React.createContext(null);
|
|
@@ -112,6 +114,12 @@ export function Conversation({ className, style, children, ref, onScroll, ...pro
|
|
|
112
114
|
// still in reach (text growing below fires no scroll event to say so). With nothing left to
|
|
113
115
|
// scroll, there is nothing to be away from: it follows again, for the next reply.
|
|
114
116
|
const changed = () => {
|
|
117
|
+
// Where layout left the view is not the reader's doing: when content shrinks and clamps the
|
|
118
|
+
// view, and grows back before the browser fires the scroll event (a slow frame in Firefox),
|
|
119
|
+
// that event must not read as a move up from where the view, or the glide, was before.
|
|
120
|
+
lastTop.current = el.scrollTop;
|
|
121
|
+
lastMax.current = el.scrollHeight - el.clientHeight;
|
|
122
|
+
glideTop.current = Number.NaN;
|
|
115
123
|
if (!stuck.current && el.scrollHeight - el.clientHeight <= 0.5)
|
|
116
124
|
setStuck(true);
|
|
117
125
|
if (stuck.current)
|
|
@@ -142,7 +150,8 @@ export function Conversation({ className, style, children, ref, onScroll, ...pro
|
|
|
142
150
|
/** Something inside the log (a message's own scroller) that this move scrolls instead. */
|
|
143
151
|
const inner = (target, up) => {
|
|
144
152
|
for (let node = target; node && node !== el; node = node.parentElement) {
|
|
145
|
-
if (!/(auto|scroll)/.test(getComputedStyle(node).overflowY) ||
|
|
153
|
+
if (!/(auto|scroll)/.test(getComputedStyle(node).overflowY) ||
|
|
154
|
+
node.scrollHeight <= node.clientHeight + 1)
|
|
146
155
|
continue;
|
|
147
156
|
if (up ? node.scrollTop > 0.5 : node.scrollTop + node.clientHeight < node.scrollHeight - 0.5)
|
|
148
157
|
return true;
|
|
@@ -262,7 +271,7 @@ export function Conversation({ className, style, children, ref, onScroll, ...pro
|
|
|
262
271
|
// Its corners are the conversation's: round that (`rounded-t-card` in a card), and the
|
|
263
272
|
// messages, the scrollbar and the focus ring follow, with no `overflow-hidden` to clip them.
|
|
264
273
|
// Its own scrolling is instant, whatever an app sets for `scroll-behavior`: the glide does the easing.
|
|
265
|
-
className: "min-h-0 flex-1 [border-radius:inherit] [--scrollbar-inset:var(--radius-card)]
|
|
274
|
+
className: "min-h-0 flex-1 overflow-y-auto overscroll-contain [scroll-behavior:auto] [border-radius:inherit] focus-ring [--scrollbar-inset:var(--radius-card)]", children: parts.filter((child) => !isButton(child)) }), parts.filter(isButton)] }) }));
|
|
266
275
|
}
|
|
267
276
|
/** The column of messages: 24px apart, padded, at a reading width. */
|
|
268
277
|
export function ConversationContent({ className, ...props }) {
|
|
@@ -273,12 +282,13 @@ export function ConversationContent({ className, ...props }) {
|
|
|
273
282
|
* latest message. It appears only once you have scrolled away from it. Put it inside
|
|
274
283
|
* `Conversation`, after `ConversationContent`.
|
|
275
284
|
*/
|
|
276
|
-
export function ConversationScrollButton({ label
|
|
285
|
+
export function ConversationScrollButton({ label, className, onClick, ...props }) {
|
|
286
|
+
const text = useMessages("conversation", defaultMessages);
|
|
277
287
|
const context = React.useContext(ConversationContext);
|
|
278
288
|
if (!context)
|
|
279
289
|
throw new Error("ConversationScrollButton must be used inside <Conversation>.");
|
|
280
290
|
const visible = !context.atBottom;
|
|
281
|
-
return (_jsx(Button, { "data-slot": "conversation-scroll-button", "data-visible": visible ? "" : undefined, variant: "secondary", size: "icon", "aria-label": label, "aria-hidden": !visible, tabIndex: visible ? 0 : -1, onClick: (event) => {
|
|
291
|
+
return (_jsx(Button, { "data-slot": "conversation-scroll-button", "data-visible": visible ? "" : undefined, variant: "secondary", size: "icon", "aria-label": label ?? text.scrollToLatest, "aria-hidden": !visible, tabIndex: visible ? 0 : -1, onClick: (event) => {
|
|
282
292
|
onClick?.(event);
|
|
283
293
|
if (event.defaultPrevented)
|
|
284
294
|
return;
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
2
|
import { type ButtonProps } from "./button.js";
|
|
3
|
+
export interface CopyButtonMessages {
|
|
4
|
+
/** The name at rest. Say what is copied in `labels`: "Copy API key". */
|
|
5
|
+
copy: string;
|
|
6
|
+
/** Announced, and shown on a labelled button, once the text is on the clipboard. */
|
|
7
|
+
copied: string;
|
|
8
|
+
/** Announced when the clipboard refused the text. */
|
|
9
|
+
failed: string;
|
|
10
|
+
}
|
|
11
|
+
declare module "./locale" {
|
|
12
|
+
interface Messages {
|
|
13
|
+
copyButton: CopyButtonMessages;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
3
16
|
export interface CopyButtonProps extends Omit<ButtonProps, "value" | "onCopy" | "render" | "onError"> {
|
|
4
17
|
/**
|
|
5
18
|
* What to copy: a string, or a function that returns it (or a promise of it) at the moment of
|
|
@@ -18,13 +31,9 @@ export interface CopyButtonProps extends Omit<ButtonProps, "value" | "onCopy" |
|
|
|
18
31
|
/**
|
|
19
32
|
* The accessible names: `copy` names the button at rest (default "Copy"), `copied` and `failed`
|
|
20
33
|
* are announced after a click (default "Copied" and "Couldn't copy"). Say what is copied:
|
|
21
|
-
* "Copy API key".
|
|
34
|
+
* "Copy API key". A LocaleProvider sets them for every CopyButton (`messages.copyButton`).
|
|
22
35
|
*/
|
|
23
|
-
labels?:
|
|
24
|
-
copy?: string;
|
|
25
|
-
copied?: string;
|
|
26
|
-
failed?: string;
|
|
27
|
-
};
|
|
36
|
+
labels?: Partial<CopyButtonMessages>;
|
|
28
37
|
/**
|
|
29
38
|
* Draw another button instead of `Button` — most often `<InputGroupButton size="icon" />` in
|
|
30
39
|
* an `InputGroup`. It receives the click, the accessible name and the icon; its own variant
|
|
@@ -4,6 +4,12 @@ import { CheckIcon, CopyIcon, XIcon } from "lucide-react";
|
|
|
4
4
|
import * as React from "react";
|
|
5
5
|
import { cn } from "../lib/cn.js";
|
|
6
6
|
import { Button } from "./button.js";
|
|
7
|
+
import { useMessages } from "./locale.js";
|
|
8
|
+
const defaultMessages = {
|
|
9
|
+
copy: "Copy",
|
|
10
|
+
copied: "Copied",
|
|
11
|
+
failed: "Couldn't copy",
|
|
12
|
+
};
|
|
7
13
|
/**
|
|
8
14
|
* Writes text to the clipboard: the async Clipboard API where the page may use it, and the old
|
|
9
15
|
* `execCommand("copy")` on a hidden textarea where it may not (an `http:` page, an iframe without
|
|
@@ -71,9 +77,16 @@ export function CopyButton({ value, onCopy, onError, timeout = 1500, labels, ren
|
|
|
71
77
|
timer.current = setTimeout(() => setState("idle"), timeout);
|
|
72
78
|
};
|
|
73
79
|
const copy = async (event) => {
|
|
80
|
+
// The rendered element's own click handler runs too, then the one passed to CopyButton.
|
|
81
|
+
render?.props.onClick?.(event);
|
|
74
82
|
onClick?.(event);
|
|
75
83
|
if (event.defaultPrevented)
|
|
76
84
|
return;
|
|
85
|
+
// A second click while "Copied" still shows empties the live region first, so it is announced again.
|
|
86
|
+
if (state !== "idle") {
|
|
87
|
+
clearTimeout(timer.current);
|
|
88
|
+
setState("idle");
|
|
89
|
+
}
|
|
77
90
|
let text;
|
|
78
91
|
try {
|
|
79
92
|
const next = typeof value === "function" ? value() : value;
|
|
@@ -89,9 +102,10 @@ export function CopyButton({ value, onCopy, onError, timeout = 1500, labels, ren
|
|
|
89
102
|
settle("copied");
|
|
90
103
|
onCopy?.(text);
|
|
91
104
|
};
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
const
|
|
105
|
+
const text = useMessages("copyButton", defaultMessages, labels);
|
|
106
|
+
const copyLabel = text.copy;
|
|
107
|
+
const copiedLabel = text.copied;
|
|
108
|
+
const failedLabel = text.failed;
|
|
95
109
|
const labelled = children != null;
|
|
96
110
|
const announcement = state === "copied" ? copiedLabel : state === "failed" ? failedLabel : "";
|
|
97
111
|
const buttonSize = size ?? (labelled ? "sm" : "icon-sm");
|
|
@@ -102,8 +116,9 @@ export function CopyButton({ value, onCopy, onError, timeout = 1500, labels, ren
|
|
|
102
116
|
const shared = {
|
|
103
117
|
"data-copied": state === "copied" ? "" : undefined,
|
|
104
118
|
"data-copy-failed": state === "failed" ? "" : undefined,
|
|
105
|
-
// A labelled button is named by its visible label; an icon-only one by `labels`.
|
|
106
|
-
"
|
|
119
|
+
// A labelled button is named by its visible label; an icon-only one by `labels`. The name stays
|
|
120
|
+
// "Copy": the live region says "Copied", and a name that changed as well would be read twice.
|
|
121
|
+
"aria-label": labelled ? undefined : copyLabel,
|
|
107
122
|
onClick: copy,
|
|
108
123
|
};
|
|
109
124
|
return (_jsxs(_Fragment, { children: [render ? (
|
|
@@ -1,5 +1,37 @@
|
|
|
1
1
|
import { type CellContext, type ColumnDef, type Row, type RowData, type SortingState } from "@tanstack/react-table";
|
|
2
2
|
import * as React from "react";
|
|
3
|
+
export interface DataTableMessages {
|
|
4
|
+
/** The search field's placeholder; its name is the same without the "…". */
|
|
5
|
+
search: string;
|
|
6
|
+
/** The column menu's button, and the heading in it. */
|
|
7
|
+
columns: string;
|
|
8
|
+
showColumns: string;
|
|
9
|
+
/** The select-all checkbox, where every row can be selected — or only this page's. */
|
|
10
|
+
selectAll: string;
|
|
11
|
+
selectPage: string;
|
|
12
|
+
/** Each row's checkbox. */
|
|
13
|
+
selectRow: string;
|
|
14
|
+
/** The one row shown when nothing matches. */
|
|
15
|
+
empty: string;
|
|
16
|
+
/** When `fetchRows` fails, and the button that asks again. */
|
|
17
|
+
loadError: string;
|
|
18
|
+
retry: string;
|
|
19
|
+
/** The footer's count of selected rows. */
|
|
20
|
+
selected: (count: number, total: number) => string;
|
|
21
|
+
/** The footer of a grouped table. */
|
|
22
|
+
groupCount: (rows: number, groups: number) => string;
|
|
23
|
+
/** The rows-per-page select's label. */
|
|
24
|
+
rowsPerPage: string;
|
|
25
|
+
/** The rows on this page: "1–10 of 42". */
|
|
26
|
+
range: (first: number, last: number, total: number) => string;
|
|
27
|
+
previous: string;
|
|
28
|
+
next: string;
|
|
29
|
+
}
|
|
30
|
+
declare module "./locale" {
|
|
31
|
+
interface Messages {
|
|
32
|
+
dataTable: DataTableMessages;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
3
35
|
/** Per-column options read by `DataTable`. Set them on a column's `meta`. */
|
|
4
36
|
export interface DataTableColumnMeta {
|
|
5
37
|
/** The column's name in the "Columns" menu when its `header` is not a plain string. */
|
|
@@ -56,6 +88,24 @@ export type DataTableRow<TData extends RowData> = Row<DataTableFeatures, TData>;
|
|
|
56
88
|
/** The argument of a column's `cell` renderer. */
|
|
57
89
|
export type DataTableCellContext<TData extends RowData, TValue = unknown> = CellContext<DataTableFeatures, TData, TValue>;
|
|
58
90
|
export type DataTableSortingState = SortingState;
|
|
91
|
+
/** What `fetchRows` is asked for: rows `start` up to `end` of the result, sorted and searched. */
|
|
92
|
+
export interface DataTableQuery {
|
|
93
|
+
start: number;
|
|
94
|
+
end: number;
|
|
95
|
+
/** The columns to sort by, in order: `[{ id, desc }]`. */
|
|
96
|
+
sorting: SortingState;
|
|
97
|
+
/** The search text, or "" — over every column, or `searchKey`'s. */
|
|
98
|
+
search: string;
|
|
99
|
+
/** The column `search` is for, when the table has a `searchKey`. */
|
|
100
|
+
searchKey?: string;
|
|
101
|
+
/** Aborted when the rows are no longer wanted: the sorting or the search changed. */
|
|
102
|
+
signal: AbortSignal;
|
|
103
|
+
}
|
|
104
|
+
/** What `fetchRows` resolves to: the rows asked for, and how many the whole result has. */
|
|
105
|
+
export interface DataTableResult<TData> {
|
|
106
|
+
rows: TData[];
|
|
107
|
+
total: number;
|
|
108
|
+
}
|
|
59
109
|
/**
|
|
60
110
|
* Typed helpers for `DataTable` columns: `accessor(key, def)`, `display(def)` and
|
|
61
111
|
* `columns([...])`. Create one per row type at module scope.
|
|
@@ -89,11 +139,18 @@ export declare function createDataTableColumnHelper<TData extends RowData>(): im
|
|
|
89
139
|
export interface DataTableProps<TData extends RowData> extends Omit<React.ComponentProps<"div">, "children" | "onChange"> {
|
|
90
140
|
/** Column definitions. Keep them stable: module scope or `useMemo`. */
|
|
91
141
|
columns: ReadonlyArray<DataTableColumnDef<TData>>;
|
|
92
|
-
/** The rows. Keep the array stable between real changes. */
|
|
93
|
-
data
|
|
142
|
+
/** The rows. Keep the array stable between real changes. Not with `fetchRows`. */
|
|
143
|
+
data?: ReadonlyArray<TData>;
|
|
144
|
+
/**
|
|
145
|
+
* Rows from a server, in place of `data`: called for each run of rows as it comes into view (or
|
|
146
|
+
* its page), with the sorting and the search, and resolving to those rows and the total. Rows
|
|
147
|
+
* on their way show as placeholders. Pass `getRowId`, so a selection survives a new sort. Not
|
|
148
|
+
* with `groupBy`.
|
|
149
|
+
*/
|
|
150
|
+
fetchRows?: (query: DataTableQuery) => Promise<DataTableResult<TData>>;
|
|
94
151
|
/** Search one column (its id or accessor key). Omit to search every text and number column. */
|
|
95
152
|
searchKey?: string;
|
|
96
|
-
/** Placeholder for the search field. */
|
|
153
|
+
/** Placeholder for the search field. Default `labels.search`. */
|
|
97
154
|
searchPlaceholder?: string;
|
|
98
155
|
/** Show the search field. Defaults to `true`. */
|
|
99
156
|
searchable?: boolean;
|
|
@@ -125,13 +182,16 @@ export interface DataTableProps<TData extends RowData> extends Omit<React.Compon
|
|
|
125
182
|
initialSorting?: SortingState;
|
|
126
183
|
/**
|
|
127
184
|
* A BCP 47 locale whose order text sorts in, e.g. `"sv"` puts "Å" after "Z". Default: the
|
|
128
|
-
* Unicode collation order, the same on the server and in every
|
|
185
|
+
* LocaleProvider's, else the Unicode collation order, the same on the server and in every
|
|
186
|
+
* browser.
|
|
129
187
|
*/
|
|
130
188
|
locale?: string;
|
|
131
189
|
/** Columns hidden at first, e.g. `{ id: false }`. */
|
|
132
190
|
initialColumnVisibility?: Record<string, boolean>;
|
|
133
|
-
/** Shown in one full-width row when nothing matches. */
|
|
191
|
+
/** Shown in one full-width row when nothing matches. Default `labels.empty`. */
|
|
134
192
|
emptyMessage?: React.ReactNode;
|
|
193
|
+
/** The table's own words — toolbar, selection, footer; a LocaleProvider sets them for every DataTable. */
|
|
194
|
+
labels?: Partial<DataTableMessages>;
|
|
135
195
|
/** Extra controls at the end of the toolbar (a secondary "Export", a filter select). */
|
|
136
196
|
toolbar?: React.ReactNode;
|
|
137
197
|
/** A short visible description under the table. */
|
|
@@ -172,4 +232,4 @@ export interface DataTableProps<TData extends RowData> extends Omit<React.Compon
|
|
|
172
232
|
* break: reaching for it for a handful of rows. Under ~10 rows with nothing to sort, use a
|
|
173
233
|
* plain `Table`.
|
|
174
234
|
*/
|
|
175
|
-
export declare function DataTable<TData extends RowData>({ columns, data, searchKey, searchPlaceholder, searchable, pageSize, pageSizeOptions, selectable, onRowSelectionChange, getRowId, columnToggle, dense, variant, fit, initialSorting, locale, initialColumnVisibility, emptyMessage, toolbar, caption, label, groupBy, groupSummary, stickyGroupHeaders, tableContainerProps, virtualize, className, ...props }: DataTableProps<TData>): React.JSX.Element;
|
|
235
|
+
export declare function DataTable<TData extends RowData>({ columns, data, fetchRows, searchKey, searchPlaceholder: searchPlaceholderProp, searchable, pageSize, pageSizeOptions, selectable, onRowSelectionChange, getRowId, columnToggle, dense, variant, fit, initialSorting, locale: localeProp, initialColumnVisibility, emptyMessage: emptyMessageProp, labels, toolbar, caption, label, groupBy, groupSummary, stickyGroupHeaders, tableContainerProps, virtualize, className, ...props }: DataTableProps<TData>): React.JSX.Element;
|