-
Notifications
You must be signed in to change notification settings - Fork 1
/
wc-menu-button.js
214 lines (186 loc) · 4.33 KB
/
wc-menu-button.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// @ts-check
const style = `
:host {
width: 37px;
}
button {
all: unset;
margin: 0;
width: inherit;
position: relative;
display: inline-block;
transform: rotate(0deg);
transition: 0.5s ease-in-out;
cursor: var(--wc-menu-button-cursor, default);
transition: all 0.2s ease-in-out;
-webkit-tap-highlight-color: transparent;
}
/* this trick makes the height 75% of the width (4:3 ratio) */
button:after {
padding-top: 75%;
display: block;
content: "";
}
@media (hover: hover) {
:host(:hover) {
opacity: 0.75;
}
}
:focus {
outline: 2px solid color-mix(in srgb, var(--wc-menu-button-color, #000000) 50%, transparent);
outline-offset: 5px;
border-radius: 5px;
}
button span {
display: block;
position: absolute;
height: 20%;
width: 100%;
background: var(--wc-menu-button-color, #000000);
border-radius: 10%;
opacity: 1;
left: 0;
transform: rotate(0deg);
transition: 0.25s ease-in-out;
}
button span:nth-child(1) {
top: 0%;
}
button span:nth-child(2),
button span:nth-child(3) {
top: 40%;
}
button span:nth-child(4) {
top: 80%;
}
:host([open]) button span:nth-child(1) {
top: 40%;
width: 0%;
left: 50%;
}
:host([open]) button span:nth-child(2) {
transform: rotate(45deg);
}
:host([open]) button span:nth-child(3) {
transform: rotate(-45deg);
}
:host([open]) button span:nth-child(4) {
top: 40%;
width: 0%;
left: 50%;
}
`;
const template = `
<button type="button" aria-label="menu button">
<span></span>
<span></span>
<span></span>
<span></span>
</button>
`;
// using a template so it only needs to be parsed once, whereas setting
// innerHTML directly in the custom element ctor means the HTML would get parsed
// for every custom element on the page
const tmpl = document.createElement("template");
tmpl.innerHTML = `<style>${style}</style>${template}`;
/**
* A simple, animating menu button custom element
*/
export class WcMenuButton extends HTMLElement {
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: "open" });
shadowRoot.appendChild(tmpl.content.cloneNode(true));
/**
* @internal
* @type {HTMLElement | null}
*/
this._menuButton = shadowRoot.querySelector("button");
}
connectedCallback() {
if (this._menuButton !== null) {
this._menuButton.addEventListener("click", this.handleMenuButtonClick);
}
this.upgradeProperty("open");
}
// from https://developers.google.com/web/fundamentals/web-components/best-practices#lazy-properties
/**
* @param {string} prop
*
* @internal
* @memberOf WcMenuButton
*/
upgradeProperty(prop) {
if (this.hasOwnProperty(prop)) {
let value = this[prop];
delete this[prop];
this[prop] = value;
}
}
get open() {
return this.hasAttribute("open");
}
set open(isOpen) {
if (isOpen) {
if (!this.hasAttribute("open")) {
this.setAttribute("open", "");
}
} else {
if (this.hasAttribute("open")) {
this.removeAttribute("open");
}
}
}
static get observedAttributes() {
return ["open", "aria-label", "type"];
}
/**
* @param {string} name
* @param {unknown} _oldValue
* @param {unknown} _newValue
* @memberof WcMenuButton
*/
attributeChangedCallback(name, _oldValue, _newValue) {
if (name === "open") {
if (!this.open) {
this.dispatchEvent(
new CustomEvent("closed", {
bubbles: true,
}),
);
} else {
this.dispatchEvent(
new CustomEvent("opened", {
bubbles: true,
}),
);
}
} else if (name === "aria-label") {
if (this._menuButton !== null) {
if (this.hasAttribute("aria-label")) {
this._menuButton.setAttribute(
"aria-label",
this.getAttribute("aria-label"),
);
} else {
this._menuButton.removeAttribute("aria-label");
}
}
} else if (name === "type") {
if (this._menuButton !== null) {
if (this.hasAttribute("type")) {
this._menuButton.setAttribute("type", this.getAttribute("type"));
} else {
this._menuButton.removeAttribute("type");
}
}
}
}
/**
* @internal
*/
handleMenuButtonClick = () => {
this.open = !this.open;
};
}
customElements.define("wc-menu-button", WcMenuButton);