@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,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <el-field>
|
|
3
|
+
* Accessible form field wrapper providing automated label, description,
|
|
4
|
+
* error state reflection, and ARIA wiring for any slotted form control.
|
|
5
|
+
* Inspired by modern headless Field primitives (Base UI, WAI-ARIA APG).
|
|
6
|
+
*
|
|
7
|
+
* @element el-field
|
|
8
|
+
* @attr {string} label - Text label for the control
|
|
9
|
+
* @attr {string} description - Helper or context description
|
|
10
|
+
* @attr {string} error - Validation error message (forces data-invalid state)
|
|
11
|
+
* @attr {boolean} required - Displays required indicator
|
|
12
|
+
* @attr {boolean} disabled - Marks field and control as disabled
|
|
13
|
+
*
|
|
14
|
+
* @slot - Default slot for form control (<el-input>, <el-select>, <el-number-field>, native <input>)
|
|
15
|
+
* @slot label - Custom label content
|
|
16
|
+
* @slot description - Custom description content
|
|
17
|
+
* @slot error - Custom error content
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { Elementum } from '../elementum.js';
|
|
21
|
+
import { html, css } from '../template.js';
|
|
22
|
+
import { signal } from '../signal.js';
|
|
23
|
+
|
|
24
|
+
let fieldIdSeq = 0;
|
|
25
|
+
|
|
26
|
+
export class ElField extends Elementum {
|
|
27
|
+
static observedAttributes = ['label', 'description', 'error', 'required', 'disabled'];
|
|
28
|
+
|
|
29
|
+
label = signal('');
|
|
30
|
+
description = signal('');
|
|
31
|
+
error = signal('');
|
|
32
|
+
required = signal(false);
|
|
33
|
+
disabled = signal(false);
|
|
34
|
+
isFocused = signal(false);
|
|
35
|
+
isInvalid = signal(false);
|
|
36
|
+
activeErrorMessage = signal('');
|
|
37
|
+
|
|
38
|
+
#fieldId = `el-field-${++fieldIdSeq}`;
|
|
39
|
+
#slottedControl = null;
|
|
40
|
+
|
|
41
|
+
static styles = css`
|
|
42
|
+
:host {
|
|
43
|
+
display: flex;
|
|
44
|
+
flex-direction: column;
|
|
45
|
+
gap: var(--el-spacing-xs, 4px);
|
|
46
|
+
width: 100%;
|
|
47
|
+
box-sizing: border-box;
|
|
48
|
+
font-family: var(--el-font-sans, system-ui, sans-serif);
|
|
49
|
+
color: var(--el-text-primary, #0f172a);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.field-label-row {
|
|
53
|
+
display: flex;
|
|
54
|
+
align-items: center;
|
|
55
|
+
justify-content: space-between;
|
|
56
|
+
gap: var(--el-spacing-xs, 4px);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.field-label {
|
|
60
|
+
font-size: 0.8125rem;
|
|
61
|
+
font-weight: 600;
|
|
62
|
+
color: var(--el-text-primary, #0f172a);
|
|
63
|
+
cursor: default;
|
|
64
|
+
display: inline-flex;
|
|
65
|
+
align-items: center;
|
|
66
|
+
gap: 4px;
|
|
67
|
+
user-select: none;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
.required-indicator {
|
|
71
|
+
color: var(--el-color-danger, #ef4444);
|
|
72
|
+
font-weight: 700;
|
|
73
|
+
font-size: 0.875rem;
|
|
74
|
+
line-height: 1;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.field-description {
|
|
78
|
+
font-size: 0.75rem;
|
|
79
|
+
color: var(--el-text-muted, #64748b);
|
|
80
|
+
line-height: 1.4;
|
|
81
|
+
margin-top: 2px;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.field-error {
|
|
85
|
+
font-size: 0.75rem;
|
|
86
|
+
color: var(--el-color-danger, #ef4444);
|
|
87
|
+
font-weight: 500;
|
|
88
|
+
line-height: 1.4;
|
|
89
|
+
display: flex;
|
|
90
|
+
align-items: center;
|
|
91
|
+
gap: 6px;
|
|
92
|
+
margin-top: 2px;
|
|
93
|
+
animation: field-fade-in 150ms ease forwards;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.error-icon {
|
|
97
|
+
flex-shrink: 0;
|
|
98
|
+
width: 14px;
|
|
99
|
+
height: 14px;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.control-container {
|
|
103
|
+
width: 100%;
|
|
104
|
+
position: relative;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/* State reflections */
|
|
108
|
+
:host([data-invalid]) .field-label {
|
|
109
|
+
color: var(--el-color-danger, #ef4444);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
:host([data-disabled]) {
|
|
113
|
+
opacity: 0.6;
|
|
114
|
+
pointer-events: none;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
@keyframes field-fade-in {
|
|
118
|
+
from {
|
|
119
|
+
opacity: 0;
|
|
120
|
+
transform: translateY(-2px);
|
|
121
|
+
}
|
|
122
|
+
to {
|
|
123
|
+
opacity: 1;
|
|
124
|
+
transform: translateY(0);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
@media (prefers-reduced-motion: reduce) {
|
|
129
|
+
*, *::before, *::after {
|
|
130
|
+
animation: none !important;
|
|
131
|
+
transition: none !important;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
`;
|
|
135
|
+
|
|
136
|
+
connected() {
|
|
137
|
+
this.label.value = this.getAttribute('label') || '';
|
|
138
|
+
this.description.value = this.getAttribute('description') || '';
|
|
139
|
+
this.error.value = this.getAttribute('error') || '';
|
|
140
|
+
this.required.value = this.hasAttribute('required');
|
|
141
|
+
this.disabled.value = this.hasAttribute('disabled');
|
|
142
|
+
|
|
143
|
+
this.#syncState();
|
|
144
|
+
|
|
145
|
+
// Wire slotted controls
|
|
146
|
+
const defaultSlot = this.root?.querySelector('slot:not([name])');
|
|
147
|
+
if (defaultSlot) {
|
|
148
|
+
defaultSlot.addEventListener('slotchange', this.#onSlotChange);
|
|
149
|
+
}
|
|
150
|
+
this.#bindControl();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
attributeChanged(name, oldValue, newValue) {
|
|
154
|
+
if (oldValue === newValue) return;
|
|
155
|
+
|
|
156
|
+
if (name === 'label') {
|
|
157
|
+
this.label.value = newValue || '';
|
|
158
|
+
} else if (name === 'description') {
|
|
159
|
+
this.description.value = newValue || '';
|
|
160
|
+
} else if (name === 'error') {
|
|
161
|
+
this.error.value = newValue || '';
|
|
162
|
+
this.#syncState();
|
|
163
|
+
} else if (name === 'required') {
|
|
164
|
+
this.required.value = newValue !== null && newValue !== 'false';
|
|
165
|
+
if (this.#slottedControl && !this.#slottedControl.hasAttribute('required') && this.required.value) {
|
|
166
|
+
this.#slottedControl.setAttribute('required', '');
|
|
167
|
+
}
|
|
168
|
+
} else if (name === 'disabled') {
|
|
169
|
+
this.disabled.value = newValue !== null && newValue !== 'false';
|
|
170
|
+
if (this.#slottedControl) {
|
|
171
|
+
if (this.disabled.value) {
|
|
172
|
+
this.#slottedControl.setAttribute('disabled', '');
|
|
173
|
+
} else {
|
|
174
|
+
this.#slottedControl.removeAttribute('disabled');
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
#onSlotChange = () => {
|
|
181
|
+
this.#bindControl();
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
#bindControl() {
|
|
185
|
+
const control = this.#findControl();
|
|
186
|
+
if (!control || control === this.#slottedControl) return;
|
|
187
|
+
|
|
188
|
+
// Detach previous control listeners
|
|
189
|
+
if (this.#slottedControl) {
|
|
190
|
+
this.#slottedControl.removeEventListener('focusin', this.#onControlFocus);
|
|
191
|
+
this.#slottedControl.removeEventListener('focusout', this.#onControlBlur);
|
|
192
|
+
this.#slottedControl.removeEventListener('invalid', this.#onControlInvalid);
|
|
193
|
+
this.#slottedControl.removeEventListener('input', this.#onControlInput);
|
|
194
|
+
this.#slottedControl.removeEventListener('change', this.#onControlInput);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
this.#slottedControl = control;
|
|
198
|
+
|
|
199
|
+
// Automatic ARIA association
|
|
200
|
+
const labelId = `${this.#fieldId}-label`;
|
|
201
|
+
const descId = `${this.#fieldId}-desc`;
|
|
202
|
+
const errId = `${this.#fieldId}-err`;
|
|
203
|
+
|
|
204
|
+
if (!control.getAttribute('aria-labelledby')) {
|
|
205
|
+
control.setAttribute('aria-labelledby', labelId);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const describedBy = [];
|
|
209
|
+
if (this.description.value) describedBy.push(descId);
|
|
210
|
+
if (this.isInvalid.value) describedBy.push(errId);
|
|
211
|
+
if (describedBy.length > 0) {
|
|
212
|
+
control.setAttribute('aria-describedby', describedBy.join(' '));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (this.required.value && !control.hasAttribute('required')) {
|
|
216
|
+
control.setAttribute('required', '');
|
|
217
|
+
}
|
|
218
|
+
if (control.hasAttribute('required')) {
|
|
219
|
+
this.required.value = true;
|
|
220
|
+
}
|
|
221
|
+
if (this.disabled.value && !control.hasAttribute('disabled')) {
|
|
222
|
+
control.setAttribute('disabled', '');
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Attach reactive listeners
|
|
226
|
+
control.addEventListener('focusin', this.#onControlFocus);
|
|
227
|
+
control.addEventListener('focusout', this.#onControlBlur);
|
|
228
|
+
control.addEventListener('invalid', this.#onControlInvalid);
|
|
229
|
+
control.addEventListener('input', this.#onControlInput);
|
|
230
|
+
control.addEventListener('change', this.#onControlInput);
|
|
231
|
+
|
|
232
|
+
this.#checkControlValidity();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
#findControl() {
|
|
236
|
+
// Look into slotted elements
|
|
237
|
+
const elements = Array.from(this.children);
|
|
238
|
+
for (const el of elements) {
|
|
239
|
+
if (el.slot && el.slot !== '') continue;
|
|
240
|
+
// Is an input, custom element, or form-associated control
|
|
241
|
+
if (
|
|
242
|
+
el.tagName.toLowerCase().startsWith('el-') ||
|
|
243
|
+
['input', 'select', 'textarea', 'button'].includes(el.tagName.toLowerCase())
|
|
244
|
+
) {
|
|
245
|
+
return el;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return this.querySelector('input, select, textarea, [tabindex="0"]');
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
#onControlFocus = () => {
|
|
252
|
+
this.isFocused.value = true;
|
|
253
|
+
this.setAttribute('data-focused', '');
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
#onControlBlur = () => {
|
|
257
|
+
this.isFocused.value = false;
|
|
258
|
+
this.removeAttribute('data-focused');
|
|
259
|
+
this.#checkControlValidity();
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
#onControlInvalid = (e) => {
|
|
263
|
+
const msg = e.target?.validationMessage || 'Invalid input';
|
|
264
|
+
this.activeErrorMessage.value = msg;
|
|
265
|
+
this.isInvalid.value = true;
|
|
266
|
+
this.#syncState();
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
#onControlInput = () => {
|
|
270
|
+
this.#checkControlValidity();
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
#checkControlValidity() {
|
|
274
|
+
if (this.error.value) {
|
|
275
|
+
this.activeErrorMessage.value = this.error.value;
|
|
276
|
+
this.isInvalid.value = true;
|
|
277
|
+
this.#syncState();
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const ctrl = this.#slottedControl;
|
|
282
|
+
if (!ctrl) return;
|
|
283
|
+
|
|
284
|
+
if (ctrl.validity) {
|
|
285
|
+
if (!ctrl.validity.valid) {
|
|
286
|
+
this.activeErrorMessage.value = ctrl.validationMessage || 'Please check this field';
|
|
287
|
+
this.isInvalid.value = true;
|
|
288
|
+
} else {
|
|
289
|
+
this.isInvalid.value = false;
|
|
290
|
+
this.activeErrorMessage.value = '';
|
|
291
|
+
}
|
|
292
|
+
} else if (ctrl.hasAttribute('aria-invalid') && ctrl.getAttribute('aria-invalid') === 'true') {
|
|
293
|
+
this.isInvalid.value = true;
|
|
294
|
+
this.activeErrorMessage.value = ctrl.getAttribute('data-error-message') || 'Invalid field';
|
|
295
|
+
} else {
|
|
296
|
+
this.isInvalid.value = false;
|
|
297
|
+
this.activeErrorMessage.value = '';
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
this.#syncState();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
#syncState() {
|
|
304
|
+
const errText = this.error.value || this.activeErrorMessage.value;
|
|
305
|
+
const invalid = Boolean(errText);
|
|
306
|
+
|
|
307
|
+
if (invalid) {
|
|
308
|
+
this.setAttribute('data-invalid', '');
|
|
309
|
+
this.isInvalid.value = true;
|
|
310
|
+
this.activeErrorMessage.value = errText;
|
|
311
|
+
if (this.#slottedControl) {
|
|
312
|
+
this.#slottedControl.setAttribute('aria-invalid', 'true');
|
|
313
|
+
this.#slottedControl.setAttribute('aria-describedby', `${this.#fieldId}-err`);
|
|
314
|
+
}
|
|
315
|
+
} else {
|
|
316
|
+
this.removeAttribute('data-invalid');
|
|
317
|
+
this.isInvalid.value = false;
|
|
318
|
+
this.activeErrorMessage.value = '';
|
|
319
|
+
if (this.#slottedControl) {
|
|
320
|
+
this.#slottedControl.removeAttribute('aria-invalid');
|
|
321
|
+
if (this.description.value) {
|
|
322
|
+
this.#slottedControl.setAttribute('aria-describedby', `${this.#fieldId}-desc`);
|
|
323
|
+
} else {
|
|
324
|
+
this.#slottedControl.removeAttribute('aria-describedby');
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
if (this.required.value) {
|
|
330
|
+
this.setAttribute('data-required', '');
|
|
331
|
+
} else {
|
|
332
|
+
this.removeAttribute('data-required');
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (this.disabled.value) {
|
|
336
|
+
this.setAttribute('data-disabled', '');
|
|
337
|
+
} else {
|
|
338
|
+
this.removeAttribute('data-disabled');
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
render() {
|
|
343
|
+
const labelId = `${this.#fieldId}-label`;
|
|
344
|
+
const descId = `${this.#fieldId}-desc`;
|
|
345
|
+
const errId = `${this.#fieldId}-err`;
|
|
346
|
+
|
|
347
|
+
return html`
|
|
348
|
+
<!-- Label Row -->
|
|
349
|
+
<div class="field-label-row">
|
|
350
|
+
<label id=${labelId} class="field-label" part="label">
|
|
351
|
+
<slot name="label">${this.label}</slot>
|
|
352
|
+
${() => (this.required.value ? html`<span class="required-indicator" aria-hidden="true">*</span>` : null)}
|
|
353
|
+
</label>
|
|
354
|
+
</div>
|
|
355
|
+
|
|
356
|
+
<!-- Slotted Form Control Container -->
|
|
357
|
+
<div class="control-container" part="control">
|
|
358
|
+
<slot></slot>
|
|
359
|
+
</div>
|
|
360
|
+
|
|
361
|
+
<!-- Description Helper -->
|
|
362
|
+
${() => {
|
|
363
|
+
const desc = this.description.value;
|
|
364
|
+
if (!desc && !this.querySelector('[slot="description"]')) return null;
|
|
365
|
+
return html`
|
|
366
|
+
<div id=${descId} class="field-description" part="description">
|
|
367
|
+
<slot name="description">${desc}</slot>
|
|
368
|
+
</div>
|
|
369
|
+
`;
|
|
370
|
+
}}
|
|
371
|
+
|
|
372
|
+
<!-- Validation Error -->
|
|
373
|
+
${() => {
|
|
374
|
+
const isErr = this.isInvalid.value;
|
|
375
|
+
const msg = this.activeErrorMessage.value || this.error.value;
|
|
376
|
+
if (!isErr && !msg) return null;
|
|
377
|
+
return html`
|
|
378
|
+
<div id=${errId} class="field-error" part="error" role="alert" aria-live="polite">
|
|
379
|
+
<svg class="error-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
380
|
+
<circle cx="12" cy="12" r="10"></circle>
|
|
381
|
+
<line x1="12" y1="8" x2="12" y2="12"></line>
|
|
382
|
+
<line x1="12" y1="16" x2="12.01" y2="16"></line>
|
|
383
|
+
</svg>
|
|
384
|
+
<slot name="error">${msg}</slot>
|
|
385
|
+
</div>
|
|
386
|
+
`;
|
|
387
|
+
}}
|
|
388
|
+
`;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (typeof customElements !== 'undefined' && !customElements.get('el-field')) {
|
|
393
|
+
customElements.define('el-field', ElField);
|
|
394
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <el-form> & <el-fieldset>
|
|
3
|
+
* Accessible reactive form orchestrator inspired by Base UI and shadcn/ui.
|
|
4
|
+
* Intercepts submission, triggers validation across all Form Associated Custom Elements (FACE),
|
|
5
|
+
* exposes validity states, and groups fields with semantic fieldset/legend tags.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Elementum } from '../elementum.js';
|
|
9
|
+
import { html, css } from '../template.js';
|
|
10
|
+
import { signal } from '../signal.js';
|
|
11
|
+
|
|
12
|
+
export class ElFieldset extends Elementum {
|
|
13
|
+
static observedAttributes = ['legend', 'disabled'];
|
|
14
|
+
|
|
15
|
+
legend = signal('');
|
|
16
|
+
disabled = signal(false);
|
|
17
|
+
|
|
18
|
+
static styles = css`
|
|
19
|
+
:host {
|
|
20
|
+
display: block;
|
|
21
|
+
margin: 0 0 20px 0;
|
|
22
|
+
padding: 0;
|
|
23
|
+
border: none;
|
|
24
|
+
box-sizing: border-box;
|
|
25
|
+
font-family: var(--el-font-sans, system-ui, sans-serif);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
fieldset {
|
|
29
|
+
border: 1px solid var(--el-surface-border, #e2e8f0);
|
|
30
|
+
border-radius: var(--el-radius-md, 8px);
|
|
31
|
+
padding: 16px 20px 20px 20px;
|
|
32
|
+
margin: 0;
|
|
33
|
+
box-sizing: border-box;
|
|
34
|
+
background: var(--el-surface-base, #ffffff);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
:root[data-theme="dark"] fieldset {
|
|
38
|
+
border-color: #334155;
|
|
39
|
+
background: #1e293b;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
legend {
|
|
43
|
+
font-size: 0.95rem;
|
|
44
|
+
font-weight: 600;
|
|
45
|
+
padding: 0 6px;
|
|
46
|
+
color: var(--el-text-primary, #0f172a);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
:root[data-theme="dark"] legend {
|
|
50
|
+
color: #f8fafc;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
:host([disabled]) {
|
|
54
|
+
opacity: 0.6;
|
|
55
|
+
pointer-events: none;
|
|
56
|
+
}
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
connected() {
|
|
60
|
+
this.legend.value = this.getAttribute('legend') || '';
|
|
61
|
+
this.disabled.value = this.hasAttribute('disabled');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
attributeChanged(name, oldValue, newValue) {
|
|
65
|
+
if (name === 'legend') this.legend.value = newValue || '';
|
|
66
|
+
if (name === 'disabled') this.disabled.value = newValue !== null;
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
render() {
|
|
71
|
+
return html`
|
|
72
|
+
<fieldset ?disabled=${() => this.disabled.value}>
|
|
73
|
+
${() => (this.legend.value ? html`<legend part="legend">${() => this.legend.value}</legend>` : '')}
|
|
74
|
+
<slot></slot>
|
|
75
|
+
</fieldset>
|
|
76
|
+
`;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export class ElForm extends Elementum {
|
|
81
|
+
static observedAttributes = ['novalidate'];
|
|
82
|
+
|
|
83
|
+
novalidate = signal(false);
|
|
84
|
+
|
|
85
|
+
static styles = css`
|
|
86
|
+
:host {
|
|
87
|
+
display: block;
|
|
88
|
+
width: 100%;
|
|
89
|
+
box-sizing: border-box;
|
|
90
|
+
font-family: var(--el-font-sans, system-ui, sans-serif);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
form {
|
|
94
|
+
display: flex;
|
|
95
|
+
flex-direction: column;
|
|
96
|
+
width: 100%;
|
|
97
|
+
box-sizing: border-box;
|
|
98
|
+
}
|
|
99
|
+
`;
|
|
100
|
+
|
|
101
|
+
connected() {
|
|
102
|
+
this.novalidate.value = this.hasAttribute('novalidate');
|
|
103
|
+
this.addEventListener('submit', this.#onSubmit);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
disconnected() {
|
|
107
|
+
this.removeEventListener('submit', this.#onSubmit);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
attributeChanged(name, oldValue, newValue) {
|
|
111
|
+
if (name === 'novalidate') this.novalidate.value = newValue !== null;
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
#onSubmit = (e) => {
|
|
116
|
+
e.preventDefault();
|
|
117
|
+
const data = this.getFormData();
|
|
118
|
+
const isValid = this.validate();
|
|
119
|
+
|
|
120
|
+
if (!isValid && !this.novalidate.value) {
|
|
121
|
+
this.emit('form-invalid', { data });
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
this.emit('form-submit', { data });
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
validate() {
|
|
129
|
+
let allValid = true;
|
|
130
|
+
const elements = Array.from(this.querySelectorAll('*')).filter(
|
|
131
|
+
el => typeof el.checkValidity === 'function' || typeof el.reportValidity === 'function'
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
for (const el of elements) {
|
|
135
|
+
if (typeof el.reportValidity === 'function') {
|
|
136
|
+
const ok = el.reportValidity();
|
|
137
|
+
if (!ok) allValid = false;
|
|
138
|
+
} else if (typeof el.checkValidity === 'function') {
|
|
139
|
+
const ok = el.checkValidity();
|
|
140
|
+
if (!ok) allValid = false;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return allValid;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
getFormData() {
|
|
147
|
+
const data = {};
|
|
148
|
+
const elements = Array.from(this.querySelectorAll('[name]'));
|
|
149
|
+
for (const el of elements) {
|
|
150
|
+
const name = el.getAttribute('name');
|
|
151
|
+
if (!name) continue;
|
|
152
|
+
if ('value' in el) {
|
|
153
|
+
data[name] = typeof el.value === 'object' && el.value !== null && 'value' in el.value ? el.value.value : el.value;
|
|
154
|
+
} else if (el.hasAttribute('value')) {
|
|
155
|
+
data[name] = el.getAttribute('value');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return data;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
reset() {
|
|
162
|
+
const elements = Array.from(this.querySelectorAll('*'));
|
|
163
|
+
for (const el of elements) {
|
|
164
|
+
if (typeof el.formReset === 'function') el.formReset();
|
|
165
|
+
if (typeof el.formResetCallback === 'function') el.formResetCallback();
|
|
166
|
+
if (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.tagName === 'SELECT') {
|
|
167
|
+
el.value = '';
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
this.emit('form-reset');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
render() {
|
|
174
|
+
return html`
|
|
175
|
+
<form part="form" ?novalidate=${() => this.novalidate.value} onsubmit=${this.#onSubmit}>
|
|
176
|
+
<slot></slot>
|
|
177
|
+
</form>
|
|
178
|
+
`;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (typeof customElements !== 'undefined') {
|
|
183
|
+
if (!customElements.get('el-fieldset')) customElements.define('el-fieldset', ElFieldset);
|
|
184
|
+
if (!customElements.get('el-form')) customElements.define('el-form', ElForm);
|
|
185
|
+
}
|