@websublime/line-components 0.2.0 → 0.2.2
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 +37 -1
- package/custom-elements.json +2013 -241
- 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/dist/stack/index.d.ts +3 -0
- package/dist/stack/index.d.ts.map +1 -0
- package/dist/stack/index.js +155 -0
- package/dist/stack/index.js.map +1 -0
- package/dist/stack/stack.d.ts +37 -0
- package/dist/stack/stack.d.ts.map +1 -0
- package/dist/stack/stack.types.d.ts +9 -0
- package/dist/stack/stack.types.d.ts.map +1 -0
- package/package.json +13 -4
- 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
- package/src/stack/index.ts +3 -0
- package/src/stack/stack.ts +169 -0
- package/src/stack/stack.types.ts +11 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { LineElement as e } from "@websublime/line-core";
|
|
2
|
+
import { commonReset as t } from "@websublime/line-core/styles";
|
|
3
|
+
import { css as n, html as r } from "lit";
|
|
4
|
+
//#region src/stack/stack.ts
|
|
5
|
+
var i = ["horizontal", "vertical"], a = [
|
|
6
|
+
"start",
|
|
7
|
+
"center",
|
|
8
|
+
"end",
|
|
9
|
+
"spaceBetween",
|
|
10
|
+
"spaceAround",
|
|
11
|
+
"spaceEvenly",
|
|
12
|
+
"stretch"
|
|
13
|
+
], o = [
|
|
14
|
+
"start",
|
|
15
|
+
"center",
|
|
16
|
+
"end",
|
|
17
|
+
"stretch"
|
|
18
|
+
], s = [
|
|
19
|
+
"0",
|
|
20
|
+
"1",
|
|
21
|
+
"2",
|
|
22
|
+
"3",
|
|
23
|
+
"4",
|
|
24
|
+
"5"
|
|
25
|
+
];
|
|
26
|
+
function c(e, t) {
|
|
27
|
+
return { fromAttribute: (n) => e.find((e) => e === n) ?? t };
|
|
28
|
+
}
|
|
29
|
+
var l = { fromAttribute: (e) => s.find((t) => t === e) }, u = class extends e {
|
|
30
|
+
static {
|
|
31
|
+
this.properties = {
|
|
32
|
+
orientation: {
|
|
33
|
+
reflect: !0,
|
|
34
|
+
useDefault: !0,
|
|
35
|
+
converter: c(i, "vertical")
|
|
36
|
+
},
|
|
37
|
+
justify: {
|
|
38
|
+
reflect: !0,
|
|
39
|
+
useDefault: !0,
|
|
40
|
+
converter: c(a, "start")
|
|
41
|
+
},
|
|
42
|
+
align: {
|
|
43
|
+
reflect: !0,
|
|
44
|
+
useDefault: !0,
|
|
45
|
+
converter: c(o, "stretch")
|
|
46
|
+
},
|
|
47
|
+
gap: {
|
|
48
|
+
reflect: !0,
|
|
49
|
+
converter: l
|
|
50
|
+
},
|
|
51
|
+
wrap: {
|
|
52
|
+
type: Boolean,
|
|
53
|
+
reflect: !0,
|
|
54
|
+
useDefault: !0
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
static {
|
|
59
|
+
this.styles = [t, n`
|
|
60
|
+
:host {
|
|
61
|
+
--line-stack-gap: var(--line-layout-gap-2);
|
|
62
|
+
|
|
63
|
+
display: flex;
|
|
64
|
+
flex-direction: column;
|
|
65
|
+
justify-content: flex-start;
|
|
66
|
+
align-items: stretch;
|
|
67
|
+
flex-wrap: nowrap;
|
|
68
|
+
gap: var(--line-stack-gap);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
:host([hidden]) {
|
|
72
|
+
display: none;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
:host([orientation='horizontal']) {
|
|
76
|
+
flex-direction: row;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
:host([justify='center']) {
|
|
80
|
+
justify-content: center;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
:host([justify='end']) {
|
|
84
|
+
justify-content: flex-end;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
:host([justify='spaceBetween']) {
|
|
88
|
+
justify-content: space-between;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
:host([justify='spaceAround']) {
|
|
92
|
+
justify-content: space-around;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
:host([justify='spaceEvenly']) {
|
|
96
|
+
justify-content: space-evenly;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
:host([justify='stretch']) ::slotted(*) {
|
|
100
|
+
flex-grow: 1;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
:host([align='start']) {
|
|
104
|
+
align-items: flex-start;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
:host([align='center']) {
|
|
108
|
+
align-items: center;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
:host([align='end']) {
|
|
112
|
+
align-items: flex-end;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
:host([wrap]) {
|
|
116
|
+
flex-wrap: wrap;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
:host([gap='0']) {
|
|
120
|
+
--line-stack-gap: 0;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
:host([gap='1']) {
|
|
124
|
+
--line-stack-gap: var(--line-layout-gap-1);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
:host([gap='2']) {
|
|
128
|
+
--line-stack-gap: var(--line-layout-gap-2);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
:host([gap='3']) {
|
|
132
|
+
--line-stack-gap: var(--line-layout-gap-3);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
:host([gap='4']) {
|
|
136
|
+
--line-stack-gap: var(--line-layout-gap-4);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
:host([gap='5']) {
|
|
140
|
+
--line-stack-gap: var(--line-layout-gap-5);
|
|
141
|
+
}
|
|
142
|
+
`];
|
|
143
|
+
}
|
|
144
|
+
constructor() {
|
|
145
|
+
super(), this.orientation = "vertical", this.justify = "start", this.align = "stretch", this.wrap = !1;
|
|
146
|
+
}
|
|
147
|
+
render() {
|
|
148
|
+
return r`<slot></slot>`;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
customElements.define("line-stack", u);
|
|
152
|
+
//#endregion
|
|
153
|
+
export { u as LineStack };
|
|
154
|
+
|
|
155
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["ORIENTATIONS","JUSTIFIES","ALIGNS","GAPS","enumConverter","values","entry","value","fallback","gapConverter","gap","LineStack","LineElement","commonReset"],"sources":["../../src/stack/stack.ts"],"sourcesContent":["import { LineElement } from '@websublime/line-core';\nimport { commonReset } from '@websublime/line-core/styles';\nimport { type ComplexAttributeConverter, css, html, type PropertyDeclarations, type TemplateResult } from 'lit';\nimport type { StackAlign, StackGap, StackJustify, StackOrientation } from './stack.types.js';\n\nconst ORIENTATIONS: readonly StackOrientation[] = ['horizontal', 'vertical'];\nconst JUSTIFIES: readonly StackJustify[] = [\n 'start',\n 'center',\n 'end',\n 'spaceBetween',\n 'spaceAround',\n 'spaceEvenly',\n 'stretch',\n];\nconst ALIGNS: readonly StackAlign[] = ['start', 'center', 'end', 'stretch'];\nconst GAPS: readonly StackGap[] = ['0', '1', '2', '3', '4', '5'];\n\n/** An unknown value behaves as `fallback`; the attribute keeps what the author wrote. */\nfunction enumConverter<T extends string>(values: readonly T[], fallback: T): ComplexAttributeConverter<T> {\n return { fromAttribute: (value) => values.find((entry) => entry === value) ?? fallback };\n}\n\n/** An unknown `gap` behaves as no attribute, so the default step applies. */\nconst gapConverter: ComplexAttributeConverter<StackGap | undefined> = {\n fromAttribute: (value) => GAPS.find((gap) => gap === value),\n};\n\n/**\n * `<line-stack>` lays out its children in one row or one column with a gap\n * between them. The host is the flex container and the slotted children are\n * its flex items. It adds no role, so it stays `generic`; put `role=\"list\"`\n * on the host and `role=\"listitem\"` on the children to build a list.\n *\n * @summary A layout container that places its children in one row or column with even spacing.\n *\n * @slot - The children; each element becomes a flex item.\n *\n * @cssprop [--line-stack-gap=var(--line-layout-gap-2)] - Space between items; `gap=\"0\"` uses `0` and `gap=\"N\"` uses `var(--line-layout-gap-N)`.\n */\nexport class LineStack extends LineElement {\n static override properties: PropertyDeclarations = {\n orientation: { reflect: true, useDefault: true, converter: enumConverter(ORIENTATIONS, 'vertical') },\n justify: { reflect: true, useDefault: true, converter: enumConverter(JUSTIFIES, 'start') },\n align: { reflect: true, useDefault: true, converter: enumConverter(ALIGNS, 'stretch') },\n gap: { reflect: true, converter: gapConverter },\n wrap: { type: Boolean, reflect: true, useDefault: true },\n };\n\n static override styles = [\n commonReset,\n css`\n :host {\n --line-stack-gap: var(--line-layout-gap-2);\n\n display: flex;\n flex-direction: column;\n justify-content: flex-start;\n align-items: stretch;\n flex-wrap: nowrap;\n gap: var(--line-stack-gap);\n }\n\n :host([hidden]) {\n display: none;\n }\n\n :host([orientation='horizontal']) {\n flex-direction: row;\n }\n\n :host([justify='center']) {\n justify-content: center;\n }\n\n :host([justify='end']) {\n justify-content: flex-end;\n }\n\n :host([justify='spaceBetween']) {\n justify-content: space-between;\n }\n\n :host([justify='spaceAround']) {\n justify-content: space-around;\n }\n\n :host([justify='spaceEvenly']) {\n justify-content: space-evenly;\n }\n\n :host([justify='stretch']) ::slotted(*) {\n flex-grow: 1;\n }\n\n :host([align='start']) {\n align-items: flex-start;\n }\n\n :host([align='center']) {\n align-items: center;\n }\n\n :host([align='end']) {\n align-items: flex-end;\n }\n\n :host([wrap]) {\n flex-wrap: wrap;\n }\n\n :host([gap='0']) {\n --line-stack-gap: 0;\n }\n\n :host([gap='1']) {\n --line-stack-gap: var(--line-layout-gap-1);\n }\n\n :host([gap='2']) {\n --line-stack-gap: var(--line-layout-gap-2);\n }\n\n :host([gap='3']) {\n --line-stack-gap: var(--line-layout-gap-3);\n }\n\n :host([gap='4']) {\n --line-stack-gap: var(--line-layout-gap-4);\n }\n\n :host([gap='5']) {\n --line-stack-gap: var(--line-layout-gap-5);\n }\n `,\n ];\n\n /** Main axis; an unknown value behaves as `vertical`. A horizontal stack follows the writing direction. */\n declare orientation: StackOrientation;\n /** Distribution on the main axis; `stretch` grows every item. An unknown value behaves as `start`. */\n declare justify: StackJustify;\n /** Alignment on the cross axis; an unknown value behaves as `stretch`. */\n declare align: StackAlign;\n /** Gap step from `0` to `5`; `undefined` or an unknown value gives step `2`. */\n declare gap: StackGap | undefined;\n /** Lets the items flow onto more lines when they run out of space. */\n declare wrap: boolean;\n\n constructor() {\n super();\n // `useDefault` keeps these initial values out of the attributes, so an unknown author value stays (C8).\n this.orientation = 'vertical';\n this.justify = 'start';\n this.align = 'stretch';\n this.wrap = false;\n }\n\n override render(): TemplateResult {\n return html`<slot></slot>`;\n }\n}\n\ncustomElements.define('line-stack', LineStack);\n\ndeclare global {\n interface HTMLElementTagNameMap {\n 'line-stack': LineStack;\n }\n}\n"],"mappings":";;;;AAKA,IAAMA,IAA4C,CAAC,cAAc,UAAU,GACrEC,IAAqC;CACzC;CACA;CACA;CACA;CACA;CACA;CACA;AACF,GACMC,IAAgC;CAAC;CAAS;CAAU;CAAO;AAAS,GACpEC,IAA4B;CAAC;CAAK;CAAK;CAAK;CAAK;CAAK;AAAG;AAG/D,SAASC,EAAgC,GAAsB,GAA2C;CACxG,OAAO,EAAE,gBAAgB,MAAUC,EAAO,MAAM,MAAUC,MAAUC,CAAK,KAAKC,EAAS;AACzF;AAGA,IAAMC,IAAgE,EACpE,gBAAgB,MAAUN,EAAK,MAAM,MAAQO,MAAQH,CAAK,EAC5D,GAcaI,IAAb,cAA+BC,EAAY;;EACU,KAAA,aAAA;GACjD,aAAa;IAAE,SAAS;IAAM,YAAY;IAAM,WAAWR,EAAcJ,GAAc,UAAU;GAAE;GACnG,SAAS;IAAE,SAAS;IAAM,YAAY;IAAM,WAAWI,EAAcH,GAAW,OAAO;GAAE;GACzF,OAAO;IAAE,SAAS;IAAM,YAAY;IAAM,WAAWG,EAAcF,GAAQ,SAAS;GAAE;GACtF,KAAK;IAAE,SAAS;IAAM,WAAWO;GAAa;GAC9C,MAAM;IAAE,MAAM;IAAS,SAAS;IAAM,YAAY;GAAK;EACzD;;;EAEyB,KAAA,SAAA,CACvBI,GACA,CAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAoFL;;CAaA,cAAc;EAMZ,AALA,MAAM,GAEN,KAAK,cAAc,YACnB,KAAK,UAAU,SACf,KAAK,QAAQ,WACb,KAAK,OAAO;CACd;CAEA,SAAkC;EAChC,OAAO,CAAI;CACb;AACF;AAEA,eAAe,OAAO,cAAcF,CAAS"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { LineElement } from '@websublime/line-core';
|
|
2
|
+
import { type PropertyDeclarations, type TemplateResult } from 'lit';
|
|
3
|
+
import type { StackAlign, StackGap, StackJustify, StackOrientation } from './stack.types.js';
|
|
4
|
+
/**
|
|
5
|
+
* `<line-stack>` lays out its children in one row or one column with a gap
|
|
6
|
+
* between them. The host is the flex container and the slotted children are
|
|
7
|
+
* its flex items. It adds no role, so it stays `generic`; put `role="list"`
|
|
8
|
+
* on the host and `role="listitem"` on the children to build a list.
|
|
9
|
+
*
|
|
10
|
+
* @summary A layout container that places its children in one row or column with even spacing.
|
|
11
|
+
*
|
|
12
|
+
* @slot - The children; each element becomes a flex item.
|
|
13
|
+
*
|
|
14
|
+
* @cssprop [--line-stack-gap=var(--line-layout-gap-2)] - Space between items; `gap="0"` uses `0` and `gap="N"` uses `var(--line-layout-gap-N)`.
|
|
15
|
+
*/
|
|
16
|
+
export declare class LineStack extends LineElement {
|
|
17
|
+
static properties: PropertyDeclarations;
|
|
18
|
+
static styles: (import("lit").CSSResult | CSSStyleSheet)[];
|
|
19
|
+
/** Main axis; an unknown value behaves as `vertical`. A horizontal stack follows the writing direction. */
|
|
20
|
+
orientation: StackOrientation;
|
|
21
|
+
/** Distribution on the main axis; `stretch` grows every item. An unknown value behaves as `start`. */
|
|
22
|
+
justify: StackJustify;
|
|
23
|
+
/** Alignment on the cross axis; an unknown value behaves as `stretch`. */
|
|
24
|
+
align: StackAlign;
|
|
25
|
+
/** Gap step from `0` to `5`; `undefined` or an unknown value gives step `2`. */
|
|
26
|
+
gap: StackGap | undefined;
|
|
27
|
+
/** Lets the items flow onto more lines when they run out of space. */
|
|
28
|
+
wrap: boolean;
|
|
29
|
+
constructor();
|
|
30
|
+
render(): TemplateResult;
|
|
31
|
+
}
|
|
32
|
+
declare global {
|
|
33
|
+
interface HTMLElementTagNameMap {
|
|
34
|
+
'line-stack': LineStack;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=stack.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stack.d.ts","sourceRoot":"","sources":["../../src/stack/stack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAEpD,OAAO,EAA6C,KAAK,oBAAoB,EAAE,KAAK,cAAc,EAAE,MAAM,KAAK,CAAC;AAChH,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAyB7F;;;;;;;;;;;GAWG;AACH,qBAAa,SAAU,SAAQ,WAAW;IACxC,OAAgB,UAAU,EAAE,oBAAoB,CAM9C;IAEF,OAAgB,MAAM,8CAsFpB;IAEF,2GAA2G;IACnG,WAAW,EAAE,gBAAgB,CAAC;IACtC,sGAAsG;IAC9F,OAAO,EAAE,YAAY,CAAC;IAC9B,0EAA0E;IAClE,KAAK,EAAE,UAAU,CAAC;IAC1B,gFAAgF;IACxE,GAAG,EAAE,QAAQ,GAAG,SAAS,CAAC;IAClC,sEAAsE;IAC9D,IAAI,EAAE,OAAO,CAAC;IAEtB,cAOC;IAEQ,MAAM,IAAI,cAAc,CAEhC;CACF;AAID,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,YAAY,EAAE,SAAS,CAAC;KACzB;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Main axis of `<line-stack>`; the A2UI basic v0.9 `List.direction` values. */
|
|
2
|
+
export type StackOrientation = 'horizontal' | 'vertical';
|
|
3
|
+
/** Main-axis distribution of `<line-stack>`; the A2UI basic v0.9 `justify` values. */
|
|
4
|
+
export type StackJustify = 'start' | 'center' | 'end' | 'spaceBetween' | 'spaceAround' | 'spaceEvenly' | 'stretch';
|
|
5
|
+
/** Cross-axis alignment of `<line-stack>`; the A2UI basic v0.9 `align` values without `baseline`. */
|
|
6
|
+
export type StackAlign = 'start' | 'center' | 'end' | 'stretch';
|
|
7
|
+
/** Gap step of `<line-stack>` on the `--line-layout-gap-*` scale (ARCH §17.2 C6). */
|
|
8
|
+
export type StackGap = '0' | '1' | '2' | '3' | '4' | '5';
|
|
9
|
+
//# sourceMappingURL=stack.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stack.types.d.ts","sourceRoot":"","sources":["../../src/stack/stack.types.ts"],"names":[],"mappings":"AAAA,gFAAgF;AAChF,MAAM,MAAM,gBAAgB,GAAG,YAAY,GAAG,UAAU,CAAC;AAEzD,sFAAsF;AACtF,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,QAAQ,GAAG,KAAK,GAAG,cAAc,GAAG,aAAa,GAAG,aAAa,GAAG,SAAS,CAAC;AAEnH,qGAAqG;AACrG,MAAM,MAAM,UAAU,GAAG,OAAO,GAAG,QAAQ,GAAG,KAAK,GAAG,SAAS,CAAC;AAEhE,qFAAqF;AACrF,MAAM,MAAM,QAAQ,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@websublime/line-components",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
@@ -22,10 +22,18 @@
|
|
|
22
22
|
"types": "./dist/index.d.ts",
|
|
23
23
|
"import": "./dist/index.js"
|
|
24
24
|
},
|
|
25
|
-
"./custom-elements.json": "./custom-elements.json"
|
|
25
|
+
"./custom-elements.json": "./custom-elements.json",
|
|
26
|
+
"./button": {
|
|
27
|
+
"types": "./dist/button/index.d.ts",
|
|
28
|
+
"import": "./dist/button/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./stack": {
|
|
31
|
+
"types": "./dist/stack/index.d.ts",
|
|
32
|
+
"import": "./dist/stack/index.js"
|
|
33
|
+
}
|
|
26
34
|
},
|
|
27
35
|
"dependencies": {
|
|
28
|
-
"@websublime/line-core": "^0.2.
|
|
36
|
+
"@websublime/line-core": "^0.2.2",
|
|
29
37
|
"@websublime/line-themes": "^0.1.2",
|
|
30
38
|
"@websublime/line-tokens": "^0.1.3",
|
|
31
39
|
"@websublime/line-utils": "^0.1.0",
|
|
@@ -33,7 +41,8 @@
|
|
|
33
41
|
"lit": "^3.3.3"
|
|
34
42
|
},
|
|
35
43
|
"devDependencies": {
|
|
36
|
-
"@websublime/line-schemas": "^0.1.1"
|
|
44
|
+
"@websublime/line-schemas": "^0.1.1",
|
|
45
|
+
"@websublime/line-testing": "^0.0.0"
|
|
37
46
|
},
|
|
38
47
|
"scripts": {
|
|
39
48
|
"build": "vite build && tsc -p tsconfig.json --noEmit false --emitDeclarationOnly && bun run analyze",
|
|
@@ -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';
|