@joralvmel/cosmos-elements 0.1.0 → 0.3.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/README.md +10 -0
- package/dist/components/icon/cosm-icon.d.ts +65 -0
- package/dist/components/icon/cosm-icon.d.ts.map +1 -0
- package/dist/components/icon/cosm-icon.js +137 -0
- package/dist/components/icon/cosm-icon.js.map +1 -0
- package/dist/components/icon-button/cosm-icon-button.d.ts +79 -0
- package/dist/components/icon-button/cosm-icon-button.d.ts.map +1 -0
- package/dist/components/icon-button/cosm-icon-button.js +206 -0
- package/dist/components/icon-button/cosm-icon-button.js.map +1 -0
- package/dist/components/listbox/cosm-listbox.d.ts +140 -0
- package/dist/components/listbox/cosm-listbox.d.ts.map +1 -0
- package/dist/components/listbox/cosm-listbox.js +580 -0
- package/dist/components/listbox/cosm-listbox.js.map +1 -0
- package/dist/components/popover/cosm-popover.d.ts +122 -0
- package/dist/components/popover/cosm-popover.d.ts.map +1 -0
- package/dist/components/popover/cosm-popover.js +809 -0
- package/dist/components/popover/cosm-popover.js.map +1 -0
- package/dist/components/text-field/cosm-text-field.d.ts +203 -0
- package/dist/components/text-field/cosm-text-field.d.ts.map +1 -0
- package/dist/components/text-field/cosm-text-field.js +679 -0
- package/dist/components/text-field/cosm-text-field.js.map +1 -0
- package/dist/custom-elements.json +3103 -0
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/llms.md +189 -0
- package/package.json +23 -2
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
import { getTextFieldState, textFieldContract, } from '@joralvmel/cosmos-core';
|
|
2
|
+
import { isIconDefinition } from '@joralvmel/cosmos-icons';
|
|
3
|
+
import { eye } from '@joralvmel/cosmos-icons/eye';
|
|
4
|
+
import { eyeOff } from '@joralvmel/cosmos-icons/eye-off';
|
|
5
|
+
import { x } from '@joralvmel/cosmos-icons/x';
|
|
6
|
+
import { css, html, LitElement, nothing } from 'lit';
|
|
7
|
+
import { CosmElement } from '../../internal/cosm-element.js';
|
|
8
|
+
import { define } from '../../internal/define.js';
|
|
9
|
+
import { boxSizing, focusRing } from '../../internal/styles.js';
|
|
10
|
+
import '../icon/cosm-icon.js';
|
|
11
|
+
let instance = 0;
|
|
12
|
+
const sizeConverter = {
|
|
13
|
+
fromAttribute(value) {
|
|
14
|
+
if (value === null)
|
|
15
|
+
return 'md';
|
|
16
|
+
const number = Number(value);
|
|
17
|
+
return value.trim() !== '' && Number.isFinite(number) ? number : value;
|
|
18
|
+
},
|
|
19
|
+
toAttribute(value) {
|
|
20
|
+
return String(value);
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
const styles = css `
|
|
24
|
+
:host {
|
|
25
|
+
--_height: var(--cosm-control-md, 2.5rem);
|
|
26
|
+
--_font-size: var(--cosm-text-sm, 0.875rem);
|
|
27
|
+
display: block;
|
|
28
|
+
inline-size: 20ch;
|
|
29
|
+
min-inline-size: 0;
|
|
30
|
+
max-inline-size: 100%;
|
|
31
|
+
color: var(--cosm-text-field-color, var(--cosm-fg, oklch(0.24 0.01 268)));
|
|
32
|
+
font-family: var(--cosm-font-sans, system-ui, sans-serif);
|
|
33
|
+
}
|
|
34
|
+
:host([size='sm']) {
|
|
35
|
+
--_height: var(--cosm-control-sm, 2rem);
|
|
36
|
+
--_font-size: var(--cosm-text-xs, 0.75rem);
|
|
37
|
+
}
|
|
38
|
+
:host([size='lg']) {
|
|
39
|
+
--_height: var(--cosm-control-lg, 3rem);
|
|
40
|
+
--_font-size: var(--cosm-text-md, 1rem);
|
|
41
|
+
}
|
|
42
|
+
.label {
|
|
43
|
+
display: block;
|
|
44
|
+
overflow-wrap: anywhere;
|
|
45
|
+
margin-block-end: var(--cosm-space-xs, 0.25rem);
|
|
46
|
+
font-size: var(--cosm-text-sm, 0.875rem);
|
|
47
|
+
font-weight: var(--cosm-weight-medium, 500);
|
|
48
|
+
}
|
|
49
|
+
.label-hidden {
|
|
50
|
+
position: absolute;
|
|
51
|
+
width: 1px;
|
|
52
|
+
height: 1px;
|
|
53
|
+
overflow: hidden;
|
|
54
|
+
clip: rect(0, 0, 0, 0);
|
|
55
|
+
white-space: nowrap;
|
|
56
|
+
clip-path: inset(50%);
|
|
57
|
+
}
|
|
58
|
+
.control {
|
|
59
|
+
display: flex;
|
|
60
|
+
min-inline-size: 0;
|
|
61
|
+
align-items: center;
|
|
62
|
+
gap: var(--cosm-space-sm, 0.5rem);
|
|
63
|
+
min-height: max(var(--_height), var(--_minimum-height, 0px));
|
|
64
|
+
padding-inline: var(--cosm-space-md, 0.75rem);
|
|
65
|
+
border: 1px solid
|
|
66
|
+
var(--cosm-text-field-border, var(--cosm-border-strong, oklch(0.64 0.01 268)));
|
|
67
|
+
border-radius: var(--cosm-text-field-radius, var(--cosm-radius-md, 0.5rem));
|
|
68
|
+
background: var(--cosm-text-field-background, var(--cosm-bg, oklch(0.985 0.007 268)));
|
|
69
|
+
transition: border-color var(--cosm-duration-fast, 120ms) var(--cosm-easing-standard, ease);
|
|
70
|
+
}
|
|
71
|
+
:host([variant='filled']) .control {
|
|
72
|
+
border-color: var(--cosm-text-field-border, var(--cosm-border-strong, oklch(0.64 0.01 268)));
|
|
73
|
+
background: var(--cosm-text-field-background, var(--cosm-neutral-2, oklch(0.96 0.008 268)));
|
|
74
|
+
}
|
|
75
|
+
:host([invalid]) .control,
|
|
76
|
+
.control[data-invalid='true'] {
|
|
77
|
+
border-color: var(--cosm-danger, oklch(0.55 0.16 19));
|
|
78
|
+
}
|
|
79
|
+
:host .control:focus-within {
|
|
80
|
+
border-color: var(--cosm-text-field-focus, var(--cosm-focus-ring, oklch(0.55 0.2 268)));
|
|
81
|
+
box-shadow: inset 0 0 0 1px
|
|
82
|
+
var(--cosm-text-field-focus, var(--cosm-focus-ring, oklch(0.55 0.2 268)));
|
|
83
|
+
}
|
|
84
|
+
.decoration {
|
|
85
|
+
display: inline-flex;
|
|
86
|
+
flex: 0 1 auto;
|
|
87
|
+
min-inline-size: 0;
|
|
88
|
+
max-inline-size: 25%;
|
|
89
|
+
align-items: center;
|
|
90
|
+
gap: var(--cosm-space-xs, 0.25rem);
|
|
91
|
+
color: var(--cosm-text-field-color, var(--cosm-fg-muted, oklch(0.48 0.01 268)));
|
|
92
|
+
font-size: var(--_font-size);
|
|
93
|
+
white-space: nowrap;
|
|
94
|
+
}
|
|
95
|
+
.decoration cosm-icon {
|
|
96
|
+
flex: none;
|
|
97
|
+
width: 1.25em;
|
|
98
|
+
height: 1.25em;
|
|
99
|
+
}
|
|
100
|
+
.start-icon {
|
|
101
|
+
color: var(--cosm-text-field-start-icon-color, inherit);
|
|
102
|
+
}
|
|
103
|
+
.end-icon {
|
|
104
|
+
color: var(--cosm-text-field-end-icon-color, inherit);
|
|
105
|
+
}
|
|
106
|
+
:host(:not([icon-focus-scope='input'])) .control:focus-within .start-icon,
|
|
107
|
+
:host([icon-focus-scope='input']) .control:has(input:focus) .start-icon {
|
|
108
|
+
color: var(
|
|
109
|
+
--cosm-text-field-start-icon-focus-color,
|
|
110
|
+
var(--cosm-text-field-start-icon-color, inherit)
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
:host(:not([icon-focus-scope='input'])) .control:focus-within .end-icon,
|
|
114
|
+
:host([icon-focus-scope='input']) .control:has(input:focus) .end-icon {
|
|
115
|
+
color: var(
|
|
116
|
+
--cosm-text-field-end-icon-focus-color,
|
|
117
|
+
var(--cosm-text-field-end-icon-color, inherit)
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
.affix {
|
|
121
|
+
min-inline-size: 0;
|
|
122
|
+
overflow: hidden;
|
|
123
|
+
text-overflow: ellipsis;
|
|
124
|
+
}
|
|
125
|
+
input {
|
|
126
|
+
flex: 1;
|
|
127
|
+
min-width: 0;
|
|
128
|
+
min-height: max(var(--_height), var(--_minimum-height, 0px));
|
|
129
|
+
padding: 0;
|
|
130
|
+
border: 0;
|
|
131
|
+
outline: 0;
|
|
132
|
+
background: transparent;
|
|
133
|
+
color: var(--cosm-text-field-color, var(--cosm-fg, oklch(0.24 0.01 268)));
|
|
134
|
+
font: inherit;
|
|
135
|
+
font-size: var(--_font-size);
|
|
136
|
+
}
|
|
137
|
+
input::placeholder {
|
|
138
|
+
color: var(--cosm-text-field-placeholder, var(--cosm-fg-muted, oklch(0.48 0.01 268)));
|
|
139
|
+
opacity: 1;
|
|
140
|
+
}
|
|
141
|
+
:host([clearable]) input[type='search']::-webkit-search-cancel-button {
|
|
142
|
+
-webkit-appearance: none;
|
|
143
|
+
}
|
|
144
|
+
input:focus-visible {
|
|
145
|
+
outline: none;
|
|
146
|
+
}
|
|
147
|
+
.action {
|
|
148
|
+
display: inline-grid;
|
|
149
|
+
flex: none;
|
|
150
|
+
width: max(2rem, var(--_height));
|
|
151
|
+
min-height: max(2rem, var(--_height));
|
|
152
|
+
place-items: center;
|
|
153
|
+
padding: 0;
|
|
154
|
+
border: 0;
|
|
155
|
+
border-radius: var(--cosm-radius-sm, 0.25rem);
|
|
156
|
+
background: transparent;
|
|
157
|
+
color: inherit;
|
|
158
|
+
font: inherit;
|
|
159
|
+
cursor: pointer;
|
|
160
|
+
}
|
|
161
|
+
.action:hover {
|
|
162
|
+
color: var(--cosm-primary-text, oklch(0.45 0.17 268));
|
|
163
|
+
}
|
|
164
|
+
.action:disabled {
|
|
165
|
+
cursor: not-allowed;
|
|
166
|
+
}
|
|
167
|
+
.message {
|
|
168
|
+
display: block;
|
|
169
|
+
overflow-wrap: anywhere;
|
|
170
|
+
margin-block-start: var(--cosm-space-xs, 0.25rem);
|
|
171
|
+
color: var(--cosm-fg-muted, oklch(0.48 0.01 268));
|
|
172
|
+
font-size: var(--cosm-text-xs, 0.75rem);
|
|
173
|
+
}
|
|
174
|
+
.error {
|
|
175
|
+
color: var(--cosm-danger-text, var(--cosm-danger, oklch(0.48 0.14 19)));
|
|
176
|
+
}
|
|
177
|
+
:host([disabled]) {
|
|
178
|
+
opacity: 0.58;
|
|
179
|
+
}
|
|
180
|
+
@media (forced-colors: active) {
|
|
181
|
+
:host .control:focus-within {
|
|
182
|
+
border-color: Highlight;
|
|
183
|
+
outline: 2px solid Highlight;
|
|
184
|
+
outline-offset: -2px;
|
|
185
|
+
box-shadow: none;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
@media (prefers-reduced-motion: reduce) {
|
|
189
|
+
.control {
|
|
190
|
+
transition: none;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
`;
|
|
194
|
+
/**
|
|
195
|
+
* Labeled, single-line text entry with native browser constraints and form association.
|
|
196
|
+
*
|
|
197
|
+
* @tag cosm-text-field
|
|
198
|
+
* @slot - No content slot; use `label`, `helpText` and `error` text properties.
|
|
199
|
+
* @csspart label - The visible or visually hidden label.
|
|
200
|
+
* @csspart control - The visual input control.
|
|
201
|
+
* @csspart input - The editable native input.
|
|
202
|
+
* @csspart start - Start icon or prefix decoration.
|
|
203
|
+
* @csspart end - End icon, suffix or action region.
|
|
204
|
+
* @csspart help - Supporting help text.
|
|
205
|
+
* @csspart error - External or browser constraint error text.
|
|
206
|
+
* @csspart clear-button - Optional accessible clear action.
|
|
207
|
+
* @csspart reveal-button - Optional accessible password visibility action.
|
|
208
|
+
* @fires {CustomEvent<TextFieldChangeDetail>} cosm-change - A value edit or clear proposal.
|
|
209
|
+
* @fires {CustomEvent<TextFieldSubmitDetail>} cosm-submit - The user invoked the submit action.
|
|
210
|
+
* @cssprop --cosm-text-field-background - Control background.
|
|
211
|
+
* @cssprop --cosm-text-field-color - Editable and decoration text color.
|
|
212
|
+
* @cssprop --cosm-text-field-placeholder - Placeholder text color.
|
|
213
|
+
* @cssprop --cosm-text-field-border - Control boundary color.
|
|
214
|
+
* @cssprop --cosm-text-field-focus - Focus indicator color.
|
|
215
|
+
* @cssprop --cosm-text-field-radius - Control corner radius.
|
|
216
|
+
* @cssprop --cosm-text-field-start-icon-color - Start icon color.
|
|
217
|
+
* @cssprop --cosm-text-field-start-icon-focus-color - Start icon focused color.
|
|
218
|
+
* @cssprop --cosm-text-field-end-icon-color - End icon color.
|
|
219
|
+
* @cssprop --cosm-text-field-end-icon-focus-color - End icon focused color.
|
|
220
|
+
*/
|
|
221
|
+
export class CosmTextField extends CosmElement {
|
|
222
|
+
static formAssociated = true;
|
|
223
|
+
static shadowRootOptions = {
|
|
224
|
+
...LitElement.shadowRootOptions,
|
|
225
|
+
delegatesFocus: true,
|
|
226
|
+
};
|
|
227
|
+
static properties = {
|
|
228
|
+
label: {},
|
|
229
|
+
labelHidden: { type: Boolean, reflect: true, attribute: 'label-hidden' },
|
|
230
|
+
value: {},
|
|
231
|
+
defaultValue: { attribute: 'default-value' },
|
|
232
|
+
type: { reflect: true },
|
|
233
|
+
variant: { reflect: true },
|
|
234
|
+
size: { reflect: true, converter: sizeConverter },
|
|
235
|
+
placeholder: {},
|
|
236
|
+
helpText: { attribute: 'help-text' },
|
|
237
|
+
error: {},
|
|
238
|
+
invalid: { type: Boolean, reflect: true },
|
|
239
|
+
required: { type: Boolean, reflect: true },
|
|
240
|
+
disabled: { type: Boolean, reflect: true },
|
|
241
|
+
readOnly: { type: Boolean, reflect: true, attribute: 'read-only' },
|
|
242
|
+
minLength: { type: Number, attribute: 'min-length' },
|
|
243
|
+
maxLength: { type: Number, attribute: 'max-length' },
|
|
244
|
+
pattern: {},
|
|
245
|
+
name: { reflect: true },
|
|
246
|
+
autoComplete: { attribute: 'auto-complete' },
|
|
247
|
+
inputMode: { attribute: 'input-mode' },
|
|
248
|
+
startIcon: { attribute: false },
|
|
249
|
+
endIcon: { attribute: false },
|
|
250
|
+
iconFocusScope: {
|
|
251
|
+
reflect: true,
|
|
252
|
+
attribute: 'icon-focus-scope',
|
|
253
|
+
converter: (value) => value ?? textFieldContract.props.iconFocusScope.default,
|
|
254
|
+
},
|
|
255
|
+
prefix: {},
|
|
256
|
+
suffix: {},
|
|
257
|
+
clearable: { type: Boolean, reflect: true },
|
|
258
|
+
clearLabel: { attribute: 'clear-label' },
|
|
259
|
+
passwordReveal: { type: Boolean, reflect: true, attribute: 'password-reveal' },
|
|
260
|
+
revealPasswordLabel: { attribute: 'reveal-password-label' },
|
|
261
|
+
hidePasswordLabel: { attribute: 'hide-password-label' },
|
|
262
|
+
};
|
|
263
|
+
static styles = [boxSizing, focusRing, styles];
|
|
264
|
+
/** Framework adapters set this metadata when `value` is actually bound. */
|
|
265
|
+
valueControlled = false;
|
|
266
|
+
#internals;
|
|
267
|
+
#id = `cosm-text-field-${++instance}`;
|
|
268
|
+
#formDisabled = false;
|
|
269
|
+
#passwordVisible = false;
|
|
270
|
+
#composing = false;
|
|
271
|
+
#showNativeError = false;
|
|
272
|
+
#edited = false;
|
|
273
|
+
#hasRendered = false;
|
|
274
|
+
#controlledReconcileCancel = null;
|
|
275
|
+
#pendingControlledRestore = null;
|
|
276
|
+
constructor() {
|
|
277
|
+
super();
|
|
278
|
+
const props = textFieldContract.props;
|
|
279
|
+
this.label = '';
|
|
280
|
+
this.labelHidden = props.labelHidden.default;
|
|
281
|
+
this.value = props.defaultValue.default;
|
|
282
|
+
this.defaultValue = props.defaultValue.default;
|
|
283
|
+
this.type = props.type.default;
|
|
284
|
+
this.variant = props.variant.default;
|
|
285
|
+
this.size = props.size.default;
|
|
286
|
+
this.placeholder = '';
|
|
287
|
+
this.helpText = '';
|
|
288
|
+
this.error = '';
|
|
289
|
+
this.invalid = props.invalid.default;
|
|
290
|
+
this.required = props.required.default;
|
|
291
|
+
this.disabled = props.disabled.default;
|
|
292
|
+
this.readOnly = props.readOnly.default;
|
|
293
|
+
this.minLength = undefined;
|
|
294
|
+
this.maxLength = undefined;
|
|
295
|
+
this.pattern = undefined;
|
|
296
|
+
this.name = undefined;
|
|
297
|
+
this.autoComplete = undefined;
|
|
298
|
+
this.inputMode = '';
|
|
299
|
+
this.startIcon = undefined;
|
|
300
|
+
this.endIcon = undefined;
|
|
301
|
+
this.iconFocusScope = props.iconFocusScope.default;
|
|
302
|
+
this.prefix = '';
|
|
303
|
+
this.suffix = '';
|
|
304
|
+
this.clearable = props.clearable.default;
|
|
305
|
+
this.clearLabel = undefined;
|
|
306
|
+
this.passwordReveal = props.passwordReveal.default;
|
|
307
|
+
this.revealPasswordLabel = undefined;
|
|
308
|
+
this.hidePasswordLabel = undefined;
|
|
309
|
+
this.#internals =
|
|
310
|
+
typeof this.attachInternals === 'function' ? this.attachInternals() : undefined;
|
|
311
|
+
}
|
|
312
|
+
connectedCallback() {
|
|
313
|
+
super.connectedCallback();
|
|
314
|
+
this.addEventListener('invalid', this.#invalid);
|
|
315
|
+
}
|
|
316
|
+
disconnectedCallback() {
|
|
317
|
+
this.removeEventListener('invalid', this.#invalid);
|
|
318
|
+
this.#cancelControlledReconcile();
|
|
319
|
+
super.disconnectedCallback();
|
|
320
|
+
}
|
|
321
|
+
/** The associated form, if any. */
|
|
322
|
+
get form() {
|
|
323
|
+
return this.#internals?.form ?? this.closest('form');
|
|
324
|
+
}
|
|
325
|
+
/** Focuses the native editable control. */
|
|
326
|
+
focus(options) {
|
|
327
|
+
this.renderRoot.querySelector('input')?.focus(options);
|
|
328
|
+
}
|
|
329
|
+
/** Returns the native validity state of the inner input. */
|
|
330
|
+
get validity() {
|
|
331
|
+
return this.renderRoot.querySelector('input')?.validity;
|
|
332
|
+
}
|
|
333
|
+
/** Returns the browser's constraint-validation message. */
|
|
334
|
+
get validationMessage() {
|
|
335
|
+
return this.renderRoot.querySelector('input')?.validationMessage ?? '';
|
|
336
|
+
}
|
|
337
|
+
/** Returns whether the input satisfies its native constraints. */
|
|
338
|
+
checkValidity() {
|
|
339
|
+
return this.renderRoot.querySelector('input')?.checkValidity() ?? true;
|
|
340
|
+
}
|
|
341
|
+
/** Checks validity and asks the browser to present its localized message. */
|
|
342
|
+
reportValidity() {
|
|
343
|
+
this.#showNativeError = true;
|
|
344
|
+
this.requestUpdate();
|
|
345
|
+
this.#syncFormState();
|
|
346
|
+
return this.#internals?.reportValidity() ?? this.#input()?.reportValidity() ?? true;
|
|
347
|
+
}
|
|
348
|
+
formDisabledCallback(disabled) {
|
|
349
|
+
this.#formDisabled = disabled;
|
|
350
|
+
this.requestUpdate();
|
|
351
|
+
}
|
|
352
|
+
formResetCallback() {
|
|
353
|
+
this.#showNativeError = false;
|
|
354
|
+
this.#edited = false;
|
|
355
|
+
if (this.valueControlled) {
|
|
356
|
+
this.emit('cosm-change', { value: this.defaultValue ?? '' });
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
this.value = this.defaultValue ?? '';
|
|
360
|
+
}
|
|
361
|
+
formStateRestoreCallback(state) {
|
|
362
|
+
if (typeof state !== 'string')
|
|
363
|
+
return;
|
|
364
|
+
this.#showNativeError = false;
|
|
365
|
+
if (this.valueControlled) {
|
|
366
|
+
this.emit('cosm-change', { value: state });
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
this.value = state;
|
|
370
|
+
}
|
|
371
|
+
willUpdate(changed) {
|
|
372
|
+
if (this.type !== 'password' ||
|
|
373
|
+
!this.passwordReveal ||
|
|
374
|
+
this.disabled ||
|
|
375
|
+
this.#formDisabled ||
|
|
376
|
+
this.readOnly) {
|
|
377
|
+
this.#passwordVisible = false;
|
|
378
|
+
}
|
|
379
|
+
if (typeof this.size !== 'number' &&
|
|
380
|
+
this.size !== 'sm' &&
|
|
381
|
+
this.size !== 'md' &&
|
|
382
|
+
this.size !== 'lg') {
|
|
383
|
+
throw new TypeError('Text field size must be sm, md, lg or a positive finite number.');
|
|
384
|
+
}
|
|
385
|
+
if (typeof this.size === 'number' && (!Number.isFinite(this.size) || this.size <= 0)) {
|
|
386
|
+
throw new TypeError('Text field size must be a positive finite number.');
|
|
387
|
+
}
|
|
388
|
+
if (!this.label.trim())
|
|
389
|
+
throw new TypeError('Text field requires a non-empty label.');
|
|
390
|
+
if (this.iconFocusScope !== undefined &&
|
|
391
|
+
this.iconFocusScope !== 'control' &&
|
|
392
|
+
this.iconFocusScope !== 'input')
|
|
393
|
+
throw new TypeError('Text field iconFocusScope must be control or input.');
|
|
394
|
+
if (this.clearable && !this.clearLabel?.trim()) {
|
|
395
|
+
throw new TypeError('Text field requires clearLabel when clearable is enabled.');
|
|
396
|
+
}
|
|
397
|
+
if (this.passwordReveal &&
|
|
398
|
+
(!this.revealPasswordLabel?.trim() || !this.hidePasswordLabel?.trim())) {
|
|
399
|
+
throw new TypeError('Text field requires revealPasswordLabel and hidePasswordLabel when passwordReveal is enabled.');
|
|
400
|
+
}
|
|
401
|
+
for (const [name, icon] of [
|
|
402
|
+
['startIcon', this.startIcon],
|
|
403
|
+
['endIcon', this.endIcon],
|
|
404
|
+
]) {
|
|
405
|
+
if (icon !== undefined && !isIconDefinition(icon)) {
|
|
406
|
+
throw new TypeError(`Text field ${name} requires a valid IconDefinition.`);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
if (typeof this.size === 'number')
|
|
410
|
+
this.style.setProperty('--_minimum-height', `${this.size}px`);
|
|
411
|
+
else
|
|
412
|
+
this.style.removeProperty('--_minimum-height');
|
|
413
|
+
if (changed.has('defaultValue') && !this.valueControlled && !this.#edited) {
|
|
414
|
+
if (this.#hasRendered || (!this.hasAttribute('value') && this.defaultValue)) {
|
|
415
|
+
this.value = this.defaultValue ?? '';
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
updated(changed) {
|
|
420
|
+
this.#hasRendered = true;
|
|
421
|
+
if (changed.has('value')) {
|
|
422
|
+
this.#cancelControlledReconcile();
|
|
423
|
+
const input = this.renderRoot.querySelector('input');
|
|
424
|
+
if (input && !this.#composing && input.value !== (this.value ?? '')) {
|
|
425
|
+
const selection = {
|
|
426
|
+
start: input.selectionStart,
|
|
427
|
+
end: input.selectionEnd,
|
|
428
|
+
direction: input.selectionDirection,
|
|
429
|
+
};
|
|
430
|
+
input.value = this.value ?? '';
|
|
431
|
+
if (selection.start !== null && selection.end !== null) {
|
|
432
|
+
input.setSelectionRange(Math.min(selection.start, input.value.length), Math.min(selection.end, input.value.length), selection.direction ?? undefined);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
this.#syncFormState();
|
|
437
|
+
}
|
|
438
|
+
#input = () => this.renderRoot.querySelector('input');
|
|
439
|
+
#syncFormState() {
|
|
440
|
+
const input = this.#input();
|
|
441
|
+
if (!input || !this.#internals)
|
|
442
|
+
return;
|
|
443
|
+
const currentValue = this.value ?? '';
|
|
444
|
+
this.#internals.setFormValue(this.disabled || this.#formDisabled ? null : currentValue, currentValue);
|
|
445
|
+
const invalidFlags = {};
|
|
446
|
+
for (const key of [
|
|
447
|
+
'badInput',
|
|
448
|
+
'patternMismatch',
|
|
449
|
+
'rangeOverflow',
|
|
450
|
+
'rangeUnderflow',
|
|
451
|
+
'stepMismatch',
|
|
452
|
+
'tooLong',
|
|
453
|
+
'tooShort',
|
|
454
|
+
'typeMismatch',
|
|
455
|
+
'valueMissing',
|
|
456
|
+
]) {
|
|
457
|
+
if (input.validity[key])
|
|
458
|
+
invalidFlags[key] = true;
|
|
459
|
+
}
|
|
460
|
+
if (input.validity.valid)
|
|
461
|
+
this.#internals.setValidity({});
|
|
462
|
+
else
|
|
463
|
+
this.#internals.setValidity(invalidFlags, input.validationMessage, input);
|
|
464
|
+
}
|
|
465
|
+
#cancelControlledReconcile() {
|
|
466
|
+
this.#controlledReconcileCancel?.();
|
|
467
|
+
this.#controlledReconcileCancel = null;
|
|
468
|
+
this.#pendingControlledRestore = null;
|
|
469
|
+
}
|
|
470
|
+
#scheduleControlledReconcile(input, value) {
|
|
471
|
+
this.#pendingControlledRestore = {
|
|
472
|
+
value,
|
|
473
|
+
selection: {
|
|
474
|
+
start: input.selectionStart,
|
|
475
|
+
end: input.selectionEnd,
|
|
476
|
+
direction: input.selectionDirection,
|
|
477
|
+
},
|
|
478
|
+
input,
|
|
479
|
+
};
|
|
480
|
+
if (this.#controlledReconcileCancel)
|
|
481
|
+
return;
|
|
482
|
+
const reconcile = () => {
|
|
483
|
+
this.#controlledReconcileCancel = null;
|
|
484
|
+
const pending = this.#pendingControlledRestore;
|
|
485
|
+
this.#pendingControlledRestore = null;
|
|
486
|
+
if (!pending || this.value !== pending.value || this.#composing || !pending.input.isConnected)
|
|
487
|
+
return;
|
|
488
|
+
pending.input.value = pending.value;
|
|
489
|
+
if (pending.selection.start !== null && pending.selection.end !== null) {
|
|
490
|
+
pending.input.setSelectionRange(Math.min(pending.selection.start, pending.value.length), Math.min(pending.selection.end, pending.value.length), pending.selection.direction ?? undefined);
|
|
491
|
+
}
|
|
492
|
+
this.#syncFormState();
|
|
493
|
+
};
|
|
494
|
+
if (typeof requestAnimationFrame === 'function') {
|
|
495
|
+
const frame = requestAnimationFrame(reconcile);
|
|
496
|
+
this.#controlledReconcileCancel = () => cancelAnimationFrame(frame);
|
|
497
|
+
}
|
|
498
|
+
else {
|
|
499
|
+
const timeout = setTimeout(reconcile, 0);
|
|
500
|
+
this.#controlledReconcileCancel = () => clearTimeout(timeout);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
#onInput = (event) => {
|
|
504
|
+
const input = event.currentTarget;
|
|
505
|
+
if (this.disabled || this.#formDisabled || this.readOnly) {
|
|
506
|
+
input.value = this.value ?? '';
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
this.#edited = true;
|
|
510
|
+
this.#showNativeError = false;
|
|
511
|
+
const proposedValue = input.value;
|
|
512
|
+
if (!this.valueControlled)
|
|
513
|
+
this.value = proposedValue;
|
|
514
|
+
this.#syncFormState();
|
|
515
|
+
this.emit('cosm-change', { value: proposedValue });
|
|
516
|
+
if (this.valueControlled && !this.#composing) {
|
|
517
|
+
this.#scheduleControlledReconcile(input, this.value ?? '');
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
#onCompositionStart = () => {
|
|
521
|
+
this.#cancelControlledReconcile();
|
|
522
|
+
this.#composing = true;
|
|
523
|
+
};
|
|
524
|
+
#onCompositionEnd = (event) => {
|
|
525
|
+
this.#composing = false;
|
|
526
|
+
this.#cancelControlledReconcile();
|
|
527
|
+
if (this.valueControlled) {
|
|
528
|
+
const input = event.currentTarget;
|
|
529
|
+
const selection = {
|
|
530
|
+
start: input.selectionStart,
|
|
531
|
+
end: input.selectionEnd,
|
|
532
|
+
direction: input.selectionDirection,
|
|
533
|
+
};
|
|
534
|
+
input.value = this.value ?? '';
|
|
535
|
+
if (selection.start !== null && selection.end !== null) {
|
|
536
|
+
input.setSelectionRange(Math.min(selection.start, input.value.length), Math.min(selection.end, input.value.length), selection.direction ?? undefined);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
};
|
|
540
|
+
#onBlur = () => {
|
|
541
|
+
this.#showNativeError = true;
|
|
542
|
+
this.#syncFormState();
|
|
543
|
+
this.requestUpdate();
|
|
544
|
+
};
|
|
545
|
+
#onKeyDown = (event) => {
|
|
546
|
+
if (event.key !== 'Enter' || event.isComposing || this.disabled || this.#formDisabled)
|
|
547
|
+
return;
|
|
548
|
+
const value = this.valueControlled ? (this.value ?? '') : (this.#input()?.value ?? '');
|
|
549
|
+
this.emit('cosm-submit', { value });
|
|
550
|
+
this.#syncFormState();
|
|
551
|
+
this.form?.requestSubmit();
|
|
552
|
+
};
|
|
553
|
+
#clear = () => {
|
|
554
|
+
if (this.disabled || this.#formDisabled || this.readOnly)
|
|
555
|
+
return;
|
|
556
|
+
const input = this.#input();
|
|
557
|
+
if (!input)
|
|
558
|
+
return;
|
|
559
|
+
if (!this.valueControlled)
|
|
560
|
+
this.value = '';
|
|
561
|
+
this.#syncFormState();
|
|
562
|
+
this.#edited = true;
|
|
563
|
+
this.emit('cosm-change', { value: '' });
|
|
564
|
+
this.focus();
|
|
565
|
+
queueMicrotask(() => this.focus());
|
|
566
|
+
};
|
|
567
|
+
#togglePassword = () => {
|
|
568
|
+
if (this.disabled || this.#formDisabled || this.readOnly)
|
|
569
|
+
return;
|
|
570
|
+
this.#passwordVisible = !this.#passwordVisible;
|
|
571
|
+
this.requestUpdate();
|
|
572
|
+
};
|
|
573
|
+
#invalid = () => {
|
|
574
|
+
this.#showNativeError = true;
|
|
575
|
+
this.requestUpdate();
|
|
576
|
+
};
|
|
577
|
+
render() {
|
|
578
|
+
const value = this.value ?? '';
|
|
579
|
+
const state = getTextFieldState({
|
|
580
|
+
value,
|
|
581
|
+
disabled: this.disabled || this.#formDisabled,
|
|
582
|
+
readOnly: this.readOnly,
|
|
583
|
+
invalid: this.invalid,
|
|
584
|
+
error: this.error,
|
|
585
|
+
clearable: this.clearable,
|
|
586
|
+
passwordReveal: this.passwordReveal,
|
|
587
|
+
type: this.type,
|
|
588
|
+
});
|
|
589
|
+
const nativeInvalid = this.#showNativeError && !(this.#input()?.validity.valid ?? true);
|
|
590
|
+
const shownError = this.error || (nativeInvalid ? this.validationMessage : '');
|
|
591
|
+
const invalid = state.invalid || nativeInvalid;
|
|
592
|
+
const describedBy = [
|
|
593
|
+
this.helpText ? `${this.#id}-help` : '',
|
|
594
|
+
shownError ? `${this.#id}-error` : '',
|
|
595
|
+
]
|
|
596
|
+
.filter(Boolean)
|
|
597
|
+
.join(' ');
|
|
598
|
+
const inputType = this.type === 'password' && this.#passwordVisible ? 'text' : (this.type ?? 'text');
|
|
599
|
+
return html `
|
|
600
|
+
<label
|
|
601
|
+
part="label"
|
|
602
|
+
class=${this.labelHidden ? 'label label-hidden' : 'label'}
|
|
603
|
+
for=${`${this.#id}-input`}
|
|
604
|
+
>${this.label}</label>
|
|
605
|
+
<div part="control" class="control" data-invalid=${invalid ? 'true' : 'false'}>
|
|
606
|
+
${this.startIcon || this.prefix
|
|
607
|
+
? html `<span part="start" class="decoration">
|
|
608
|
+
${this.startIcon
|
|
609
|
+
? html `<cosm-icon class="start-icon" .icon=${this.startIcon} decorative size="md"></cosm-icon>`
|
|
610
|
+
: nothing}
|
|
611
|
+
${this.prefix ? html `<span class="affix">${this.prefix}</span>` : nothing}
|
|
612
|
+
</span>`
|
|
613
|
+
: nothing}
|
|
614
|
+
<input
|
|
615
|
+
part="input"
|
|
616
|
+
id=${`${this.#id}-input`}
|
|
617
|
+
type=${inputType}
|
|
618
|
+
.value=${value}
|
|
619
|
+
placeholder=${this.placeholder || nothing}
|
|
620
|
+
?required=${this.required}
|
|
621
|
+
?disabled=${this.disabled || this.#formDisabled}
|
|
622
|
+
?readonly=${this.readOnly}
|
|
623
|
+
minlength=${this.minLength ?? nothing}
|
|
624
|
+
maxlength=${this.maxLength ?? nothing}
|
|
625
|
+
pattern=${this.pattern ?? nothing}
|
|
626
|
+
name=${nothing}
|
|
627
|
+
autocomplete=${this.autoComplete || nothing}
|
|
628
|
+
inputmode=${this.inputMode || nothing}
|
|
629
|
+
aria-invalid=${invalid ? 'true' : nothing}
|
|
630
|
+
aria-describedby=${describedBy || nothing}
|
|
631
|
+
@input=${this.#onInput}
|
|
632
|
+
@compositionstart=${this.#onCompositionStart}
|
|
633
|
+
@compositionend=${this.#onCompositionEnd}
|
|
634
|
+
@blur=${this.#onBlur}
|
|
635
|
+
@keydown=${this.#onKeyDown}
|
|
636
|
+
@invalid=${this.#invalid}
|
|
637
|
+
/>
|
|
638
|
+
${this.suffix || this.endIcon
|
|
639
|
+
? html `<span part="end" class="decoration">
|
|
640
|
+
${this.suffix ? html `<span class="affix">${this.suffix}</span>` : nothing}
|
|
641
|
+
${this.endIcon
|
|
642
|
+
? html `<cosm-icon class="end-icon" .icon=${this.endIcon} decorative size="md"></cosm-icon>`
|
|
643
|
+
: nothing}
|
|
644
|
+
</span>`
|
|
645
|
+
: nothing}
|
|
646
|
+
${state.showClearAction
|
|
647
|
+
? html `<button
|
|
648
|
+
part="clear-button"
|
|
649
|
+
class="action"
|
|
650
|
+
type="button"
|
|
651
|
+
aria-label=${this.clearLabel}
|
|
652
|
+
@click=${this.#clear}
|
|
653
|
+
><cosm-icon .icon=${x} decorative size="sm"></cosm-icon></button>`
|
|
654
|
+
: nothing}
|
|
655
|
+
${state.showPasswordReveal
|
|
656
|
+
? html `<button
|
|
657
|
+
part="reveal-button"
|
|
658
|
+
class="action"
|
|
659
|
+
type="button"
|
|
660
|
+
aria-label=${this.#passwordVisible ? this.hidePasswordLabel : this.revealPasswordLabel}
|
|
661
|
+
aria-pressed=${this.#passwordVisible ? 'true' : 'false'}
|
|
662
|
+
@click=${this.#togglePassword}
|
|
663
|
+
><cosm-icon .icon=${this.#passwordVisible ? eyeOff : eye} decorative size="sm"></cosm-icon
|
|
664
|
+
></button>`
|
|
665
|
+
: nothing}
|
|
666
|
+
</div>
|
|
667
|
+
${this.helpText
|
|
668
|
+
? html `<span part="help" class="message" id=${`${this.#id}-help`}>${this.helpText}</span>`
|
|
669
|
+
: nothing}
|
|
670
|
+
${shownError
|
|
671
|
+
? html `<span part="error" class="message error" id=${`${this.#id}-error`} role="alert"
|
|
672
|
+
>${shownError}</span
|
|
673
|
+
>`
|
|
674
|
+
: nothing}
|
|
675
|
+
`;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
define('cosm-text-field', CosmTextField);
|
|
679
|
+
//# sourceMappingURL=cosm-text-field.js.map
|