@websublime/line-components 0.1.4 → 0.2.1
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 +12 -1
- package/custom-elements.json +2535 -0
- package/dist/button/button.d.ts +84 -0
- package/dist/button/button.d.ts.map +1 -0
- package/dist/button/button.types.d.ts +13 -0
- package/dist/button/button.types.d.ts.map +1 -0
- package/dist/button/index.d.ts +3 -0
- package/dist/button/index.d.ts.map +1 -0
- package/dist/button/index.js +333 -0
- package/dist/button/index.js.map +1 -0
- package/dist/shared/button-machine.d.ts +71 -0
- package/dist/shared/button-machine.d.ts.map +1 -0
- package/package.json +15 -6
- package/src/button/button.ts +372 -0
- package/src/button/button.types.ts +16 -0
- package/src/button/index.ts +9 -0
- package/src/shared/button-machine.ts +140 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { FormAssociated, LineElement } from '@websublime/line-core';
|
|
2
|
+
import { LineMachineController } from '@websublime/line-core/machine';
|
|
3
|
+
import { buttonReset, commonReset } from '@websublime/line-core/styles';
|
|
4
|
+
import {
|
|
5
|
+
type ComplexAttributeConverter,
|
|
6
|
+
css,
|
|
7
|
+
html,
|
|
8
|
+
LitElement,
|
|
9
|
+
nothing,
|
|
10
|
+
type PropertyDeclarations,
|
|
11
|
+
type PropertyValues,
|
|
12
|
+
type TemplateResult,
|
|
13
|
+
} from 'lit';
|
|
14
|
+
import { ifDefined } from 'lit/directives/if-defined.js';
|
|
15
|
+
import {
|
|
16
|
+
type ButtonApi,
|
|
17
|
+
type ButtonMachineProps,
|
|
18
|
+
type ButtonMachineSchema,
|
|
19
|
+
buttonMachine,
|
|
20
|
+
connect,
|
|
21
|
+
connectStatic,
|
|
22
|
+
} from '../shared/button-machine.js';
|
|
23
|
+
import type {
|
|
24
|
+
ButtonFormEnctype,
|
|
25
|
+
ButtonFormMethod,
|
|
26
|
+
ButtonType,
|
|
27
|
+
ButtonVariant,
|
|
28
|
+
LineButtonChangeDetail,
|
|
29
|
+
} from './button.types.js';
|
|
30
|
+
|
|
31
|
+
const VARIANTS: readonly ButtonVariant[] = ['default', 'primary', 'borderless'];
|
|
32
|
+
const TYPES: readonly ButtonType[] = ['submit', 'reset', 'button'];
|
|
33
|
+
|
|
34
|
+
/** The host attributes the temporary submitter copies, besides `name` and `value`. */
|
|
35
|
+
const FORM_ATTRIBUTES = ['formaction', 'formmethod', 'formenctype', 'formtarget', 'formnovalidate'] as const;
|
|
36
|
+
|
|
37
|
+
/** An unknown `variant` behaves as `default`; the attribute keeps what the author wrote. */
|
|
38
|
+
const variantConverter: ComplexAttributeConverter<ButtonVariant> = {
|
|
39
|
+
fromAttribute: (value) => VARIANTS.find((variant) => variant === value) ?? 'default',
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** An unknown or missing `type` behaves as `submit`, as on a native button; the match ignores case. */
|
|
43
|
+
const typeConverter: ComplexAttributeConverter<ButtonType> = {
|
|
44
|
+
fromAttribute: (value) => TYPES.find((type) => type === value?.toLowerCase()) ?? 'submit',
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** Absent means not a toggle; `"false"` parses to `false` and any other value to `true`. */
|
|
48
|
+
const pressedConverter: ComplexAttributeConverter<boolean | undefined> = {
|
|
49
|
+
fromAttribute: (value) => (value === null ? undefined : value !== 'false'),
|
|
50
|
+
toAttribute: (value) => (value === undefined ? null : String(value)),
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/** Reflects a string and removes the attribute when the string is empty. */
|
|
54
|
+
const emptyRemovesConverter: ComplexAttributeConverter<string> = {
|
|
55
|
+
fromAttribute: (value) => value ?? '',
|
|
56
|
+
toAttribute: (value) => value || null,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* `<line-button>` runs an action when the user activates it. It renders a
|
|
61
|
+
* native `<button>` in its shadow root, and the host is a form-associated
|
|
62
|
+
* custom element, so `type="submit"` and `type="reset"` act on its form as a
|
|
63
|
+
* native button does. `pressed` makes it a toggle; `loading` keeps it
|
|
64
|
+
* focusable while it ignores presses.
|
|
65
|
+
*
|
|
66
|
+
* @summary A form-associated button that runs an action, submits or resets its form, or toggles with `pressed`.
|
|
67
|
+
*
|
|
68
|
+
* @slot - The label: text or inline content.
|
|
69
|
+
* @slot prefix - A leading icon.
|
|
70
|
+
* @slot suffix - A trailing icon.
|
|
71
|
+
* @slot loading - A spinner, loading text or both; rendered only while `loading` is on.
|
|
72
|
+
*
|
|
73
|
+
* @csspart root - The inner native `<button>`: box, background, border and focus ring.
|
|
74
|
+
* @csspart label - Wraps the default slot.
|
|
75
|
+
* @csspart loading - Wraps the `loading` slot; present only while `loading` is on.
|
|
76
|
+
*
|
|
77
|
+
* @cssprop [--line-button-radius=var(--line-radius-2)] - Corner radius of `root`.
|
|
78
|
+
* @cssprop [--line-button-bg=var(--line-gray-bg)] - Background of `root`; `primary` uses `var(--line-accent-solid)` and `borderless` uses `transparent`.
|
|
79
|
+
* @cssprop [--line-button-color=var(--line-gray-text)] - Text colour of `root`; `primary` uses `var(--line-accent-contrast)` and `borderless` uses `var(--line-accent-text)`.
|
|
80
|
+
* @cssprop [--line-button-height=var(--line-size-7)] - Minimum block size of `root`.
|
|
81
|
+
* @cssprop [--line-button-padding-inline=var(--line-size-3)] - Inline padding of `root`.
|
|
82
|
+
* @cssprop [--line-button-gap=var(--line-size-2)] - Gap between the prefix, label, suffix and loading zones.
|
|
83
|
+
* @cssprop [--line-button-font-size=var(--line-font-size-1)] - Font size of `root`.
|
|
84
|
+
* @cssprop [--line-button-font-weight=var(--line-font-weight-5)] - Font weight of `root`.
|
|
85
|
+
* @cssprop [--line-button-focus-ring=var(--line-focus-ring, 2px solid currentColor)] - `outline` of `root` while the host has `data-focus-visible`.
|
|
86
|
+
*
|
|
87
|
+
* @fires {CustomEvent<LineButtonChangeDetail>} line-change - A user activation flipped `pressed` on a toggle button; `detail.value` is the new state. Bubbles, not composed.
|
|
88
|
+
*
|
|
89
|
+
* @attr {string} name - The key the submitter value goes under; read through the inherited `name` getter.
|
|
90
|
+
*/
|
|
91
|
+
export class LineButton extends FormAssociated(LineElement) {
|
|
92
|
+
static override shadowRootOptions: ShadowRootInit = { ...LitElement.shadowRootOptions, delegatesFocus: true };
|
|
93
|
+
|
|
94
|
+
static override properties: PropertyDeclarations = {
|
|
95
|
+
variant: { reflect: true, converter: variantConverter },
|
|
96
|
+
type: { reflect: true, converter: typeConverter },
|
|
97
|
+
disabled: { type: Boolean, reflect: true },
|
|
98
|
+
loading: { type: Boolean, reflect: true },
|
|
99
|
+
pressed: { reflect: true, converter: pressedConverter },
|
|
100
|
+
value: { reflect: true, converter: emptyRemovesConverter },
|
|
101
|
+
formAction: { attribute: 'formaction', reflect: true, converter: emptyRemovesConverter },
|
|
102
|
+
formMethod: { attribute: 'formmethod', reflect: true },
|
|
103
|
+
formEnctype: { attribute: 'formenctype', reflect: true },
|
|
104
|
+
formTarget: { attribute: 'formtarget', reflect: true },
|
|
105
|
+
formNoValidate: { attribute: 'formnovalidate', type: Boolean, reflect: true },
|
|
106
|
+
customValidity: { attribute: 'custom-validity' },
|
|
107
|
+
ariaLabel: { attribute: 'aria-label', reflect: true },
|
|
108
|
+
ariaDescription: { attribute: 'aria-description', reflect: true },
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
static override styles = [
|
|
112
|
+
commonReset,
|
|
113
|
+
buttonReset,
|
|
114
|
+
css`
|
|
115
|
+
:host {
|
|
116
|
+
--line-button-radius: var(--line-radius-2);
|
|
117
|
+
--line-button-bg: var(--line-gray-bg);
|
|
118
|
+
--line-button-color: var(--line-gray-text);
|
|
119
|
+
--line-button-height: var(--line-size-7);
|
|
120
|
+
--line-button-padding-inline: var(--line-size-3);
|
|
121
|
+
--line-button-gap: var(--line-size-2);
|
|
122
|
+
--line-button-font-size: var(--line-font-size-1);
|
|
123
|
+
--line-button-font-weight: var(--line-font-weight-5);
|
|
124
|
+
--line-button-focus-ring: var(--line-focus-ring, 2px solid currentColor);
|
|
125
|
+
|
|
126
|
+
display: inline-block;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
:host([hidden]) {
|
|
130
|
+
display: none;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
:host([variant='primary']) {
|
|
134
|
+
--line-button-bg: var(--line-accent-solid);
|
|
135
|
+
--line-button-color: var(--line-accent-contrast);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
:host([variant='borderless']) {
|
|
139
|
+
--line-button-bg: transparent;
|
|
140
|
+
--line-button-color: var(--line-accent-text);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
[part='root'] {
|
|
144
|
+
display: inline-flex;
|
|
145
|
+
align-items: center;
|
|
146
|
+
justify-content: center;
|
|
147
|
+
gap: var(--line-button-gap);
|
|
148
|
+
min-block-size: var(--line-button-height);
|
|
149
|
+
padding-inline: var(--line-button-padding-inline);
|
|
150
|
+
border-radius: var(--line-button-radius);
|
|
151
|
+
background: var(--line-button-bg);
|
|
152
|
+
color: var(--line-button-color);
|
|
153
|
+
font-size: var(--line-button-font-size);
|
|
154
|
+
font-weight: var(--line-button-font-weight);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
:host([data-focus-visible]) [part='root'] {
|
|
158
|
+
outline: var(--line-button-focus-ring);
|
|
159
|
+
}
|
|
160
|
+
`,
|
|
161
|
+
];
|
|
162
|
+
|
|
163
|
+
/** Semantic emphasis; an unknown value behaves as `default`. */
|
|
164
|
+
declare variant: ButtonVariant;
|
|
165
|
+
/** What activation does to the form owner; an unknown value behaves as `submit`. */
|
|
166
|
+
declare type: ButtonType;
|
|
167
|
+
/** Removes the button from the tab order and blocks activation. */
|
|
168
|
+
declare disabled: boolean;
|
|
169
|
+
/** Keeps focus, sets `aria-disabled="true"`, blocks activation and renders the `loading` part. */
|
|
170
|
+
declare loading: boolean;
|
|
171
|
+
/** `undefined` means not a toggle; a toggle flips it on each activation and fires `line-change`. */
|
|
172
|
+
declare pressed: boolean | undefined;
|
|
173
|
+
/** The submitter value; it enters the form data only when this button submits. */
|
|
174
|
+
declare value: string;
|
|
175
|
+
/** Overrides the form's `action` when this button submits. */
|
|
176
|
+
declare formAction: string;
|
|
177
|
+
/** Overrides the form's `method` when this button submits. */
|
|
178
|
+
declare formMethod: ButtonFormMethod | undefined;
|
|
179
|
+
/** Overrides the form's `enctype` when this button submits. */
|
|
180
|
+
declare formEnctype: ButtonFormEnctype | undefined;
|
|
181
|
+
/** Overrides the form's `target` when this button submits. */
|
|
182
|
+
declare formTarget: string | undefined;
|
|
183
|
+
/** Skips constraint validation when this button submits. */
|
|
184
|
+
declare formNoValidate: boolean;
|
|
185
|
+
/** External validity: a non-empty message marks the button invalid while `type` is `submit`. */
|
|
186
|
+
declare customValidity: string;
|
|
187
|
+
/** Copied to the inner button, where it names the role. */
|
|
188
|
+
declare ariaLabel: string | null;
|
|
189
|
+
/** Copied to the inner button. */
|
|
190
|
+
declare ariaDescription: string | null;
|
|
191
|
+
|
|
192
|
+
/** A disabled `fieldset` ancestor or the host's own `disabled`, from `formDisabledCallback`. */
|
|
193
|
+
#formDisabled = false;
|
|
194
|
+
|
|
195
|
+
readonly #onPressedChange = (next: boolean): void => {
|
|
196
|
+
this.pressed = next;
|
|
197
|
+
this.dispatchEvent(
|
|
198
|
+
new CustomEvent<LineButtonChangeDetail>('line-change', {
|
|
199
|
+
detail: { value: next },
|
|
200
|
+
bubbles: true,
|
|
201
|
+
composed: false,
|
|
202
|
+
}),
|
|
203
|
+
);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
readonly #machineProps = (): ButtonMachineProps => ({
|
|
207
|
+
disabled: this.disabled || this.#formDisabled,
|
|
208
|
+
loading: this.loading,
|
|
209
|
+
pressed: this.pressed,
|
|
210
|
+
onPressedChange: this.#onPressedChange,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
readonly #ctrl = new LineMachineController<ButtonMachineSchema>(this, {
|
|
214
|
+
machine: buttonMachine,
|
|
215
|
+
props: this.#machineProps,
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
constructor() {
|
|
219
|
+
super();
|
|
220
|
+
this.variant = 'default';
|
|
221
|
+
this.type = 'submit';
|
|
222
|
+
this.disabled = false;
|
|
223
|
+
this.loading = false;
|
|
224
|
+
this.value = '';
|
|
225
|
+
this.formAction = '';
|
|
226
|
+
this.formNoValidate = false;
|
|
227
|
+
this.customValidity = '';
|
|
228
|
+
this.addEventListener('click', this.#onClick, { capture: true });
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
override formDisabledCallback(disabled: boolean): void {
|
|
232
|
+
this.#formDisabled = disabled;
|
|
233
|
+
this.requestUpdate();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** The machine view, or the static view from the host props when the machine failed to start. */
|
|
237
|
+
#api(): ButtonApi {
|
|
238
|
+
const service = this.#ctrl.service;
|
|
239
|
+
return service && !this.#ctrl.fallback ? connect(service) : connectStatic(this.#machineProps());
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Stops a click while the button is not interactive. It listens in the
|
|
244
|
+
* capture phase, so it runs before every bubble listener on the host,
|
|
245
|
+
* including an inline `onclick` or a listener added before the upgrade.
|
|
246
|
+
* Otherwise it activates in a later task, once the click dispatch has
|
|
247
|
+
* ended, unless a listener called `preventDefault()`.
|
|
248
|
+
*/
|
|
249
|
+
readonly #onClick = (event: MouseEvent): void => {
|
|
250
|
+
if (!this.#api().interactive) {
|
|
251
|
+
event.preventDefault();
|
|
252
|
+
event.stopImmediatePropagation();
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
setTimeout(() => {
|
|
256
|
+
if (!event.defaultPrevented) this.#activate();
|
|
257
|
+
});
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
#activate(): void {
|
|
261
|
+
this.#ctrl.send({ type: 'ACTIVATE' });
|
|
262
|
+
const form = this.form;
|
|
263
|
+
if (!form) return;
|
|
264
|
+
if (this.type === 'reset') {
|
|
265
|
+
// A control with id or name "reset" shadows the method on the form.
|
|
266
|
+
HTMLFormElement.prototype.reset.call(form);
|
|
267
|
+
} else if (this.type === 'submit') {
|
|
268
|
+
this.#submit(form);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Submits through a temporary native submitter, so the submission gets the native submitter semantics. */
|
|
273
|
+
#submit(form: HTMLFormElement): void {
|
|
274
|
+
const submitter = form.ownerDocument.createElement('button');
|
|
275
|
+
submitter.type = 'submit';
|
|
276
|
+
submitter.hidden = true;
|
|
277
|
+
submitter.setAttribute('data-line-button-submitter', '');
|
|
278
|
+
const name = this.getAttribute('name');
|
|
279
|
+
if (name !== null) submitter.name = name;
|
|
280
|
+
submitter.value = this.value;
|
|
281
|
+
for (const attribute of FORM_ATTRIBUTES) {
|
|
282
|
+
const value = this.getAttribute(attribute);
|
|
283
|
+
if (value !== null) submitter.setAttribute(attribute, value);
|
|
284
|
+
}
|
|
285
|
+
form.append(submitter);
|
|
286
|
+
try {
|
|
287
|
+
submitter.click();
|
|
288
|
+
} finally {
|
|
289
|
+
submitter.remove();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
readonly #onPointerDown = (event: PointerEvent): void => {
|
|
294
|
+
if (event.button !== 0) return;
|
|
295
|
+
this.#ctrl.send({ type: 'PRESS_START' });
|
|
296
|
+
const release = new AbortController();
|
|
297
|
+
const end = (ended: Event): void => {
|
|
298
|
+
release.abort();
|
|
299
|
+
this.#ctrl.send({ type: ended.type === 'pointerup' ? 'PRESS_END' : 'PRESS_CANCEL' });
|
|
300
|
+
};
|
|
301
|
+
window.addEventListener('pointerup', end, { capture: true, signal: release.signal });
|
|
302
|
+
window.addEventListener('pointercancel', end, { capture: true, signal: release.signal });
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
readonly #onKeyDown = (event: KeyboardEvent): void => {
|
|
306
|
+
if (!event.repeat && (event.key === 'Enter' || event.key === ' ')) this.#ctrl.send({ type: 'PRESS_START' });
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
readonly #onKeyUp = (event: KeyboardEvent): void => {
|
|
310
|
+
if (event.key === 'Enter' || event.key === ' ') this.#ctrl.send({ type: 'PRESS_END' });
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
readonly #onFocus = (event: FocusEvent): void => {
|
|
314
|
+
const visible = (event.currentTarget as HTMLButtonElement).matches(':focus-visible');
|
|
315
|
+
this.#ctrl.send({ type: 'FOCUS', visible });
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
readonly #onBlur = (): void => {
|
|
319
|
+
this.#ctrl.send({ type: 'PRESS_CANCEL' });
|
|
320
|
+
this.#ctrl.send({ type: 'BLUR' });
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
protected override update(changed: PropertyValues<this>): void {
|
|
324
|
+
const api = this.#api();
|
|
325
|
+
this.reflectState('active', api.active);
|
|
326
|
+
this.reflectState('pressed', api.pressed);
|
|
327
|
+
this.reflectState('loading', api.loading);
|
|
328
|
+
this.reflectState('disabled', api.disabled);
|
|
329
|
+
this.reflectState('focus-visible', api.focusVisible);
|
|
330
|
+
super.update(changed);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
protected override updated(changed: PropertyValues<this>): void {
|
|
334
|
+
if (!(changed.has('customValidity') || changed.has('type'))) return;
|
|
335
|
+
const message = this.type === 'submit' ? this.customValidity : '';
|
|
336
|
+
const root = this.renderRoot.querySelector<HTMLButtonElement>('[part="root"]') ?? undefined;
|
|
337
|
+
if (message) {
|
|
338
|
+
this.setValidity({ customError: true }, message, root);
|
|
339
|
+
} else {
|
|
340
|
+
this.setValidity({});
|
|
341
|
+
}
|
|
342
|
+
this.reflectState('invalid', message !== '');
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
override render(): TemplateResult {
|
|
346
|
+
const { loading, buttonAttrs } = this.#api();
|
|
347
|
+
return html`<button
|
|
348
|
+
part="root"
|
|
349
|
+
type="button"
|
|
350
|
+
?disabled=${buttonAttrs.disabled}
|
|
351
|
+
aria-pressed=${ifDefined(buttonAttrs['aria-pressed'])}
|
|
352
|
+
aria-disabled=${ifDefined(buttonAttrs['aria-disabled'])}
|
|
353
|
+
aria-label=${ifDefined(this.ariaLabel ?? undefined)}
|
|
354
|
+
aria-description=${ifDefined(this.ariaDescription ?? undefined)}
|
|
355
|
+
@pointerdown=${this.#onPointerDown}
|
|
356
|
+
@keydown=${this.#onKeyDown}
|
|
357
|
+
@keyup=${this.#onKeyUp}
|
|
358
|
+
@focus=${this.#onFocus}
|
|
359
|
+
@blur=${this.#onBlur}
|
|
360
|
+
><slot name="prefix"></slot><span part="label"><slot></slot></span><slot name="suffix"></slot>${
|
|
361
|
+
loading ? html`<span part="loading"><slot name="loading"></slot></span>` : nothing
|
|
362
|
+
}</button>`;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
customElements.define('line-button', LineButton);
|
|
367
|
+
|
|
368
|
+
declare global {
|
|
369
|
+
interface HTMLElementTagNameMap {
|
|
370
|
+
'line-button': LineButton;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Semantic emphasis of `<line-button>` (ARCH §17.2 C6). */
|
|
2
|
+
export type ButtonVariant = 'default' | 'primary' | 'borderless';
|
|
3
|
+
|
|
4
|
+
/** What activation does to the form owner, as on a native `<button>`. */
|
|
5
|
+
export type ButtonType = 'submit' | 'reset' | 'button';
|
|
6
|
+
|
|
7
|
+
/** The `formmethod` override values. */
|
|
8
|
+
export type ButtonFormMethod = 'get' | 'post' | 'dialog';
|
|
9
|
+
|
|
10
|
+
/** The `formenctype` override values. */
|
|
11
|
+
export type ButtonFormEnctype = 'application/x-www-form-urlencoded' | 'multipart/form-data' | 'text/plain';
|
|
12
|
+
|
|
13
|
+
/** `detail` of the `line-change` event: the new pressed state of a toggle button. */
|
|
14
|
+
export interface LineButtonChangeDetail {
|
|
15
|
+
value: boolean;
|
|
16
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// biome-ignore lint/performance/noBarrelFile: the `./button` subpath entry registers `line-button` and exposes its class and types (spec 0002-button.md Bundle).
|
|
2
|
+
export { LineButton } from './button.js';
|
|
3
|
+
export type {
|
|
4
|
+
ButtonFormEnctype,
|
|
5
|
+
ButtonFormMethod,
|
|
6
|
+
ButtonType,
|
|
7
|
+
ButtonVariant,
|
|
8
|
+
LineButtonChangeDetail,
|
|
9
|
+
} from './button.types.js';
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Button press machine, shared by `<line-button>` and `<line-icon-button>`
|
|
3
|
+
* (spec `0002-button.md` States; Phase 1 spec §6.C.5; ARCH §13.1).
|
|
4
|
+
*
|
|
5
|
+
* The press is the only finite state: `idle → active`. `disabled`, `loading`
|
|
6
|
+
* and `pressed` are props that the `isInteractive` guard and the actions read
|
|
7
|
+
* at event time. `ACTIVATE` flips a toggle through `onPressedChange`. A `watch`
|
|
8
|
+
* cancels a running press when `disabled` or `loading` turns true. The module
|
|
9
|
+
* knows nothing about forms or DOM events and imports no component
|
|
10
|
+
* (MANIFESTO Law 4).
|
|
11
|
+
*
|
|
12
|
+
* @module shared/button-machine
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { createMachine, type Service } from '@zag-js/core';
|
|
16
|
+
|
|
17
|
+
/** Machine props. A component passes them as a live function of its own properties. */
|
|
18
|
+
export interface ButtonMachineProps {
|
|
19
|
+
disabled?: boolean | undefined;
|
|
20
|
+
loading?: boolean | undefined;
|
|
21
|
+
/** `undefined` means the button is not a toggle. */
|
|
22
|
+
pressed?: boolean | undefined;
|
|
23
|
+
/** Called with the next pressed state when an activation flips a toggle. */
|
|
24
|
+
onPressedChange?: ((next: boolean) => void) | undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type ButtonMachineEvent =
|
|
28
|
+
| { type: 'PRESS_START' }
|
|
29
|
+
| { type: 'PRESS_END' }
|
|
30
|
+
| { type: 'PRESS_CANCEL' }
|
|
31
|
+
| { type: 'ACTIVATE' }
|
|
32
|
+
| { type: 'FOCUS'; visible: boolean }
|
|
33
|
+
| { type: 'BLUR' };
|
|
34
|
+
|
|
35
|
+
export interface ButtonMachineSchema {
|
|
36
|
+
props: ButtonMachineProps;
|
|
37
|
+
context: { focusVisible: boolean };
|
|
38
|
+
state: 'idle' | 'active';
|
|
39
|
+
guard: 'isInteractive';
|
|
40
|
+
action: 'togglePressed' | 'setFocusVisible' | 'clearFocusVisible';
|
|
41
|
+
event: ButtonMachineEvent;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type ButtonService = Service<ButtonMachineSchema>;
|
|
45
|
+
|
|
46
|
+
export const buttonMachine = createMachine<ButtonMachineSchema>({
|
|
47
|
+
initialState: () => 'idle',
|
|
48
|
+
context: ({ bindable }) => ({
|
|
49
|
+
focusVisible: bindable<boolean>(() => ({ defaultValue: false })),
|
|
50
|
+
}),
|
|
51
|
+
watch({ track, prop, guard, send }) {
|
|
52
|
+
track([() => prop('disabled'), () => prop('loading')], () => {
|
|
53
|
+
if (!guard('isInteractive')) send({ type: 'PRESS_CANCEL' });
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
on: {
|
|
57
|
+
ACTIVATE: { guard: 'isInteractive', actions: ['togglePressed'] },
|
|
58
|
+
FOCUS: { actions: ['setFocusVisible'] },
|
|
59
|
+
BLUR: { actions: ['clearFocusVisible'] },
|
|
60
|
+
},
|
|
61
|
+
states: {
|
|
62
|
+
idle: {
|
|
63
|
+
on: { PRESS_START: { target: 'active', guard: 'isInteractive' } },
|
|
64
|
+
},
|
|
65
|
+
active: {
|
|
66
|
+
on: {
|
|
67
|
+
PRESS_END: { target: 'idle' },
|
|
68
|
+
PRESS_CANCEL: { target: 'idle' },
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
implementations: {
|
|
73
|
+
guards: {
|
|
74
|
+
isInteractive: ({ prop }) => !(prop('disabled') || prop('loading')),
|
|
75
|
+
},
|
|
76
|
+
actions: {
|
|
77
|
+
togglePressed({ prop }) {
|
|
78
|
+
const pressed = prop('pressed');
|
|
79
|
+
if (pressed !== undefined) prop('onPressedChange')?.(!pressed);
|
|
80
|
+
},
|
|
81
|
+
setFocusVisible({ context, event }) {
|
|
82
|
+
context.set('focusVisible', event.type === 'FOCUS' && event.visible === true);
|
|
83
|
+
},
|
|
84
|
+
clearFocusVisible({ context }) {
|
|
85
|
+
context.set('focusVisible', false);
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
/** What the inner native `<button>` and the host reflection read. */
|
|
92
|
+
export interface ButtonApi {
|
|
93
|
+
active: boolean;
|
|
94
|
+
/** `true` only on a pressed toggle. */
|
|
95
|
+
pressed: boolean;
|
|
96
|
+
loading: boolean;
|
|
97
|
+
disabled: boolean;
|
|
98
|
+
focusVisible: boolean;
|
|
99
|
+
/** `!disabled && !loading`: whether an activation runs now. */
|
|
100
|
+
interactive: boolean;
|
|
101
|
+
/** ARIA and `disabled` for the inner `<button>`; an `undefined` attribute is left off. */
|
|
102
|
+
buttonAttrs: {
|
|
103
|
+
'aria-pressed': 'true' | 'false' | undefined;
|
|
104
|
+
'aria-disabled': 'true' | undefined;
|
|
105
|
+
disabled: boolean;
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function api(props: ButtonMachineProps, active: boolean, focusVisible: boolean): ButtonApi {
|
|
110
|
+
const disabled = props.disabled === true;
|
|
111
|
+
const loading = props.loading === true;
|
|
112
|
+
return {
|
|
113
|
+
active,
|
|
114
|
+
pressed: props.pressed === true,
|
|
115
|
+
loading,
|
|
116
|
+
disabled,
|
|
117
|
+
focusVisible,
|
|
118
|
+
interactive: !(disabled || loading),
|
|
119
|
+
buttonAttrs: {
|
|
120
|
+
'aria-pressed': props.pressed === undefined ? undefined : (String(props.pressed) as 'true' | 'false'),
|
|
121
|
+
'aria-disabled': loading && !disabled ? 'true' : undefined,
|
|
122
|
+
disabled,
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Reads the running machine at call time. */
|
|
128
|
+
export function connect(service: ButtonService): ButtonApi {
|
|
129
|
+
const { prop } = service;
|
|
130
|
+
return api(
|
|
131
|
+
{ disabled: prop('disabled'), loading: prop('loading'), pressed: prop('pressed') },
|
|
132
|
+
service.state.matches('active'),
|
|
133
|
+
service.context.get('focusVisible'),
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The same view without a machine, for the controller's static fallback (MANIFESTO Law 9). */
|
|
138
|
+
export function connectStatic(props: ButtonMachineProps): ButtonApi {
|
|
139
|
+
return api(props, false, false);
|
|
140
|
+
}
|