@wcomp/elementum 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +282 -2
- package/custom-elements.json +3613 -0
- package/mcp/server.js +593 -0
- package/package.json +77 -4
- package/skills/elementum-break-ui/CATALOG.md +11 -0
- package/skills/elementum-break-ui/SKILL.md +16 -0
- package/skills/elementum-mobile/SKILL.md +18 -0
- package/skills/elementum-motion/SKILL.md +18 -0
- package/skills/elementum-motion/STANDARDS.md +15 -0
- package/skills/elementum-penpot/SKILL.md +45 -0
- package/skills/elementum-ui/CATALOG.md +14 -0
- package/skills/elementum-ui/SKILL.md +21 -0
- package/src/ag-ui.js +474 -0
- package/src/behaviors/index.js +760 -0
- package/src/components/el-accordion.js +201 -0
- package/src/components/el-alert-dialog.js +186 -0
- package/src/components/el-alert.js +254 -0
- package/src/components/el-aspect-ratio.js +73 -0
- package/src/components/el-autocomplete.js +775 -0
- package/src/components/el-avatar.js +271 -0
- package/src/components/el-badge.js +131 -0
- package/src/components/el-border-beam.js +135 -0
- package/src/components/el-breadcrumb.js +203 -0
- package/src/components/el-button.js +231 -0
- package/src/components/el-calendar.js +436 -0
- package/src/components/el-callout.js +158 -0
- package/src/components/el-card.js +298 -0
- package/src/components/el-carousel.js +308 -0
- package/src/components/el-chart.js +632 -0
- package/src/components/el-checkbox-card.js +221 -0
- package/src/components/el-checkbox.js +208 -0
- package/src/components/el-chip.js +276 -0
- package/src/components/el-code-block.js +230 -0
- package/src/components/el-collapsible.js +124 -0
- package/src/components/el-command.js +556 -0
- package/src/components/el-context-menu.js +328 -0
- package/src/components/el-data-list.js +210 -0
- package/src/components/el-datepicker.js +192 -0
- package/src/components/el-dialog.js +272 -0
- package/src/components/el-diff-viewer.js +441 -0
- package/src/components/el-drawer.js +782 -0
- package/src/components/el-dropdown.js +182 -0
- package/src/components/el-dropzone.js +242 -0
- package/src/components/el-empty-state.js +130 -0
- package/src/components/el-field.js +394 -0
- package/src/components/el-form.js +185 -0
- package/src/components/el-input.js +229 -0
- package/src/components/el-inset.js +90 -0
- package/src/components/el-insight-card.js +263 -0
- package/src/components/el-kbd.js +151 -0
- package/src/components/el-menubar.js +254 -0
- package/src/components/el-meter.js +183 -0
- package/src/components/el-navbar.js +294 -0
- package/src/components/el-navigation-menu.js +327 -0
- package/src/components/el-number-field.js +510 -0
- package/src/components/el-number-flow.js +164 -0
- package/src/components/el-otp-input.js +266 -0
- package/src/components/el-pagination.js +231 -0
- package/src/components/el-popover.js +288 -0
- package/src/components/el-preview-card.js +202 -0
- package/src/components/el-progress.js +179 -0
- package/src/components/el-prompt-input.js +383 -0
- package/src/components/el-radio-card.js +360 -0
- package/src/components/el-radio.js +257 -0
- package/src/components/el-rating.js +165 -0
- package/src/components/el-reasoning-card.js +293 -0
- package/src/components/el-resizable.js +324 -0
- package/src/components/el-router.js +305 -0
- package/src/components/el-scroll-area.js +150 -0
- package/src/components/el-segmented-control.js +282 -0
- package/src/components/el-select.js +220 -0
- package/src/components/el-separator.js +85 -0
- package/src/components/el-sidebar.js +386 -0
- package/src/components/el-skeleton.js +156 -0
- package/src/components/el-slider.js +351 -0
- package/src/components/el-sources.js +288 -0
- package/src/components/el-spinner.js +159 -0
- package/src/components/el-streaming-text.js +199 -0
- package/src/components/el-switch.js +221 -0
- package/src/components/el-table.js +397 -0
- package/src/components/el-tabs.js +235 -0
- package/src/components/el-task-list.js +230 -0
- package/src/components/el-textarea.js +175 -0
- package/src/components/el-theme-switch.js +120 -0
- package/src/components/el-timeline.js +254 -0
- package/src/components/el-toast.js +778 -0
- package/src/components/el-toggle.js +311 -0
- package/src/components/el-tool-call.js +386 -0
- package/src/components/el-toolbar.js +183 -0
- package/src/components/el-tooltip.js +172 -0
- package/src/components/el-tree-view.js +261 -0
- package/src/components/el-visually-hidden.js +32 -0
- package/src/elementum.js +504 -0
- package/src/index.d.ts +1093 -0
- package/src/index.js +112 -0
- package/src/signal.js +215 -0
- package/src/ssr.js +136 -0
- package/src/template.js +321 -0
- package/src/tokens.css +416 -0
- package/tokens/figma-tokens.json +69 -0
- package/tokens/penpot-tokens.json +98 -0
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <el-number-field>
|
|
3
|
+
* Accessible numerical input with stepper buttons, precision clamping,
|
|
4
|
+
* and Figma/Blender-style drag scrubbing.
|
|
5
|
+
* Form-associated with native ElementInternals and zero dependencies.
|
|
6
|
+
*
|
|
7
|
+
* @element el-number-field
|
|
8
|
+
* @attr {number} value - Numerical value
|
|
9
|
+
* @attr {number} min - Minimum value constraint
|
|
10
|
+
* @attr {number} max - Maximum value constraint
|
|
11
|
+
* @attr {number} step - Step increment/decrement amount (default: 1)
|
|
12
|
+
* @attr {number} precision - Fixed decimal places (optional)
|
|
13
|
+
* @attr {string} prefix - Text or currency symbol shown before number (e.g. '$')
|
|
14
|
+
* @attr {string} suffix - Unit or text shown after number (e.g. 'px', '%')
|
|
15
|
+
* @attr {boolean} scrub - Enable click-and-drag scrubbing on prefix/label zone
|
|
16
|
+
* @attr {boolean} disabled - Disable the field
|
|
17
|
+
* @attr {boolean} readonly - Prevent user editing
|
|
18
|
+
* @attr {boolean} required - Form validation constraint
|
|
19
|
+
* @attr {string} name - Form submission field name
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import { Elementum } from '../elementum.js';
|
|
23
|
+
import { html, css } from '../template.js';
|
|
24
|
+
import { signal } from '../signal.js';
|
|
25
|
+
|
|
26
|
+
export class ElNumberField extends Elementum {
|
|
27
|
+
static formAssociated = true;
|
|
28
|
+
static observedAttributes = [
|
|
29
|
+
'value',
|
|
30
|
+
'min',
|
|
31
|
+
'max',
|
|
32
|
+
'step',
|
|
33
|
+
'precision',
|
|
34
|
+
'prefix',
|
|
35
|
+
'suffix',
|
|
36
|
+
'scrub',
|
|
37
|
+
'disabled',
|
|
38
|
+
'readonly',
|
|
39
|
+
'required',
|
|
40
|
+
'name'
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
value = signal(0);
|
|
44
|
+
min = signal(null);
|
|
45
|
+
max = signal(null);
|
|
46
|
+
step = signal(1);
|
|
47
|
+
precision = signal(null);
|
|
48
|
+
prefix = signal('');
|
|
49
|
+
suffix = signal('');
|
|
50
|
+
scrub = signal(false);
|
|
51
|
+
disabled = signal(false);
|
|
52
|
+
readonly = signal(false);
|
|
53
|
+
required = signal(false);
|
|
54
|
+
name = signal('');
|
|
55
|
+
|
|
56
|
+
#isScrubbing = signal(false);
|
|
57
|
+
#dragStartX = 0;
|
|
58
|
+
#dragStartVal = 0;
|
|
59
|
+
|
|
60
|
+
static styles = css`
|
|
61
|
+
:host {
|
|
62
|
+
display: inline-block;
|
|
63
|
+
font-family: var(--el-font-sans, system-ui, sans-serif);
|
|
64
|
+
color: var(--el-text-primary, #0f172a);
|
|
65
|
+
box-sizing: border-box;
|
|
66
|
+
vertical-align: middle;
|
|
67
|
+
width: 100%;
|
|
68
|
+
max-width: 220px;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.field-wrapper {
|
|
72
|
+
display: flex;
|
|
73
|
+
align-items: center;
|
|
74
|
+
position: relative;
|
|
75
|
+
background: var(--el-surface-base, #ffffff);
|
|
76
|
+
border: 1px solid var(--el-surface-border, #e5e7eb);
|
|
77
|
+
border-radius: var(--el-radius-md, 8px);
|
|
78
|
+
box-shadow: var(--el-shadow-subtle, 0 1px 2px 0 rgba(0, 0, 0, 0.05));
|
|
79
|
+
transition: border-color 150ms ease, box-shadow 150ms ease;
|
|
80
|
+
overflow: hidden;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.field-wrapper:focus-within {
|
|
84
|
+
border-color: var(--el-color-primary, #2563eb);
|
|
85
|
+
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
:host([data-scrubbing]) .field-wrapper {
|
|
89
|
+
border-color: var(--el-color-primary, #2563eb);
|
|
90
|
+
cursor: ew-resize !important;
|
|
91
|
+
user-select: none;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/* Stepper Buttons */
|
|
95
|
+
.stepper-btn {
|
|
96
|
+
display: flex;
|
|
97
|
+
align-items: center;
|
|
98
|
+
justify-content: center;
|
|
99
|
+
width: 32px;
|
|
100
|
+
height: 36px;
|
|
101
|
+
padding: 0;
|
|
102
|
+
background: var(--el-surface-muted, #f8fafc);
|
|
103
|
+
border: none;
|
|
104
|
+
color: var(--el-text-secondary, #475569);
|
|
105
|
+
cursor: pointer;
|
|
106
|
+
user-select: none;
|
|
107
|
+
transition: background-color 120ms ease, color 120ms ease;
|
|
108
|
+
flex-shrink: 0;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
.stepper-btn:hover:not(:disabled) {
|
|
112
|
+
background: var(--el-surface-border, #e2e8f0);
|
|
113
|
+
color: var(--el-text-primary, #0f172a);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
.stepper-btn:active:not(:disabled) {
|
|
117
|
+
background: rgba(37, 99, 235, 0.15);
|
|
118
|
+
color: var(--el-color-primary, #2563eb);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
.stepper-btn:disabled {
|
|
122
|
+
opacity: 0.35;
|
|
123
|
+
cursor: not-allowed;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
.stepper-btn.decrement {
|
|
127
|
+
border-right: 1px solid var(--el-surface-border, #e5e7eb);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.stepper-btn.increment {
|
|
131
|
+
border-left: 1px solid var(--el-surface-border, #e5e7eb);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/* Inner Input & Affixes */
|
|
135
|
+
.input-row {
|
|
136
|
+
display: flex;
|
|
137
|
+
align-items: center;
|
|
138
|
+
flex: 1;
|
|
139
|
+
height: 36px;
|
|
140
|
+
padding: 0 8px;
|
|
141
|
+
min-width: 0;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.affix {
|
|
145
|
+
font-size: 0.8125rem;
|
|
146
|
+
font-weight: 500;
|
|
147
|
+
color: var(--el-text-muted, #64748b);
|
|
148
|
+
user-select: none;
|
|
149
|
+
flex-shrink: 0;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
.affix.prefix.scrubbable {
|
|
153
|
+
cursor: ew-resize;
|
|
154
|
+
padding: 2px 4px;
|
|
155
|
+
border-radius: var(--el-radius-sm, 4px);
|
|
156
|
+
transition: background-color 120ms ease, color 120ms ease;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
.affix.prefix.scrubbable:hover {
|
|
160
|
+
background: var(--el-surface-muted, #f1f5f9);
|
|
161
|
+
color: var(--el-color-primary, #2563eb);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
input {
|
|
165
|
+
flex: 1;
|
|
166
|
+
width: 100%;
|
|
167
|
+
min-width: 0;
|
|
168
|
+
height: 100%;
|
|
169
|
+
border: none;
|
|
170
|
+
outline: none;
|
|
171
|
+
background: transparent;
|
|
172
|
+
font-family: inherit;
|
|
173
|
+
font-size: 0.875rem;
|
|
174
|
+
font-weight: 600;
|
|
175
|
+
font-variant-numeric: tabular-nums;
|
|
176
|
+
color: var(--el-text-primary, #0f172a);
|
|
177
|
+
text-align: center;
|
|
178
|
+
padding: 0 4px;
|
|
179
|
+
box-sizing: border-box;
|
|
180
|
+
-moz-appearance: textfield;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
input::-webkit-outer-spin-button,
|
|
184
|
+
input::-webkit-inner-spin-button {
|
|
185
|
+
-webkit-appearance: none;
|
|
186
|
+
margin: 0;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
:host([disabled]) {
|
|
190
|
+
opacity: 0.55;
|
|
191
|
+
pointer-events: none;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
:host([data-invalid]) .field-wrapper {
|
|
195
|
+
border-color: var(--el-color-danger, #ef4444);
|
|
196
|
+
}
|
|
197
|
+
`;
|
|
198
|
+
|
|
199
|
+
connected() {
|
|
200
|
+
this.#parseInitialAttributes();
|
|
201
|
+
this.setFormValue(String(this.value.value));
|
|
202
|
+
this.#validate();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
formReset() {
|
|
206
|
+
this.#parseInitialAttributes();
|
|
207
|
+
this.setFormValue(String(this.value.value));
|
|
208
|
+
this.#validate();
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
#parseInitialAttributes() {
|
|
212
|
+
if (this.hasAttribute('value')) {
|
|
213
|
+
this.value.value = Number(this.getAttribute('value')) || 0;
|
|
214
|
+
}
|
|
215
|
+
if (this.hasAttribute('min')) {
|
|
216
|
+
this.min.value = Number(this.getAttribute('min'));
|
|
217
|
+
}
|
|
218
|
+
if (this.hasAttribute('max')) {
|
|
219
|
+
this.max.value = Number(this.getAttribute('max'));
|
|
220
|
+
}
|
|
221
|
+
if (this.hasAttribute('step')) {
|
|
222
|
+
this.step.value = Number(this.getAttribute('step')) || 1;
|
|
223
|
+
}
|
|
224
|
+
if (this.hasAttribute('precision')) {
|
|
225
|
+
this.precision.value = Number(this.getAttribute('precision'));
|
|
226
|
+
}
|
|
227
|
+
this.prefix.value = this.getAttribute('prefix') || '';
|
|
228
|
+
this.suffix.value = this.getAttribute('suffix') || '';
|
|
229
|
+
this.scrub.value = this.hasAttribute('scrub');
|
|
230
|
+
this.disabled.value = this.hasAttribute('disabled');
|
|
231
|
+
this.readonly.value = this.hasAttribute('readonly');
|
|
232
|
+
this.required.value = this.hasAttribute('required');
|
|
233
|
+
this.name.value = this.getAttribute('name') || '';
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
attributeChanged(name, oldValue, newValue) {
|
|
237
|
+
if (oldValue === newValue) return;
|
|
238
|
+
|
|
239
|
+
if (name === 'value') {
|
|
240
|
+
this.value.value = Number(newValue) || 0;
|
|
241
|
+
this.setFormValue(String(this.value.value));
|
|
242
|
+
this.#validate();
|
|
243
|
+
} else if (name === 'min') {
|
|
244
|
+
this.min.value = newValue !== null ? Number(newValue) : null;
|
|
245
|
+
this.#validate();
|
|
246
|
+
} else if (name === 'max') {
|
|
247
|
+
this.max.value = newValue !== null ? Number(newValue) : null;
|
|
248
|
+
this.#validate();
|
|
249
|
+
} else if (name === 'step') {
|
|
250
|
+
this.step.value = Number(newValue) || 1;
|
|
251
|
+
} else if (name === 'precision') {
|
|
252
|
+
this.precision.value = newValue !== null ? Number(newValue) : null;
|
|
253
|
+
} else if (name === 'prefix') {
|
|
254
|
+
this.prefix.value = newValue || '';
|
|
255
|
+
} else if (name === 'suffix') {
|
|
256
|
+
this.suffix.value = newValue || '';
|
|
257
|
+
} else if (name === 'scrub') {
|
|
258
|
+
this.scrub.value = newValue !== null && newValue !== 'false';
|
|
259
|
+
} else if (name === 'disabled') {
|
|
260
|
+
this.disabled.value = newValue !== null && newValue !== 'false';
|
|
261
|
+
} else if (name === 'readonly') {
|
|
262
|
+
this.readonly.value = newValue !== null && newValue !== 'false';
|
|
263
|
+
} else if (name === 'required') {
|
|
264
|
+
this.required.value = newValue !== null && newValue !== 'false';
|
|
265
|
+
this.#validate();
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
#formatValue(val) {
|
|
270
|
+
if (val === null || val === undefined || isNaN(val)) return '0';
|
|
271
|
+
const prec = this.precision.value;
|
|
272
|
+
if (prec !== null && prec >= 0) {
|
|
273
|
+
return Number(val).toFixed(prec);
|
|
274
|
+
}
|
|
275
|
+
return String(val);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
#clamp(val) {
|
|
279
|
+
let res = val;
|
|
280
|
+
if (this.min.value !== null && res < this.min.value) {
|
|
281
|
+
res = this.min.value;
|
|
282
|
+
}
|
|
283
|
+
if (this.max.value !== null && res > this.max.value) {
|
|
284
|
+
res = this.max.value;
|
|
285
|
+
}
|
|
286
|
+
const prec = this.precision.value;
|
|
287
|
+
if (prec !== null && prec >= 0) {
|
|
288
|
+
res = Number(res.toFixed(prec));
|
|
289
|
+
}
|
|
290
|
+
return res;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
#updateValue(newVal, emitChange = true) {
|
|
294
|
+
const clamped = this.#clamp(newVal);
|
|
295
|
+
if (this.value.value !== clamped) {
|
|
296
|
+
this.value.value = clamped;
|
|
297
|
+
this.setFormValue(String(clamped));
|
|
298
|
+
this.#validate();
|
|
299
|
+
|
|
300
|
+
this.emit('input', { value: clamped });
|
|
301
|
+
if (emitChange) {
|
|
302
|
+
this.emit('change', { value: clamped });
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
stepUp(multiplier = 1) {
|
|
308
|
+
if (this.disabled.value || this.readonly.value) return;
|
|
309
|
+
const stp = (this.step.value || 1) * multiplier;
|
|
310
|
+
this.#updateValue(this.value.value + stp);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
stepDown(multiplier = 1) {
|
|
314
|
+
if (this.disabled.value || this.readonly.value) return;
|
|
315
|
+
const stp = (this.step.value || 1) * multiplier;
|
|
316
|
+
this.#updateValue(this.value.value - stp);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
#validate() {
|
|
320
|
+
const val = this.value.value;
|
|
321
|
+
const inputEl = this.root?.querySelector?.('input');
|
|
322
|
+
|
|
323
|
+
if (this.required.value && (val === null || val === undefined || isNaN(val))) {
|
|
324
|
+
this.setValidity({ valueMissing: true }, 'Value is required', inputEl);
|
|
325
|
+
this.setAttribute('data-invalid', '');
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (this.min.value !== null && val < this.min.value) {
|
|
330
|
+
this.setValidity({ rangeUnderflow: true }, `Value must be at least ${this.min.value}`, inputEl);
|
|
331
|
+
this.setAttribute('data-invalid', '');
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (this.max.value !== null && val > this.max.value) {
|
|
336
|
+
this.setValidity({ rangeOverflow: true }, `Value must not exceed ${this.max.value}`, inputEl);
|
|
337
|
+
this.setAttribute('data-invalid', '');
|
|
338
|
+
return false;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
this.setValidity({}, '', inputEl);
|
|
342
|
+
this.removeAttribute('data-invalid');
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
#onInputChange = (e) => {
|
|
347
|
+
const parsed = parseFloat(e.target.value);
|
|
348
|
+
if (!isNaN(parsed)) {
|
|
349
|
+
this.#updateValue(parsed, true);
|
|
350
|
+
} else {
|
|
351
|
+
e.target.value = this.#formatValue(this.value.value);
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
#onKeyDown = (e) => {
|
|
356
|
+
if (this.disabled.value || this.readonly.value) return;
|
|
357
|
+
|
|
358
|
+
const mult = e.shiftKey ? 10 : e.altKey ? 0.1 : 1;
|
|
359
|
+
|
|
360
|
+
if (e.key === 'ArrowUp') {
|
|
361
|
+
e.preventDefault();
|
|
362
|
+
this.stepUp(mult);
|
|
363
|
+
} else if (e.key === 'ArrowDown') {
|
|
364
|
+
e.preventDefault();
|
|
365
|
+
this.stepDown(mult);
|
|
366
|
+
} else if (e.key === 'Home' && this.min.value !== null) {
|
|
367
|
+
e.preventDefault();
|
|
368
|
+
this.#updateValue(this.min.value);
|
|
369
|
+
} else if (e.key === 'End' && this.max.value !== null) {
|
|
370
|
+
e.preventDefault();
|
|
371
|
+
this.#updateValue(this.max.value);
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
/* Pointer Scrubbing (Figma / Blender Drag Interaction) */
|
|
376
|
+
#onPointerDownScrub = (e) => {
|
|
377
|
+
if (this.disabled.value || this.readonly.value || !this.scrub.value) return;
|
|
378
|
+
e.preventDefault();
|
|
379
|
+
|
|
380
|
+
this.#isScrubbing.value = true;
|
|
381
|
+
this.setAttribute('data-scrubbing', '');
|
|
382
|
+
this.#dragStartX = e.clientX;
|
|
383
|
+
this.#dragStartVal = this.value.value;
|
|
384
|
+
|
|
385
|
+
const target = e.currentTarget;
|
|
386
|
+
if (target && typeof target.setPointerCapture === 'function') {
|
|
387
|
+
try {
|
|
388
|
+
target.setPointerCapture(e.pointerId);
|
|
389
|
+
} catch {
|
|
390
|
+
// Safe fallback
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
window.addEventListener('pointermove', this.#onPointerMoveScrub);
|
|
395
|
+
window.addEventListener('pointerup', this.#onPointerUpScrub, { once: true });
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
#onPointerMoveScrub = (e) => {
|
|
399
|
+
if (!this.#isScrubbing.value) return;
|
|
400
|
+
const dx = e.clientX - this.#dragStartX;
|
|
401
|
+
const mult = e.shiftKey ? 10 : e.altKey ? 0.1 : 1;
|
|
402
|
+
const stepVal = (this.step.value || 1) * mult;
|
|
403
|
+
const deltaSteps = Math.round(dx / 3); // 3px per step
|
|
404
|
+
const newVal = this.#dragStartVal + deltaSteps * stepVal;
|
|
405
|
+
|
|
406
|
+
this.#updateValue(newVal, false);
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
#onPointerUpScrub = () => {
|
|
410
|
+
if (this.#isScrubbing.value) {
|
|
411
|
+
this.#isScrubbing.value = false;
|
|
412
|
+
this.removeAttribute('data-scrubbing');
|
|
413
|
+
window.removeEventListener('pointermove', this.#onPointerMoveScrub);
|
|
414
|
+
this.emit('change', { value: this.value.value });
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
render() {
|
|
419
|
+
const isDis = this.disabled.value;
|
|
420
|
+
const isMin = this.min.value !== null && this.value.value <= this.min.value;
|
|
421
|
+
const isMax = this.max.value !== null && this.value.value >= this.max.value;
|
|
422
|
+
const scrubbable = this.scrub.value && !isDis && !this.readonly.value;
|
|
423
|
+
|
|
424
|
+
return html`
|
|
425
|
+
<div
|
|
426
|
+
class="field-wrapper"
|
|
427
|
+
part="wrapper"
|
|
428
|
+
role="group"
|
|
429
|
+
aria-label="Numerical Input"
|
|
430
|
+
>
|
|
431
|
+
<!-- Decrement Stepper Button -->
|
|
432
|
+
<button
|
|
433
|
+
type="button"
|
|
434
|
+
class="stepper-btn decrement"
|
|
435
|
+
part="decrement"
|
|
436
|
+
aria-label="Decrease value"
|
|
437
|
+
tabindex="-1"
|
|
438
|
+
?disabled=${isDis || isMin}
|
|
439
|
+
@click=${() => this.stepDown(1)}
|
|
440
|
+
>
|
|
441
|
+
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
|
|
442
|
+
<line x1="5" y1="12" x2="19" y2="12"></line>
|
|
443
|
+
</svg>
|
|
444
|
+
</button>
|
|
445
|
+
|
|
446
|
+
<!-- Input and Optional Affixes -->
|
|
447
|
+
<div class="input-row">
|
|
448
|
+
${() => {
|
|
449
|
+
const p = this.prefix.value;
|
|
450
|
+
if (!p) return null;
|
|
451
|
+
return html`
|
|
452
|
+
<span
|
|
453
|
+
class=${`affix prefix ${scrubbable ? 'scrubbable' : ''}`}
|
|
454
|
+
part="prefix"
|
|
455
|
+
title=${scrubbable ? 'Click and drag horizontally to scrub value' : ''}
|
|
456
|
+
@pointerdown=${this.#onPointerDownScrub}
|
|
457
|
+
>
|
|
458
|
+
${p}
|
|
459
|
+
</span>
|
|
460
|
+
`;
|
|
461
|
+
}}
|
|
462
|
+
|
|
463
|
+
<input
|
|
464
|
+
type="text"
|
|
465
|
+
inputmode="decimal"
|
|
466
|
+
part="input"
|
|
467
|
+
role="spinbutton"
|
|
468
|
+
aria-valuenow=${() => this.value.value}
|
|
469
|
+
aria-valuemin=${() => (this.min.value !== null ? this.min.value : null)}
|
|
470
|
+
aria-valuemax=${() => (this.max.value !== null ? this.max.value : null)}
|
|
471
|
+
.value=${() => this.#formatValue(this.value.value)}
|
|
472
|
+
?disabled=${isDis}
|
|
473
|
+
?readonly=${this.readonly.value}
|
|
474
|
+
?required=${this.required.value}
|
|
475
|
+
@change=${this.#onInputChange}
|
|
476
|
+
@keydown=${this.#onKeyDown}
|
|
477
|
+
/>
|
|
478
|
+
|
|
479
|
+
${() => {
|
|
480
|
+
const s = this.suffix.value;
|
|
481
|
+
if (!s) return null;
|
|
482
|
+
return html`
|
|
483
|
+
<span class="affix suffix" part="suffix">${s}</span>
|
|
484
|
+
`;
|
|
485
|
+
}}
|
|
486
|
+
</div>
|
|
487
|
+
|
|
488
|
+
<!-- Increment Stepper Button -->
|
|
489
|
+
<button
|
|
490
|
+
type="button"
|
|
491
|
+
class="stepper-btn increment"
|
|
492
|
+
part="increment"
|
|
493
|
+
aria-label="Increase value"
|
|
494
|
+
tabindex="-1"
|
|
495
|
+
?disabled=${isDis || isMax}
|
|
496
|
+
@click=${() => this.stepUp(1)}
|
|
497
|
+
>
|
|
498
|
+
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
|
|
499
|
+
<line x1="12" y1="5" x2="12" y2="19"></line>
|
|
500
|
+
<line x1="5" y1="12" x2="19" y2="12"></line>
|
|
501
|
+
</svg>
|
|
502
|
+
</button>
|
|
503
|
+
</div>
|
|
504
|
+
`;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
if (typeof customElements !== 'undefined' && !customElements.get('el-number-field')) {
|
|
509
|
+
customElements.define('el-number-field', ElNumberField);
|
|
510
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { Elementum } from '../elementum.js';
|
|
2
|
+
import { html, css } from '../template.js';
|
|
3
|
+
import { signal, effect } from '../signal.js';
|
|
4
|
+
import { formatLocaleNumber } from '../behaviors/index.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* <el-number-flow>
|
|
8
|
+
* Smooth rolling odometer / slot-machine character reel component.
|
|
9
|
+
*
|
|
10
|
+
* @attr {number|string} value - The numeric value to display and animate
|
|
11
|
+
* @attr {string} prefix - Optional prefix (e.g. '$' or '+')
|
|
12
|
+
* @attr {string} suffix - Optional suffix (e.g. '%' or 'ms')
|
|
13
|
+
* @attr {number} duration - Animation duration in ms (default: 400)
|
|
14
|
+
* @attr {string} locale - Optional BCP 47 locale for Intl.NumberFormat (e.g. 'en-US', 'de-DE')
|
|
15
|
+
*/
|
|
16
|
+
export class ElNumberFlow extends Elementum {
|
|
17
|
+
static observedAttributes = ['value', 'prefix', 'suffix', 'duration', 'animated', 'locale'];
|
|
18
|
+
|
|
19
|
+
value = signal('0');
|
|
20
|
+
prefix = signal('');
|
|
21
|
+
suffix = signal('');
|
|
22
|
+
duration = signal(400);
|
|
23
|
+
animated = signal(true);
|
|
24
|
+
locale = signal('');
|
|
25
|
+
|
|
26
|
+
connected() {
|
|
27
|
+
this.#sync();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
attributeChanged(name, oldValue, newValue) {
|
|
31
|
+
this.#sync();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
#sync() {
|
|
35
|
+
if (this.hasAttribute('value')) this.value.value = String(this.getAttribute('value') ?? '0');
|
|
36
|
+
if (this.hasAttribute('prefix')) this.prefix.value = this.getAttribute('prefix') || '';
|
|
37
|
+
if (this.hasAttribute('suffix')) this.suffix.value = this.getAttribute('suffix') || '';
|
|
38
|
+
if (this.hasAttribute('duration')) this.duration.value = Number(this.getAttribute('duration')) || 400;
|
|
39
|
+
if (this.hasAttribute('animated')) this.animated.value = this.getAttribute('animated') !== 'false';
|
|
40
|
+
if (this.hasAttribute('locale')) this.locale.value = this.getAttribute('locale') || '';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static styles = css`
|
|
44
|
+
:host {
|
|
45
|
+
display: inline-flex;
|
|
46
|
+
align-items: center;
|
|
47
|
+
font-variant-numeric: tabular-nums;
|
|
48
|
+
font-family: inherit;
|
|
49
|
+
line-height: 1;
|
|
50
|
+
vertical-align: baseline;
|
|
51
|
+
box-sizing: border-box;
|
|
52
|
+
overflow: hidden;
|
|
53
|
+
height: 1.25em;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.flow-wrapper {
|
|
57
|
+
display: inline-flex;
|
|
58
|
+
align-items: center;
|
|
59
|
+
height: 1.25em;
|
|
60
|
+
overflow: hidden;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.affix {
|
|
64
|
+
display: inline-block;
|
|
65
|
+
user-select: none;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.digit-column {
|
|
69
|
+
position: relative;
|
|
70
|
+
display: inline-flex;
|
|
71
|
+
flex-direction: column;
|
|
72
|
+
width: 1ch;
|
|
73
|
+
height: 1.25em;
|
|
74
|
+
transition: transform var(--flow-duration, 400ms) cubic-bezier(0.16, 1, 0.3, 1);
|
|
75
|
+
will-change: transform;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.digit-cell {
|
|
79
|
+
display: flex;
|
|
80
|
+
align-items: center;
|
|
81
|
+
justify-content: center;
|
|
82
|
+
height: 1.25em;
|
|
83
|
+
line-height: 1.25em;
|
|
84
|
+
text-align: center;
|
|
85
|
+
user-select: none;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.static-char {
|
|
89
|
+
display: inline-flex;
|
|
90
|
+
align-items: center;
|
|
91
|
+
justify-content: center;
|
|
92
|
+
height: 1.25em;
|
|
93
|
+
user-select: none;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@media (prefers-reduced-motion: reduce) {
|
|
97
|
+
.digit-column {
|
|
98
|
+
transition: none !important;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
`;
|
|
102
|
+
|
|
103
|
+
render() {
|
|
104
|
+
const rawVal = () => {
|
|
105
|
+
const str = String(this.value.value ?? '0');
|
|
106
|
+
const loc = (this.locale.value || '').trim();
|
|
107
|
+
if (loc && /^-?\d+(\.\d+)?$/.test(str)) {
|
|
108
|
+
return formatLocaleNumber(Number(str), loc);
|
|
109
|
+
}
|
|
110
|
+
return str;
|
|
111
|
+
};
|
|
112
|
+
const fullText = () => `${this.prefix.value}${rawVal()}${this.suffix.value}`;
|
|
113
|
+
|
|
114
|
+
return html`
|
|
115
|
+
<div
|
|
116
|
+
class="flow-wrapper"
|
|
117
|
+
part="wrapper"
|
|
118
|
+
role="text"
|
|
119
|
+
aria-label=${fullText}
|
|
120
|
+
style=${() => `--flow-duration: ${this.duration.value}ms;`}
|
|
121
|
+
>
|
|
122
|
+
${() => this.prefix.value ? html`<span class="affix prefix" part="prefix">${this.prefix}</span>` : null}
|
|
123
|
+
|
|
124
|
+
${() => {
|
|
125
|
+
const chars = rawVal().split('');
|
|
126
|
+
return chars.map((ch, idx) => {
|
|
127
|
+
const digit = parseInt(ch, 10);
|
|
128
|
+
if (!isNaN(digit)) {
|
|
129
|
+
// Roll to digit index: 0 is top, 9 is bottom
|
|
130
|
+
const translateY = -digit * 1.25;
|
|
131
|
+
return html`
|
|
132
|
+
<span
|
|
133
|
+
class="digit-column digit-reel"
|
|
134
|
+
part="digit"
|
|
135
|
+
style=${`transform: translateY(${translateY}em);`}
|
|
136
|
+
aria-hidden="true"
|
|
137
|
+
>
|
|
138
|
+
<span class="digit-cell">0</span>
|
|
139
|
+
<span class="digit-cell">1</span>
|
|
140
|
+
<span class="digit-cell">2</span>
|
|
141
|
+
<span class="digit-cell">3</span>
|
|
142
|
+
<span class="digit-cell">4</span>
|
|
143
|
+
<span class="digit-cell">5</span>
|
|
144
|
+
<span class="digit-cell">6</span>
|
|
145
|
+
<span class="digit-cell">7</span>
|
|
146
|
+
<span class="digit-cell">8</span>
|
|
147
|
+
<span class="digit-cell">9</span>
|
|
148
|
+
</span>
|
|
149
|
+
`;
|
|
150
|
+
} else {
|
|
151
|
+
return html`<span class="static-char" part="char" aria-hidden="true">${ch}</span>`;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}}
|
|
155
|
+
|
|
156
|
+
${() => this.suffix.value ? html`<span class="affix suffix" part="suffix">${this.suffix}</span>` : null}
|
|
157
|
+
</div>
|
|
158
|
+
`;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (typeof customElements !== 'undefined' && !customElements.get('el-number-flow')) {
|
|
163
|
+
customElements.define('el-number-flow', ElNumberFlow);
|
|
164
|
+
}
|