-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.html
More file actions
530 lines (414 loc) · 14.6 KB
/
Copy pathindex.html
File metadata and controls
530 lines (414 loc) · 14.6 KB
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<title>CTA Modal - Web Component</title>
<meta
name="viewport"
content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1"
/>
<link rel="stylesheet" href="./src/assets/css/sanitize.css" />
<link rel="stylesheet" href="./src/assets/css/index.css" />
<script type="module" src="./src/assets/ts/cta-modal.ts"></script>
<script>
/*
=====
NOTE:
=====
This is not needed for the typical Web Component
use case. It exists simply to show how "external"
triggers can still control toggling the modal.
*/
function toggleModal(id) {
// Get modal.
const modal = document.getElementById(id);
// Modal exists?
if (modal) {
// Get flag.
const bool = modal.getAttribute('active') === String(true);
// Set flag.
modal.setAttribute('active', !bool);
}
}
</script>
</head>
<body>
<h1>CTA Modal 🦒</h1>
<p>
CTA Modal is a self-contained call to action
<a href="https://www.w3.org/TR/wai-aria-practices/examples/dialog-modal/dialog.html"
>ARIA modal</a
>, built as a
<a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components">Web Component</a>. It
has zero runtime dependencies beyond a single JavaScript file, and only requires authoring
HTML to use.
</p>
<ul>
<li>
<p>
All the TypeScript code is well tested, with 💯
<a href="https://host.sonspring.com/cta-modal/coverage/cta-modal.ts.html">coverage</a>.
</p>
</li>
<li>
<p
>The compiled
<a href="https://host.sonspring.com/cta-modal/dist/cta-modal.js">cta-modal.js</a> file is
less than <b>8</b> kilobytes.</p
>
</li>
<li>
<p>
You can view the source code on
<a href="https://github.com/nathansmith/cta-modal">GitHub</a>.
</p>
</li>
<li>
<p>
This
<a href="https://www.smashingmagazine.com/2022/04/cta-modal-build-web-component/"
>article</a
>
explains how it was built.
</p>
</li>
</ul>
<hr />
<h2>Example: modal form</h2>
<p>The following toggle triggers are located within a <code><cta-modal></code> tag.</p>
<cta-modal id="ID_FOR_CTA_MODAL">
<div slot="button">
<p>
<a class="cta-modal-toggle" href="#">Toggle modal (href="#")</a>
</p>
<p>
<a class="cta-modal-toggle" role="button" tabindex="0">Toggle modal (role="button")</a>
</p>
<p>
<button class="cta-modal-toggle" type="button">Toggle modal (button)</button>
</p>
<p>
<input class="cta-modal-toggle" type="button" value="Toggle modal (input)" />
</p>
<p>
<button class="cta-modal-toggle" type="button" disabled>
Toggle modal (disabled button)
</button>
</p>
<p>
<input
class="cta-modal-toggle"
type="button"
disabled
value="Toggle modal (disabled input)"
/>
</p>
</div>
<div slot="modal">
<h2>Hello world</h2>
<p>
<label for="ID_FOR_INPUT_NAME">Name</label>
<input type="text" id="ID_FOR_INPUT_NAME" />
</p>
<p>
<label for="ID_FOR_INPUT_EMAIL">Email</label>
<input type="email" id="ID_FOR_INPUT_EMAIL" />
</p>
<p>
<button type="button">Submit</button>
<a class="cta-modal-toggle" role="button" tabindex="0">Cancel</a>
</p>
</div>
</cta-modal>
<hr />
<h2>Example: modal scroll</h2>
<p>The following toggle triggers are located within a <code><cta-modal></code> tag.</p>
<cta-modal>
<div slot="button">
<p>
<a class="cta-modal-toggle" href="#">Toggle modal (href="#")</a>
</p>
<p>
<a class="cta-modal-toggle" role="button" tabindex="0">Toggle modal (role="button")</a>
</p>
<p>
<button class="cta-modal-toggle" type="button">Toggle modal (button)</button>
</p>
<p>
<input class="cta-modal-toggle" type="button" value="Toggle modal (input)" />
</p>
<p>
<button class="cta-modal-toggle" type="button" disabled>
Toggle modal (disabled button)
</button>
</p>
<p>
<input
class="cta-modal-toggle"
type="button"
disabled
value="Toggle modal (disabled input)"
/>
</p>
</div>
<div slot="modal">
<h2>This is a tall tale</h2>
<p>The modal container will scroll, if necessary.</p>
<p
>Here is a <code><div></code> tag that is
<a href="https://berenstainbears.fandom.com/wiki/Too-Tall_Grizzly">too tall</a>.</p
>
<div class="too-tall-grizzly"></div>
<p>You have reached the end.</p>
</div>
</cta-modal>
<hr />
<h2>Example: external trigger</h2>
<p>
The following toggle trigger is <i>not</i> located within a
<code><cta-modal></code> tag.
</p>
<p>Rather, it changes the <code>active="true"</code> attribute of its modal directly.</p>
<p>
That is handled by the <code>toggleModal</code> function in the
<a href="https://github.com/nathansmith/cta-modal/blob/main/index.html">index.html</a> file.
</p>
<p>
<button onclick="toggleModal('ID_FOR_CTA_MODAL')" type="button">External trigger</button>
</p>
<hr />
<h2>How to use: install</h2>
<p>
If you are using <a href="https://nodejs.org/en/">Node</a> with
<a href="https://www.npmjs.com/">NPM</a> (or <a href="https://yarnpkg.com/">Yarn</a>) the
install would look something like this.
</p>
<pre><code># NPM.
npm install cta-modal</code></pre>
<pre><code># Yarn.
yarn add cta-modal</code></pre>
<p>Then simply import it into your own <code>*.js</code> or <code>*.ts</code> file.</p>
<pre><code>// Node.
require('cta-modal');</code></pre>
<pre><code>// ES6.
import 'cta-modal';</code></pre>
<hr />
<h2>How to use: flat file</h2>
<p>
If you are building a site where you want to include CTA Modal directly, then download the
<a href="https://host.sonspring.com/cta-modal/dist/cta-modal.js">cta-modal.js</a> file and
reference it within your <code>*.html</code> file.
</p>
<pre><code><head>
<script src="/path/to/cta-modal.js"></script>
</head></code></pre>
<p>
<b>Note:</b> Alternatively, you can put the <code>script</code> at the end of your page,
before the closing <code></body></code> tag.
</p>
<p>
But you may see a flash of unstyled content, before the JS is parsed and attaches to the
<code><cta-modal></code> tag. Placing it in the <code><head></code> ensures that
the JS is parsed <i>ahead</i> of time (no pun intended).
</p>
<hr />
<h2>How to use: basics</h2>
<p>After ensuring the JS is loaded in your code, all you need to do is type HTML like so.</p>
<p>
The rest of the details are abstracted away. Additional accessibility hooks are added
automatically.
</p>
<pre><code><cta-modal>
<div slot="button">
<p>
<button
class="cta-modal-toggle"
type="button"
>Open modal</button>
</p>
</div>
<div slot="modal">
<h2>Modal title</h2>
<p>Modal content</p>
<p>
<button
class="cta-modal-toggle"
type="button"
>Close modal</button>
</p>
</div>
</cta-modal></code></pre>
<p>
<b>Note:</b> The inner tags with <code>slot</code> attributes yield specific functionality.
Beyond that, any markup you want can go inside each of them.
</p>
<ul>
<li>
<p><code><div slot="button"></code> is <b>optional</b>.</p>
<p>It contains anything you want shown in the <i>page</i> (not in the <i>modal</i>).</p>
</li>
<li>
<p><code><div slot="modal"></code> is <b>required</b>.</p>
<p>It contains anything you want shown in the <i>modal</i> (not in the <i>page</i>).</p>
</li>
</ul>
<p>
If you do not include a <code>slot="button"</code> element, then you will need to manually
control setting the <code><cta-modal active="true"></code> attribute yourself.
</p>
<hr />
<h2>How to use: extras</h2>
<p>
There are a few optional attributes that can be set on the
<code><cta-modal></code> tag.
</p>
<pre><code><cta-modal
active="true"
animated="false"
close="Get outta here!"
static="true"
>
<!-- etc. -->
</cta-modal></code></pre>
<ul>
<li>
<p><code>active="true"</code> — Implicitly <code>false</code> by default.</p>
<p>You can use this to force a modal to display, without a user clicking anything.</p>
<p>
I suggest using this sparingly, because we all know that unexpected modals can be
annoying.
</p>
</li>
<li>
<p><code>animated="false"</code> — Implicitly <code>true</code> by default.</p>
<p>This will cause the modal to hide/show immediately, without a fade animation.</p>
<p>
If a user has their operating system's preference set for
<code>prefers-reduced-motion</code> the animation will be disabled regardless of the flag.
</p>
</li>
<li>
<p><code>close="TEXT HERE"</code> — Implicitly <code>"Close"</code> by default.</p>
<p>
You can use this attribute to localize the text string shown in the close button's
<code>title="Close"</code> tooltip.
</p>
<p>
Whatever you put here will be shown on mouse hover, and will be read aloud to
<a href="https://en.wikipedia.org/wiki/Screen_reader">screen readers</a>.
</p>
</li>
<li>
<p><code>static="true"</code> — Implicitly <code>false</code> by default.</p>
<p>This will prevent clicks on the page overlay from triggering a modal close.</p>
<p>
Additionally, the <code>escape</code> key will be ignored. Whereas, normally that key
would also dismiss the modal.
</p>
</li>
</ul>
<hr />
<h2>How to use: styling</h2>
<p>
If you want to override the default styles of the modal, that needs to be done via CSS
variables. The reason for this is Web Components use the
<a href="https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM"
>shadow DOM</a
>
which does not inherit styles from the parent page.
</p>
<p>
The following variables have been set to their default values. Feel free to tweak them to your
liking.
</p>
<pre><code>cta-modal {
/* Modal overlay. */
--cta-modal-overlay-background-color: rgba(0, 0, 0, 0.5);
--cta-modal-overlay-padding-top: 20px;
--cta-modal-overlay-padding-left: 20px;
--cta-modal-overlay-padding-right: 20px;
--cta-modal-overlay-padding-bottom: 20px;
--cta-modal-overlay-z-index: 100000;
/* Modal window. */
--cta-modal-dialog-background-color: #fff;
--cta-modal-dialog-border-radius: 5px;
--cta-modal-dialog-box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.5);
--cta-modal-dialog-padding-top: 20px;
--cta-modal-dialog-padding-left: 20px;
--cta-modal-dialog-padding-right: 20px;
--cta-modal-dialog-padding-bottom: 20px;
--cta-modal-dialog-width: 500px;
/* Modal close button. */
--cta-modal-close-color: #fff;
--cta-modal-close-background-color: #000;
--cta-modal-close-border-radius: 50%;
--cta-modal-close-box-shadow: 0 0 0 1px #fff;
--cta-modal-close-display: block;
--cta-modal-close-font-family: 'Arial', sans-serif;
--cta-modal-close-font-size: 23px;
--cta-modal-close-line-height: 26px;
--cta-modal-close-width: 26px;
/* Modal close button - hover. */
--cta-modal-close-color-hover: #000;
--cta-modal-close-background-color-hover: #fff;
--cta-modal-close-box-shadow-hover: 0 0 0 1px #000;
}</code></pre>
<h3>Styling content</h3>
<p
>If you want to target content that is displayed <i>within</i> a modal, you can use a
descendant selector scoped to the context of the <code>cta-modal</code> tag.</p
>
<p
>For instance: Let's say you want to make all content within the
<code>slot="button"</code> area blue, and wanted to make all the content within the
<code>slot="modal"</code> area red. That would look something like this.</p
>
<pre><code>cta-modal [slot="button"] {
color: blue;
}
cta-modal [slot="modal"] {
color: red;
}</code></pre>
<h3>No close button</h3>
<p>
Here is another scenario. Perhaps you want to have a static modal, with only a single toggle
trigger to close it. If you intend to provide that yourself, you can hide the default close
button with CSS such as this.
</p>
<pre><code>cta-modal[static="true"] {
--cta-modal-close-display: none;
}</code></pre>
<h3>Dark mode</h3>
<p>
If you want to provide a different set of style overrides for users who prefer
<a href="https://css-tricks.com/dark-modes-with-css/">dark mode</a>, you can scope your
selector within a media query. This would make the modal background dark gray.
</p>
<pre><code>@media (prefers-color-scheme: dark) {
cta-modal {
--cta-modal-dialog-background-color: #333;
/* Dark mode overrides here. */
}
}</code></pre>
<p>
Or, you could scope the selector to a specific extra attribute of your choosing. Let's say
some modals in your page need to be dark, whereas others do not. You might consider this
approach.
</p>
<pre><code>cta-modal {
--cta-modal-dialog-background-color: #fff;
/* Default overrides here. */
}
cta-modal[theme="dark"] {
--cta-modal-dialog-background-color: #333;
/* Dark mode overrides here. */
}</code></pre>
<hr />
<p>
Created by <a href="http://twitter.com/nathansmith">Nathan Smith</a>. Licensed under
<a href="https://en.wikipedia.org/wiki/MIT_License">MIT</a> and
<a href="https://en.wikipedia.org/wiki/GNU_General_Public_License">GPL</a>.
</p>
</body>
</html>