-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-only
import { wrapElement, YamoriElement } from "../../element.ts";
import css from "./button.css?inline";
type ObservedAttributes = readonly ["pending"];
export const YamoriButton = wrapElement(
"yamori-button",
class extends YamoriElement {
static formAssociated = true;
static get observedAttributes(): ObservedAttributes {
return ["pending"];
}
#label: HTMLSpanElement;
#spinner: HTMLDivElement;
#pending: boolean = this.hasAttribute("pending");
attributeChangedCallback(
name: ObservedAttributes[number],
oldValue: string | null,
newValue: string | null,
) {
if (oldValue === newValue) {
return;
}
switch (name) {
case "pending":
this.setPending(typeof newValue === "string");
return;
}
}
setPending(value: boolean) {
this.#pending = value;
if (value) {
this.setCustomState("pending");
// NOTE: `aria-busy` は Live Region が更新中かどうかのフラグであり、
// ボタンといった内容の変わらないトリガーの状態表示としては不適切
// であるため弄らない。
this.internals.ariaDisabled = "true";
} else {
this.removeCustomState("pending");
this.internals.ariaDisabled = "false";
}
}
constructor() {
super();
const shadow = this.attachShadow({
mode: "open",
});
const style = document.createElement("style");
style.textContent = css;
shadow.appendChild(style);
this.#label = document.createElement("span");
this.#label.classList.add("inner");
shadow.appendChild(this.#label);
const slot = document.createElement("slot");
this.#label.appendChild(slot);
this.#spinner = document.createElement("div");
this.#spinner.classList.add("spinner");
shadow.appendChild(this.#spinner);
this.internals.role = "button";
}
connectedCallback() {
// NOTE: 現状属性に影響を与えずに `tabIndex` を変える方法がない。
// 属性を変える操作は DOM に接続してから行わないと DOM のエラー
// となるためここで行う必要がある。
this.tabIndex = 0;
this.addEventListener("click", this.#onClick, {
capture: true,
});
this.addEventListener("keydown", this.#onKeyDown);
this.addEventListener("keyup", this.#onKeyUp);
}
disconnectedCallback() {
this.removeEventListener("click", this.#onClick, {
capture: true,
});
this.removeEventListener("keydown", this.#onKeyDown);
this.removeEventListener("keyup", this.#onKeyUp);
}
get disabled(): boolean {
return this.hasAttribute("disabled");
}
#isEnabled = (): boolean => {
return (
!this.#pending && !this.hasAttribute("disabled") && this.ariaDisabled !== "true"
);
};
#onClick = (event: MouseEvent): void => {
if (!this.#isEnabled()) {
event.preventDefault();
event.stopPropagation();
return;
}
};
#onKeyDown = (event: KeyboardEvent): void => {
if (event.key !== " ") {
return;
}
if (!this.#isEnabled()) {
event.preventDefault();
return;
}
this.setCustomState("pressed");
};
#onKeyUp = (event: KeyboardEvent): void => {
if (event.key !== " ") {
return;
}
if (!this.#isEnabled()) {
event.preventDefault();
return;
}
this.removeCustomState("pressed");
// `clientX/Y` は `getBoundingClientRect()` を使うことで計算できるが、
// レイアウトにアクセスするためコストがかかる。現状これらのプロパティは
// 利用していないためスキップしている。
this.dispatchEvent(
new MouseEvent("click", {
view: event.view,
bubbles: true,
cancelable: true,
}),
);
};
},
);