@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,159 @@
|
|
|
1
|
+
import { Elementum } from '../elementum.js';
|
|
2
|
+
import { html, css } from '../template.js';
|
|
3
|
+
import { signal } from '../signal.js';
|
|
4
|
+
|
|
5
|
+
export class ElSpinner extends Elementum {
|
|
6
|
+
static observedAttributes = ['size', 'variant', 'label'];
|
|
7
|
+
|
|
8
|
+
size = signal('md'); // 'sm' | 'md' | 'lg'
|
|
9
|
+
variant = signal('primary'); // 'primary' | 'muted' | 'white'
|
|
10
|
+
label = signal('Loading');
|
|
11
|
+
|
|
12
|
+
connected() {
|
|
13
|
+
this.#sync();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
attributeChanged(name, oldValue, newValue) {
|
|
17
|
+
this.#sync();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
#sync() {
|
|
21
|
+
this.size.value = this.getAttribute('size') || 'md';
|
|
22
|
+
this.variant.value = this.getAttribute('variant') || 'primary';
|
|
23
|
+
this.label.value = this.getAttribute('label') || 'Loading';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static styles = css`
|
|
27
|
+
:host {
|
|
28
|
+
display: inline-flex;
|
|
29
|
+
align-items: center;
|
|
30
|
+
gap: 8px;
|
|
31
|
+
vertical-align: middle;
|
|
32
|
+
box-sizing: border-box;
|
|
33
|
+
font-family: var(--el-font-sans, system-ui, sans-serif);
|
|
34
|
+
font-size: 0.875rem;
|
|
35
|
+
color: var(--el-text-primary, #0f172a);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.spinner {
|
|
39
|
+
animation: el-spin 0.8s linear infinite;
|
|
40
|
+
transform-origin: center;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
:host([size='sm']) .spinner,
|
|
44
|
+
.spinner.size-sm {
|
|
45
|
+
width: 16px;
|
|
46
|
+
height: 16px;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
:host([size='md']) .spinner,
|
|
50
|
+
.spinner,
|
|
51
|
+
.spinner.size-md {
|
|
52
|
+
width: 24px;
|
|
53
|
+
height: 24px;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
:host([size='lg']) .spinner,
|
|
57
|
+
.spinner.size-lg {
|
|
58
|
+
width: 36px;
|
|
59
|
+
height: 36px;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.circle-bg {
|
|
63
|
+
opacity: 0.25;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.circle-arc {
|
|
67
|
+
opacity: 0.9;
|
|
68
|
+
stroke-dasharray: 80;
|
|
69
|
+
stroke-dashoffset: 60;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.variant-primary {
|
|
73
|
+
color: var(--el-color-primary, #2563eb);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.variant-secondary {
|
|
77
|
+
color: var(--el-text-secondary, #475569);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.variant-success {
|
|
81
|
+
color: var(--el-color-success, #16a34a);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.variant-danger {
|
|
85
|
+
color: var(--el-color-danger, #dc2626);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.variant-warning {
|
|
89
|
+
color: var(--el-color-warning, #d97706);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.variant-info {
|
|
93
|
+
color: var(--el-color-info, #0284c7);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.variant-muted {
|
|
97
|
+
color: var(--el-text-muted, #64748b);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.variant-white {
|
|
101
|
+
color: #ffffff;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
@keyframes el-spin {
|
|
105
|
+
from {
|
|
106
|
+
transform: rotate(0deg);
|
|
107
|
+
}
|
|
108
|
+
to {
|
|
109
|
+
transform: rotate(360deg);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.spinner-label {
|
|
114
|
+
line-height: 1;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
.sr-only {
|
|
118
|
+
position: absolute;
|
|
119
|
+
width: 1px;
|
|
120
|
+
height: 1px;
|
|
121
|
+
padding: 0;
|
|
122
|
+
margin: -1px;
|
|
123
|
+
overflow: hidden;
|
|
124
|
+
clip: rect(0, 0, 0, 0);
|
|
125
|
+
white-space: nowrap;
|
|
126
|
+
border: 0;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
@media (prefers-reduced-motion: reduce) {
|
|
130
|
+
svg, circle {
|
|
131
|
+
animation-duration: 3s;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
`;
|
|
135
|
+
|
|
136
|
+
render() {
|
|
137
|
+
return html`
|
|
138
|
+
<div
|
|
139
|
+
class=${() => `spinner size-${this.size.value} variant-${this.variant.value}`}
|
|
140
|
+
part="spinner"
|
|
141
|
+
role="status"
|
|
142
|
+
aria-label=${() => this.label.value}
|
|
143
|
+
>
|
|
144
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" style="width:100%;height:100%;">
|
|
145
|
+
<circle class="circle-bg" cx="12" cy="12" r="10" stroke="currentColor"></circle>
|
|
146
|
+
<path class="circle-arc" d="M12 2a10 10 0 0 1 10 10" stroke="currentColor" stroke-linecap="round"></path>
|
|
147
|
+
</svg>
|
|
148
|
+
</div>
|
|
149
|
+
${() =>
|
|
150
|
+
this.label.value
|
|
151
|
+
? html`<span class="spinner-label" part="label"><slot>${() => this.label.value}</slot></span>`
|
|
152
|
+
: html`<slot></slot>`}
|
|
153
|
+
`;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (typeof customElements !== 'undefined' && !customElements.get('el-spinner')) {
|
|
158
|
+
customElements.define('el-spinner', ElSpinner);
|
|
159
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { Elementum } from '../elementum.js';
|
|
2
|
+
import { html, css } from '../template.js';
|
|
3
|
+
import { signal } from '../signal.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* <el-streaming-text>
|
|
7
|
+
* Realistic token-by-token streaming animation for AI generation responses,
|
|
8
|
+
* with customizable cadence, blinking cursor, and completion events.
|
|
9
|
+
*
|
|
10
|
+
* @element el-streaming-text
|
|
11
|
+
* @fires stream-start - Dispatched when streaming begins
|
|
12
|
+
* @fires stream-end - Dispatched when all characters/tokens have streamed
|
|
13
|
+
* @fires stream-tick - Dispatched on each character/token typed
|
|
14
|
+
* @csspart text - The text output container
|
|
15
|
+
* @csspart cursor - The animated blinking cursor
|
|
16
|
+
*/
|
|
17
|
+
export class ElStreamingText extends Elementum {
|
|
18
|
+
static observedAttributes = ['text', 'speed', 'cursor', 'auto-start'];
|
|
19
|
+
|
|
20
|
+
text = signal('');
|
|
21
|
+
displayedText = signal('');
|
|
22
|
+
speed = signal(20); // ms per character
|
|
23
|
+
showCursor = signal(true);
|
|
24
|
+
isStreaming = signal(false);
|
|
25
|
+
isComplete = signal(false);
|
|
26
|
+
|
|
27
|
+
#timer = null;
|
|
28
|
+
#targetIndex = 0;
|
|
29
|
+
|
|
30
|
+
connected() {
|
|
31
|
+
this.text.value = this.getAttribute('text') || this.textContent.trim();
|
|
32
|
+
if (this.hasAttribute('speed')) {
|
|
33
|
+
this.speed.value = parseInt(this.getAttribute('speed'), 10) || 20;
|
|
34
|
+
}
|
|
35
|
+
this.showCursor.value = this.getAttribute('cursor') !== 'false';
|
|
36
|
+
|
|
37
|
+
const prefersReducedMotion = typeof window.matchMedia === 'function' && window.matchMedia('(prefers-reduced-motion: reduce)')?.matches;
|
|
38
|
+
if (prefersReducedMotion || this.getAttribute('auto-start') === 'false') {
|
|
39
|
+
if (prefersReducedMotion) {
|
|
40
|
+
this.finish();
|
|
41
|
+
}
|
|
42
|
+
} else if (this.text.value) {
|
|
43
|
+
this.start();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
disconnected() {
|
|
48
|
+
this.#clearTimer();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
52
|
+
if (oldValue === newValue) return;
|
|
53
|
+
switch (name) {
|
|
54
|
+
case 'text':
|
|
55
|
+
this.text.value = newValue || '';
|
|
56
|
+
if (this.isConnected && this.getAttribute('auto-start') !== 'false') {
|
|
57
|
+
this.start();
|
|
58
|
+
}
|
|
59
|
+
break;
|
|
60
|
+
case 'speed':
|
|
61
|
+
this.speed.value = parseInt(newValue, 10) || 20;
|
|
62
|
+
break;
|
|
63
|
+
case 'cursor':
|
|
64
|
+
this.showCursor.value = newValue !== 'false';
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#clearTimer() {
|
|
70
|
+
if (this.#timer) {
|
|
71
|
+
clearTimeout(this.#timer);
|
|
72
|
+
this.#timer = null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
start() {
|
|
77
|
+
this.#clearTimer();
|
|
78
|
+
const full = this.text.value;
|
|
79
|
+
if (!full) {
|
|
80
|
+
this.displayedText.value = '';
|
|
81
|
+
this.isComplete.value = true;
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this.#targetIndex = 0;
|
|
86
|
+
this.displayedText.value = '';
|
|
87
|
+
this.isStreaming.value = true;
|
|
88
|
+
this.isComplete.value = false;
|
|
89
|
+
|
|
90
|
+
this.dispatchEvent(new CustomEvent('stream-start', { bubbles: true, composed: true }));
|
|
91
|
+
this.#step();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#step = () => {
|
|
95
|
+
const full = this.text.value;
|
|
96
|
+
if (this.#targetIndex < full.length) {
|
|
97
|
+
// Chunk 1-3 chars for natural LLM token simulation
|
|
98
|
+
const chunkSize = full[this.#targetIndex] === ' ' ? 1 : Math.min(3, full.length - this.#targetIndex);
|
|
99
|
+
this.#targetIndex += chunkSize;
|
|
100
|
+
this.displayedText.value = full.slice(0, this.#targetIndex);
|
|
101
|
+
|
|
102
|
+
this.dispatchEvent(
|
|
103
|
+
new CustomEvent('stream-tick', {
|
|
104
|
+
bubbles: true,
|
|
105
|
+
composed: true,
|
|
106
|
+
detail: { text: this.displayedText.value }
|
|
107
|
+
})
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
// Add micro-jitter for organic human/LLM typing rhythm
|
|
111
|
+
const jitter = (Math.random() * 0.4 + 0.8) * this.speed.value;
|
|
112
|
+
this.#timer = setTimeout(this.#step, jitter);
|
|
113
|
+
} else {
|
|
114
|
+
this.finish();
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
pause() {
|
|
119
|
+
this.#clearTimer();
|
|
120
|
+
this.isStreaming.value = false;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
resume() {
|
|
124
|
+
if (!this.isComplete.value && !this.isStreaming.value) {
|
|
125
|
+
this.isStreaming.value = true;
|
|
126
|
+
this.#step();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
finish() {
|
|
131
|
+
this.#clearTimer();
|
|
132
|
+
this.displayedText.value = this.text.value;
|
|
133
|
+
this.#targetIndex = this.text.value.length;
|
|
134
|
+
this.isStreaming.value = false;
|
|
135
|
+
this.isComplete.value = true;
|
|
136
|
+
|
|
137
|
+
this.dispatchEvent(
|
|
138
|
+
new CustomEvent('stream-end', {
|
|
139
|
+
bubbles: true,
|
|
140
|
+
composed: true,
|
|
141
|
+
detail: { text: this.text.value }
|
|
142
|
+
})
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
static styles = css`
|
|
147
|
+
:host {
|
|
148
|
+
display: inline-block;
|
|
149
|
+
box-sizing: border-box;
|
|
150
|
+
font-family: inherit;
|
|
151
|
+
line-height: inherit;
|
|
152
|
+
color: inherit;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
.text-content {
|
|
156
|
+
white-space: pre-wrap;
|
|
157
|
+
word-break: break-word;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
.cursor {
|
|
161
|
+
display: inline-block;
|
|
162
|
+
width: 0.55em;
|
|
163
|
+
height: 1.1em;
|
|
164
|
+
margin-left: 2px;
|
|
165
|
+
vertical-align: -0.15em;
|
|
166
|
+
background-color: var(--el-color-primary, #2563eb);
|
|
167
|
+
border-radius: 1px;
|
|
168
|
+
animation: cursor-blink 0.9s cubic-bezier(0.4, 0, 0.6, 1) infinite;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
:host([complete]) .cursor {
|
|
172
|
+
display: none;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
@keyframes cursor-blink {
|
|
176
|
+
0%, 100% { opacity: 1; }
|
|
177
|
+
50% { opacity: 0; }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
@media (prefers-reduced-motion: reduce) {
|
|
181
|
+
.cursor {
|
|
182
|
+
animation: none;
|
|
183
|
+
opacity: 0.8;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
`;
|
|
187
|
+
|
|
188
|
+
render() {
|
|
189
|
+
return html`
|
|
190
|
+
<span class="text-content" part="text">${this.displayedText}</span>${() => (this.showCursor.value && !this.isComplete.value
|
|
191
|
+
? html`<span class="cursor" part="cursor" aria-hidden="true"></span>`
|
|
192
|
+
: null)}
|
|
193
|
+
`;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (typeof customElements !== 'undefined' && !customElements.get('el-streaming-text')) {
|
|
198
|
+
customElements.define('el-streaming-text', ElStreamingText);
|
|
199
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <el-switch>
|
|
3
|
+
* Form-associated binary toggle switch with keyboard support and token styling.
|
|
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 ElSwitch extends Elementum {
|
|
11
|
+
static formAssociated = true;
|
|
12
|
+
static observedAttributes = ['checked', 'disabled', 'name', 'value', 'size', 'required', 'label'];
|
|
13
|
+
|
|
14
|
+
checked = signal(false);
|
|
15
|
+
disabled = signal(false);
|
|
16
|
+
required = signal(false);
|
|
17
|
+
name = signal('');
|
|
18
|
+
value = signal('on');
|
|
19
|
+
size = signal('md'); // 'sm' | 'md' | 'lg'
|
|
20
|
+
label = signal('');
|
|
21
|
+
|
|
22
|
+
static styles = css`
|
|
23
|
+
:host {
|
|
24
|
+
display: inline-flex;
|
|
25
|
+
align-items: center;
|
|
26
|
+
gap: var(--el-spacing-sm, 8px);
|
|
27
|
+
font-family: inherit;
|
|
28
|
+
cursor: pointer;
|
|
29
|
+
user-select: none;
|
|
30
|
+
vertical-align: middle;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
:host([disabled]) {
|
|
34
|
+
cursor: not-allowed;
|
|
35
|
+
opacity: 0.6;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
.track {
|
|
39
|
+
position: relative;
|
|
40
|
+
display: inline-flex;
|
|
41
|
+
align-items: center;
|
|
42
|
+
background: var(--el-surface-border, #d1d5db);
|
|
43
|
+
border-radius: 9999px;
|
|
44
|
+
transition: background-color 0.2s ease, box-shadow 0.2s ease;
|
|
45
|
+
box-sizing: border-box;
|
|
46
|
+
outline: none;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
:host(:focus-visible) .track,
|
|
50
|
+
.track:focus-visible {
|
|
51
|
+
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.25);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
.track.checked {
|
|
55
|
+
background: var(--el-color-primary, #2563eb);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.thumb {
|
|
59
|
+
position: absolute;
|
|
60
|
+
background: #ffffff;
|
|
61
|
+
border-radius: 50%;
|
|
62
|
+
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
|
|
63
|
+
transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/* Size variants */
|
|
67
|
+
:host([size="sm"]) .track {
|
|
68
|
+
width: 32px;
|
|
69
|
+
height: 18px;
|
|
70
|
+
}
|
|
71
|
+
:host([size="sm"]) .thumb {
|
|
72
|
+
width: 14px;
|
|
73
|
+
height: 14px;
|
|
74
|
+
left: 2px;
|
|
75
|
+
}
|
|
76
|
+
:host([size="sm"]) .track.checked .thumb {
|
|
77
|
+
transform: translateX(14px);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
:host([size="md"]) .track,
|
|
81
|
+
.track {
|
|
82
|
+
width: 44px;
|
|
83
|
+
height: 24px;
|
|
84
|
+
}
|
|
85
|
+
:host([size="md"]) .thumb,
|
|
86
|
+
.thumb {
|
|
87
|
+
width: 20px;
|
|
88
|
+
height: 20px;
|
|
89
|
+
left: 2px;
|
|
90
|
+
}
|
|
91
|
+
:host([size="md"]) .track.checked .thumb,
|
|
92
|
+
.track.checked .thumb {
|
|
93
|
+
transform: translateX(20px);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
:host([size="lg"]) .track {
|
|
97
|
+
width: 56px;
|
|
98
|
+
height: 30px;
|
|
99
|
+
}
|
|
100
|
+
:host([size="lg"]) .thumb {
|
|
101
|
+
width: 26px;
|
|
102
|
+
height: 26px;
|
|
103
|
+
left: 2px;
|
|
104
|
+
}
|
|
105
|
+
:host([size="lg"]) .track.checked .thumb {
|
|
106
|
+
transform: translateX(26px);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
.label {
|
|
110
|
+
font-size: 0.9375rem;
|
|
111
|
+
color: var(--el-text-base, #111827);
|
|
112
|
+
}
|
|
113
|
+
`;
|
|
114
|
+
|
|
115
|
+
connected() {
|
|
116
|
+
this.checked.value = this.hasAttribute('checked');
|
|
117
|
+
this.disabled.value = this.hasAttribute('disabled');
|
|
118
|
+
this.required.value = this.hasAttribute('required');
|
|
119
|
+
if (this.hasAttribute('label')) this.label.value = this.getAttribute('label');
|
|
120
|
+
this.#syncForm();
|
|
121
|
+
this.validate();
|
|
122
|
+
|
|
123
|
+
this.addEventListener('click', this.#onClick);
|
|
124
|
+
this.addEventListener('keydown', this.#onKeyDown);
|
|
125
|
+
|
|
126
|
+
if (!this.hasAttribute('tabindex') && !this.disabled.value) {
|
|
127
|
+
this.setAttribute('tabindex', '0');
|
|
128
|
+
}
|
|
129
|
+
this.setAttribute('role', 'switch');
|
|
130
|
+
this.setAttribute('aria-checked', String(this.checked.value));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
attributeChanged(name, oldValue, newValue) {
|
|
134
|
+
if (name === 'checked') {
|
|
135
|
+
const isChecked = newValue !== null && newValue !== 'false';
|
|
136
|
+
this.checked.value = isChecked;
|
|
137
|
+
this.setAttribute('aria-checked', String(isChecked));
|
|
138
|
+
this.#syncForm();
|
|
139
|
+
this.validate();
|
|
140
|
+
} else if (name === 'required') {
|
|
141
|
+
this.required.value = newValue !== null && newValue !== 'false';
|
|
142
|
+
this.validate();
|
|
143
|
+
} else if (name === 'disabled') {
|
|
144
|
+
const isDisabled = newValue !== null && newValue !== 'false';
|
|
145
|
+
this.disabled.value = isDisabled;
|
|
146
|
+
this.setAttribute('aria-disabled', String(isDisabled));
|
|
147
|
+
if (isDisabled) {
|
|
148
|
+
this.removeAttribute('tabindex');
|
|
149
|
+
} else {
|
|
150
|
+
this.setAttribute('tabindex', '0');
|
|
151
|
+
}
|
|
152
|
+
} else if (name === 'label') {
|
|
153
|
+
this.label.value = newValue || '';
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
formReset() {
|
|
158
|
+
this.checked.value = this.hasAttribute('checked');
|
|
159
|
+
this.setAttribute('aria-checked', String(this.checked.value));
|
|
160
|
+
this.#syncForm();
|
|
161
|
+
this.validate();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
validate() {
|
|
165
|
+
if (this.required.value && !this.checked.value) {
|
|
166
|
+
this.setValidity({ valueMissing: true }, 'Please turn this switch on.');
|
|
167
|
+
this.setAttribute('aria-invalid', 'true');
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
this.setValidity({});
|
|
171
|
+
this.removeAttribute('aria-invalid');
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
#syncForm() {
|
|
176
|
+
if (this.checked.value) {
|
|
177
|
+
this.setFormValue(this.value.value || 'on');
|
|
178
|
+
} else {
|
|
179
|
+
this.setFormValue(null);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
toggle() {
|
|
184
|
+
if (this.disabled.value) return;
|
|
185
|
+
this.checked.value = !this.checked.value;
|
|
186
|
+
this.setAttribute('aria-checked', String(this.checked.value));
|
|
187
|
+
this.#syncForm();
|
|
188
|
+
this.validate();
|
|
189
|
+
this.emit('change', { checked: this.checked.value, value: this.value.value });
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
#onClick = (e) => {
|
|
193
|
+
e.preventDefault();
|
|
194
|
+
this.toggle();
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
#onKeyDown = (e) => {
|
|
198
|
+
if (e.key === ' ' || e.key === 'Enter') {
|
|
199
|
+
e.preventDefault();
|
|
200
|
+
this.toggle();
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
render() {
|
|
205
|
+
return html`
|
|
206
|
+
<div
|
|
207
|
+
class=${() => `track ${this.checked.value ? 'checked' : ''}`}
|
|
208
|
+
part="track"
|
|
209
|
+
>
|
|
210
|
+
<div class="thumb" part="thumb"></div>
|
|
211
|
+
</div>
|
|
212
|
+
<span class="label" part="label">
|
|
213
|
+
<slot>${() => this.label.value || ''}</slot>
|
|
214
|
+
</span>
|
|
215
|
+
`;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (typeof customElements !== 'undefined' && !customElements.get('el-switch')) {
|
|
220
|
+
customElements.define('el-switch', ElSwitch);
|
|
221
|
+
}
|