@lenso/primitives 0.8.0 → 0.9.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/README.md +31 -11
- package/dist/list/list.d.ts +56 -0
- package/dist/list/list.js +163 -0
- package/dist/model-control/model-control.d.ts +50 -0
- package/dist/model-control/model-control.js +113 -0
- package/dist/section-index/index.d.ts +9 -0
- package/dist/section-index/index.js +8 -0
- package/dist/section-index/section-index.d.ts +42 -0
- package/dist/section-index/section-index.js +145 -0
- package/dist/sidebar/index.d.ts +31 -110
- package/dist/sidebar/index.js +29 -300
- package/dist/sidebar/sidebar.d.ts +41 -0
- package/dist/sidebar/sidebar.js +151 -0
- package/dist/swipe-actions/index.d.ts +12 -0
- package/dist/swipe-actions/index.js +11 -0
- package/dist/swipe-actions/swipe-actions.d.ts +40 -0
- package/dist/swipe-actions/swipe-actions.js +200 -0
- package/dist/wheel-picker/wheel-picker.d.ts +36 -0
- package/dist/wheel-picker/wheel-picker.js +68 -0
- package/package.json +34 -17
- package/dist/command/command.d.ts +0 -25
- package/dist/command/index.d.ts +0 -16
- package/dist/command/index.js +0 -76
- package/dist/data-grid/index.d.ts +0 -117
- package/dist/data-grid/index.js +0 -178
- package/dist/resize-handle/index.d.ts +0 -44
- package/dist/resize-handle/index.js +0 -179
package/README.md
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
# @lenso/primitives
|
|
2
2
|
|
|
3
|
-
`@lenso/primitives` contains headless, style-free
|
|
3
|
+
`@lenso/primitives` contains headless, style-free product primitives composed
|
|
4
|
+
with UI SDK. Sidebar supplies the composable React shell panel:
|
|
4
5
|
|
|
5
6
|
```bash
|
|
6
|
-
pnpm add @lenso/primitives @
|
|
7
|
+
pnpm add @lenso/primitives @ui-sdk/react @ui-sdk/core react
|
|
7
8
|
```
|
|
8
9
|
|
|
9
10
|
```tsx
|
|
@@ -11,8 +12,8 @@ import { Sidebar } from "@lenso/primitives/sidebar";
|
|
|
11
12
|
|
|
12
13
|
export function AppShell() {
|
|
13
14
|
return (
|
|
14
|
-
<Sidebar.Root defaultOpen>
|
|
15
|
-
<Sidebar.Panel>
|
|
15
|
+
<Sidebar.Root aria-label="Workspace navigation" defaultOpen>
|
|
16
|
+
<Sidebar.Panel aria-label="Workspace destinations">
|
|
16
17
|
<Sidebar.Content>Navigation</Sidebar.Content>
|
|
17
18
|
</Sidebar.Panel>
|
|
18
19
|
</Sidebar.Root>
|
|
@@ -20,15 +21,34 @@ export function AppShell() {
|
|
|
20
21
|
}
|
|
21
22
|
```
|
|
22
23
|
|
|
23
|
-
`Sidebar` supports controlled and uncontrolled state, independent left and right panels, nested
|
|
24
|
+
`Sidebar` supports controlled and uncontrolled state, independent left and right panels, nested roots, native controllers, cancelable state changes, keyboard dismissal, and focus recovery. Its public API exposes behavior and semantic DOM only; it does not impose StyleX, theme tokens, default CSS, an animation runtime, persistence, or routing.
|
|
24
25
|
|
|
25
|
-
`
|
|
26
|
+
The framework-independent `sidebar` definition and controller are exported from
|
|
27
|
+
`@lenso/primitives/sidebar/core`; React parts are generated with SDK `createParts`.
|
|
28
|
+
Use `@lenso/ui/sidebar` for styled navigation inside a panel. The registry also includes an editable Sidebar Recipe for Consumers that need structural ownership.
|
|
26
29
|
|
|
27
|
-
|
|
30
|
+
Other public entries provide SectionIndex and SwipeActions React parts and core
|
|
31
|
+
definitions (`/section-index`, `/swipe-actions` and their `/core` entries).
|
|
32
|
+
`/model-control`, `/list/core` and `/wheel-picker/core` provide shared domain
|
|
33
|
+
value, editable-list and wheel-picker primitives used by the styled UI families.
|
|
34
|
+
These core entries have no React runtime; they do not add replacement Command,
|
|
35
|
+
DataGrid or ResizeHandle component APIs.
|
|
28
36
|
|
|
29
|
-
Source and browser tests live under `packages/primitives/src/sidebar`. See the repository [
|
|
37
|
+
Source and browser tests live under `packages/primitives/src/sidebar`. See the repository [component conventions](../../docs/design/component-conventions.md) for the dependency boundary and the [contributing guide](../../CONTRIBUTING.md) for changes and release verification.
|
|
30
38
|
|
|
31
|
-
|
|
39
|
+
Unstyled Command parts are available directly from `@ui-sdk/react/command`.
|
|
40
|
+
Use `@lenso/ui/command` for the Lenso presentation.
|
|
32
41
|
|
|
33
|
-
|
|
34
|
-
|
|
42
|
+
## Temporary TypeScript compatibility
|
|
43
|
+
|
|
44
|
+
UI SDK 0.1.0-beta.1 has a declaration incompatibility with
|
|
45
|
+
`exactOptionalPropertyTypes: true`. The supported configuration for these
|
|
46
|
+
SDK-backed primitives currently uses `strict: true` and
|
|
47
|
+
`exactOptionalPropertyTypes: false`; declaration checking can remain enabled
|
|
48
|
+
with `skipLibCheck: false`. Building Lenso with this setting does not repair
|
|
49
|
+
the upstream declarations for consumers that enable exact optional properties.
|
|
50
|
+
|
|
51
|
+
No dependency patch or runtime compatibility layer is included. Restore exact
|
|
52
|
+
optional property checking when an official SDK release passes both the strict
|
|
53
|
+
primitive build and the installed-consumer checks. Investigation and removal
|
|
54
|
+
criteria are recorded in [the compatibility report](../../docs/upstream-ui-sdk-types.md).
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { definePrimitive, type ChangeDetails, type ChangeOptions, type ReorderDraft, type SlotDefinition, type SwipeOpen } from "@ui-sdk/core";
|
|
2
|
+
export type ListKey = string | number | bigint;
|
|
3
|
+
export type ListReason = "item-press" | "list-navigation" | "item-removed" | "imperative";
|
|
4
|
+
export type ListMove = {
|
|
5
|
+
keys: ReadonlySet<ListKey>;
|
|
6
|
+
targetKey: ListKey;
|
|
7
|
+
dropPosition: "before" | "after";
|
|
8
|
+
};
|
|
9
|
+
export type ListEntry = {
|
|
10
|
+
key: ListKey;
|
|
11
|
+
text: string;
|
|
12
|
+
node: HTMLElement;
|
|
13
|
+
disabled: boolean;
|
|
14
|
+
group?: string;
|
|
15
|
+
};
|
|
16
|
+
export type ListController = {
|
|
17
|
+
getValue(): string[];
|
|
18
|
+
setValue(value: string[], options?: ChangeOptions<ListReason>): boolean;
|
|
19
|
+
selectAll(options?: ChangeOptions<ListReason>): void;
|
|
20
|
+
move(key: string, index: number): boolean;
|
|
21
|
+
};
|
|
22
|
+
export type ListOptions = {
|
|
23
|
+
value?: string[];
|
|
24
|
+
defaultValue?: string[];
|
|
25
|
+
onValueChange?: (value: string[], details: ChangeDetails<ListReason>) => void;
|
|
26
|
+
onReorder?: (move: ListMove, details: ChangeDetails<"drag" | "move-key" | "imperative">) => void;
|
|
27
|
+
editing?: boolean;
|
|
28
|
+
disabled?: boolean;
|
|
29
|
+
controller?: ListController;
|
|
30
|
+
};
|
|
31
|
+
type State = {
|
|
32
|
+
value: string[];
|
|
33
|
+
order: string[];
|
|
34
|
+
reordering: ReorderDraft | null;
|
|
35
|
+
open: SwipeOpen | null;
|
|
36
|
+
editing: boolean;
|
|
37
|
+
disabled: boolean;
|
|
38
|
+
};
|
|
39
|
+
type Api = ListController & {
|
|
40
|
+
register(entry: ListEntry): () => void;
|
|
41
|
+
entry(key: string): ListEntry | undefined;
|
|
42
|
+
entries(): ListEntry[];
|
|
43
|
+
setChecked(key: ListKey, checked: boolean, options?: ChangeOptions<ListReason>): void;
|
|
44
|
+
setOpen(open: SwipeOpen | null): void;
|
|
45
|
+
keydown(event: KeyboardEvent, key: ListKey): boolean;
|
|
46
|
+
pointerdown(event: PointerEvent, key: ListKey): void;
|
|
47
|
+
activate(key: ListKey): void;
|
|
48
|
+
cancel(): void;
|
|
49
|
+
};
|
|
50
|
+
type Slots = {
|
|
51
|
+
root: SlotDefinition<State, Api, {}, State>;
|
|
52
|
+
};
|
|
53
|
+
export declare const listKey: (key: ListKey) => string;
|
|
54
|
+
export declare function createListController(): ListController;
|
|
55
|
+
export declare const list: ReturnType<typeof definePrimitive<ListOptions, State, Api, Slots>>;
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { createChange, createController, createReorder, createSelection, definePrimitive, dispatch } from "@ui-sdk/core";
|
|
2
|
+
// src/list/list.ts
|
|
3
|
+
const listKey = (key) => `${typeof key}:${String(key)}`;
|
|
4
|
+
function createListController() {
|
|
5
|
+
return createController();
|
|
6
|
+
}
|
|
7
|
+
const list = definePrimitive({
|
|
8
|
+
name: "list",
|
|
9
|
+
options: {
|
|
10
|
+
value: "list",
|
|
11
|
+
defaultValue: "list",
|
|
12
|
+
onValueChange: "callback",
|
|
13
|
+
onReorder: "callback",
|
|
14
|
+
editing: "boolean",
|
|
15
|
+
disabled: "boolean",
|
|
16
|
+
controller: "value"
|
|
17
|
+
},
|
|
18
|
+
controlled: ["value"],
|
|
19
|
+
state: (options) => ({
|
|
20
|
+
value: options.value ?? options.defaultValue ?? [],
|
|
21
|
+
order: [],
|
|
22
|
+
reordering: null,
|
|
23
|
+
open: null,
|
|
24
|
+
editing: !!options.editing,
|
|
25
|
+
disabled: !!options.disabled
|
|
26
|
+
}),
|
|
27
|
+
sync: (options) => ({
|
|
28
|
+
...options.value === void 0 ? {} : { value: options.value },
|
|
29
|
+
editing: !!options.editing,
|
|
30
|
+
disabled: !!options.disabled
|
|
31
|
+
}),
|
|
32
|
+
api: (context) => {
|
|
33
|
+
const change = createChange(context);
|
|
34
|
+
const entries = /* @__PURE__ */ new Map();
|
|
35
|
+
const ordered = () => [...entries.entries()].sort(([, a], [, b]) => a.node.compareDocumentPosition(b.node) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1);
|
|
36
|
+
const enabled = (key) => {
|
|
37
|
+
const state = context.store.get();
|
|
38
|
+
return state.editing && !state.disabled && entries.has(key) && !entries.get(key).disabled;
|
|
39
|
+
};
|
|
40
|
+
const setValue = (value, options = {}) => {
|
|
41
|
+
if (!context.store.get().editing || context.store.get().disabled || value.some((key) => !enabled(key))) return false;
|
|
42
|
+
return change("value", value, {
|
|
43
|
+
...options,
|
|
44
|
+
reason: options.reason ?? "imperative"
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
const selection = createSelection(context, {
|
|
48
|
+
key: "value",
|
|
49
|
+
set: setValue,
|
|
50
|
+
order: () => context.store.get().order,
|
|
51
|
+
multiple: () => true,
|
|
52
|
+
selectable: enabled
|
|
53
|
+
});
|
|
54
|
+
let moving;
|
|
55
|
+
const reorder = createReorder(context, {
|
|
56
|
+
slot: "item",
|
|
57
|
+
key: "order",
|
|
58
|
+
draft: "reordering",
|
|
59
|
+
preview: true,
|
|
60
|
+
disabled: (item) => !context.options().onReorder || !enabled(item.key),
|
|
61
|
+
label: (key) => entries.get(key)?.text,
|
|
62
|
+
set(order, details) {
|
|
63
|
+
const state = context.store.get();
|
|
64
|
+
const source = state.reordering?.key ?? moving;
|
|
65
|
+
if (!source || order.every((key, index) => key === state.order[index])) return false;
|
|
66
|
+
const at = order.indexOf(source), before = at < state.order.indexOf(source);
|
|
67
|
+
const target = order[at + (before ? 1 : -1)];
|
|
68
|
+
if (!target || entries.get(source)?.group !== entries.get(target)?.group) return false;
|
|
69
|
+
return !dispatch(context, "onReorder", {
|
|
70
|
+
keys: /* @__PURE__ */ new Set([entries.get(source).key]),
|
|
71
|
+
targetKey: entries.get(target).key,
|
|
72
|
+
dropPosition: before ? "before" : "after"
|
|
73
|
+
}, details);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
context.onOptions((options) => {
|
|
77
|
+
if (!options.editing || options.disabled) reorder.cancel();
|
|
78
|
+
if (options.editing || options.disabled) context.store.set({ open: null });
|
|
79
|
+
});
|
|
80
|
+
const refresh = () => context.store.set({ order: ordered().filter(([, entry]) => !entry.disabled).map(([key]) => key) });
|
|
81
|
+
return {
|
|
82
|
+
getValue: () => context.store.get().value,
|
|
83
|
+
setValue,
|
|
84
|
+
entries: () => ordered().map(([, entry]) => entry),
|
|
85
|
+
entry: (key) => entries.get(key),
|
|
86
|
+
register(entry) {
|
|
87
|
+
const key = listKey(entry.key);
|
|
88
|
+
if (entries.has(key)) throw new Error(`Duplicate List itemKey: ${String(entry.key)}`);
|
|
89
|
+
entries.set(key, entry);
|
|
90
|
+
const unbind = reorder.bind(entry.node, {
|
|
91
|
+
key,
|
|
92
|
+
props: { disabled: entry.disabled }
|
|
93
|
+
});
|
|
94
|
+
refresh();
|
|
95
|
+
return () => {
|
|
96
|
+
unbind();
|
|
97
|
+
entries.delete(key);
|
|
98
|
+
refresh();
|
|
99
|
+
if (context.options().value === void 0) change("value", context.store.get().value.filter((key) => entries.has(key)), { reason: "item-removed" });
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
setChecked(key, checked, options = {}) {
|
|
103
|
+
const id = listKey(key);
|
|
104
|
+
if (selection.isSelected(id) !== checked) selection.toggle(id, {
|
|
105
|
+
...options,
|
|
106
|
+
reason: options.reason ?? "item-press"
|
|
107
|
+
});
|
|
108
|
+
},
|
|
109
|
+
selectAll(options = {}) {
|
|
110
|
+
selection.toggleAll({
|
|
111
|
+
...options,
|
|
112
|
+
reason: options.reason ?? "list-navigation"
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
setOpen: (open) => {
|
|
116
|
+
context.store.set({ open });
|
|
117
|
+
},
|
|
118
|
+
pointerdown: (event, key) => reorder.pointerdown(event, listKey(key)),
|
|
119
|
+
keydown(event, key) {
|
|
120
|
+
const handled = reorder.keydown(event, listKey(key));
|
|
121
|
+
const draft = context.store.get().reordering;
|
|
122
|
+
if (!draft) return handled;
|
|
123
|
+
const group = entries.get(draft.key)?.group;
|
|
124
|
+
const indices = context.store.get().order.flatMap((key, index) => entries.get(key)?.group === group ? [index] : []);
|
|
125
|
+
const index = draft.order.indexOf(draft.key);
|
|
126
|
+
if (indices.length && (index < indices[0] || index > indices.at(-1))) reorder.moveTo(Math.max(indices[0], Math.min(indices.at(-1), index)));
|
|
127
|
+
return handled;
|
|
128
|
+
},
|
|
129
|
+
activate(key) {
|
|
130
|
+
if (context.store.get().reordering) reorder.drop();
|
|
131
|
+
else reorder.grab(listKey(key));
|
|
132
|
+
},
|
|
133
|
+
cancel: reorder.cancel,
|
|
134
|
+
move(key, index) {
|
|
135
|
+
moving = key;
|
|
136
|
+
try {
|
|
137
|
+
return reorder.move(key, index);
|
|
138
|
+
} finally {
|
|
139
|
+
moving = void 0;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
},
|
|
144
|
+
slots: (slot) => ({ root: slot({
|
|
145
|
+
state: (state) => state,
|
|
146
|
+
attrs: (state) => ({
|
|
147
|
+
"data-editing": state.editing ? "" : void 0,
|
|
148
|
+
"data-disabled": state.disabled ? "" : void 0
|
|
149
|
+
})
|
|
150
|
+
}) }),
|
|
151
|
+
agent: {
|
|
152
|
+
actions: [
|
|
153
|
+
"setValue",
|
|
154
|
+
"selectAll",
|
|
155
|
+
"move"
|
|
156
|
+
],
|
|
157
|
+
items: (api) => api.entries().map((entry) => ({
|
|
158
|
+
value: listKey(entry.key),
|
|
159
|
+
label: entry.text
|
|
160
|
+
}))
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
export { createListController, list, listKey };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { createRovingFocus, definePrimitive, type ChangeDetails, type ChangeOptions, type SlotDefinition } from "@ui-sdk/core";
|
|
2
|
+
export type ModelValue = string | string[] | null;
|
|
3
|
+
export type ModelChangeReason = "input-change" | "input-commit" | "step-key" | "item-press" | "list-navigation" | "drag" | "reset" | "imperative";
|
|
4
|
+
export type ModelController = {
|
|
5
|
+
getValue(): ModelValue;
|
|
6
|
+
setValue(value: ModelValue, options?: ChangeOptions<ModelChangeReason>): boolean;
|
|
7
|
+
clear(options?: ChangeOptions<ModelChangeReason>): boolean;
|
|
8
|
+
reset(): boolean;
|
|
9
|
+
focus(key?: string): void;
|
|
10
|
+
};
|
|
11
|
+
export type ModelControlOptions = {
|
|
12
|
+
value?: ModelValue;
|
|
13
|
+
defaultValue?: ModelValue;
|
|
14
|
+
onValueChange?: (value: ModelValue, details: ChangeDetails<ModelChangeReason>) => void;
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
readOnly?: boolean;
|
|
17
|
+
validate?: (value: ModelValue) => boolean;
|
|
18
|
+
controller?: ModelController;
|
|
19
|
+
move?: (event: KeyboardEvent, from: string) => string | undefined;
|
|
20
|
+
};
|
|
21
|
+
type State = {
|
|
22
|
+
value: ModelValue;
|
|
23
|
+
focused: string | null;
|
|
24
|
+
disabled: boolean;
|
|
25
|
+
readOnly: boolean;
|
|
26
|
+
};
|
|
27
|
+
type Item = {
|
|
28
|
+
value: string;
|
|
29
|
+
disabled?: boolean;
|
|
30
|
+
};
|
|
31
|
+
type Api = ModelController & {
|
|
32
|
+
roving: ReturnType<typeof createRovingFocus<State, Item>>;
|
|
33
|
+
run(action: () => void, options: ChangeOptions<ModelChangeReason>): boolean | undefined;
|
|
34
|
+
bind(el: HTMLElement, key: string, disabled?: boolean): () => void;
|
|
35
|
+
navigate(event: KeyboardEvent): void;
|
|
36
|
+
};
|
|
37
|
+
type Slots = {
|
|
38
|
+
root: SlotDefinition<State, Api, {}, State>;
|
|
39
|
+
};
|
|
40
|
+
export declare function createModelController(): ModelController;
|
|
41
|
+
declare function modelControl(name: string): ReturnType<typeof definePrimitive<ModelControlOptions, State, Api, Slots>>;
|
|
42
|
+
export declare const dateField: ReturnType<typeof modelControl>;
|
|
43
|
+
export declare const timeField: ReturnType<typeof modelControl>;
|
|
44
|
+
export declare const datePicker: ReturnType<typeof modelControl>;
|
|
45
|
+
export declare const dateRangePicker: ReturnType<typeof modelControl>;
|
|
46
|
+
export declare const calendar: ReturnType<typeof modelControl>;
|
|
47
|
+
export declare const rangeCalendar: ReturnType<typeof modelControl>;
|
|
48
|
+
export declare const colorField: ReturnType<typeof modelControl>;
|
|
49
|
+
export declare const yearPicker: ReturnType<typeof modelControl>;
|
|
50
|
+
export {};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { createChange, createController, createRovingFocus, definePrimitive } from "@ui-sdk/core";
|
|
2
|
+
// src/model-control/model-control.ts
|
|
3
|
+
function createModelController() {
|
|
4
|
+
return createController();
|
|
5
|
+
}
|
|
6
|
+
function modelControl(name) {
|
|
7
|
+
return definePrimitive({
|
|
8
|
+
name,
|
|
9
|
+
options: {
|
|
10
|
+
value: "value",
|
|
11
|
+
defaultValue: "value",
|
|
12
|
+
onValueChange: "callback",
|
|
13
|
+
disabled: "boolean",
|
|
14
|
+
readOnly: "boolean",
|
|
15
|
+
validate: "action",
|
|
16
|
+
controller: "value",
|
|
17
|
+
move: "action"
|
|
18
|
+
},
|
|
19
|
+
controlled: ["value"],
|
|
20
|
+
state: (options) => ({
|
|
21
|
+
value: options.value === void 0 ? options.defaultValue ?? null : options.value,
|
|
22
|
+
focused: null,
|
|
23
|
+
disabled: !!options.disabled,
|
|
24
|
+
readOnly: !!options.readOnly
|
|
25
|
+
}),
|
|
26
|
+
sync: (options) => ({
|
|
27
|
+
...options.value === void 0 ? {} : { value: options.value },
|
|
28
|
+
disabled: !!options.disabled,
|
|
29
|
+
readOnly: !!options.readOnly
|
|
30
|
+
}),
|
|
31
|
+
api: (context) => {
|
|
32
|
+
const change = createChange(context);
|
|
33
|
+
let cause = {};
|
|
34
|
+
let result;
|
|
35
|
+
const roving = createRovingFocus(context, {
|
|
36
|
+
slot: "item",
|
|
37
|
+
key: "focused",
|
|
38
|
+
orientation: () => "horizontal",
|
|
39
|
+
...context.options().move ? { move: (event, { from }) => context.options().move?.(event, from.key) } : {},
|
|
40
|
+
focusable: (item) => !context.store.get().disabled && !item.props.disabled
|
|
41
|
+
});
|
|
42
|
+
const setValue = (value, options = {}) => {
|
|
43
|
+
const state = context.store.get();
|
|
44
|
+
if ((state.disabled || state.readOnly) && options.reason !== "reset" || context.options().validate?.(value) === false) return result = false;
|
|
45
|
+
return result = change("value", value, {
|
|
46
|
+
...cause,
|
|
47
|
+
...options,
|
|
48
|
+
reason: options.reason ?? cause.reason ?? "imperative"
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
roving,
|
|
53
|
+
getValue: () => context.store.get().value,
|
|
54
|
+
setValue,
|
|
55
|
+
clear: (options) => setValue(null, options),
|
|
56
|
+
reset: () => setValue(context.options().defaultValue ?? null, { reason: "reset" }),
|
|
57
|
+
focus: (key) => roving.focus(key),
|
|
58
|
+
run(action, options) {
|
|
59
|
+
const previous = cause;
|
|
60
|
+
const previousResult = result;
|
|
61
|
+
cause = options;
|
|
62
|
+
result = void 0;
|
|
63
|
+
try {
|
|
64
|
+
action();
|
|
65
|
+
return result;
|
|
66
|
+
} finally {
|
|
67
|
+
cause = previous;
|
|
68
|
+
result = previousResult;
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
bind: (el, key, disabled) => roving.bind(el, {
|
|
72
|
+
key,
|
|
73
|
+
props: {
|
|
74
|
+
value: key,
|
|
75
|
+
...disabled === void 0 ? {} : { disabled }
|
|
76
|
+
}
|
|
77
|
+
}),
|
|
78
|
+
navigate(event) {
|
|
79
|
+
if (event.target instanceof HTMLElement) roving.keydown(event, event.target);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
},
|
|
83
|
+
slots: (slot) => ({ root: slot({
|
|
84
|
+
state: (state) => state,
|
|
85
|
+
attrs: (state) => ({
|
|
86
|
+
role: "group",
|
|
87
|
+
"data-disabled": state.disabled ? "" : void 0,
|
|
88
|
+
"data-readonly": state.readOnly ? "" : void 0
|
|
89
|
+
})
|
|
90
|
+
}) }),
|
|
91
|
+
agent: {
|
|
92
|
+
actions: name === "year-picker" ? ["setValue"] : [
|
|
93
|
+
"setValue",
|
|
94
|
+
"clear",
|
|
95
|
+
"reset"
|
|
96
|
+
],
|
|
97
|
+
...name === "year-picker" ? { closed: true } : {},
|
|
98
|
+
...name === "calendar" || name === "range-calendar" || name === "year-picker" ? { items: (api) => api.roving.items().map((item) => ({
|
|
99
|
+
value: item.key,
|
|
100
|
+
label: item.el.getAttribute("aria-label") ?? item.el.textContent ?? item.key
|
|
101
|
+
})) } : {}
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
const dateField = modelControl("date-field");
|
|
106
|
+
const timeField = modelControl("time-field");
|
|
107
|
+
const datePicker = modelControl("date-picker");
|
|
108
|
+
const dateRangePicker = modelControl("date-range-picker");
|
|
109
|
+
const calendar = modelControl("calendar");
|
|
110
|
+
const rangeCalendar = modelControl("range-calendar");
|
|
111
|
+
const colorField = modelControl("color-field");
|
|
112
|
+
const yearPicker = modelControl("year-picker");
|
|
113
|
+
export { calendar, colorField, createModelController, dateField, datePicker, dateRangePicker, rangeCalendar, timeField, yearPicker };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Parts } from "@ui-sdk/react/primitive";
|
|
2
|
+
import { sectionIndex } from "./section-index.js";
|
|
3
|
+
declare const tags: {
|
|
4
|
+
readonly root: "nav";
|
|
5
|
+
readonly item: "a";
|
|
6
|
+
};
|
|
7
|
+
export declare const SectionIndex: Parts<typeof sectionIndex, typeof tags>;
|
|
8
|
+
export { sectionIndex, createSectionIndexController } from "./section-index.js";
|
|
9
|
+
export type { SectionIndexOptions, SectionIndexItem, SectionIndexController, SectionIndexReason, } from "./section-index.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createSectionIndexController, sectionIndex } from "./section-index.js";
|
|
3
|
+
import { createParts } from "@ui-sdk/react/primitive";
|
|
4
|
+
const SectionIndex = createParts(sectionIndex, {
|
|
5
|
+
root: "nav",
|
|
6
|
+
item: "a"
|
|
7
|
+
});
|
|
8
|
+
export { SectionIndex, createSectionIndexController, sectionIndex };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { createRovingFocus, definePrimitive, type ChangeDetails, type ChangeOptions, type SlotDefinition } from "@ui-sdk/core";
|
|
2
|
+
export type SectionIndexItem = {
|
|
3
|
+
value: string;
|
|
4
|
+
label: string;
|
|
5
|
+
href: `#${string}`;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export type SectionIndexReason = "item-press" | "drag" | "imperative";
|
|
9
|
+
export type SectionIndexController = {
|
|
10
|
+
getValue(): string | null;
|
|
11
|
+
setValue(value: string, options?: ChangeOptions<SectionIndexReason>): boolean;
|
|
12
|
+
};
|
|
13
|
+
export type SectionIndexOptions = {
|
|
14
|
+
items: readonly SectionIndexItem[];
|
|
15
|
+
value?: string | null;
|
|
16
|
+
defaultValue?: string | null;
|
|
17
|
+
onValueChange?: (value: string | null, details: ChangeDetails<SectionIndexReason>) => void;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
controller?: SectionIndexController;
|
|
20
|
+
};
|
|
21
|
+
type State = {
|
|
22
|
+
value: string | null;
|
|
23
|
+
focused: string | null;
|
|
24
|
+
disabled: boolean;
|
|
25
|
+
items: readonly SectionIndexItem[];
|
|
26
|
+
};
|
|
27
|
+
type ItemProps = {
|
|
28
|
+
value: string;
|
|
29
|
+
href: string;
|
|
30
|
+
disabled?: boolean;
|
|
31
|
+
};
|
|
32
|
+
type Api = SectionIndexController & {
|
|
33
|
+
roving: ReturnType<typeof createRovingFocus<State, ItemProps>>;
|
|
34
|
+
scrub(event: PointerEvent, el: HTMLElement): void;
|
|
35
|
+
};
|
|
36
|
+
type Slots = {
|
|
37
|
+
root: SlotDefinition<State, Api, {}, State>;
|
|
38
|
+
item: SlotDefinition<State, Api, ItemProps>;
|
|
39
|
+
};
|
|
40
|
+
export declare function createSectionIndexController(): SectionIndexController;
|
|
41
|
+
export declare const sectionIndex: ReturnType<typeof definePrimitive<SectionIndexOptions, State, Api, Slots>>;
|
|
42
|
+
export {};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { bindDrag, createChange, createController, createRovingFocus, definePrimitive, elements, selects } from "@ui-sdk/core";
|
|
2
|
+
// src/section-index/section-index.ts
|
|
3
|
+
function createSectionIndexController() {
|
|
4
|
+
return createController();
|
|
5
|
+
}
|
|
6
|
+
const sectionIndex = definePrimitive({
|
|
7
|
+
name: "section-index",
|
|
8
|
+
options: {
|
|
9
|
+
items: "list",
|
|
10
|
+
value: "string",
|
|
11
|
+
defaultValue: "string",
|
|
12
|
+
onValueChange: "callback",
|
|
13
|
+
disabled: "boolean",
|
|
14
|
+
controller: "value"
|
|
15
|
+
},
|
|
16
|
+
controlled: ["value"],
|
|
17
|
+
state: (options) => ({
|
|
18
|
+
value: options.value === void 0 ? options.defaultValue ?? null : options.value,
|
|
19
|
+
focused: null,
|
|
20
|
+
disabled: !!options.disabled,
|
|
21
|
+
items: options.items
|
|
22
|
+
}),
|
|
23
|
+
sync: (options) => ({
|
|
24
|
+
...options.value === void 0 ? {} : { value: options.value },
|
|
25
|
+
disabled: !!options.disabled,
|
|
26
|
+
items: options.items
|
|
27
|
+
}),
|
|
28
|
+
api: (context) => {
|
|
29
|
+
const change = createChange(context);
|
|
30
|
+
const roving = createRovingFocus(context, {
|
|
31
|
+
slot: "item",
|
|
32
|
+
key: "focused",
|
|
33
|
+
focusable: (item) => !context.store.get().disabled && !item.props.disabled,
|
|
34
|
+
preferred: (item) => item.key === context.store.get().value,
|
|
35
|
+
claims: (state, item) => !state.disabled && !item.props.disabled && (!state.value || item.key === state.value)
|
|
36
|
+
});
|
|
37
|
+
let release;
|
|
38
|
+
context.onDestroy(() => release?.());
|
|
39
|
+
const setValue = (value, options = {}) => {
|
|
40
|
+
const state = context.store.get();
|
|
41
|
+
const item = state.items.find((item) => item.value === value);
|
|
42
|
+
if (state.disabled || !item || item.disabled) return false;
|
|
43
|
+
const accepted = change("value", value, {
|
|
44
|
+
...options,
|
|
45
|
+
reason: options.reason ?? "imperative"
|
|
46
|
+
});
|
|
47
|
+
if (accepted || state.value === value) {
|
|
48
|
+
const document = elements(context, "root")[0]?.el.ownerDocument;
|
|
49
|
+
let fragment = item.href.slice(1);
|
|
50
|
+
try {
|
|
51
|
+
fragment = decodeURIComponent(fragment);
|
|
52
|
+
} catch {}
|
|
53
|
+
document?.getElementById(fragment)?.scrollIntoView();
|
|
54
|
+
}
|
|
55
|
+
return accepted;
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
getValue: () => context.store.get().value,
|
|
59
|
+
setValue,
|
|
60
|
+
roving,
|
|
61
|
+
scrub(event, el) {
|
|
62
|
+
if (release || context.store.get().disabled || event.button !== 0 || !event.isPrimary || event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
|
|
63
|
+
const pick = (y, event) => {
|
|
64
|
+
const nearest = roving.items().map((item) => {
|
|
65
|
+
const rect = item.el.getBoundingClientRect();
|
|
66
|
+
return {
|
|
67
|
+
item,
|
|
68
|
+
distance: Math.abs(y - rect.top - rect.height / 2)
|
|
69
|
+
};
|
|
70
|
+
}).sort((a, b) => a.distance - b.distance)[0]?.item;
|
|
71
|
+
if (nearest) setValue(nearest.key, {
|
|
72
|
+
reason: "drag",
|
|
73
|
+
event
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
if (event.target instanceof Element && event.target.closest("[aria-disabled=\"true\"]")) return;
|
|
77
|
+
event.preventDefault();
|
|
78
|
+
pick(event.clientY, event);
|
|
79
|
+
release = bindDrag(el, event, {
|
|
80
|
+
cancelOnEscape: true,
|
|
81
|
+
onMove: ({ y, event }) => pick(y, event),
|
|
82
|
+
onEnd: () => {
|
|
83
|
+
release = void 0;
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
},
|
|
89
|
+
slots: (slot) => ({
|
|
90
|
+
root: slot({
|
|
91
|
+
state: (state) => state,
|
|
92
|
+
attrs: (state) => ({ "data-disabled": state.disabled ? "" : void 0 }),
|
|
93
|
+
on: { pointerdown: (event, { api, el }) => api.scrub(event, el) }
|
|
94
|
+
}),
|
|
95
|
+
item: slot({
|
|
96
|
+
props: {
|
|
97
|
+
value: "string",
|
|
98
|
+
href: "string",
|
|
99
|
+
disabled: "boolean"
|
|
100
|
+
},
|
|
101
|
+
selects: selects({
|
|
102
|
+
value: ({ key }) => [key, null],
|
|
103
|
+
focused: ({ key }) => [key, null]
|
|
104
|
+
}),
|
|
105
|
+
attrs: (state, part) => ({
|
|
106
|
+
href: part.props.href,
|
|
107
|
+
draggable: "false",
|
|
108
|
+
"aria-disabled": state.disabled || part.props.disabled ? "true" : void 0,
|
|
109
|
+
"aria-current": state.value === part.key ? "location" : void 0,
|
|
110
|
+
tabindex: state.disabled || part.props.disabled ? "-1" : part.api.roving.tabIndex(state, part)
|
|
111
|
+
}),
|
|
112
|
+
press: ({ api, key, props, store }, event) => {
|
|
113
|
+
if (event instanceof MouseEvent && (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey)) return;
|
|
114
|
+
const state = store.get();
|
|
115
|
+
if (props.disabled || state.disabled) {
|
|
116
|
+
event?.preventDefault();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (!api.setValue(key, {
|
|
120
|
+
reason: "item-press",
|
|
121
|
+
...event ? { event } : {}
|
|
122
|
+
}) && state.value !== key) event?.preventDefault();
|
|
123
|
+
},
|
|
124
|
+
on: { keydown: (event, { api, el, key }) => {
|
|
125
|
+
if (event.key === "Enter" && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
|
|
126
|
+
event.preventDefault();
|
|
127
|
+
api.setValue(key, {
|
|
128
|
+
reason: "item-press",
|
|
129
|
+
event
|
|
130
|
+
});
|
|
131
|
+
} else api.roving.keydown(event, el);
|
|
132
|
+
} },
|
|
133
|
+
bind: (el, part) => part.api.roving.bind(el, part)
|
|
134
|
+
})
|
|
135
|
+
}),
|
|
136
|
+
agent: {
|
|
137
|
+
actions: ["setValue"],
|
|
138
|
+
closed: true,
|
|
139
|
+
items: (api) => api.roving.items().map(({ key, el }) => ({
|
|
140
|
+
value: key,
|
|
141
|
+
label: el.textContent ?? key
|
|
142
|
+
}))
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
export { createSectionIndexController, sectionIndex };
|