@llui/components 0.0.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 +21 -0
- package/README.md +143 -0
- package/dist/components/accordion.d.ts +115 -0
- package/dist/components/accordion.d.ts.map +1 -0
- package/dist/components/accordion.js +138 -0
- package/dist/components/alert-dialog.d.ts +45 -0
- package/dist/components/alert-dialog.d.ts.map +1 -0
- package/dist/components/alert-dialog.js +12 -0
- package/dist/components/angle-slider.d.ts +121 -0
- package/dist/components/angle-slider.d.ts.map +1 -0
- package/dist/components/angle-slider.js +145 -0
- package/dist/components/async-list.d.ts +104 -0
- package/dist/components/async-list.d.ts.map +1 -0
- package/dist/components/async-list.js +117 -0
- package/dist/components/avatar.d.ts +58 -0
- package/dist/components/avatar.d.ts.map +1 -0
- package/dist/components/avatar.js +43 -0
- package/dist/components/carousel.d.ts +128 -0
- package/dist/components/carousel.d.ts.map +1 -0
- package/dist/components/carousel.js +131 -0
- package/dist/components/cascade-select.d.ts +95 -0
- package/dist/components/cascade-select.d.ts.map +1 -0
- package/dist/components/cascade-select.js +100 -0
- package/dist/components/checkbox.d.ts +74 -0
- package/dist/components/checkbox.d.ts.map +1 -0
- package/dist/components/checkbox.js +73 -0
- package/dist/components/clipboard.d.ts +72 -0
- package/dist/components/clipboard.d.ts.map +1 -0
- package/dist/components/clipboard.js +73 -0
- package/dist/components/collapsible.d.ts +64 -0
- package/dist/components/collapsible.d.ts.map +1 -0
- package/dist/components/collapsible.js +51 -0
- package/dist/components/color-picker.d.ts +125 -0
- package/dist/components/color-picker.d.ts.map +1 -0
- package/dist/components/color-picker.js +169 -0
- package/dist/components/combobox.d.ts +163 -0
- package/dist/components/combobox.d.ts.map +1 -0
- package/dist/components/combobox.js +345 -0
- package/dist/components/context-menu.d.ts +105 -0
- package/dist/components/context-menu.d.ts.map +1 -0
- package/dist/components/context-menu.js +177 -0
- package/dist/components/date-input.d.ts +117 -0
- package/dist/components/date-input.d.ts.map +1 -0
- package/dist/components/date-input.js +149 -0
- package/dist/components/date-picker.d.ts +142 -0
- package/dist/components/date-picker.d.ts.map +1 -0
- package/dist/components/date-picker.js +294 -0
- package/dist/components/dialog.d.ts +152 -0
- package/dist/components/dialog.d.ts.map +1 -0
- package/dist/components/dialog.js +140 -0
- package/dist/components/drawer.d.ts +106 -0
- package/dist/components/drawer.d.ts.map +1 -0
- package/dist/components/drawer.js +136 -0
- package/dist/components/editable.d.ts +92 -0
- package/dist/components/editable.d.ts.map +1 -0
- package/dist/components/editable.js +112 -0
- package/dist/components/file-upload.d.ts +251 -0
- package/dist/components/file-upload.d.ts.map +1 -0
- package/dist/components/file-upload.js +324 -0
- package/dist/components/floating-panel.d.ts +171 -0
- package/dist/components/floating-panel.d.ts.map +1 -0
- package/dist/components/floating-panel.js +198 -0
- package/dist/components/hover-card.d.ts +85 -0
- package/dist/components/hover-card.d.ts.map +1 -0
- package/dist/components/hover-card.js +128 -0
- package/dist/components/image-cropper.d.ts +129 -0
- package/dist/components/image-cropper.d.ts.map +1 -0
- package/dist/components/image-cropper.js +208 -0
- package/dist/components/index.d.ts +109 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +54 -0
- package/dist/components/listbox.d.ts +98 -0
- package/dist/components/listbox.d.ts.map +1 -0
- package/dist/components/listbox.js +174 -0
- package/dist/components/marquee.d.ts +84 -0
- package/dist/components/marquee.d.ts.map +1 -0
- package/dist/components/marquee.js +73 -0
- package/dist/components/menu.d.ts +131 -0
- package/dist/components/menu.d.ts.map +1 -0
- package/dist/components/menu.js +262 -0
- package/dist/components/navigation-menu.d.ts +111 -0
- package/dist/components/navigation-menu.d.ts.map +1 -0
- package/dist/components/navigation-menu.js +102 -0
- package/dist/components/number-input.d.ts +106 -0
- package/dist/components/number-input.d.ts.map +1 -0
- package/dist/components/number-input.js +178 -0
- package/dist/components/pagination.d.ts +113 -0
- package/dist/components/pagination.d.ts.map +1 -0
- package/dist/components/pagination.js +135 -0
- package/dist/components/password-input.d.ts +64 -0
- package/dist/components/password-input.d.ts.map +1 -0
- package/dist/components/password-input.js +52 -0
- package/dist/components/pin-input.d.ts +89 -0
- package/dist/components/pin-input.d.ts.map +1 -0
- package/dist/components/pin-input.js +139 -0
- package/dist/components/popover.d.ts +116 -0
- package/dist/components/popover.d.ts.map +1 -0
- package/dist/components/popover.js +146 -0
- package/dist/components/presence.d.ts +71 -0
- package/dist/components/presence.d.ts.map +1 -0
- package/dist/components/presence.js +57 -0
- package/dist/components/progress.d.ts +74 -0
- package/dist/components/progress.d.ts.map +1 -0
- package/dist/components/progress.js +80 -0
- package/dist/components/qr-code.d.ts +114 -0
- package/dist/components/qr-code.d.ts.map +1 -0
- package/dist/components/qr-code.js +108 -0
- package/dist/components/radio-group.d.ts +89 -0
- package/dist/components/radio-group.d.ts.map +1 -0
- package/dist/components/radio-group.js +161 -0
- package/dist/components/rating-group.d.ts +88 -0
- package/dist/components/rating-group.d.ts.map +1 -0
- package/dist/components/rating-group.js +122 -0
- package/dist/components/scroll-area.d.ts +124 -0
- package/dist/components/scroll-area.d.ts.map +1 -0
- package/dist/components/scroll-area.js +152 -0
- package/dist/components/select.d.ts +161 -0
- package/dist/components/select.d.ts.map +1 -0
- package/dist/components/select.js +333 -0
- package/dist/components/signature-pad.d.ts +138 -0
- package/dist/components/signature-pad.d.ts.map +1 -0
- package/dist/components/signature-pad.js +142 -0
- package/dist/components/slider.d.ts +117 -0
- package/dist/components/slider.d.ts.map +1 -0
- package/dist/components/slider.js +210 -0
- package/dist/components/splitter.d.ts +87 -0
- package/dist/components/splitter.d.ts.map +1 -0
- package/dist/components/splitter.js +119 -0
- package/dist/components/steps.d.ts +104 -0
- package/dist/components/steps.d.ts.map +1 -0
- package/dist/components/steps.js +133 -0
- package/dist/components/switch.d.ts +66 -0
- package/dist/components/switch.d.ts.map +1 -0
- package/dist/components/switch.js +59 -0
- package/dist/components/tabs.d.ts +146 -0
- package/dist/components/tabs.d.ts.map +1 -0
- package/dist/components/tabs.js +244 -0
- package/dist/components/tags-input.d.ts +118 -0
- package/dist/components/tags-input.d.ts.map +1 -0
- package/dist/components/tags-input.js +168 -0
- package/dist/components/time-picker.d.ts +121 -0
- package/dist/components/time-picker.d.ts.map +1 -0
- package/dist/components/time-picker.js +147 -0
- package/dist/components/timer.d.ts +131 -0
- package/dist/components/timer.d.ts.map +1 -0
- package/dist/components/timer.js +117 -0
- package/dist/components/toast.d.ts +119 -0
- package/dist/components/toast.d.ts.map +1 -0
- package/dist/components/toast.js +102 -0
- package/dist/components/toc.d.ts +119 -0
- package/dist/components/toc.d.ts.map +1 -0
- package/dist/components/toc.js +107 -0
- package/dist/components/toggle-group.d.ts +80 -0
- package/dist/components/toggle-group.d.ts.map +1 -0
- package/dist/components/toggle-group.js +93 -0
- package/dist/components/toggle.d.ts +47 -0
- package/dist/components/toggle.d.ts.map +1 -0
- package/dist/components/toggle.js +41 -0
- package/dist/components/tooltip.d.ts +92 -0
- package/dist/components/tooltip.d.ts.map +1 -0
- package/dist/components/tooltip.js +147 -0
- package/dist/components/tour.d.ts +145 -0
- package/dist/components/tour.d.ts.map +1 -0
- package/dist/components/tour.js +133 -0
- package/dist/components/tree-view.d.ts +216 -0
- package/dist/components/tree-view.d.ts.map +1 -0
- package/dist/components/tree-view.js +293 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/patterns/confirm-dialog.d.ts +92 -0
- package/dist/patterns/confirm-dialog.d.ts.map +1 -0
- package/dist/patterns/confirm-dialog.js +92 -0
- package/dist/patterns/index.d.ts +3 -0
- package/dist/patterns/index.d.ts.map +1 -0
- package/dist/patterns/index.js +1 -0
- package/dist/utils/anatomy.d.ts +40 -0
- package/dist/utils/anatomy.d.ts.map +1 -0
- package/dist/utils/anatomy.js +41 -0
- package/dist/utils/aria-hidden.d.ts +12 -0
- package/dist/utils/aria-hidden.d.ts.map +1 -0
- package/dist/utils/aria-hidden.js +72 -0
- package/dist/utils/dismissable.d.ts +25 -0
- package/dist/utils/dismissable.d.ts.map +1 -0
- package/dist/utils/dismissable.js +65 -0
- package/dist/utils/dom.d.ts +8 -0
- package/dist/utils/dom.d.ts.map +1 -0
- package/dist/utils/dom.js +21 -0
- package/dist/utils/floating.d.ts +44 -0
- package/dist/utils/floating.d.ts.map +1 -0
- package/dist/utils/floating.js +44 -0
- package/dist/utils/focus-trap.d.ts +18 -0
- package/dist/utils/focus-trap.d.ts.map +1 -0
- package/dist/utils/focus-trap.js +85 -0
- package/dist/utils/focusables.d.ts +6 -0
- package/dist/utils/focusables.d.ts.map +1 -0
- package/dist/utils/focusables.js +65 -0
- package/dist/utils/index.d.ts +18 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +10 -0
- package/dist/utils/interact-outside.d.ts +26 -0
- package/dist/utils/interact-outside.d.ts.map +1 -0
- package/dist/utils/interact-outside.js +46 -0
- package/dist/utils/remove-scroll.d.ts +8 -0
- package/dist/utils/remove-scroll.d.ts.map +1 -0
- package/dist/utils/remove-scroll.js +37 -0
- package/dist/utils/tree-collection.d.ts +61 -0
- package/dist/utils/tree-collection.d.ts.map +1 -0
- package/dist/utils/tree-collection.js +137 -0
- package/dist/utils/typeahead.d.ts +49 -0
- package/dist/utils/typeahead.d.ts.map +1 -0
- package/dist/utils/typeahead.js +81 -0
- package/package.json +282 -0
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { show, portal, onMount, div } from '@llui/dom';
|
|
2
|
+
import { pushDismissable } from '../utils/dismissable';
|
|
3
|
+
import { attachFloating } from '../utils/floating';
|
|
4
|
+
import { typeaheadAccumulate, typeaheadMatchByItems, isTypeaheadKey, TYPEAHEAD_TIMEOUT_MS, } from '../utils/typeahead';
|
|
5
|
+
export function init(opts = {}) {
|
|
6
|
+
return {
|
|
7
|
+
open: false,
|
|
8
|
+
value: opts.value ?? [],
|
|
9
|
+
items: opts.items ?? [],
|
|
10
|
+
disabledItems: opts.disabledItems ?? [],
|
|
11
|
+
selectionMode: opts.selectionMode ?? 'single',
|
|
12
|
+
highlightedIndex: null,
|
|
13
|
+
disabled: opts.disabled ?? false,
|
|
14
|
+
required: opts.required ?? false,
|
|
15
|
+
typeahead: '',
|
|
16
|
+
typeaheadExpiresAt: 0,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function firstEnabledIndex(items, disabled) {
|
|
20
|
+
for (let i = 0; i < items.length; i++) {
|
|
21
|
+
if (!disabled.includes(items[i]))
|
|
22
|
+
return i;
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
function lastEnabledIndex(items, disabled) {
|
|
27
|
+
for (let i = items.length - 1; i >= 0; i--) {
|
|
28
|
+
if (!disabled.includes(items[i]))
|
|
29
|
+
return i;
|
|
30
|
+
}
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
function nextEnabledIndex(items, disabled, from, delta) {
|
|
34
|
+
if (items.length === 0)
|
|
35
|
+
return null;
|
|
36
|
+
const start = from === null ? (delta === 1 ? -1 : items.length) : from;
|
|
37
|
+
const n = items.length;
|
|
38
|
+
for (let i = 1; i <= n; i++) {
|
|
39
|
+
const idx = (start + delta * i + n * n) % n;
|
|
40
|
+
if (!disabled.includes(items[idx]))
|
|
41
|
+
return idx;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
function applySelection(state, value) {
|
|
46
|
+
if (state.disabledItems.includes(value))
|
|
47
|
+
return state.value;
|
|
48
|
+
if (state.selectionMode === 'single')
|
|
49
|
+
return [value];
|
|
50
|
+
const isActive = state.value.includes(value);
|
|
51
|
+
return isActive ? state.value.filter((v) => v !== value) : [...state.value, value];
|
|
52
|
+
}
|
|
53
|
+
/** Index of the first selected item, or null. */
|
|
54
|
+
function firstSelectedIndex(state) {
|
|
55
|
+
if (state.value.length === 0)
|
|
56
|
+
return null;
|
|
57
|
+
const idx = state.items.indexOf(state.value[0]);
|
|
58
|
+
return idx === -1 ? null : idx;
|
|
59
|
+
}
|
|
60
|
+
export function update(state, msg) {
|
|
61
|
+
if (state.disabled && msg.type !== 'setItems')
|
|
62
|
+
return [state, []];
|
|
63
|
+
switch (msg.type) {
|
|
64
|
+
case 'open': {
|
|
65
|
+
const highlightedIndex = firstSelectedIndex(state) ?? firstEnabledIndex(state.items, state.disabledItems);
|
|
66
|
+
return [{ ...state, open: true, highlightedIndex }, []];
|
|
67
|
+
}
|
|
68
|
+
case 'close':
|
|
69
|
+
return [{ ...state, open: false, highlightedIndex: null }, []];
|
|
70
|
+
case 'toggle':
|
|
71
|
+
return state.open
|
|
72
|
+
? [{ ...state, open: false, highlightedIndex: null }, []]
|
|
73
|
+
: [
|
|
74
|
+
{
|
|
75
|
+
...state,
|
|
76
|
+
open: true,
|
|
77
|
+
highlightedIndex: firstSelectedIndex(state) ?? firstEnabledIndex(state.items, state.disabledItems),
|
|
78
|
+
},
|
|
79
|
+
[],
|
|
80
|
+
];
|
|
81
|
+
case 'selectOption': {
|
|
82
|
+
const value = applySelection(state, msg.value);
|
|
83
|
+
// Single mode closes on selection; multi stays open
|
|
84
|
+
const open = state.selectionMode === 'single' ? false : state.open;
|
|
85
|
+
const highlightedIndex = open ? state.highlightedIndex : null;
|
|
86
|
+
return [{ ...state, value, open, highlightedIndex }, []];
|
|
87
|
+
}
|
|
88
|
+
case 'setValue':
|
|
89
|
+
return [{ ...state, value: msg.value }, []];
|
|
90
|
+
case 'clear':
|
|
91
|
+
return [{ ...state, value: [] }, []];
|
|
92
|
+
case 'highlight':
|
|
93
|
+
return [{ ...state, highlightedIndex: msg.index }, []];
|
|
94
|
+
case 'highlightNext':
|
|
95
|
+
return [
|
|
96
|
+
{
|
|
97
|
+
...state,
|
|
98
|
+
highlightedIndex: nextEnabledIndex(state.items, state.disabledItems, state.highlightedIndex, 1),
|
|
99
|
+
},
|
|
100
|
+
[],
|
|
101
|
+
];
|
|
102
|
+
case 'highlightPrev':
|
|
103
|
+
return [
|
|
104
|
+
{
|
|
105
|
+
...state,
|
|
106
|
+
highlightedIndex: nextEnabledIndex(state.items, state.disabledItems, state.highlightedIndex, -1),
|
|
107
|
+
},
|
|
108
|
+
[],
|
|
109
|
+
];
|
|
110
|
+
case 'highlightFirst':
|
|
111
|
+
return [
|
|
112
|
+
{ ...state, highlightedIndex: firstEnabledIndex(state.items, state.disabledItems) },
|
|
113
|
+
[],
|
|
114
|
+
];
|
|
115
|
+
case 'highlightLast':
|
|
116
|
+
return [
|
|
117
|
+
{ ...state, highlightedIndex: lastEnabledIndex(state.items, state.disabledItems) },
|
|
118
|
+
[],
|
|
119
|
+
];
|
|
120
|
+
case 'selectHighlighted': {
|
|
121
|
+
if (state.highlightedIndex === null)
|
|
122
|
+
return [state, []];
|
|
123
|
+
const v = state.items[state.highlightedIndex];
|
|
124
|
+
if (v === undefined)
|
|
125
|
+
return [state, []];
|
|
126
|
+
const value = applySelection(state, v);
|
|
127
|
+
const open = state.selectionMode === 'single' ? false : state.open;
|
|
128
|
+
return [{ ...state, value, open, highlightedIndex: open ? state.highlightedIndex : null }, []];
|
|
129
|
+
}
|
|
130
|
+
case 'setItems': {
|
|
131
|
+
const disabled = msg.disabled ?? state.disabledItems;
|
|
132
|
+
const value = state.value.filter((v) => msg.items.includes(v) && !disabled.includes(v));
|
|
133
|
+
return [{ ...state, items: msg.items, disabledItems: disabled, value }, []];
|
|
134
|
+
}
|
|
135
|
+
case 'typeahead': {
|
|
136
|
+
const acc = typeaheadAccumulate(state.typeahead, msg.char, msg.now, state.typeaheadExpiresAt);
|
|
137
|
+
const match = typeaheadMatchByItems(state.items, state.disabledItems, acc, state.highlightedIndex);
|
|
138
|
+
return [
|
|
139
|
+
{
|
|
140
|
+
...state,
|
|
141
|
+
typeahead: acc,
|
|
142
|
+
typeaheadExpiresAt: msg.now + TYPEAHEAD_TIMEOUT_MS,
|
|
143
|
+
highlightedIndex: match ?? state.highlightedIndex,
|
|
144
|
+
},
|
|
145
|
+
[],
|
|
146
|
+
];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const HIDDEN_STYLE = 'position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap;border:0;';
|
|
151
|
+
export function connect(get, send, opts) {
|
|
152
|
+
const base = opts.id;
|
|
153
|
+
const triggerId = `${base}:trigger`;
|
|
154
|
+
const contentId = `${base}:content`;
|
|
155
|
+
const itemId = (index) => `${base}:item:${index}`;
|
|
156
|
+
const placeholder = opts.placeholder ?? '';
|
|
157
|
+
const separator = opts.separator ?? ', ';
|
|
158
|
+
const handleTriggerKey = (e) => {
|
|
159
|
+
switch (e.key) {
|
|
160
|
+
case 'ArrowDown':
|
|
161
|
+
case 'Enter':
|
|
162
|
+
case ' ':
|
|
163
|
+
e.preventDefault();
|
|
164
|
+
send({ type: 'open' });
|
|
165
|
+
return;
|
|
166
|
+
case 'ArrowUp':
|
|
167
|
+
e.preventDefault();
|
|
168
|
+
send({ type: 'open' });
|
|
169
|
+
send({ type: 'highlightLast' });
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
const handleContentKey = (e) => {
|
|
174
|
+
switch (e.key) {
|
|
175
|
+
case 'ArrowDown':
|
|
176
|
+
e.preventDefault();
|
|
177
|
+
send({ type: 'highlightNext' });
|
|
178
|
+
return;
|
|
179
|
+
case 'ArrowUp':
|
|
180
|
+
e.preventDefault();
|
|
181
|
+
send({ type: 'highlightPrev' });
|
|
182
|
+
return;
|
|
183
|
+
case 'Home':
|
|
184
|
+
e.preventDefault();
|
|
185
|
+
send({ type: 'highlightFirst' });
|
|
186
|
+
return;
|
|
187
|
+
case 'End':
|
|
188
|
+
e.preventDefault();
|
|
189
|
+
send({ type: 'highlightLast' });
|
|
190
|
+
return;
|
|
191
|
+
case 'Enter':
|
|
192
|
+
case ' ':
|
|
193
|
+
e.preventDefault();
|
|
194
|
+
send({ type: 'selectHighlighted' });
|
|
195
|
+
return;
|
|
196
|
+
case 'Escape':
|
|
197
|
+
e.preventDefault();
|
|
198
|
+
send({ type: 'close' });
|
|
199
|
+
return;
|
|
200
|
+
default:
|
|
201
|
+
if (isTypeaheadKey(e)) {
|
|
202
|
+
send({ type: 'typeahead', char: e.key, now: Date.now() });
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
return {
|
|
207
|
+
trigger: {
|
|
208
|
+
type: 'button',
|
|
209
|
+
role: 'combobox',
|
|
210
|
+
'aria-haspopup': 'listbox',
|
|
211
|
+
'aria-expanded': (s) => get(s).open,
|
|
212
|
+
'aria-controls': contentId,
|
|
213
|
+
'aria-activedescendant': (s) => {
|
|
214
|
+
const idx = get(s).highlightedIndex;
|
|
215
|
+
return idx === null ? undefined : itemId(idx);
|
|
216
|
+
},
|
|
217
|
+
'aria-disabled': (s) => (get(s).disabled ? 'true' : undefined),
|
|
218
|
+
'aria-required': (s) => (get(s).required ? 'true' : undefined),
|
|
219
|
+
id: triggerId,
|
|
220
|
+
disabled: (s) => get(s).disabled,
|
|
221
|
+
'data-state': (s) => (get(s).open ? 'open' : 'closed'),
|
|
222
|
+
'data-scope': 'select',
|
|
223
|
+
'data-part': 'trigger',
|
|
224
|
+
onClick: () => send({ type: 'toggle' }),
|
|
225
|
+
onKeyDown: handleTriggerKey,
|
|
226
|
+
},
|
|
227
|
+
positioner: {
|
|
228
|
+
'data-scope': 'select',
|
|
229
|
+
'data-part': 'positioner',
|
|
230
|
+
style: 'position:absolute;top:0;left:0;',
|
|
231
|
+
},
|
|
232
|
+
content: {
|
|
233
|
+
role: 'listbox',
|
|
234
|
+
id: contentId,
|
|
235
|
+
'aria-multiselectable': (s) => (get(s).selectionMode === 'multiple' ? 'true' : undefined),
|
|
236
|
+
'aria-labelledby': triggerId,
|
|
237
|
+
tabIndex: -1,
|
|
238
|
+
'data-state': (s) => (get(s).open ? 'open' : 'closed'),
|
|
239
|
+
'data-scope': 'select',
|
|
240
|
+
'data-part': 'content',
|
|
241
|
+
onKeyDown: handleContentKey,
|
|
242
|
+
},
|
|
243
|
+
hiddenSelect: {
|
|
244
|
+
'aria-hidden': 'true',
|
|
245
|
+
tabIndex: -1,
|
|
246
|
+
style: HIDDEN_STYLE,
|
|
247
|
+
disabled: (s) => get(s).disabled,
|
|
248
|
+
multiple: (s) => get(s).selectionMode === 'multiple',
|
|
249
|
+
required: (s) => get(s).required,
|
|
250
|
+
'data-scope': 'select',
|
|
251
|
+
'data-part': 'hidden-select',
|
|
252
|
+
},
|
|
253
|
+
item: (value, index) => ({
|
|
254
|
+
item: {
|
|
255
|
+
role: 'option',
|
|
256
|
+
id: itemId(index),
|
|
257
|
+
'aria-selected': (s) => get(s).value.includes(value),
|
|
258
|
+
'aria-disabled': (s) => (get(s).disabledItems.includes(value) ? 'true' : undefined),
|
|
259
|
+
'data-state': (s) => (get(s).value.includes(value) ? 'selected' : undefined),
|
|
260
|
+
'data-highlighted': (s) => (get(s).highlightedIndex === index ? '' : undefined),
|
|
261
|
+
'data-disabled': (s) => (get(s).disabledItems.includes(value) ? '' : undefined),
|
|
262
|
+
'data-scope': 'select',
|
|
263
|
+
'data-part': 'item',
|
|
264
|
+
'data-value': value,
|
|
265
|
+
'data-index': String(index),
|
|
266
|
+
onClick: () => send({ type: 'selectOption', value }),
|
|
267
|
+
onPointerMove: () => send({ type: 'highlight', index }),
|
|
268
|
+
},
|
|
269
|
+
}),
|
|
270
|
+
valueText: (s) => {
|
|
271
|
+
const v = get(s).value;
|
|
272
|
+
if (v.length === 0)
|
|
273
|
+
return placeholder;
|
|
274
|
+
return v.join(separator);
|
|
275
|
+
},
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
export function overlay(opts) {
|
|
279
|
+
const target = opts.target ?? 'body';
|
|
280
|
+
const placement = opts.placement ?? 'bottom-start';
|
|
281
|
+
const offset = opts.offset ?? 4;
|
|
282
|
+
const flip = opts.flip !== false;
|
|
283
|
+
const shift = opts.shift !== false;
|
|
284
|
+
const sameWidth = opts.sameWidth !== false;
|
|
285
|
+
const parts = opts.parts;
|
|
286
|
+
const contentId = parts.content.id;
|
|
287
|
+
const triggerId = parts.trigger.id;
|
|
288
|
+
return show({
|
|
289
|
+
when: (s) => opts.get(s).open,
|
|
290
|
+
render: () => portal({
|
|
291
|
+
target,
|
|
292
|
+
render: () => {
|
|
293
|
+
onMount(() => {
|
|
294
|
+
const contentEl = document.getElementById(contentId);
|
|
295
|
+
const triggerEl = document.getElementById(triggerId);
|
|
296
|
+
if (!contentEl || !triggerEl)
|
|
297
|
+
return;
|
|
298
|
+
const cleanups = [];
|
|
299
|
+
const positioner = contentEl.closest('[data-part="positioner"]');
|
|
300
|
+
const floatingEl = positioner ?? contentEl;
|
|
301
|
+
if (sameWidth) {
|
|
302
|
+
floatingEl.style.minWidth = `${triggerEl.offsetWidth}px`;
|
|
303
|
+
}
|
|
304
|
+
cleanups.push(attachFloating({
|
|
305
|
+
anchor: triggerEl,
|
|
306
|
+
floating: floatingEl,
|
|
307
|
+
placement,
|
|
308
|
+
offset,
|
|
309
|
+
flip,
|
|
310
|
+
shift,
|
|
311
|
+
}));
|
|
312
|
+
cleanups.push(pushDismissable({
|
|
313
|
+
element: contentEl,
|
|
314
|
+
ignore: () => [triggerEl],
|
|
315
|
+
onDismiss: () => {
|
|
316
|
+
opts.send({ type: 'close' });
|
|
317
|
+
triggerEl.focus();
|
|
318
|
+
},
|
|
319
|
+
}));
|
|
320
|
+
contentEl.focus({ preventScroll: true });
|
|
321
|
+
return () => {
|
|
322
|
+
for (let i = cleanups.length - 1; i >= 0; i--)
|
|
323
|
+
cleanups[i]();
|
|
324
|
+
};
|
|
325
|
+
});
|
|
326
|
+
return [div(parts.positioner, opts.content())];
|
|
327
|
+
},
|
|
328
|
+
}),
|
|
329
|
+
enter: opts.transition?.enter,
|
|
330
|
+
leave: opts.transition?.leave,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
export const select = { init, update, connect, overlay };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { Send } from '@llui/dom';
|
|
2
|
+
/**
|
|
3
|
+
* Signature pad — capture free-form strokes on a canvas. The state
|
|
4
|
+
* machine tracks strokes as arrays of points; the view renders them
|
|
5
|
+
* onto a <canvas> element (consumer owns the canvas drawing, typically
|
|
6
|
+
* by redrawing all strokes in an onMount effect whenever `state.strokes`
|
|
7
|
+
* changes, or by drawing incrementally on each `addPoint` message).
|
|
8
|
+
*
|
|
9
|
+
* Pointer event wiring in the view layer:
|
|
10
|
+
*
|
|
11
|
+
* onPointerDown: (e) => {
|
|
12
|
+
* canvas.setPointerCapture(e.pointerId)
|
|
13
|
+
* send({ type: 'strokeStart', x: e.offsetX, y: e.offsetY })
|
|
14
|
+
* }
|
|
15
|
+
* onPointerMove: (e) => {
|
|
16
|
+
* if (state.drawing) send({ type: 'strokePoint', x: e.offsetX, y: e.offsetY })
|
|
17
|
+
* }
|
|
18
|
+
* onPointerUp: () => send({ type: 'strokeEnd' })
|
|
19
|
+
*/
|
|
20
|
+
export interface Point {
|
|
21
|
+
x: number;
|
|
22
|
+
y: number;
|
|
23
|
+
/** Pressure 0..1 (optional; from PointerEvent.pressure). */
|
|
24
|
+
pressure?: number;
|
|
25
|
+
}
|
|
26
|
+
export type Stroke = Point[];
|
|
27
|
+
export interface SignaturePadState {
|
|
28
|
+
strokes: Stroke[];
|
|
29
|
+
/** Stroke currently being drawn, or null. */
|
|
30
|
+
current: Stroke | null;
|
|
31
|
+
drawing: boolean;
|
|
32
|
+
disabled: boolean;
|
|
33
|
+
readOnly: boolean;
|
|
34
|
+
}
|
|
35
|
+
export type SignaturePadMsg = {
|
|
36
|
+
type: 'strokeStart';
|
|
37
|
+
x: number;
|
|
38
|
+
y: number;
|
|
39
|
+
pressure?: number;
|
|
40
|
+
} | {
|
|
41
|
+
type: 'strokePoint';
|
|
42
|
+
x: number;
|
|
43
|
+
y: number;
|
|
44
|
+
pressure?: number;
|
|
45
|
+
} | {
|
|
46
|
+
type: 'strokeEnd';
|
|
47
|
+
} | {
|
|
48
|
+
type: 'strokeCancel';
|
|
49
|
+
} | {
|
|
50
|
+
type: 'undo';
|
|
51
|
+
} | {
|
|
52
|
+
type: 'redo';
|
|
53
|
+
stroke: Stroke;
|
|
54
|
+
} | {
|
|
55
|
+
type: 'clear';
|
|
56
|
+
} | {
|
|
57
|
+
type: 'setStrokes';
|
|
58
|
+
strokes: Stroke[];
|
|
59
|
+
};
|
|
60
|
+
export interface SignaturePadInit {
|
|
61
|
+
strokes?: Stroke[];
|
|
62
|
+
disabled?: boolean;
|
|
63
|
+
readOnly?: boolean;
|
|
64
|
+
}
|
|
65
|
+
export declare function init(opts?: SignaturePadInit): SignaturePadState;
|
|
66
|
+
export declare function update(state: SignaturePadState, msg: SignaturePadMsg): [SignaturePadState, never[]];
|
|
67
|
+
export declare function isEmpty(state: SignaturePadState): boolean;
|
|
68
|
+
/** Total number of points across all strokes + current. */
|
|
69
|
+
export declare function pointCount(state: SignaturePadState): number;
|
|
70
|
+
/**
|
|
71
|
+
* Compute the axis-aligned bounding box of all strokes, or null if empty.
|
|
72
|
+
* Useful for cropping the exported signature tightly.
|
|
73
|
+
*/
|
|
74
|
+
export declare function getBounds(state: SignaturePadState): {
|
|
75
|
+
x: number;
|
|
76
|
+
y: number;
|
|
77
|
+
width: number;
|
|
78
|
+
height: number;
|
|
79
|
+
} | null;
|
|
80
|
+
export interface SignaturePadParts<S> {
|
|
81
|
+
root: {
|
|
82
|
+
role: 'application';
|
|
83
|
+
'aria-label': string;
|
|
84
|
+
'data-scope': 'signature-pad';
|
|
85
|
+
'data-part': 'root';
|
|
86
|
+
'data-disabled': (s: S) => '' | undefined;
|
|
87
|
+
'data-readonly': (s: S) => '' | undefined;
|
|
88
|
+
'data-drawing': (s: S) => '' | undefined;
|
|
89
|
+
};
|
|
90
|
+
control: {
|
|
91
|
+
'data-scope': 'signature-pad';
|
|
92
|
+
'data-part': 'control';
|
|
93
|
+
};
|
|
94
|
+
clearTrigger: {
|
|
95
|
+
type: 'button';
|
|
96
|
+
'aria-label': string;
|
|
97
|
+
disabled: (s: S) => boolean;
|
|
98
|
+
'data-scope': 'signature-pad';
|
|
99
|
+
'data-part': 'clear-trigger';
|
|
100
|
+
onClick: (e: MouseEvent) => void;
|
|
101
|
+
};
|
|
102
|
+
undoTrigger: {
|
|
103
|
+
type: 'button';
|
|
104
|
+
'aria-label': string;
|
|
105
|
+
disabled: (s: S) => boolean;
|
|
106
|
+
'data-scope': 'signature-pad';
|
|
107
|
+
'data-part': 'undo-trigger';
|
|
108
|
+
onClick: (e: MouseEvent) => void;
|
|
109
|
+
};
|
|
110
|
+
guide: {
|
|
111
|
+
'data-scope': 'signature-pad';
|
|
112
|
+
'data-part': 'guide';
|
|
113
|
+
'aria-hidden': 'true';
|
|
114
|
+
};
|
|
115
|
+
hiddenInput: {
|
|
116
|
+
type: 'hidden';
|
|
117
|
+
value: (s: S) => string;
|
|
118
|
+
name?: string;
|
|
119
|
+
'data-scope': 'signature-pad';
|
|
120
|
+
'data-part': 'hidden-input';
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
export interface ConnectOptions {
|
|
124
|
+
label?: string;
|
|
125
|
+
clearLabel?: string;
|
|
126
|
+
undoLabel?: string;
|
|
127
|
+
name?: string;
|
|
128
|
+
}
|
|
129
|
+
export declare function connect<S>(get: (s: S) => SignaturePadState, send: Send<SignaturePadMsg>, opts?: ConnectOptions): SignaturePadParts<S>;
|
|
130
|
+
export declare const signaturePad: {
|
|
131
|
+
init: typeof init;
|
|
132
|
+
update: typeof update;
|
|
133
|
+
connect: typeof connect;
|
|
134
|
+
isEmpty: typeof isEmpty;
|
|
135
|
+
pointCount: typeof pointCount;
|
|
136
|
+
getBounds: typeof getBounds;
|
|
137
|
+
};
|
|
138
|
+
//# sourceMappingURL=signature-pad.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signature-pad.d.ts","sourceRoot":"","sources":["../../src/components/signature-pad.ts"],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return {
|
|
3
|
+
strokes: opts.strokes ?? [],
|
|
4
|
+
current: null,
|
|
5
|
+
drawing: false,
|
|
6
|
+
disabled: opts.disabled ?? false,
|
|
7
|
+
readOnly: opts.readOnly ?? false,
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
function makePoint(x, y, pressure) {
|
|
11
|
+
return pressure !== undefined ? { x, y, pressure } : { x, y };
|
|
12
|
+
}
|
|
13
|
+
export function update(state, msg) {
|
|
14
|
+
if (state.disabled || state.readOnly) {
|
|
15
|
+
// Allow reads (undo/clear are still useful for clearing a disabled pad).
|
|
16
|
+
if (msg.type === 'strokeStart' || msg.type === 'strokePoint' || msg.type === 'strokeEnd') {
|
|
17
|
+
return [state, []];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
switch (msg.type) {
|
|
21
|
+
case 'strokeStart': {
|
|
22
|
+
const current = [makePoint(msg.x, msg.y, msg.pressure)];
|
|
23
|
+
return [{ ...state, current, drawing: true }, []];
|
|
24
|
+
}
|
|
25
|
+
case 'strokePoint': {
|
|
26
|
+
if (!state.drawing || state.current === null)
|
|
27
|
+
return [state, []];
|
|
28
|
+
const current = [...state.current, makePoint(msg.x, msg.y, msg.pressure)];
|
|
29
|
+
return [{ ...state, current }, []];
|
|
30
|
+
}
|
|
31
|
+
case 'strokeEnd': {
|
|
32
|
+
if (!state.drawing || state.current === null)
|
|
33
|
+
return [state, []];
|
|
34
|
+
// Drop 1-point strokes (accidental taps).
|
|
35
|
+
const strokes = state.current.length > 1 ? [...state.strokes, state.current] : state.strokes;
|
|
36
|
+
return [{ ...state, strokes, current: null, drawing: false }, []];
|
|
37
|
+
}
|
|
38
|
+
case 'strokeCancel':
|
|
39
|
+
return [{ ...state, current: null, drawing: false }, []];
|
|
40
|
+
case 'undo': {
|
|
41
|
+
if (state.strokes.length === 0)
|
|
42
|
+
return [state, []];
|
|
43
|
+
return [{ ...state, strokes: state.strokes.slice(0, -1) }, []];
|
|
44
|
+
}
|
|
45
|
+
case 'redo':
|
|
46
|
+
return [{ ...state, strokes: [...state.strokes, msg.stroke] }, []];
|
|
47
|
+
case 'clear':
|
|
48
|
+
return [{ ...state, strokes: [], current: null, drawing: false }, []];
|
|
49
|
+
case 'setStrokes':
|
|
50
|
+
return [{ ...state, strokes: msg.strokes }, []];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export function isEmpty(state) {
|
|
54
|
+
return state.strokes.length === 0 && state.current === null;
|
|
55
|
+
}
|
|
56
|
+
/** Total number of points across all strokes + current. */
|
|
57
|
+
export function pointCount(state) {
|
|
58
|
+
let n = state.current?.length ?? 0;
|
|
59
|
+
for (const s of state.strokes)
|
|
60
|
+
n += s.length;
|
|
61
|
+
return n;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Compute the axis-aligned bounding box of all strokes, or null if empty.
|
|
65
|
+
* Useful for cropping the exported signature tightly.
|
|
66
|
+
*/
|
|
67
|
+
export function getBounds(state) {
|
|
68
|
+
let minX = Infinity;
|
|
69
|
+
let minY = Infinity;
|
|
70
|
+
let maxX = -Infinity;
|
|
71
|
+
let maxY = -Infinity;
|
|
72
|
+
const all = state.current ? [...state.strokes, state.current] : state.strokes;
|
|
73
|
+
for (const stroke of all) {
|
|
74
|
+
for (const p of stroke) {
|
|
75
|
+
if (p.x < minX)
|
|
76
|
+
minX = p.x;
|
|
77
|
+
if (p.y < minY)
|
|
78
|
+
minY = p.y;
|
|
79
|
+
if (p.x > maxX)
|
|
80
|
+
maxX = p.x;
|
|
81
|
+
if (p.y > maxY)
|
|
82
|
+
maxY = p.y;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (minX === Infinity)
|
|
86
|
+
return null;
|
|
87
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
88
|
+
}
|
|
89
|
+
export function connect(get, send, opts = {}) {
|
|
90
|
+
return {
|
|
91
|
+
root: {
|
|
92
|
+
role: 'application',
|
|
93
|
+
'aria-label': opts.label ?? 'Signature pad',
|
|
94
|
+
'data-scope': 'signature-pad',
|
|
95
|
+
'data-part': 'root',
|
|
96
|
+
'data-disabled': (s) => (get(s).disabled ? '' : undefined),
|
|
97
|
+
'data-readonly': (s) => (get(s).readOnly ? '' : undefined),
|
|
98
|
+
'data-drawing': (s) => (get(s).drawing ? '' : undefined),
|
|
99
|
+
},
|
|
100
|
+
control: {
|
|
101
|
+
'data-scope': 'signature-pad',
|
|
102
|
+
'data-part': 'control',
|
|
103
|
+
},
|
|
104
|
+
clearTrigger: {
|
|
105
|
+
type: 'button',
|
|
106
|
+
'aria-label': opts.clearLabel ?? 'Clear signature',
|
|
107
|
+
disabled: (s) => isEmpty(get(s)),
|
|
108
|
+
'data-scope': 'signature-pad',
|
|
109
|
+
'data-part': 'clear-trigger',
|
|
110
|
+
onClick: () => send({ type: 'clear' }),
|
|
111
|
+
},
|
|
112
|
+
undoTrigger: {
|
|
113
|
+
type: 'button',
|
|
114
|
+
'aria-label': opts.undoLabel ?? 'Undo last stroke',
|
|
115
|
+
disabled: (s) => get(s).strokes.length === 0,
|
|
116
|
+
'data-scope': 'signature-pad',
|
|
117
|
+
'data-part': 'undo-trigger',
|
|
118
|
+
onClick: () => send({ type: 'undo' }),
|
|
119
|
+
},
|
|
120
|
+
guide: {
|
|
121
|
+
'data-scope': 'signature-pad',
|
|
122
|
+
'data-part': 'guide',
|
|
123
|
+
'aria-hidden': 'true',
|
|
124
|
+
},
|
|
125
|
+
hiddenInput: {
|
|
126
|
+
type: 'hidden',
|
|
127
|
+
// Serialize strokes as JSON for form submission.
|
|
128
|
+
value: (s) => JSON.stringify(get(s).strokes),
|
|
129
|
+
...(opts.name !== undefined ? { name: opts.name } : {}),
|
|
130
|
+
'data-scope': 'signature-pad',
|
|
131
|
+
'data-part': 'hidden-input',
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
export const signaturePad = {
|
|
136
|
+
init,
|
|
137
|
+
update,
|
|
138
|
+
connect,
|
|
139
|
+
isEmpty,
|
|
140
|
+
pointCount,
|
|
141
|
+
getBounds,
|
|
142
|
+
};
|