-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
components.tsx
238 lines (191 loc) · 5.33 KB
/
components.tsx
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import * as React from "react"
/* Web Components */
class MyButton extends HTMLElement {
connectedCallback () {
const shadow = this.attachShadow({mode: 'open'});
shadow.innerHTML = `<button><slot></slot></button>`;
}
}
class MyTextArea extends HTMLElement {
_label: string = ""
get label () {
return this._label
}
set label (value: string) {
this._label = value
this.render()
}
connectedCallback () {
this.attachShadow({mode: 'open'});
this._label = String(this.getAttribute("label"))
this.render()
}
render () {
if (this.isConnected === false) return ""
if (this.shadowRoot == null) return ""
this.shadowRoot.innerHTML = `
<label for="textarea">${this.label}</label>
<textarea id="textarea"><slot></slot></textarea>
`;
}
}
class MyImage extends HTMLElement {
_src: string = ""
_alt: string = ""
get src () {
return this._src
}
set src (value: string) {
this._src = value
this.render()
}
get alt () {
return this._alt
}
set alt (value: string) {
this._alt = value
this.render()
}
connectedCallback () {
this.attachShadow({mode: 'open'});
this._alt = String(this.getAttribute("alt"))
this._src = String(this.getAttribute("src"))
// Leaving the alt attribute on confuses dom-testing-library
this.removeAttribute("src")
this.removeAttribute("alt")
this.render()
}
render () {
if (this.shadowRoot == null) return ""
if (this.isConnected === false) return ""
this.shadowRoot.innerHTML = `
<img src="${this.src}" alt="${this.alt}">
`;
}
}
class DuplicateButtons extends HTMLElement {
connectedCallback () {
this.attachShadow({mode: 'open'});
this.render()
}
render () {
if (this.shadowRoot == null) return ""
if (this.isConnected === false) return ""
this.shadowRoot.innerHTML = `
<slot name="start"></slot>
<button>Button One</button>
<br>
<slot></slot>
<br>
<button>Button Two</button>
<slot name="end"></slot>
`;
}
}
class NestedShadowRootsElement extends HTMLElement {
connectedCallback () {
this.attachShadow({mode: 'open'});
this.render()
}
render () {
if (this.shadowRoot == null) return ""
if (this.isConnected === false) return ""
this.shadowRoot.innerHTML = `
<duplicate-buttons></duplicate-buttons>
<slot></slot>
`;
}
}
class TripleShadowRootsElement extends HTMLElement {
connectedCallback () {
this.attachShadow({mode: 'open'});
this.render()
}
render () {
if (this.shadowRoot == null) return ""
if (this.isConnected === false) return ""
this.shadowRoot.innerHTML = `
<nested-shadow-roots>
</nested-shadow-roots>
`;
}
}
export class MySelect extends HTMLElement {
_label: string = ""
get label () {
return this._label
}
set label (value: string) {
this._label = value
this.render()
}
connectedCallback () {
this.attachShadow({mode: 'open'});
this.render()
}
render () {
if (this.shadowRoot == null) return ""
if (this.isConnected === false) return ""
this.shadowRoot.innerHTML = `
<label>
<slot name="label">
${this.label}
</slot>
<select>
<slot></slot>
</select>
</label>
`;
}
}
/* Custom element declarations */
type CustomEvents<K extends string> = { [key in K] : (event: CustomEvent) => void };
type CustomElement<T, K extends string = string> = Partial<T & React.DOMAttributes<T> & { children: any } & CustomEvents<`on${K}`>>;
window.customElements.define("my-button", class extends MyButton {})
window.customElements.define("my-image", class extends MyImage {})
window.customElements.define("my-text-area", class extends MyTextArea {})
window.customElements.define("duplicate-buttons", DuplicateButtons)
window.customElements.define("nested-shadow-roots", class extends NestedShadowRootsElement {})
window.customElements.define("triple-shadow-roots", class extends TripleShadowRootsElement {})
window.customElements.define("my-select", class extends MySelect {})
declare global {
namespace JSX {
interface IntrinsicElements {
['my-button']: CustomElement<MyButton>;
['my-image']: CustomElement<MyImage>;
['my-text-area']: CustomElement<MyTextArea>;
['duplicate-buttons']: CustomElement<DuplicateButtons>;
['nested-shadow-roots']: CustomElement<NestedShadowRootsElement>;
['triple-shadow-roots']: CustomElement<TripleShadowRootsElement>;
['my-select']: CustomElement<MySelect>
}
}
}
/* React Components */
export function SimpleButton () {
const [count, setCount] = React.useState(0)
return (
<button onClick={() => setCount(count + 1)}>
{count}
</button>
);
}
export function Button() {
const [count, setCount] = React.useState(0)
return (
<my-button onClick={() => setCount(count + 1)}>
{count}
</my-button>
);
}
export const TextArea = () => <my-text-area label="Comments" />
export const AnimatedImage = () => (
<my-image
src="https://shoelace.style/assets/images/walk.gif"
alt="Animation of untied shoes walking on pavement"
/>
);
export const Duplicates = (props: Record<string, any>) => <duplicate-buttons {...props} />
export const NestedShadowRoots = (props: Record<string, any>) => <nested-shadow-roots {...props} />
export const TripleShadowRoots = (props: Record<string, any>) => <triple-shadow-roots {...props} />
export const Select = (props: Record<string, any>) => <my-select {...props} />