-
Notifications
You must be signed in to change notification settings - Fork 0
/
kitchen-sink.tsx
267 lines (238 loc) · 8.33 KB
/
kitchen-sink.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import * as DeStagnate from "../../.."
const width = 250
// Class which contains reference to some SVG rectangle and a value that is stateful
class RectState extends DeStagnate.StateContainer<number, SVGRectElement | null> {
constructor() {
super(0)
}
// Define what an update should do
updateDOM(rectRef: DeStagnate.Ref<SVGRectElement>) {
// Use the bindProps helper function to update the x value of the rectangle and change its colour
DeStagnate.bindProps(
rectRef.current,
{
x: this.value.toString(),
fill: this.value % 2 === 0 ? "#0D6EFD" : "#28A745",
},
true,
)
}
}
const paragraphContent = '"My paragraph" <i>Escaped</i> <script>alert("xss")</script>'
createElement: {
const rectState = new RectState()
const divRef = DeStagnate.createRef<HTMLDivElement>()
const ReactiveRect: DeStagnate.FC<{state: RectState}> = ({state}) =>
DeStagnate.createElement(
"svg:svg",
{
width: width * 2,
height: width,
viewBox: `0 0 ${width * 2} ${width}`,
fill: "none",
},
DeStagnate.createElement("svg:rect", {
width,
height: width,
fill: state.value % 2 === 0 ? "#0D6EFD" : "#28A745",
x: state.value,
ref: state.ref,
}),
)
document.getElementById("my-container-1")?.appendChild(
// `createElement` can be abbreviated to `ce`
DeStagnate.createElement(
DeStagnate.Fragment,
null,
DeStagnate.createElement(
"div",
{
class: "my-class",
ref: divRef,
onMyCustomEvent: (event: Event) => {
console.log(event)
rectState.update(rectState.value + 25)
},
style: {
borderStyle: "solid",
borderColor: "black",
borderWidth: "1px",
paddingTop: "1rem",
},
},
DeStagnate.createElement(
"p",
null,
paragraphContent,
DeStagnate.createElement("i", null, " italic"),
),
),
DeStagnate.createElement(
"button",
{
onClick: (event) =>
divRef.current?.dispatchEvent(
new CustomEvent("mycustomevent", {detail: event}),
),
class: "btn btn-secondary mr-3",
},
"Click me!",
),
DeStagnate.createElement(ReactiveRect, {state: rectState}),
),
)
}
// Alternatively, you can use JSX. You will need a transpiler, though.
jsx: {
const rectState = new RectState()
const divRef = DeStagnate.createRef<HTMLDivElement>()
const ReactiveRect: DeStagnate.FC<{state: RectState}> = ({state}) => (
<svg:svg
width={width * 2}
height={width}
viewBox={`0 0 ${width * 2} ${width}`}
fill="none"
>
<svg:rect
width={width}
height={width}
fill={state.value % 2 === 0 ? "#0D6EFD" : "#28A745"}
x={state.value}
ref={state.ref}
/>
</svg:svg>
)
document.getElementById("my-container-2")?.appendChild(
<>
<div
class="my-class"
ref={divRef}
onMyCustomEvent={(event: Event) => {
console.log(event)
rectState.update(rectState.value + 25)
}}
style={{
borderStyle: "solid",
borderColor: "black",
borderWidth: "1px",
paddingTop: "1rem",
}}
>
<p>
{paragraphContent}
<i> italic</i>
</p>
</div>
<button
onClick={(event) =>
divRef.current?.dispatchEvent(
new CustomEvent("mycustomevent", {detail: event}),
)
}
class="btn btn-secondary mr-3"
>
Click me!
</button>
<ReactiveRect state={rectState} />
</>,
)
}
// Using the HTM library on top:
import htm from "htm"
htm: {
const html = htm.bind(DeStagnate.createElement)
const rectState = new RectState()
const divRef = DeStagnate.createRef<HTMLDivElement>()
const ReactiveRect: DeStagnate.FC<{state: RectState}> = ({state}) =>
html`<svg:svg
width=${width * 2}
height=${width}
viewBox="0 0 ${width * 2} ${width}"
fill="none"
>
<svg:rect
width=${width}
height=${width}
fill=${state.value % 2 === 0 ? "#0D6EFD" : "#28A745"}
x=${state.value}
ref=${state.ref}
/>
</svg:svg>` as Element
document.getElementById("my-container-3")?.appendChild(
html`<${DeStagnate.Fragment}>
<div
class="my-class"
ref=${divRef}
onMyCustomEvent=${(event: Event) => {
console.log(event)
rectState.update(rectState.value + 25)
}}
style=${{
borderStyle: "solid",
borderColor: "black",
borderWidth: "1px",
paddingTop: "1rem",
}}
>
<p>
${paragraphContent}
<i> italic</i>
</p>
</div>
<button
onClick=${(event: MouseEvent) =>
divRef.current?.dispatchEvent(
new CustomEvent("mycustomevent", {detail: event}),
)}
class="btn btn-secondary mr-3"
>
Click me!
</button>
<${ReactiveRect} state=${rectState} />
<//>` as Element,
)
}
// Using vanilla DOM methods:
vanillaDOM: {
const svgURI = "http://www.w3.org/2000/svg"
let rectStateValue = 0
const container = document.getElementById("my-container-4")
const svg = document.createElementNS(svgURI, "svg")
svg.setAttributeNS(null, "width", (width * 2).toString())
svg.setAttributeNS(null, "height", width.toString())
svg.setAttributeNS(null, "viewBox", `0 0 ${width * 2} ${width}`)
svg.setAttributeNS(null, "fill", "none")
const rect = document.createElementNS(svgURI, "rect")
rect.setAttributeNS(null, "width", width.toString())
rect.setAttributeNS(null, "height", width.toString())
rect.setAttributeNS(null, "fill", rectStateValue % 2 === 0 ? "#0D6EFD" : "#28A745")
rect.setAttributeNS(null, "x", rectStateValue.toString())
svg.appendChild(rect)
const div = document.createElement("div")
div.classList.add("my-class")
div.addEventListener("mycustomevent", (event) => {
console.log(event)
rectStateValue += 25
rect.setAttributeNS(null, "fill", rectStateValue % 2 === 0 ? "#0D6EFD" : "#28A745")
rect.setAttributeNS(null, "x", rectStateValue.toString())
})
div.style.borderStyle = "solid"
div.style.borderColor = "black"
div.style.borderWidth = "1px"
div.style.paddingTop = "1rem"
const paragraph = document.createElement("p")
paragraph.appendChild(document.createTextNode(paragraphContent))
const italic = document.createElement("i")
italic.innerText = " italic"
paragraph.appendChild(italic)
div.appendChild(paragraph)
container?.appendChild(div)
const button = document.createElement("button")
button.addEventListener("click", (event) =>
div.dispatchEvent(new CustomEvent("mycustomevent", {detail: event})),
)
button.innerText = "Click me!"
button.className = "btn btn-secondary mr-3"
container?.appendChild(button)
container?.appendChild(svg)
}