rockin 0.0.41 → 0.0.42
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/dist/styles.css +3 -1
- package/dist/ui/index.d.ts +1216 -3
- package/dist/ui/index.js +1 -0
- package/dist/ui/list.js +357 -0
- package/package.json +1 -1
package/dist/ui/index.js
CHANGED
|
@@ -37,6 +37,7 @@ export { Item, ItemActions, ItemContent, ItemDescription, ItemFooter, ItemGroup,
|
|
|
37
37
|
export { BlobSpeech, JellyAction, JellyBlob, JellyBlobMascot, JellyBlobMascotDemo, JellyEmpty, JellyGroup, JellyLink, JellyMascot, JellyMini, useJellyMood } from "./jelly.js"
|
|
38
38
|
export { Kbd, KbdGroup } from "./kbd.js"
|
|
39
39
|
export { Label } from "./label.js"
|
|
40
|
+
export { List, ListGroup, ListItem, ListLabel, createListController, listItemVariants } from "./list.js"
|
|
40
41
|
export { Message, MessageAvatar, MessageContent, MessageFooter, MessageGroup, MessageHeader } from "./message.js"
|
|
41
42
|
export { MessageScroller, MessageScrollerButton, MessageScrollerContent, MessageScrollerItem, MessageScrollerProvider, MessageScrollerViewport, useMessageScroller, useMessageScrollerScrollable, useMessageScrollerVisibility } from "./message-scroller.js"
|
|
42
43
|
export { NumberStepper, RollingNumber } from "./numberstepper.js"
|
package/dist/ui/list.js
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/ui/list.tsx
|
|
4
|
+
import {
|
|
5
|
+
createController,
|
|
6
|
+
createRovingFocus,
|
|
7
|
+
definePrimitive,
|
|
8
|
+
idPart,
|
|
9
|
+
live,
|
|
10
|
+
selects,
|
|
11
|
+
spatialMove
|
|
12
|
+
} from "@ui-sdk/core";
|
|
13
|
+
import { createParts } from "@ui-sdk/react/primitive";
|
|
14
|
+
import { cva } from "class-variance-authority";
|
|
15
|
+
import { cn } from "cn";
|
|
16
|
+
import { jsx } from "react/jsx-runtime";
|
|
17
|
+
var orientationOf = (value) => value === "horizontal" || value === "grid" ? value : "vertical";
|
|
18
|
+
var fromOptions = (options) => ({
|
|
19
|
+
orientation: orientationOf(options.orientation)
|
|
20
|
+
});
|
|
21
|
+
var itemSelector = '[data-slot="list-item"]';
|
|
22
|
+
var pageKeys = /* @__PURE__ */ new Set([
|
|
23
|
+
"ArrowUp",
|
|
24
|
+
"ArrowDown",
|
|
25
|
+
"ArrowLeft",
|
|
26
|
+
"ArrowRight",
|
|
27
|
+
"Home",
|
|
28
|
+
"End"
|
|
29
|
+
]);
|
|
30
|
+
var labelId = (id, key) => `${id}-label-${idPart(key)}`;
|
|
31
|
+
var spatial = spatialMove();
|
|
32
|
+
var steps = {
|
|
33
|
+
vertical: { ArrowDown: 1, ArrowUp: -1 },
|
|
34
|
+
horizontal: { ArrowRight: 1, ArrowLeft: -1 },
|
|
35
|
+
grid: {}
|
|
36
|
+
};
|
|
37
|
+
var activates = (event) => (event.key === "Enter" || event.key === " ") && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey;
|
|
38
|
+
function createListController() {
|
|
39
|
+
return createController();
|
|
40
|
+
}
|
|
41
|
+
var list = definePrimitive({
|
|
42
|
+
name: "list",
|
|
43
|
+
options: {
|
|
44
|
+
orientation: "string",
|
|
45
|
+
loop: "boolean",
|
|
46
|
+
controller: "value"
|
|
47
|
+
},
|
|
48
|
+
state: (options) => ({
|
|
49
|
+
...fromOptions(options),
|
|
50
|
+
stop: null,
|
|
51
|
+
labels: [],
|
|
52
|
+
/** The item shown as current: the hovered one, or the focused one while keys drive. */
|
|
53
|
+
highlighted: null,
|
|
54
|
+
/** True after arrow-key navigation until the mouse really moves, like cmdk. */
|
|
55
|
+
pointerHidden: false
|
|
56
|
+
}),
|
|
57
|
+
live: ["labels", "highlighted", "pointerHidden"],
|
|
58
|
+
sync: fromOptions,
|
|
59
|
+
api: (context) => {
|
|
60
|
+
const { store, options } = context;
|
|
61
|
+
const move = (event, ctx) => {
|
|
62
|
+
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const { orientation } = store.get();
|
|
66
|
+
if (orientation === "grid") return spatial(event, ctx);
|
|
67
|
+
const { items, from, rtl } = ctx;
|
|
68
|
+
if (!items.length) return;
|
|
69
|
+
if (event.key === "Home") return items[0].key;
|
|
70
|
+
if (event.key === "End") return items[items.length - 1].key;
|
|
71
|
+
let step = steps[orientation][event.key];
|
|
72
|
+
if (step === void 0) return;
|
|
73
|
+
if (orientation === "horizontal" && rtl) step = -step;
|
|
74
|
+
const index = items.findIndex((item) => item.key === from.key);
|
|
75
|
+
let next = index + step;
|
|
76
|
+
if (next < 0 || next >= items.length) {
|
|
77
|
+
if (!options().loop) return from.key;
|
|
78
|
+
next = (next + items.length) % items.length;
|
|
79
|
+
}
|
|
80
|
+
return items[next].key;
|
|
81
|
+
};
|
|
82
|
+
const roving = createRovingFocus(
|
|
83
|
+
context,
|
|
84
|
+
{
|
|
85
|
+
slot: "item",
|
|
86
|
+
key: "stop",
|
|
87
|
+
disabled: (item) => !!item.props?.disabled,
|
|
88
|
+
move
|
|
89
|
+
}
|
|
90
|
+
);
|
|
91
|
+
let hovered = null;
|
|
92
|
+
let pointer = { x: Number.NaN, y: Number.NaN };
|
|
93
|
+
const itemOf = (target) => {
|
|
94
|
+
const el = target?.closest?.(
|
|
95
|
+
itemSelector
|
|
96
|
+
);
|
|
97
|
+
return el ? roving.items().find((item) => item.el === el) : void 0;
|
|
98
|
+
};
|
|
99
|
+
const focusedItem = () => itemOf(document.activeElement)?.el === document.activeElement ? itemOf(document.activeElement) : void 0;
|
|
100
|
+
const highlight = (key) => {
|
|
101
|
+
if (store.get().highlighted !== key) store.set({ highlighted: key });
|
|
102
|
+
};
|
|
103
|
+
return {
|
|
104
|
+
focus: (value) => roving.focus(value),
|
|
105
|
+
internal: {
|
|
106
|
+
roving,
|
|
107
|
+
keydown(event, el) {
|
|
108
|
+
if (event.defaultPrevented || event.target !== el) return;
|
|
109
|
+
const moved = roving.keydown(event, el);
|
|
110
|
+
if (moved) {
|
|
111
|
+
highlight(moved.key);
|
|
112
|
+
if (!store.get().pointerHidden) store.set({ pointerHidden: true });
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (!activates(event)) return;
|
|
116
|
+
event.preventDefault();
|
|
117
|
+
if (el.getAttribute("aria-disabled") !== "true") el.click();
|
|
118
|
+
},
|
|
119
|
+
pointermove(event) {
|
|
120
|
+
if (event.pointerType !== "mouse" && event.pointerType !== "pen") {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (event.clientX === pointer.x && event.clientY === pointer.y) return;
|
|
124
|
+
pointer = { x: event.clientX, y: event.clientY };
|
|
125
|
+
if (store.get().pointerHidden) store.set({ pointerHidden: false });
|
|
126
|
+
const item = itemOf(event.target);
|
|
127
|
+
if (!item || item.props?.disabled) return;
|
|
128
|
+
hovered = item.key;
|
|
129
|
+
highlight(item.key);
|
|
130
|
+
const focused = focusedItem();
|
|
131
|
+
if (focused && focused.el !== item.el) {
|
|
132
|
+
item.el.focus({ preventScroll: true });
|
|
133
|
+
} else if (!focused) {
|
|
134
|
+
roving.remember(item.key);
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
pointerleave() {
|
|
138
|
+
hovered = null;
|
|
139
|
+
highlight(focusedItem()?.key ?? null);
|
|
140
|
+
},
|
|
141
|
+
focusin(event) {
|
|
142
|
+
const item = itemOf(event.target);
|
|
143
|
+
if (item && item.el === event.target) highlight(item.key);
|
|
144
|
+
},
|
|
145
|
+
focusout(event, root) {
|
|
146
|
+
if (root.contains(event.relatedTarget)) return;
|
|
147
|
+
highlight(hovered && !store.get().pointerHidden ? hovered : null);
|
|
148
|
+
},
|
|
149
|
+
/**
|
|
150
|
+
* Keys pressed while nothing has focus (fresh page load, after closing a
|
|
151
|
+
* drawer) act on the hovered item, like cmdk: arrows move from it and
|
|
152
|
+
* Enter opens it. Focus in an input or another control is left alone.
|
|
153
|
+
*/
|
|
154
|
+
listenToPage() {
|
|
155
|
+
const onKeyDown = (event) => {
|
|
156
|
+
if (event.defaultPrevented || !hovered) return;
|
|
157
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
158
|
+
const active = document.activeElement;
|
|
159
|
+
if (active && active !== document.body && active !== document.documentElement) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (event.key !== "Enter" && !pageKeys.has(event.key)) return;
|
|
163
|
+
const item = roving.items().find((candidate) => candidate.key === hovered);
|
|
164
|
+
if (!item) return;
|
|
165
|
+
if (event.key === "Enter") {
|
|
166
|
+
event.preventDefault();
|
|
167
|
+
if (!item.props?.disabled) item.el.click();
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
item.el.focus({ preventScroll: true });
|
|
171
|
+
const moved = roving.keydown(event, item.el);
|
|
172
|
+
event.preventDefault();
|
|
173
|
+
highlight(moved?.key ?? item.key);
|
|
174
|
+
if (!store.get().pointerHidden) store.set({ pointerHidden: true });
|
|
175
|
+
};
|
|
176
|
+
document.addEventListener("keydown", onKeyDown);
|
|
177
|
+
return () => document.removeEventListener("keydown", onKeyDown);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
},
|
|
182
|
+
slots: (slot) => ({
|
|
183
|
+
root: slot({
|
|
184
|
+
role: "list",
|
|
185
|
+
selects: selects({ stop: [], labels: [], highlighted: [] }),
|
|
186
|
+
state: (state) => ({ orientation: state.orientation }),
|
|
187
|
+
attrs: (state) => ({ "data-orientation": state.orientation }),
|
|
188
|
+
live: live((state) => ({
|
|
189
|
+
"data-pointer-hidden": state.pointerHidden ? "" : void 0
|
|
190
|
+
})),
|
|
191
|
+
on: {
|
|
192
|
+
pointermove: (event, { api }) => api.internal.pointermove(event),
|
|
193
|
+
pointerleave: (_event, { api }) => api.internal.pointerleave(),
|
|
194
|
+
focusin: (event, { api }) => api.internal.focusin(event),
|
|
195
|
+
focusout: (event, { el, api }) => {
|
|
196
|
+
if (el) api.internal.focusout(event, el);
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
bind: (_el, { api }) => api.internal.listenToPage()
|
|
200
|
+
}),
|
|
201
|
+
item: slot({
|
|
202
|
+
role: "listitem",
|
|
203
|
+
props: { value: "id", disabled: "boolean" },
|
|
204
|
+
selects: selects({
|
|
205
|
+
stop: ({ key }) => [key, null],
|
|
206
|
+
labels: [],
|
|
207
|
+
highlighted: ({ key }) => [key],
|
|
208
|
+
pointerHidden: []
|
|
209
|
+
}),
|
|
210
|
+
state: (state, { key, props }) => ({
|
|
211
|
+
highlighted: state.highlighted === key,
|
|
212
|
+
disabled: !!props.disabled
|
|
213
|
+
}),
|
|
214
|
+
attrs: (state, { key, props, api }) => ({
|
|
215
|
+
tabindex: api.internal.roving.tabIndex(state, { key, props }),
|
|
216
|
+
"aria-disabled": props.disabled ? "true" : void 0,
|
|
217
|
+
"data-disabled": props.disabled ? "" : void 0,
|
|
218
|
+
"data-orientation": state.orientation
|
|
219
|
+
}),
|
|
220
|
+
live: live((state, { key }) => ({
|
|
221
|
+
"data-highlighted": state.highlighted === key ? "" : void 0
|
|
222
|
+
})),
|
|
223
|
+
on: {
|
|
224
|
+
keydown: (event, { el, api }) => {
|
|
225
|
+
if (el) api.internal.keydown(event, el);
|
|
226
|
+
},
|
|
227
|
+
click: (event, { props }) => {
|
|
228
|
+
if (props.disabled) event.preventDefault();
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
bind: (el, part) => part.api.internal.roving.bind(el, part)
|
|
232
|
+
}),
|
|
233
|
+
group: slot({
|
|
234
|
+
role: "group",
|
|
235
|
+
props: { value: "id" },
|
|
236
|
+
selects: selects({
|
|
237
|
+
stop: [],
|
|
238
|
+
labels: ({ key }) => [key],
|
|
239
|
+
highlighted: [],
|
|
240
|
+
pointerHidden: []
|
|
241
|
+
}),
|
|
242
|
+
live: live((state, { id, key }) => ({
|
|
243
|
+
"aria-labelledby": state.labels.includes(key) ? labelId(id, key) : void 0
|
|
244
|
+
}))
|
|
245
|
+
}),
|
|
246
|
+
label: slot({
|
|
247
|
+
props: { value: { kind: "string", from: "group" } },
|
|
248
|
+
selects: selects({
|
|
249
|
+
stop: [],
|
|
250
|
+
labels: [],
|
|
251
|
+
highlighted: [],
|
|
252
|
+
pointerHidden: []
|
|
253
|
+
}),
|
|
254
|
+
attrs: (_state, { id, key }) => ({ id: labelId(id, key) }),
|
|
255
|
+
bind: (_el, { key, store }) => {
|
|
256
|
+
store.set({ labels: [...store.get().labels, key] });
|
|
257
|
+
return () => store.set({
|
|
258
|
+
labels: store.get().labels.filter((label) => label !== key)
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
})
|
|
262
|
+
})
|
|
263
|
+
});
|
|
264
|
+
var Parts = createParts(list, {
|
|
265
|
+
root: "div",
|
|
266
|
+
item: "div",
|
|
267
|
+
group: "div",
|
|
268
|
+
label: "div"
|
|
269
|
+
});
|
|
270
|
+
var withClass = (className, base) => typeof className === "function" ? (state) => cn(base, className(state)) : cn(base, className);
|
|
271
|
+
function List({ className, ...props }) {
|
|
272
|
+
return /* @__PURE__ */ jsx(
|
|
273
|
+
Parts.Root,
|
|
274
|
+
{
|
|
275
|
+
...props,
|
|
276
|
+
className: withClass(
|
|
277
|
+
className,
|
|
278
|
+
"outline-none data-pointer-hidden:cursor-none data-pointer-hidden:**:cursor-none data-[orientation=horizontal]:flex data-[orientation=horizontal]:flex-row data-[orientation=vertical]:flex data-[orientation=vertical]:flex-col"
|
|
279
|
+
)
|
|
280
|
+
}
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
var surface = "data-highlighted:bg-(--list-item-hover) active:bg-(--list-item-active) data-active:bg-(--list-item-active)";
|
|
284
|
+
var ring = "data-highlighted:ring-2 data-highlighted:ring-(--list-item-ring) data-active:ring-2 data-active:ring-(--list-item-ring)";
|
|
285
|
+
var focusRing = "focus-visible:ring-2 focus-visible:ring-(--list-item-ring) data-active:ring-2 data-active:ring-(--list-item-ring)";
|
|
286
|
+
var listItemVariants = cva(
|
|
287
|
+
cn(
|
|
288
|
+
"relative cursor-pointer transition-[background-color,box-shadow] duration-150 ease-out outline-none [-webkit-tap-highlight-color:transparent] motion-reduce:transition-none",
|
|
289
|
+
"[--list-item-active:var(--color-muted)] [--list-item-hover:color-mix(in_oklab,var(--color-muted)_60%,transparent)] [--list-item-ring:var(--color-ring)]",
|
|
290
|
+
"data-disabled:cursor-not-allowed data-disabled:opacity-60"
|
|
291
|
+
),
|
|
292
|
+
{
|
|
293
|
+
variants: {
|
|
294
|
+
highlight: {
|
|
295
|
+
/** Tinted background on hover, focus, press and active. */
|
|
296
|
+
background: surface,
|
|
297
|
+
/** Ring only; the background never changes. */
|
|
298
|
+
outline: ring,
|
|
299
|
+
/** Background on highlight, plus a ring on keyboard focus and active. */
|
|
300
|
+
both: cn(surface, focusRing),
|
|
301
|
+
/** No built-in states. Style `data-highlighted`, `:focus-visible` and `data-active` yourself. */
|
|
302
|
+
none: ""
|
|
303
|
+
},
|
|
304
|
+
ring: {
|
|
305
|
+
/** Ring drawn inside the item; safe for full-width rows and scroll areas. */
|
|
306
|
+
inset: "ring-inset",
|
|
307
|
+
/** Ring drawn outside the item, for spaced cards in a grid. */
|
|
308
|
+
offset: "ring-offset-2 ring-offset-background"
|
|
309
|
+
}
|
|
310
|
+
},
|
|
311
|
+
defaultVariants: { highlight: "background", ring: "inset" }
|
|
312
|
+
}
|
|
313
|
+
);
|
|
314
|
+
function ListItem({
|
|
315
|
+
className,
|
|
316
|
+
highlight,
|
|
317
|
+
ring: ringPlacement,
|
|
318
|
+
active,
|
|
319
|
+
...props
|
|
320
|
+
}) {
|
|
321
|
+
return /* @__PURE__ */ jsx(
|
|
322
|
+
Parts.Item,
|
|
323
|
+
{
|
|
324
|
+
"aria-current": active ? "true" : void 0,
|
|
325
|
+
"data-active": active ? "" : void 0,
|
|
326
|
+
"data-highlight": highlight ?? "background",
|
|
327
|
+
...props,
|
|
328
|
+
className: withClass(
|
|
329
|
+
className,
|
|
330
|
+
listItemVariants({ highlight, ring: ringPlacement })
|
|
331
|
+
)
|
|
332
|
+
}
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
function ListGroup({ className, ...props }) {
|
|
336
|
+
return /* @__PURE__ */ jsx(Parts.Group, { ...props, className: withClass(className, "") });
|
|
337
|
+
}
|
|
338
|
+
function ListLabel({ className, ...props }) {
|
|
339
|
+
return /* @__PURE__ */ jsx(
|
|
340
|
+
Parts.Label,
|
|
341
|
+
{
|
|
342
|
+
...props,
|
|
343
|
+
className: withClass(
|
|
344
|
+
className,
|
|
345
|
+
"text-xs font-medium text-muted-foreground"
|
|
346
|
+
)
|
|
347
|
+
}
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
export {
|
|
351
|
+
List,
|
|
352
|
+
ListGroup,
|
|
353
|
+
ListItem,
|
|
354
|
+
ListLabel,
|
|
355
|
+
createListController,
|
|
356
|
+
listItemVariants
|
|
357
|
+
};
|