@dinachi/cli 0.12.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +117 -0
- package/dist/index.js +73 -7
- package/package.json +1 -1
- package/templates/accordion/accordion.tsx +66 -12
- package/templates/agent/agent.tsx +1865 -0
- package/templates/agent/index.ts +52 -0
- package/templates/alert-dialog/alert-dialog.tsx +46 -17
- package/templates/alert-dialog/index.ts +2 -0
- package/templates/animated-tabs/animated-tabs.tsx +65 -5
- package/templates/autocomplete/autocomplete.tsx +174 -18
- package/templates/carousel/carousel.tsx +102 -24
- package/templates/checkbox/checkbox.tsx +60 -4
- package/templates/checkbox-group/checkbox-group.tsx +82 -8
- package/templates/collapsible/collapsible.tsx +13 -4
- package/templates/combobox/combobox.tsx +297 -23
- package/templates/compare-slider/compare-slider.tsx +103 -28
- package/templates/compare-slider/index.ts +1 -1
- package/templates/context-menu/context-menu.tsx +37 -15
- package/templates/dialog/dialog.tsx +41 -12
- package/templates/drawer/drawer.tsx +18 -3
- package/templates/expandable-card/expandable-card.tsx +65 -42
- package/templates/field/field.tsx +2 -0
- package/templates/hold-to-confirm/hold-to-confirm.tsx +99 -16
- package/templates/hold-to-confirm/index.ts +1 -1
- package/templates/input/input.tsx +59 -1
- package/templates/menu/menu.tsx +26 -9
- package/templates/menubar/menubar.tsx +26 -8
- package/templates/navigation-menu/navigation-menu.tsx +67 -23
- package/templates/number-field/index.ts +1 -0
- package/templates/number-field/number-field.tsx +71 -2
- package/templates/otp-field/otp-field.tsx +57 -11
- package/templates/popover/popover.tsx +18 -3
- package/templates/preview-card/preview-card.tsx +16 -2
- package/templates/radio/radio.tsx +71 -6
- package/templates/select/select.tsx +223 -10
- package/templates/slider/slider.tsx +77 -8
- package/templates/sortable/sortable.tsx +76 -9
- package/templates/swipeable-row/index.ts +1 -1
- package/templates/swipeable-row/swipeable-row.tsx +125 -41
- package/templates/switch/switch.tsx +41 -4
- package/templates/tabs/tabs.tsx +74 -10
- package/templates/textarea/index.ts +1 -1
- package/templates/textarea/textarea.tsx +70 -4
- package/templates/toast/toast.tsx +3 -1
- package/templates/toggle/toggle.tsx +39 -7
- package/templates/toggle-group/toggle-group.tsx +96 -18
|
@@ -4,9 +4,105 @@ import * as React from "react"
|
|
|
4
4
|
import { Select as SelectPrimitive } from "@base-ui/react/select"
|
|
5
5
|
import { cn } from "@/lib/utils"
|
|
6
6
|
import { Check, ChevronDown } from "lucide-react"
|
|
7
|
+
import {
|
|
8
|
+
agentNodeText,
|
|
9
|
+
changeAsAgent,
|
|
10
|
+
matchAgentChoice,
|
|
11
|
+
sameAgentValue,
|
|
12
|
+
useAgentControl,
|
|
13
|
+
useAgentState,
|
|
14
|
+
useHasAgent,
|
|
15
|
+
useMergedRef,
|
|
16
|
+
waitForAgent,
|
|
17
|
+
} from "@/components/ui/agent"
|
|
18
|
+
|
|
19
|
+
interface SelectAgent {
|
|
20
|
+
trigger: React.MutableRefObject<HTMLElement | null>
|
|
21
|
+
/** Items in the open popup. */
|
|
22
|
+
items: Map<HTMLElement, unknown>
|
|
23
|
+
/** The same items, rendered hidden while the popup is closed so the agent can see them. */
|
|
24
|
+
catalog: Map<HTMLElement, unknown>
|
|
25
|
+
labels: Map<unknown, string>
|
|
26
|
+
value: unknown
|
|
27
|
+
multiple: boolean
|
|
28
|
+
open: boolean
|
|
29
|
+
disabled: boolean
|
|
30
|
+
setOpen: (open: boolean) => void
|
|
31
|
+
change: (value: unknown) => string | void
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const SelectAgentContext = React.createContext<SelectAgent | null>(null)
|
|
35
|
+
const SelectCatalogContext = React.createContext(false)
|
|
36
|
+
|
|
37
|
+
function itemLabels(items: SelectPrimitive.Root.Props<unknown>["items"]): Map<unknown, string> {
|
|
38
|
+
const labels = new Map<unknown, string>()
|
|
39
|
+
if (!items) return labels
|
|
40
|
+
const entries = Array.isArray(items)
|
|
41
|
+
? (items as ReadonlyArray<{ value: unknown; label: React.ReactNode }>).map((i) => [i.value, i.label] as const)
|
|
42
|
+
: Object.entries(items as Record<string, React.ReactNode>)
|
|
43
|
+
for (const [value, label] of entries) labels.set(value, agentNodeText(label))
|
|
44
|
+
return labels
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Base UI's root, holding its own value and open state so an agent can set them the way
|
|
49
|
+
* a parent would. Controlled use is unchanged: the props win and every change, the
|
|
50
|
+
* agent's included, goes through `onValueChange` and `onOpenChange` first.
|
|
51
|
+
*/
|
|
52
|
+
function Select<Value, Multiple extends boolean | undefined = false>({
|
|
53
|
+
value: valueProp,
|
|
54
|
+
defaultValue,
|
|
55
|
+
onValueChange,
|
|
56
|
+
open: openProp,
|
|
57
|
+
defaultOpen = false,
|
|
58
|
+
onOpenChange,
|
|
59
|
+
...props
|
|
60
|
+
}: SelectPrimitive.Root.Props<Value, Multiple>) {
|
|
61
|
+
type Props = SelectPrimitive.Root.Props<Value, Multiple>
|
|
62
|
+
type ValueChange = NonNullable<Props["onValueChange"]>
|
|
63
|
+
type OpenChange = NonNullable<Props["onOpenChange"]>
|
|
64
|
+
type Details = Parameters<ValueChange>[1]
|
|
65
|
+
type OpenDetails = Parameters<OpenChange>[1]
|
|
66
|
+
type Current = Parameters<ValueChange>[0]
|
|
67
|
+
|
|
68
|
+
const [value, changeValue] = useAgentState<Current, Details>(
|
|
69
|
+
valueProp as Current | undefined,
|
|
70
|
+
(defaultValue ?? (props.multiple ? [] : null)) as Current,
|
|
71
|
+
onValueChange
|
|
72
|
+
)
|
|
73
|
+
const [open, changeOpen] = useAgentState<boolean, OpenDetails>(openProp, defaultOpen, onOpenChange)
|
|
74
|
+
|
|
75
|
+
const trigger = React.useRef<HTMLElement | null>(null)
|
|
76
|
+
const [items] = React.useState(() => new Map<HTMLElement, unknown>())
|
|
77
|
+
const [catalog] = React.useState(() => new Map<HTMLElement, unknown>())
|
|
78
|
+
|
|
79
|
+
const agent: SelectAgent = {
|
|
80
|
+
trigger,
|
|
81
|
+
items,
|
|
82
|
+
catalog,
|
|
83
|
+
labels: itemLabels(props.items as Props["items"]),
|
|
84
|
+
value,
|
|
85
|
+
multiple: Boolean(props.multiple),
|
|
86
|
+
open,
|
|
87
|
+
disabled: Boolean(props.disabled),
|
|
88
|
+
setOpen: (next) => changeAsAgent(changeOpen, next),
|
|
89
|
+
// A person cannot choose a different option either, though they can open it to look.
|
|
90
|
+
change: (next) => (props.readOnly ? "The select is read-only." : changeAsAgent(changeValue, next as Current)),
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<SelectAgentContext.Provider value={useHasAgent() ? agent : null}>
|
|
95
|
+
<SelectPrimitive.Root<Value, Multiple>
|
|
96
|
+
{...props}
|
|
97
|
+
value={value as Props["value"]}
|
|
98
|
+
onValueChange={changeValue}
|
|
99
|
+
open={open}
|
|
100
|
+
onOpenChange={changeOpen}
|
|
101
|
+
/>
|
|
102
|
+
</SelectAgentContext.Provider>
|
|
103
|
+
)
|
|
104
|
+
}
|
|
7
105
|
|
|
8
|
-
// Direct re-exports
|
|
9
|
-
const Select = SelectPrimitive.Root
|
|
10
106
|
const SelectGroup = SelectPrimitive.Group
|
|
11
107
|
const SelectValue = SelectPrimitive.Value
|
|
12
108
|
|
|
@@ -15,9 +111,82 @@ const SelectTrigger = React.forwardRef<
|
|
|
15
111
|
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger> & {
|
|
16
112
|
readonly children?: React.ReactNode
|
|
17
113
|
}
|
|
18
|
-
>(({ className, children, ...props }, ref) =>
|
|
114
|
+
>(({ className, children, ...props }, ref) => {
|
|
115
|
+
const select = React.useContext(SelectAgentContext)
|
|
116
|
+
const mergedRef = useMergedRef(ref, select?.trigger)
|
|
117
|
+
|
|
118
|
+
useAgentControl(() => {
|
|
119
|
+
if (!select) return null
|
|
120
|
+
const element = select.trigger.current
|
|
121
|
+
const choices = () => {
|
|
122
|
+
const read = (map: Map<HTMLElement, unknown>) =>
|
|
123
|
+
Array.from(map, ([el, value]) => ({
|
|
124
|
+
value,
|
|
125
|
+
label: agentNodeText(el.textContent),
|
|
126
|
+
disabled: el.hasAttribute("data-disabled"),
|
|
127
|
+
}))
|
|
128
|
+
const live = read(select.items)
|
|
129
|
+
if (live.length) return live
|
|
130
|
+
const hidden = read(select.catalog)
|
|
131
|
+
if (hidden.length) return hidden
|
|
132
|
+
return Array.from(select.labels, ([value, label]) => ({ value, label, disabled: false }))
|
|
133
|
+
}
|
|
134
|
+
const labelOf = (value: unknown) =>
|
|
135
|
+
choices().find((c) => sameAgentValue(c.value, value))?.label ?? (value == null ? "" : String(value))
|
|
136
|
+
const find = (wanted: unknown) => matchAgentChoice(choices(), wanted)
|
|
137
|
+
// Waits for the popup when nothing has listed the choices yet, outside the catalog.
|
|
138
|
+
const ready = async () => {
|
|
139
|
+
if (choices().length) return
|
|
140
|
+
select.setOpen(true)
|
|
141
|
+
await waitForAgent(() => (choices().length ? true : null))
|
|
142
|
+
}
|
|
143
|
+
const current: unknown[] = Array.isArray(select.value) ? select.value : []
|
|
144
|
+
const items = choices().map((c) => c.label)
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
kind: "select",
|
|
148
|
+
element,
|
|
149
|
+
value: select.multiple ? current.map(labelOf) : labelOf(select.value),
|
|
150
|
+
open: select.open,
|
|
151
|
+
disabled: select.disabled,
|
|
152
|
+
...(select.multiple ? { multiple: true } : {}),
|
|
153
|
+
...(items.length ? { items } : {}),
|
|
154
|
+
actions: {
|
|
155
|
+
select: async (wanted: string) => {
|
|
156
|
+
await ready()
|
|
157
|
+
const match = find(wanted)
|
|
158
|
+
if (typeof match === "string") return match
|
|
159
|
+
if (!select.multiple) {
|
|
160
|
+
const refused = select.change(match.value)
|
|
161
|
+
select.setOpen(false)
|
|
162
|
+
return refused
|
|
163
|
+
}
|
|
164
|
+
if (current.some((v) => sameAgentValue(v, match.value))) return
|
|
165
|
+
return select.change([...current, match.value])
|
|
166
|
+
},
|
|
167
|
+
...(select.multiple
|
|
168
|
+
? {
|
|
169
|
+
setValue: async (wanted: unknown) => {
|
|
170
|
+
await ready()
|
|
171
|
+
const list = Array.isArray(wanted) ? wanted : [wanted]
|
|
172
|
+
const next: unknown[] = []
|
|
173
|
+
for (const one of list) {
|
|
174
|
+
const match = find(one)
|
|
175
|
+
if (typeof match === "string") return match
|
|
176
|
+
next.push(match.value)
|
|
177
|
+
}
|
|
178
|
+
return select.change(next)
|
|
179
|
+
},
|
|
180
|
+
}
|
|
181
|
+
: {}),
|
|
182
|
+
[select.open ? "close" : "open"]: () => select.setOpen(!select.open),
|
|
183
|
+
},
|
|
184
|
+
}
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
return (
|
|
19
188
|
<SelectPrimitive.Trigger
|
|
20
|
-
ref={
|
|
189
|
+
ref={mergedRef}
|
|
21
190
|
className={cn(
|
|
22
191
|
"flex h-10 w-full items-center justify-between rounded-md border border-input bg-transparent px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
|
|
23
192
|
className
|
|
@@ -29,7 +198,8 @@ const SelectTrigger = React.forwardRef<
|
|
|
29
198
|
<ChevronDown className="h-4 w-4 opacity-50" />
|
|
30
199
|
</SelectPrimitive.Icon>
|
|
31
200
|
</SelectPrimitive.Trigger>
|
|
32
|
-
)
|
|
201
|
+
)
|
|
202
|
+
})
|
|
33
203
|
SelectTrigger.displayName = "SelectTrigger"
|
|
34
204
|
|
|
35
205
|
const SelectContent = React.forwardRef<
|
|
@@ -82,9 +252,26 @@ const SelectContent = React.forwardRef<
|
|
|
82
252
|
</SelectPrimitive.Positioner>
|
|
83
253
|
)
|
|
84
254
|
|
|
85
|
-
|
|
255
|
+
const select = React.useContext(SelectAgentContext)
|
|
256
|
+
// Base UI unmounts a closed popup, which would leave the agent nothing to choose from.
|
|
257
|
+
// Inside AgentProvider the items also render hidden while it is closed.
|
|
258
|
+
const catalog =
|
|
259
|
+
select && !select.open ? (
|
|
260
|
+
<SelectCatalogContext.Provider value>
|
|
261
|
+
<div hidden data-agent-hidden>
|
|
262
|
+
{children}
|
|
263
|
+
</div>
|
|
264
|
+
</SelectCatalogContext.Provider>
|
|
265
|
+
) : null
|
|
86
266
|
|
|
87
|
-
|
|
267
|
+
const popup = portal ? <SelectPrimitive.Portal>{content}</SelectPrimitive.Portal> : content
|
|
268
|
+
if (!catalog) return popup
|
|
269
|
+
return (
|
|
270
|
+
<>
|
|
271
|
+
{popup}
|
|
272
|
+
{catalog}
|
|
273
|
+
</>
|
|
274
|
+
)
|
|
88
275
|
})
|
|
89
276
|
SelectContent.displayName = "SelectContent"
|
|
90
277
|
|
|
@@ -163,9 +350,34 @@ const SelectItem = React.forwardRef<
|
|
|
163
350
|
React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item> & {
|
|
164
351
|
readonly showIndicator?: boolean
|
|
165
352
|
}
|
|
166
|
-
>(({ className, children, showIndicator = false, ...props }, ref) =>
|
|
353
|
+
>(({ className, children, showIndicator = false, ...props }, ref) => {
|
|
354
|
+
const select = React.useContext(SelectAgentContext)
|
|
355
|
+
const inCatalog = React.useContext(SelectCatalogContext)
|
|
356
|
+
const element = React.useRef<HTMLDivElement | null>(null)
|
|
357
|
+
const mergedRef = useMergedRef(ref, element)
|
|
358
|
+
const { value } = props
|
|
359
|
+
const registry = select ? (inCatalog ? select.catalog : select.items) : null
|
|
360
|
+
|
|
361
|
+
React.useEffect(() => {
|
|
362
|
+
const el = element.current
|
|
363
|
+
if (!registry || !el) return
|
|
364
|
+
registry.set(el, value)
|
|
365
|
+
return () => {
|
|
366
|
+
registry.delete(el)
|
|
367
|
+
}
|
|
368
|
+
}, [registry, value])
|
|
369
|
+
|
|
370
|
+
if (inCatalog) {
|
|
371
|
+
return (
|
|
372
|
+
<div ref={element} data-disabled={props.disabled ? "" : undefined}>
|
|
373
|
+
{children}
|
|
374
|
+
</div>
|
|
375
|
+
)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return (
|
|
167
379
|
<SelectPrimitive.Item
|
|
168
|
-
ref={
|
|
380
|
+
ref={mergedRef}
|
|
169
381
|
className={cn(
|
|
170
382
|
"relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 text-sm outline-none",
|
|
171
383
|
"data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
|
|
@@ -183,7 +395,8 @@ const SelectItem = React.forwardRef<
|
|
|
183
395
|
)}
|
|
184
396
|
<SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
|
|
185
397
|
</SelectPrimitive.Item>
|
|
186
|
-
)
|
|
398
|
+
)
|
|
399
|
+
})
|
|
187
400
|
SelectItem.displayName = "SelectItem"
|
|
188
401
|
|
|
189
402
|
const SelectItemIndicator = React.forwardRef<
|
|
@@ -2,19 +2,87 @@ import * as React from "react";
|
|
|
2
2
|
import { Slider as BaseSlider } from "@base-ui/react/slider";
|
|
3
3
|
import { DirectionProvider } from "@base-ui/react/direction-provider";
|
|
4
4
|
import { cn } from "@/lib/utils";
|
|
5
|
+
import { agentRound, changeAsAgent, createAgentChangeDetails, useAgentControl, useAgentState, useHasAgent, useMergedRef } from "@/components/ui/agent";
|
|
5
6
|
|
|
7
|
+
type SliderNumber = number | readonly number[];
|
|
8
|
+
|
|
9
|
+
/** One number, snapped to the step from min, or what is wrong with it. */
|
|
10
|
+
function snapped(item: unknown, min: number, max: number, step: number): number | string {
|
|
11
|
+
const n = Number(item);
|
|
12
|
+
if (item === "" || item === null || !Number.isFinite(n)) return `"${String(item)}" is not a number.`;
|
|
13
|
+
if (n < min || n > max) return `${n} is out of range: ${min} to ${max}.`;
|
|
14
|
+
return Math.min(max, agentRound(min + Math.round((n - min) / step) * step, min, step));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Snaps to the step and checks the bounds and shape, or says what is wrong. */
|
|
18
|
+
function sliderValue(wanted: unknown, current: SliderNumber, min: number, max: number, step: number): SliderNumber | string {
|
|
19
|
+
const count = Array.isArray(current) ? current.length : 0;
|
|
20
|
+
const list: unknown[] = Array.isArray(wanted) ? wanted : [wanted];
|
|
21
|
+
if (list.length !== Math.max(count, 1)) return count ? `Send ${count} numbers, low to high.` : "Send one number.";
|
|
22
|
+
const next = list.map((item) => snapped(item, min, max, step));
|
|
23
|
+
const wrong = next.find((n) => typeof n === "string");
|
|
24
|
+
if (wrong !== undefined) return wrong;
|
|
25
|
+
if (next.some((n, i) => i > 0 && n < next[i - 1])) return "Values must go from low to high.";
|
|
26
|
+
return count ? (next as number[]) : (next[0] as number);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Base UI's root, holding its own value so an agent can set it the way a parent would.
|
|
31
|
+
* Controlled use is unchanged, and `onValueChange` sees the agent's change first and can
|
|
32
|
+
* cancel it.
|
|
33
|
+
*/
|
|
6
34
|
const Slider = React.forwardRef<
|
|
7
35
|
React.ComponentRef<typeof BaseSlider.Root>,
|
|
8
36
|
React.ComponentProps<typeof BaseSlider.Root>
|
|
9
|
-
>(({ className, ...props }, ref) =>
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
))
|
|
37
|
+
>(({ className, value: valueProp, defaultValue, onValueChange, ...props }, ref) => {
|
|
38
|
+
type Details = BaseSlider.Root.ChangeEventDetails;
|
|
39
|
+
const { min = 0, max = 100, step = 1 } = props;
|
|
40
|
+
const [value, changeValue] = useAgentState<SliderNumber, Details>(
|
|
41
|
+
valueProp,
|
|
42
|
+
defaultValue ?? min,
|
|
43
|
+
onValueChange as ((value: SliderNumber, details: Details) => void) | undefined
|
|
44
|
+
);
|
|
45
|
+
const element = React.useRef<HTMLDivElement>(null);
|
|
46
|
+
|
|
47
|
+
useAgentControl(() => {
|
|
48
|
+
const root = element.current;
|
|
49
|
+
// Unnamed, the slider takes the name a thumb was given.
|
|
50
|
+
const named = root?.matches("[aria-label], [aria-labelledby], [data-agent-label]");
|
|
51
|
+
return {
|
|
52
|
+
kind: "slider",
|
|
53
|
+
element: root,
|
|
54
|
+
label: named ? undefined : root?.querySelector("input")?.getAttribute("aria-label") ?? undefined,
|
|
55
|
+
value: Array.isArray(value) ? [...value] : (value as number),
|
|
56
|
+
min,
|
|
57
|
+
max,
|
|
58
|
+
step,
|
|
59
|
+
disabled: props.disabled,
|
|
60
|
+
actions: {
|
|
61
|
+
setValue: (wanted: unknown) => {
|
|
62
|
+
const next = sliderValue(wanted, value, min, max, step);
|
|
63
|
+
if (typeof next === "string") return next;
|
|
64
|
+
// An agent's change is whole at once, so it is committed too, as a drag is on release.
|
|
65
|
+
return changeAsAgent(changeValue, next) ?? void props.onValueCommitted?.(next, createAgentChangeDetails("none"));
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<BaseSlider.Root
|
|
73
|
+
ref={useMergedRef(ref, element)}
|
|
74
|
+
className={cn("relative flex w-full flex-col gap-2", className)}
|
|
75
|
+
value={value}
|
|
76
|
+
onValueChange={changeValue}
|
|
77
|
+
{...props}
|
|
78
|
+
/>
|
|
79
|
+
);
|
|
80
|
+
});
|
|
16
81
|
Slider.displayName = "Slider";
|
|
17
82
|
|
|
83
|
+
/** The root reports every thumb as one control, so the thumbs' own inputs stay out. */
|
|
84
|
+
const hideFromAgent = (node: HTMLInputElement | null) => node?.setAttribute("data-agent-part", "");
|
|
85
|
+
|
|
18
86
|
const SliderValue = React.forwardRef<
|
|
19
87
|
React.ComponentRef<typeof BaseSlider.Value>,
|
|
20
88
|
React.ComponentProps<typeof BaseSlider.Value>
|
|
@@ -69,9 +137,10 @@ SliderRange.displayName = "SliderRange";
|
|
|
69
137
|
const SliderThumb = React.forwardRef<
|
|
70
138
|
React.ComponentRef<typeof BaseSlider.Thumb>,
|
|
71
139
|
React.ComponentProps<typeof BaseSlider.Thumb>
|
|
72
|
-
>(({ className, ...props }, ref) => (
|
|
140
|
+
>(({ className, inputRef, ...props }, ref) => (
|
|
73
141
|
<BaseSlider.Thumb
|
|
74
142
|
ref={ref}
|
|
143
|
+
inputRef={useMergedRef(inputRef, useHasAgent() ? hideFromAgent : undefined)}
|
|
75
144
|
className={cn(
|
|
76
145
|
"block h-5 w-5 rounded-full border-2 border-primary bg-background ring-offset-background",
|
|
77
146
|
"transition-[color,background-color,border-color,transform,scale] duration-150",
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
type HTMLMotionProps,
|
|
11
11
|
} from "motion/react"
|
|
12
12
|
import { cn } from "@/lib/utils"
|
|
13
|
+
import { changeAsAgent, matchAgentChoice, useAgentControl, useHasAgent, useMergedRef } from "@/components/ui/agent"
|
|
13
14
|
|
|
14
15
|
/** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
|
|
15
16
|
* app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
|
|
@@ -32,6 +33,9 @@ interface SortableContextValue {
|
|
|
32
33
|
cancel: (id: string, label: string) => void
|
|
33
34
|
move: (id: string, delta: number, label: string) => void
|
|
34
35
|
reducedMotion: boolean
|
|
36
|
+
disabled: boolean
|
|
37
|
+
/** Row labels by id, kept only inside an AgentProvider. */
|
|
38
|
+
labels: Map<string, string> | null
|
|
35
39
|
}
|
|
36
40
|
|
|
37
41
|
const SortableContext = React.createContext<SortableContextValue | null>(null)
|
|
@@ -51,10 +55,32 @@ export interface SortableProps
|
|
|
51
55
|
/** The current order, as stable ids. Ids rather than objects because identity is what a
|
|
52
56
|
* reorder is about, and it is what React's keys need anyway. */
|
|
53
57
|
value: string[]
|
|
54
|
-
onValueChange: (next: string[]) => void
|
|
58
|
+
onValueChange: (next: string[], details: SortableChangeDetails) => void
|
|
59
|
+
/** Turns off the handles, so no row can be moved. */
|
|
60
|
+
disabled?: boolean
|
|
55
61
|
children?: React.ReactNode
|
|
56
62
|
}
|
|
57
63
|
|
|
64
|
+
export interface SortableChangeDetails {
|
|
65
|
+
reason: "drag" | "keyboard" | "escape-key" | "imperative-action"
|
|
66
|
+
event: Event | undefined
|
|
67
|
+
isCanceled: boolean
|
|
68
|
+
/** Marks the change as refused. The order is yours, so this only tells an agent it failed. */
|
|
69
|
+
cancel: () => void
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function personChange(reason: Exclude<SortableChangeDetails["reason"], "imperative-action">): SortableChangeDetails {
|
|
73
|
+
const details: SortableChangeDetails = {
|
|
74
|
+
reason,
|
|
75
|
+
event: undefined,
|
|
76
|
+
isCanceled: false,
|
|
77
|
+
cancel: () => {
|
|
78
|
+
details.isCanceled = true
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
return details
|
|
82
|
+
}
|
|
83
|
+
|
|
58
84
|
/**
|
|
59
85
|
* A list the reader can reorder.
|
|
60
86
|
*
|
|
@@ -68,7 +94,7 @@ export interface SortableProps
|
|
|
68
94
|
* reads as having been pushed rather than repainted somewhere else.
|
|
69
95
|
*/
|
|
70
96
|
const Sortable = React.forwardRef<HTMLUListElement, SortableProps>(
|
|
71
|
-
({ value, onValueChange, className, children, ...props }, ref) => {
|
|
97
|
+
({ value, onValueChange, disabled = false, className, children, ...props }, ref) => {
|
|
72
98
|
const reducedMotion = useReducedMotionConfig()
|
|
73
99
|
const [grabbed, setGrabbed] = React.useState<string | null>(null)
|
|
74
100
|
const [announcement, setAnnouncement] = React.useState("")
|
|
@@ -112,7 +138,7 @@ const Sortable = React.forwardRef<HTMLUListElement, SortableProps>(
|
|
|
112
138
|
const original = snapshot.current
|
|
113
139
|
snapshot.current = null
|
|
114
140
|
setGrabbed(null)
|
|
115
|
-
if (original) onValueChange(original)
|
|
141
|
+
if (original) onValueChange(original, personChange("escape-key"))
|
|
116
142
|
setAnnouncement(
|
|
117
143
|
`Cancelled. ${label} back at position ${positionOf(original ?? value, id)} of ${value.length}.`
|
|
118
144
|
)
|
|
@@ -128,25 +154,60 @@ const Sortable = React.forwardRef<HTMLUListElement, SortableProps>(
|
|
|
128
154
|
|
|
129
155
|
const next = [...value]
|
|
130
156
|
;[next[from], next[to]] = [next[to], next[from]]
|
|
131
|
-
|
|
157
|
+
const details = personChange("keyboard")
|
|
158
|
+
onValueChange(next, details)
|
|
159
|
+
if (details.isCanceled) return
|
|
132
160
|
setAnnouncement(`${label}, position ${to + 1} of ${value.length}`)
|
|
133
161
|
},
|
|
134
162
|
[value, onValueChange]
|
|
135
163
|
)
|
|
136
164
|
|
|
165
|
+
const hasAgent = useHasAgent()
|
|
166
|
+
const [labelMap] = React.useState(() => new Map<string, string>())
|
|
167
|
+
const labels = hasAgent ? labelMap : null
|
|
168
|
+
const root = React.useRef<HTMLUListElement>(null)
|
|
169
|
+
useAgentControl(() => {
|
|
170
|
+
const choices = value.map((id) => ({ value: id, label: labelMap.get(id) ?? id }))
|
|
171
|
+
return {
|
|
172
|
+
kind: "list",
|
|
173
|
+
element: root.current,
|
|
174
|
+
value: choices.map((c) => c.label).join(", "),
|
|
175
|
+
items: choices.map((c) => c.label),
|
|
176
|
+
disabled,
|
|
177
|
+
actions: {
|
|
178
|
+
move: (wanted: unknown, position: unknown) => {
|
|
179
|
+
if (wanted === null || typeof wanted === "object") {
|
|
180
|
+
return `move takes an item and a position from 1 as two args, e.g. ["${choices[0]?.label ?? "Item"}", 1].`
|
|
181
|
+
}
|
|
182
|
+
const match = matchAgentChoice(choices, wanted)
|
|
183
|
+
if (typeof match === "string") return match
|
|
184
|
+
const to = Number(position)
|
|
185
|
+
if (!Number.isInteger(to) || to < 1 || to > value.length) {
|
|
186
|
+
return `Position must be a whole number from 1 to ${value.length}.`
|
|
187
|
+
}
|
|
188
|
+
if (value[to - 1] === match.value) return
|
|
189
|
+
const next = value.filter((id) => id !== match.value)
|
|
190
|
+
next.splice(to - 1, 0, match.value)
|
|
191
|
+
return changeAsAgent<string[], SortableChangeDetails>(onValueChange, next)
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
})
|
|
196
|
+
|
|
137
197
|
const context = React.useMemo(
|
|
138
|
-
() => ({ value, grabbed, grab, drop, cancel, move, reducedMotion }),
|
|
139
|
-
[value, grabbed, grab, drop, cancel, move, reducedMotion]
|
|
198
|
+
() => ({ value, grabbed, grab, drop, cancel, move, reducedMotion, disabled, labels }),
|
|
199
|
+
[value, grabbed, grab, drop, cancel, move, reducedMotion, disabled, labels]
|
|
140
200
|
)
|
|
141
201
|
|
|
142
202
|
return (
|
|
143
203
|
<SortableContext.Provider value={context}>
|
|
144
204
|
<Reorder.Group
|
|
145
|
-
ref={ref}
|
|
205
|
+
ref={useMergedRef(ref, root)}
|
|
146
206
|
as="ul"
|
|
147
207
|
axis="y"
|
|
148
208
|
values={value}
|
|
149
|
-
onReorder={onValueChange}
|
|
209
|
+
onReorder={(next: string[]) => onValueChange(next, personChange("drag"))}
|
|
210
|
+
data-disabled={disabled ? "" : undefined}
|
|
150
211
|
className={cn("list-none space-y-2", className)}
|
|
151
212
|
{...props}
|
|
152
213
|
>
|
|
@@ -193,6 +254,10 @@ const SortableItem = React.forwardRef<HTMLLIElement, SortableItemProps>(
|
|
|
193
254
|
const [dragging, setDragging] = React.useState(false)
|
|
194
255
|
const grabbed = list.grabbed === id
|
|
195
256
|
const lifted = dragging || grabbed
|
|
257
|
+
const labels = list.labels
|
|
258
|
+
React.useEffect(() => {
|
|
259
|
+
labels?.set(id, label)
|
|
260
|
+
}, [labels, id, label])
|
|
196
261
|
|
|
197
262
|
const item = React.useMemo(
|
|
198
263
|
() => ({ id, label, grabbed, dragControls, setDragging }),
|
|
@@ -303,9 +368,11 @@ const SortableHandle = React.forwardRef<HTMLButtonElement, SortableHandleProps>(
|
|
|
303
368
|
type="button"
|
|
304
369
|
aria-label={`Reorder ${label}`}
|
|
305
370
|
aria-pressed={grabbed}
|
|
371
|
+
disabled={list.disabled}
|
|
372
|
+
data-agent-part={list.labels ? "" : undefined}
|
|
306
373
|
onPointerDown={(event) => {
|
|
307
374
|
onPointerDown?.(event)
|
|
308
|
-
if (event.defaultPrevented) return
|
|
375
|
+
if (event.defaultPrevented || list.disabled) return
|
|
309
376
|
dragControls.start(event)
|
|
310
377
|
}}
|
|
311
378
|
onKeyDown={handleKeyDown}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { SwipeableRow, SwipeableRowGroup } from './swipeable-row'
|
|
2
|
-
export type { SwipeableRowProps, SwipeableRowAction } from './swipeable-row'
|
|
2
|
+
export type { SwipeableRowProps, SwipeableRowAction, SwipeableRowChangeDetails } from './swipeable-row'
|