-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
504 lines (438 loc) · 16.4 KB
/
index.html
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>domxy — Lightweight JavaScript Library for Creating HTML and SVG Elements</title>
<script type="module">
import { domxy } from "./domxy.js";
const {
a,
button,
code,
div,
fieldset,
form,
fragment,
h1,
h2,
h3,
hr,
i,
input,
label,
legend,
li,
link,
main,
ol,
p,
path,
pre,
script,
span,
strong,
style,
svg,
u,
ul,
} = domxy;
document.head.append(
fragment(
style(`
@font-face {
font-family: "Label";
src: url("./glyphicons-label-bold.woff2") format("woff2");
}
:root {
box-sizing: border-box;
line-height: 1.4;
-moz-text-size-adjust: none;
-webkit-text-size-adjust: none;
text-size-adjust: none;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
a {
color: currentColor;
font-weight: bold;
text-underline-offset: 0.25rem;
text-decoration-thickness: 0.125rem;
}
a:focus {
outline: 2px solid currentColor;
outline-offset: 0.25rem;
}
body {
background-color: aquamarine;
font-family: monospace-ui, monospace;
margin: 0 auto;
max-width: 80ch;
padding: 0;
}
*:not(pre) code {
background-color: white;
border: 0.125rem solid black;
border-radius: 0.25rem;
box-shadow:
1px 1px 0 0 black,
2px 2px 0 0 black;
font-weight: bold;
padding: 0.125rem 0.25rem;
}
h1, h2 {
font-family: "Label", sans-serif;
margin-bottom: 1rem;
}
h1 {
font-size: 2.5rem;
}
h2 {
border-bottom: 0.25rem solid currentColor;
font-size: 1.875rem;
padding-bottom: 0.125rem;
}
h3 {
font-size: 1.25rem;
}
i {
font-family: sans-serif;
font-style: normal;
}
li {
margin-bottom: 1rem;
}
main {
padding: 1rem;
}
p {
line-height: 2;
margin-bottom: 1.5rem;
}
pre {
background-color: white;
border: 0.25rem solid black;
border-radius: 0.666rem;
box-shadow:
1px 1px 0 0 black,
2px 2px 0 0 black,
3px 3px 0 0 black,
4px 4px 0 0 black;
font-size: 1rem;
margin-bottom: 1rem;
overflow: auto;
padding: 0.5rem;
white-space: pre-wrap;
}
strong {
font-weight: bold;
}
.alert {
background-color: yellow;
border: 0.25rem solid black;
border-radius: 0.666rem;
box-shadow:
1px 1px 0 0 black,
2px 2px 0 0 black,
3px 3px 0 0 black,
4px 4px 0 0 black;
display: block;
margin: 1.5rem 0;
padding: 1rem;
}
ol, ul {
margin-bottom: 2rem;
padding-left: 2rem;
}
u {
text-decoration-style: double;
text-decoration-thickness: 0.125rem;
text-underline-offset: 0.25rem;
}
.logo-icon {
height: 3rem;
width: 3rem;
}
[class*=shj-lang-] {
margin: 1rem 0 2rem 0;
max-width: min(100%, 100vw);
white-space: pre;
}
.shj-inline {
border-radius: 0.25rem;
display: inline-block;
margin: 0;
padding: 0.125rem 0.25rem;
}
[class*=shj-lang-]::selection {
background: aquamarine;
}
[class*=shj-lang-] ::selection {
background: aquamarine;
}
[class*=shj-lang-] > div {
display: flex;
overflow: auto;
}
[class*=shj-lang-] >div :last-child {
flex: 1;
outline: none;
}
.shj-numbers {
counter-reset: line;
padding-left: 0.25rem;
}
.shj-numbers div {
padding-right: 0.25rem;
}
.shj-numbers div:before {
color: gray;
content: counter(line);
counter-increment: line;
display: block;
margin-right: 0.5rem;
text-align: right;
}
.shj-syn-cmnt {
font-style: italic;
}
.shj-syn-deleted,
.shj-syn-var {
color: tomato;
}
.shj-oneline {
padding: 0.75rem;
}
.shj-syn-bool,
.shj-syn-deleted,
.shj-syn-err,
.shj-syn-kwd,
.shj-syn-num,
.shj-syn-var {
color: teal;
font-weight: bold;
}
.shj-syn-section,
.shj-syn-type,
.shj-syn-class,
.shj-syn-func {
color: darkviolet;
font-weight: bold;
}
.shj-syn-insert,
.shj-syn-str,
.shj-numbers {
color: deeppink;
}
.shj-syn-cmnt {
font-weight: bold;
}
`),
)
);
document.body.append(
fragment(
main(
h1({ class: "title", id: "title" },
span({ ariaHidden: true },
"</>",
span({ style: "opacity: 0.5;" }, ">"),
span({ style: "opacity: 0.25;" }, ">"),
),
" domxy"
),
p("A lightweight library for effortlessly creating HTML and SVG elements in JavaScript."),
h2("Example"),
h3("JavaScript"),
pre({ class: "shj-lang-js" },
code(`// Shoelace SlButton component
import "https://cdn.jsdelivr.net/npm/@shoelace-style/[email protected]/cdn/components/button/button.js"
// Domxy library
import { domxy } from "https://domxy.js.org/domxy.js"
// Destructure elements you want to use (including Custom Elements)
const { div, form, input, label, SlButton } = domxy
// Example form
const nameForm = form({ dataController: "hello" } },
div({ class: "form-group" },
label({ for: "name" }, "Name"),
input({
dataset: {
helloTarget: "name"
},
id: "name",
name: "name",
placeholder: "enter a name",
type: "text"
}),
),
SlButton({ dataset: { action: "click->hello#greet" } }, "Submit"),
)
// Append to the DOM
document.body.append(nameForm)`,
),
),
h3("DOM"),
pre({ class: "shj-lang-html" },
code(`<form data-controller="hello">
<div class="form-group">
<label for="name">Name</label>
<input data-hello-target="name" id="name" name="name" placeholder="enter a name" type="text">
</div>
<sl-button data-action="click->hello#greet">Submit</sl-button>
</form>`,
),
),
h2("Features"),
ul(
li(i({ ariaHidden: true }, "✨"), " Create HTML and SVG elements expressively"),
li(i({ ariaHidden: true }, "☕"), " Pure JavaScript, no special syntax to learn"),
li(i({ ariaHidden: true }, "🤙"), " Every element is a function call"),
li(i({ ariaHidden: true }, "🪪"), " Optionally pass attributes and properties, including ", code("data"), " and ", code("aria")),
li(i({ ariaHidden: true }, "⚡"), " Supports Custom Elements with Pascal-cased names"),
li(i({ ariaHidden: true }, "🚫"), " No need for a build tool"),
li(i({ ariaHidden: true }, "📦"), " Standard ES Module"),
),
h2("Overview"),
p(strong("domxy"), " is a ", a({ href: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy" }, "JavaScript Proxy"), " object that generates HTML and SVG DOM elements using an HTML-like syntax in pure JavaScript."),
p("You must ", a({ href: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment" }, "destructure"), " the elements from the ", strong("domxy"), " object before using:"),
pre({ class: "shj-lang-js" },
code(
`const { div, form, input, label, SlButton } = domxy`,
),
),
strong({ class: "alert" }, "An error will be thrown if you try to access an element without it being declared first."),
p("Custom Element instances can be created using Pascal case naming:"),
pre({ class: "shj-lang-js" },
code(`form(
SlInput({ variant: "danger" }), // sl-input
SlButton({ type: "submit" }, "Submit") // sl-button
)`,
),
),
p(a({ href: "https://developer.mozilla.org/en-US/docs/Learn_web_development/Howto/Solve_HTML_problems/Use_data_attributes" }, "Data attributes"), " and ", a({ href: "https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA" }, "ARIA attributes"), " can be assigned using Camel case names or the ", code("dataset"), " or ", code("aria"), " property:"),
h3("JavaScript"),
pre({ class: "shj-lang-js" },
code(`span({ aria: { labelledby: "tac", }, ariaChecked: true, dataset: { test: true }, role: "checkbox", tabindex: 0 }),
span({ id: "tac" }, "I agree to the Terms and Conditions.")`)
),
h3("DOM"),
pre({ class: "shj-lang-html" },
code(`<span aria-checked="false" aria-labelledby="tac" data-test="true" role="checkbox" tabindex="0"></span>
<span id="tac">I agree to the Terms and Conditions.</span>`)
),
p("Create SVG elements just like any other HTML element:"),
pre({ class: "shj-lang-js" },
code(`button({ type: "button" },
svg({ ariaHidden: true, class: "icon", viewBox: "0 0 20 20" },
path({
d: "M17,15.98059V17a1,1,0,0,1-1,1H4a1,1,0,0,1-1-1V15.98059a.9998.9998,0,0,1,.37531-.78082l2.556-2.0448A4.00431,4.00431,0,0,0,9.2998,15h1.4004a4.00431,4.00431,0,0,0,3.36853-1.845l2.556,2.0448A.9998.9998,0,0,1,17,15.98059ZM7.47211,11.81226A2,2,0,0,0,9.29974,13h1.40052a2,2,0,0,0,1.82763-1.18774L13.65527,9.2757A4.00024,4.00024,0,0,0,14,7.65112V6a3.97577,3.97577,0,0,0-4-4A4.1928,4.1928,0,0,0,6,6.17206V7.65112A4.00024,4.00024,0,0,0,6.34473,9.2757Z",
})
),
"Acccount"
)`)
),
p("Create a ", a({ href: "https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment" }, "DocumentFragment"), " using the ", code("fragment"), " function:"),
h3("JavaScript"),
pre({ class: "shj-lang-js" },
code(`fragment(
h1("Title"),
p("Intro paragraph.")
)`),
),
h3("DOM"),
pre({ class: "shj-lang-html" },
code(`#document-fragment
<h1>Title</h1>
<p>Intro paragraph.</p>`),
),
strong({ class: "alert" }, "DocumentFragments do not accept attributes or properties. A warning will be thrown when attempted."),
p("In addition to ", code("String"), ", ", code("Number"), ", and ", code("Boolean"), " types, attributes can accept ", code("Date"), ", ", code("Array"), " and ", code("Object"), " types as values by calling ", a({ href: "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify" }, "JSON.stringify()"), " on them:"),
h3("JavaScript"),
pre({ class: "shj-lang-js" },
code(`div({ dataModel: { type: "user", active: false } })`)
),
h3("DOM"),
pre({ class: "shj-lang-html" },
code(`<div data-model='{"type":"user","active":false}'></div>`)
),
strong({ class: "alert" }, "Property values do not get stringified. Not recommended for large sets of data."),
p("While not a ", strong("domxy"), " feature, if used inside of a JavaScript class, you can take advantage of inline property assignment to \"define and assign\" elements as references for later use:"),
pre({ class: "shj-lang-js" },
code(`class HsCard extends BitElement {
static properties = {
copy: { type: String, required: true },
image: { type: String },
link: { type: URL },
title: { type: String, required: true },
}
constructor() {
super();
this.shadowRoot.append(
this.imageEl = img({ alt: "" }),
this.titleEl = h2(),
this.copyEl = p("Default copy ",
this.linkEl = a("Card link")
),
)
}
update() {
this.copyEl.textContent = this.copy
this.imageEl.src = this.image
this.linkEl.href = this.link.href
this.titleEl.textContent = this.title
}
}`)
),
p("For more examples of how to use ", strong("domxy"), " in your projects, be sure to ", strong("View Source"), " in your browser."),
h2("Why?"),
p("Whether you ", u("need to"), " or ", u("enjoy"), " working with DOM elements directly in JavaScript, you know how verbose creating and forming them into even a modest tree can get."),
p(strong("domxy"), " makes working with DOM nodes easy, expressive and powerful by using regular JavaScript functions as HTML, SVG, and DocumentFragment node names."),
p("Create DOM nodes as easy as writing HTML."),
h2("Installation"),
p("Import ", strong("domxy"), " as an ES module:"),
pre({ class: "shj-lang-js" },
code(`import { domxy } from "https://domxy.js.org/domxy.js"
const { h1, main } = domxy
document.body.append(
main(
h1("Hello, domxy.")
)
)`)
),
h2("Downloads"),
ul(
li(
a({ href: "./domxy.js" }, "domxy.js (3.7 KB)"),
),
li(
a({ href: "./domxy.min.js" }, "domxy.min.js (1.6 KB)"),
)
),
h2("Source Code"),
a({ href: "https://github.com/joe-dakroub/domxy" }, "Github Repository"),
h2("Tests"),
ol(
li("Run ", a({ href: "/tests.html" }, "tests.html"), " in a web browser"),
li("Open the Developer Tools and select the Console tab"),
li(strong("All tests passed!"), " or the error and line number will display in the console"),
),
h2("License"),
p(strong("domxy"), " was created by ", a({ href: "https://github.com/joe-dakroub" }, "Joe Dakroub"), " and is released under the ", a({ href: "https://unlicense.org" }, "Unlicense"), "."),
h2("Changelog"),
p("Detailed changes for each release are documented in the ", a({ href: "https://github.com/joe-dakroub/domxy/releases" }, "release notes"), "."),
h2("Donations"),
p("If you find ", strong("domxy"), " useful, please consider ", a({ href: "https://www.paypal.com/donate/?business=4QSWBKHDKBNEN&no_recurring=0&item_name=Building+thoughtful+software+for+the+Web.¤cy_code=USD" }, "a small donation"), "; definitely not required. Thank you! 🙏"),
),
script({ type: "module"}, `
import { highlightAll } from "./speed-highlight.js";
highlightAll();
`),
)
);
</script>
</head>
<body></body>
</html>