@wcomp/elementum 0.0.0-stage → 0.1.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 +266 -2
- package/custom-elements.json +3613 -0
- package/mcp/server.js +408 -0
- package/package.json +72 -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/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 +220 -0
- package/src/components/el-calendar.js +413 -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 +228 -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 +212 -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 +182 -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 +153 -0
- package/src/components/el-otp-input.js +266 -0
- package/src/components/el-pagination.js +229 -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 +219 -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 +485 -0
- package/src/index.d.ts +1007 -0
- package/src/index.js +99 -0
- package/src/signal.js +194 -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,397 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <el-table>
|
|
3
|
+
* Fine-grained reactive data table with column sorting and row selection.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Elementum } from '../elementum.js';
|
|
7
|
+
import { html, css } from '../template.js';
|
|
8
|
+
import { signal, computed } from '../signal.js';
|
|
9
|
+
|
|
10
|
+
export class ElTable extends Elementum {
|
|
11
|
+
static observedAttributes = ['selectable', 'data', 'columns', 'hover', 'striped'];
|
|
12
|
+
|
|
13
|
+
selectable = signal(false);
|
|
14
|
+
hover = signal(false);
|
|
15
|
+
striped = signal(false);
|
|
16
|
+
selected = signal([]); // Array of selected row indices/keys
|
|
17
|
+
sortKey = signal('');
|
|
18
|
+
sortDirection = signal(''); // 'asc' | 'desc' | ''
|
|
19
|
+
|
|
20
|
+
#dataSignal = signal([]);
|
|
21
|
+
#columnsSignal = signal([]);
|
|
22
|
+
|
|
23
|
+
constructor() {
|
|
24
|
+
super();
|
|
25
|
+
Object.defineProperty(this, 'data', {
|
|
26
|
+
get: () => this.#dataSignal,
|
|
27
|
+
set: (val) => {
|
|
28
|
+
this.#applyData(val);
|
|
29
|
+
},
|
|
30
|
+
configurable: true
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
Object.defineProperty(this, 'columns', {
|
|
34
|
+
get: () => this.#columnsSignal,
|
|
35
|
+
set: (val) => {
|
|
36
|
+
this.#applyColumns(val);
|
|
37
|
+
},
|
|
38
|
+
configurable: true
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get tableData() {
|
|
43
|
+
return this.#dataSignal.value;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
set tableData(val) {
|
|
47
|
+
this.#applyData(val);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get tableColumns() {
|
|
51
|
+
return this.#columnsSignal.value;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
set tableColumns(val) {
|
|
55
|
+
this.#applyColumns(val);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
#applyData(val) {
|
|
59
|
+
let list = [];
|
|
60
|
+
if (Array.isArray(val)) {
|
|
61
|
+
list = val;
|
|
62
|
+
} else if (typeof val === 'string') {
|
|
63
|
+
try {
|
|
64
|
+
list = JSON.parse(val);
|
|
65
|
+
} catch {
|
|
66
|
+
list = [];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
this.#dataSignal.value = list;
|
|
70
|
+
|
|
71
|
+
// Auto-infer columns if not already defined
|
|
72
|
+
if ((!this.#columnsSignal.value || this.#columnsSignal.value.length === 0) && list.length > 0) {
|
|
73
|
+
const first = list[0];
|
|
74
|
+
if (first && typeof first === 'object') {
|
|
75
|
+
this.#columnsSignal.value = Object.keys(first).map((key) => ({
|
|
76
|
+
key,
|
|
77
|
+
label: key.charAt(0).toUpperCase() + key.slice(1).replace(/_/g, ' '),
|
|
78
|
+
sortable: true
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#applyColumns(val) {
|
|
85
|
+
let cols = [];
|
|
86
|
+
if (Array.isArray(val)) {
|
|
87
|
+
cols = val;
|
|
88
|
+
} else if (typeof val === 'string') {
|
|
89
|
+
try {
|
|
90
|
+
cols = JSON.parse(val);
|
|
91
|
+
} catch {
|
|
92
|
+
cols = [];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
this.#columnsSignal.value = cols;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
sortedData = computed(() => {
|
|
99
|
+
const raw = this.#dataSignal.value;
|
|
100
|
+
const list = Array.isArray(raw) ? [...raw] : [];
|
|
101
|
+
const key = this.sortKey.value;
|
|
102
|
+
const dir = this.sortDirection.value;
|
|
103
|
+
|
|
104
|
+
if (!key || !dir) return list;
|
|
105
|
+
|
|
106
|
+
return list.sort((a, b) => {
|
|
107
|
+
const valA = a[key];
|
|
108
|
+
const valB = b[key];
|
|
109
|
+
|
|
110
|
+
if (valA === valB) return 0;
|
|
111
|
+
if (valA === null || valA === undefined) return 1;
|
|
112
|
+
if (valB === null || valB === undefined) return -1;
|
|
113
|
+
|
|
114
|
+
let comparison = 0;
|
|
115
|
+
if (typeof valA === 'number' && typeof valB === 'number') {
|
|
116
|
+
comparison = valA - valB;
|
|
117
|
+
} else {
|
|
118
|
+
comparison = String(valA).localeCompare(String(valB));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return dir === 'desc' ? -comparison : comparison;
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
static styles = css`
|
|
126
|
+
:host {
|
|
127
|
+
display: block;
|
|
128
|
+
width: 100%;
|
|
129
|
+
overflow-x: auto;
|
|
130
|
+
-webkit-overflow-scrolling: touch;
|
|
131
|
+
font-family: inherit;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
table {
|
|
135
|
+
width: 100%;
|
|
136
|
+
min-width: 480px;
|
|
137
|
+
border-collapse: collapse;
|
|
138
|
+
text-align: left;
|
|
139
|
+
font-size: 0.875rem;
|
|
140
|
+
font-variant-numeric: tabular-nums;
|
|
141
|
+
color: var(--el-text-base, #111827);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
th {
|
|
145
|
+
background: var(--el-surface-muted, #f8fafc);
|
|
146
|
+
padding: 12px 16px;
|
|
147
|
+
font-weight: 600;
|
|
148
|
+
color: var(--el-text-muted, #475569);
|
|
149
|
+
border-bottom: 2px solid var(--el-surface-border, #e2e8f0);
|
|
150
|
+
user-select: none;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
th.sortable {
|
|
154
|
+
cursor: pointer;
|
|
155
|
+
transition: background-color 0.15s ease;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
th.sortable:hover {
|
|
159
|
+
background: var(--el-surface-border, #e2e8f0);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
.sort-icon {
|
|
163
|
+
display: inline-block;
|
|
164
|
+
margin-left: 6px;
|
|
165
|
+
font-size: 0.75rem;
|
|
166
|
+
color: var(--el-text-muted, #94a3b8);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
.sort-icon.active {
|
|
170
|
+
color: var(--el-color-primary, #2563eb);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
td {
|
|
174
|
+
padding: 12px 16px;
|
|
175
|
+
border-bottom: 1px solid var(--el-surface-border, #f1f5f9);
|
|
176
|
+
transition: background-color 0.1s ease;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
:host([striped]) tbody tr:nth-child(even) td {
|
|
180
|
+
background: var(--el-surface-muted, #f8fafc);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
:host([hover]) tbody tr:hover td {
|
|
184
|
+
background: rgba(37, 99, 235, 0.08);
|
|
185
|
+
cursor: pointer;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
:host([hover][striped]) tbody tr:nth-child(even):hover td {
|
|
189
|
+
background: rgba(37, 99, 235, 0.12);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
tr.selected td {
|
|
193
|
+
background: rgba(37, 99, 235, 0.08);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
.checkbox-cell {
|
|
197
|
+
width: 40px;
|
|
198
|
+
text-align: center;
|
|
199
|
+
padding-left: 12px;
|
|
200
|
+
padding-right: 8px;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
input[type="checkbox"] {
|
|
204
|
+
cursor: pointer;
|
|
205
|
+
width: 16px;
|
|
206
|
+
height: 16px;
|
|
207
|
+
accent-color: var(--el-color-primary, #2563eb);
|
|
208
|
+
}
|
|
209
|
+
`;
|
|
210
|
+
|
|
211
|
+
connected() {
|
|
212
|
+
this.selectable.value = this.hasAttribute('selectable');
|
|
213
|
+
this.hover.value = this.hasAttribute('hover');
|
|
214
|
+
this.striped.value = this.hasAttribute('striped');
|
|
215
|
+
if (this.hasAttribute('columns')) {
|
|
216
|
+
this.#applyColumns(this.getAttribute('columns'));
|
|
217
|
+
}
|
|
218
|
+
if (this.hasAttribute('data')) {
|
|
219
|
+
this.#applyData(this.getAttribute('data'));
|
|
220
|
+
} else if (this.#dataSignal.value.length === 0) {
|
|
221
|
+
const slottedTable = this.querySelector('table');
|
|
222
|
+
if (slottedTable) {
|
|
223
|
+
const headers = Array.from(slottedTable.querySelectorAll('thead th, tr:first-child th')).map((th, i) => {
|
|
224
|
+
const text = th.textContent.trim();
|
|
225
|
+
return {
|
|
226
|
+
key: th.getAttribute('data-key') || text.toLowerCase().replace(/[^a-z0-9_]/g, '_') || `col_${i}`,
|
|
227
|
+
label: text || `Column ${i + 1}`,
|
|
228
|
+
sortable: true
|
|
229
|
+
};
|
|
230
|
+
});
|
|
231
|
+
const rows = Array.from(slottedTable.querySelectorAll('tbody tr')).map(tr => {
|
|
232
|
+
const cells = Array.from(tr.querySelectorAll('td'));
|
|
233
|
+
const rowObj = {};
|
|
234
|
+
cells.forEach((td, i) => {
|
|
235
|
+
const key = headers[i]?.key || `col_${i}`;
|
|
236
|
+
rowObj[key] = td.textContent.trim();
|
|
237
|
+
});
|
|
238
|
+
return rowObj;
|
|
239
|
+
});
|
|
240
|
+
if (headers.length > 0) this.#columnsSignal.value = headers;
|
|
241
|
+
if (rows.length > 0) this.#dataSignal.value = rows;
|
|
242
|
+
slottedTable.style.display = 'none';
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
attributeChanged(name, oldValue, newValue) {
|
|
248
|
+
if (name === 'selectable') {
|
|
249
|
+
this.selectable.value = newValue !== null && newValue !== 'false';
|
|
250
|
+
return true;
|
|
251
|
+
} else if (name === 'hover') {
|
|
252
|
+
this.hover.value = newValue !== null && newValue !== 'false';
|
|
253
|
+
return true;
|
|
254
|
+
} else if (name === 'striped') {
|
|
255
|
+
this.striped.value = newValue !== null && newValue !== 'false';
|
|
256
|
+
return true;
|
|
257
|
+
} else if (name === 'data') {
|
|
258
|
+
this.#applyData(newValue);
|
|
259
|
+
return true;
|
|
260
|
+
} else if (name === 'columns') {
|
|
261
|
+
this.#applyColumns(newValue);
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
sortBy(key) {
|
|
267
|
+
if (this.sortKey.value === key) {
|
|
268
|
+
if (this.sortDirection.value === 'asc') {
|
|
269
|
+
this.sortDirection.value = 'desc';
|
|
270
|
+
} else if (this.sortDirection.value === 'desc') {
|
|
271
|
+
this.sortKey.value = '';
|
|
272
|
+
this.sortDirection.value = '';
|
|
273
|
+
}
|
|
274
|
+
} else {
|
|
275
|
+
this.sortKey.value = key;
|
|
276
|
+
this.sortDirection.value = 'asc';
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
this.emit('sort', {
|
|
280
|
+
key: this.sortKey.value,
|
|
281
|
+
direction: this.sortDirection.value,
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
toggleSelectAll() {
|
|
286
|
+
const dataList = this.sortedData.value;
|
|
287
|
+
if (this.selected.value.length === dataList.length && dataList.length > 0) {
|
|
288
|
+
this.selected.value = [];
|
|
289
|
+
} else {
|
|
290
|
+
this.selected.value = dataList.map((_, i) => i);
|
|
291
|
+
}
|
|
292
|
+
this.emit('selectionchange', { selected: this.selected.value });
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
toggleRow(index) {
|
|
296
|
+
const cur = [...this.selected.value];
|
|
297
|
+
const exists = cur.indexOf(index);
|
|
298
|
+
if (exists > -1) {
|
|
299
|
+
cur.splice(exists, 1);
|
|
300
|
+
} else {
|
|
301
|
+
cur.push(index);
|
|
302
|
+
}
|
|
303
|
+
this.selected.value = cur;
|
|
304
|
+
this.emit('selectionchange', { selected: this.selected.value });
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
isRowSelected(index) {
|
|
308
|
+
return this.selected.value.includes(index);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
render() {
|
|
312
|
+
return html`
|
|
313
|
+
<table part="table">
|
|
314
|
+
<thead>
|
|
315
|
+
<tr>
|
|
316
|
+
${() =>
|
|
317
|
+
this.selectable.value
|
|
318
|
+
? html`
|
|
319
|
+
<th class="checkbox-cell">
|
|
320
|
+
<input
|
|
321
|
+
type="checkbox"
|
|
322
|
+
.checked=${() =>
|
|
323
|
+
this.data.value.length > 0 &&
|
|
324
|
+
this.selected.value.length === this.data.value.length}
|
|
325
|
+
@change=${() => this.toggleSelectAll()}
|
|
326
|
+
/>
|
|
327
|
+
</th>
|
|
328
|
+
`
|
|
329
|
+
: ''}
|
|
330
|
+
${() => {
|
|
331
|
+
const cols = Array.isArray(this.columns.value) ? this.columns.value : [];
|
|
332
|
+
return cols.map(
|
|
333
|
+
(col) => html`
|
|
334
|
+
<th
|
|
335
|
+
class=${col.sortable ? 'sortable' : ''}
|
|
336
|
+
style=${col.width ? `width: ${col.width};` : ''}
|
|
337
|
+
@click=${col.sortable ? () => this.sortBy(col.key) : null}
|
|
338
|
+
>
|
|
339
|
+
${col.label || col.key}
|
|
340
|
+
${() =>
|
|
341
|
+
col.sortable
|
|
342
|
+
? html`
|
|
343
|
+
<span
|
|
344
|
+
class=${() =>
|
|
345
|
+
`sort-icon ${this.sortKey.value === col.key ? 'active' : ''}`}
|
|
346
|
+
>
|
|
347
|
+
${() =>
|
|
348
|
+
this.sortKey.value === col.key
|
|
349
|
+
? this.sortDirection.value === 'asc'
|
|
350
|
+
? '▲'
|
|
351
|
+
: '▼'
|
|
352
|
+
: '⇅'}
|
|
353
|
+
</span>
|
|
354
|
+
`
|
|
355
|
+
: ''}
|
|
356
|
+
</th>
|
|
357
|
+
`
|
|
358
|
+
);
|
|
359
|
+
}}
|
|
360
|
+
</tr>
|
|
361
|
+
</thead>
|
|
362
|
+
<tbody>
|
|
363
|
+
${() => {
|
|
364
|
+
const rows = Array.isArray(this.sortedData.value) ? this.sortedData.value : [];
|
|
365
|
+
const cols = Array.isArray(this.columns.value) ? this.columns.value : [];
|
|
366
|
+
return rows.map(
|
|
367
|
+
(row, idx) => html`
|
|
368
|
+
<tr class=${() => (this.isRowSelected(idx) ? 'selected' : '')}>
|
|
369
|
+
${() =>
|
|
370
|
+
this.selectable.value
|
|
371
|
+
? html`
|
|
372
|
+
<td class="checkbox-cell">
|
|
373
|
+
<input
|
|
374
|
+
type="checkbox"
|
|
375
|
+
.checked=${() => this.isRowSelected(idx)}
|
|
376
|
+
@change=${() => this.toggleRow(idx)}
|
|
377
|
+
/>
|
|
378
|
+
</td>
|
|
379
|
+
`
|
|
380
|
+
: ''}
|
|
381
|
+
${() =>
|
|
382
|
+
cols.map(
|
|
383
|
+
(col) => html`<td>${row[col.key] ?? ''}</td>`
|
|
384
|
+
)}
|
|
385
|
+
</tr>
|
|
386
|
+
`
|
|
387
|
+
);
|
|
388
|
+
}}
|
|
389
|
+
</tbody>
|
|
390
|
+
</table>
|
|
391
|
+
`;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (typeof customElements !== 'undefined' && !customElements.get('el-table')) {
|
|
396
|
+
customElements.define('el-table', ElTable);
|
|
397
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <el-tabs> and <el-tab>
|
|
3
|
+
* Accessible tabs component following W3C design patterns with keyboard navigation.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Elementum } from '../elementum.js';
|
|
7
|
+
import { html, css } from '../template.js';
|
|
8
|
+
import { signal } from '../signal.js';
|
|
9
|
+
|
|
10
|
+
export class ElTab extends Elementum {
|
|
11
|
+
static observedAttributes = ['label', 'value', 'name', 'disabled'];
|
|
12
|
+
|
|
13
|
+
label = signal('');
|
|
14
|
+
value = signal('');
|
|
15
|
+
name = signal('');
|
|
16
|
+
disabled = signal(false);
|
|
17
|
+
|
|
18
|
+
static styles = css`
|
|
19
|
+
:host {
|
|
20
|
+
display: none;
|
|
21
|
+
}
|
|
22
|
+
:host([active]) {
|
|
23
|
+
display: block;
|
|
24
|
+
}
|
|
25
|
+
`;
|
|
26
|
+
|
|
27
|
+
attributeChanged(name, oldValue, newValue) {
|
|
28
|
+
if (name === 'label') this.label.value = newValue || '';
|
|
29
|
+
else if (name === 'value') this.value.value = newValue || '';
|
|
30
|
+
else if (name === 'name') this.name.value = newValue || '';
|
|
31
|
+
else if (name === 'disabled') this.disabled.value = newValue !== null && newValue !== 'false';
|
|
32
|
+
this.closest('el-tabs')?.rescan?.();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
render() {
|
|
36
|
+
return html`<slot></slot>`;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (typeof customElements !== 'undefined' && !customElements.get('el-tab')) {
|
|
41
|
+
customElements.define('el-tab', ElTab);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class ElTabs extends Elementum {
|
|
45
|
+
static observedAttributes = ['value'];
|
|
46
|
+
|
|
47
|
+
activeTab = signal('');
|
|
48
|
+
tabs = signal([]); // [{ label, value, disabled, el }]
|
|
49
|
+
|
|
50
|
+
static styles = css`
|
|
51
|
+
:host {
|
|
52
|
+
display: block;
|
|
53
|
+
font-family: inherit;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.tablist {
|
|
57
|
+
display: flex;
|
|
58
|
+
gap: 4px;
|
|
59
|
+
border-bottom: 1px solid var(--el-surface-border, #e5e7eb);
|
|
60
|
+
margin-bottom: var(--el-spacing-md, 16px);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.tab-btn {
|
|
64
|
+
display: inline-flex;
|
|
65
|
+
align-items: center;
|
|
66
|
+
padding: var(--el-spacing-sm, 8px) var(--el-spacing-md, 16px);
|
|
67
|
+
font-size: 0.9375rem;
|
|
68
|
+
font-weight: 500;
|
|
69
|
+
color: var(--el-text-muted, #6b7280);
|
|
70
|
+
background: none;
|
|
71
|
+
border: none;
|
|
72
|
+
border-bottom: 2px solid transparent;
|
|
73
|
+
cursor: pointer;
|
|
74
|
+
transition: color 0.15s ease, border-color 0.15s ease;
|
|
75
|
+
outline: none;
|
|
76
|
+
user-select: none;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.tab-btn:hover:not(:disabled) {
|
|
80
|
+
color: var(--el-text-base, #111827);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.tab-btn:disabled {
|
|
84
|
+
opacity: 0.45;
|
|
85
|
+
cursor: not-allowed;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.tab-btn:focus-visible {
|
|
89
|
+
background: var(--el-surface-muted, #f3f4f6);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.tab-btn.active {
|
|
93
|
+
color: var(--el-color-primary, #2563eb);
|
|
94
|
+
border-bottom-color: var(--el-color-primary, #2563eb);
|
|
95
|
+
font-weight: 600;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
.panels {
|
|
99
|
+
display: block;
|
|
100
|
+
}
|
|
101
|
+
`;
|
|
102
|
+
|
|
103
|
+
connected() {
|
|
104
|
+
if (this.hasAttribute('value')) {
|
|
105
|
+
this.activeTab.value = this.getAttribute('value');
|
|
106
|
+
}
|
|
107
|
+
this.#scanTabs();
|
|
108
|
+
const observer = new MutationObserver(() => this.#scanTabs());
|
|
109
|
+
observer.observe(this, {
|
|
110
|
+
childList: true,
|
|
111
|
+
subtree: true,
|
|
112
|
+
attributes: true,
|
|
113
|
+
attributeFilter: ['label', 'value', 'name', 'disabled']
|
|
114
|
+
});
|
|
115
|
+
this.signal.addEventListener('abort', () => observer.disconnect());
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
rescan() {
|
|
119
|
+
this.#scanTabs();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
attributeChanged(name, oldValue, newValue) {
|
|
123
|
+
if (name === 'value' && newValue !== oldValue) {
|
|
124
|
+
this.selectTab(newValue);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#scanTabs() {
|
|
129
|
+
const tabEls = Array.from(this.querySelectorAll('el-tab'));
|
|
130
|
+
const tabData = tabEls.map((el, i) => {
|
|
131
|
+
const val = el.getAttribute('value') || el.getAttribute('name') || `tab-${i}`;
|
|
132
|
+
const lbl =
|
|
133
|
+
el.getAttribute('label') ||
|
|
134
|
+
(el.children.length === 0 ? el.textContent.trim() : '') ||
|
|
135
|
+
val ||
|
|
136
|
+
`Tab ${i + 1}`;
|
|
137
|
+
const disabled = el.hasAttribute('disabled') && el.getAttribute('disabled') !== 'false';
|
|
138
|
+
return { label: lbl, value: val, disabled, el };
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
this.tabs.value = tabData;
|
|
142
|
+
|
|
143
|
+
if (
|
|
144
|
+
(!this.activeTab.value || !tabData.some((t) => t.value === this.activeTab.value)) &&
|
|
145
|
+
tabData.length > 0
|
|
146
|
+
) {
|
|
147
|
+
const firstEnabled = tabData.find((t) => !t.disabled) || tabData[0];
|
|
148
|
+
this.selectTab(firstEnabled.value);
|
|
149
|
+
} else {
|
|
150
|
+
this.#syncActivePanel();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
selectTab(val) {
|
|
155
|
+
this.activeTab.value = val;
|
|
156
|
+
this.#syncActivePanel();
|
|
157
|
+
this.emit('tabchange', { value: val });
|
|
158
|
+
this.emit('change', { value: val });
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
#syncActivePanel() {
|
|
162
|
+
for (const item of this.tabs.value) {
|
|
163
|
+
if (item.value === this.activeTab.value) {
|
|
164
|
+
item.el.setAttribute('active', '');
|
|
165
|
+
} else {
|
|
166
|
+
item.el.removeAttribute('active');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
#onKeyDown = (e) => {
|
|
172
|
+
const tabList = this.tabs.value.filter((t) => !t.disabled);
|
|
173
|
+
if (tabList.length === 0) return;
|
|
174
|
+
const currentIndex = Math.max(0, tabList.findIndex((t) => t.value === this.activeTab.value));
|
|
175
|
+
|
|
176
|
+
let nextIndex = currentIndex;
|
|
177
|
+
if (e.key === 'ArrowRight') {
|
|
178
|
+
nextIndex = (currentIndex + 1) % tabList.length;
|
|
179
|
+
} else if (e.key === 'ArrowLeft') {
|
|
180
|
+
nextIndex = (currentIndex - 1 + tabList.length) % tabList.length;
|
|
181
|
+
} else if (e.key === 'Home') {
|
|
182
|
+
nextIndex = 0;
|
|
183
|
+
} else if (e.key === 'End') {
|
|
184
|
+
nextIndex = tabList.length - 1;
|
|
185
|
+
} else {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
e.preventDefault();
|
|
190
|
+
const targetVal = tabList[nextIndex].value;
|
|
191
|
+
this.selectTab(targetVal);
|
|
192
|
+
// Focus the button
|
|
193
|
+
const allIdx = this.tabs.value.findIndex((t) => t.value === targetVal);
|
|
194
|
+
const buttons = this.root.querySelectorAll('.tab-btn');
|
|
195
|
+
buttons[allIdx]?.focus();
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
render() {
|
|
199
|
+
return html`
|
|
200
|
+
<div
|
|
201
|
+
class="tablist"
|
|
202
|
+
role="tablist"
|
|
203
|
+
part="tablist"
|
|
204
|
+
@keydown=${this.#onKeyDown}
|
|
205
|
+
>
|
|
206
|
+
${() =>
|
|
207
|
+
this.tabs.value.map(
|
|
208
|
+
(tab) => html`
|
|
209
|
+
<button
|
|
210
|
+
type="button"
|
|
211
|
+
role="tab"
|
|
212
|
+
?disabled=${tab.disabled}
|
|
213
|
+
class=${() => `tab-btn ${this.activeTab.value === tab.value ? 'active' : ''}`}
|
|
214
|
+
aria-selected=${() => (this.activeTab.value === tab.value ? 'true' : 'false')}
|
|
215
|
+
tabindex=${() => (this.activeTab.value === tab.value ? '0' : '-1')}
|
|
216
|
+
@click=${() => {
|
|
217
|
+
if (!tab.disabled) this.selectTab(tab.value);
|
|
218
|
+
}}
|
|
219
|
+
>
|
|
220
|
+
${tab.label}
|
|
221
|
+
</button>
|
|
222
|
+
`
|
|
223
|
+
)}
|
|
224
|
+
</div>
|
|
225
|
+
|
|
226
|
+
<div class="panels" part="panels">
|
|
227
|
+
<slot></slot>
|
|
228
|
+
</div>
|
|
229
|
+
`;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (typeof customElements !== 'undefined' && !customElements.get('el-tabs')) {
|
|
234
|
+
customElements.define('el-tabs', ElTabs);
|
|
235
|
+
}
|