|
20 | 20 | </ul>
|
21 | 21 | </nav>
|
22 | 22 |
|
23 |
| -<p id="demo"></p> |
| 23 | +<p id="demo"><button type="button" id="open-modal">Click here to open dialog</button></p> |
| 24 | + |
| 25 | +<p id="random-text">Some random text to use as "something outside the dialog element". Clicking here should close the dialog (if open).</p> |
24 | 26 |
|
25 | 27 | <div class="">
|
26 | 28 | <p>Form created in modal after click (<b>should not prompt automatically</b>).</p>
|
27 | 29 |
|
28 |
| - <div class="dialog"> |
29 |
| - <p><button type="button">Click here to login</button></p> |
30 |
| - </div> |
31 |
| - <script> |
32 |
| - const initialButton = document.querySelector('button'); |
33 |
| - initialButton.addEventListener('click', () => { |
34 |
| - const formEl = `<form id="click-form" novalidate action="3-multistep-form.html"> |
35 |
| - <h3>Sign in form dia</h3> |
| 30 | + <div class="dialog" hidden></div> |
| 31 | +</div> |
| 32 | + |
| 33 | +<script type="module"> |
| 34 | + const initialButton = document.querySelector('button'); |
| 35 | + const dialogEl = document.querySelector('.dialog'); |
| 36 | + initialButton.addEventListener('click', () => { |
| 37 | + const formEl = `<form id="login" novalidate action="3-multistep-form.html"> |
| 38 | + <h3>Sign in form</h3> |
36 | 39 | <fieldset>
|
37 | 40 | <label for="email">Email</label>
|
38 | 41 | <input id="email" type="email">
|
39 | 42 | <label for="password">Password</label>
|
40 | 43 | <input id="password" type="password">
|
41 |
| - <button type="submit">Sign in</button> |
| 44 | + <button type="submit">Log in</button> |
42 | 45 | </fieldset>
|
43 |
| - </form>` |
44 |
| - initialButton.outerHTML = formEl; |
45 |
| - const clickForm = document.getElementById('click-form'); |
46 |
| - const button = clickForm?.querySelector('button'); |
| 46 | + </form>`; |
| 47 | + dialogEl.innerHTML = formEl; |
| 48 | + dialogEl.removeAttribute('hidden'); |
| 49 | + window.addEventListener('keydown', (e) => { |
| 50 | + if (e.key === 'Escape') { dialogEl.setAttribute('hidden', ''); } |
| 51 | + }); |
| 52 | + window.addEventListener('pointerdown', (e) => { |
| 53 | + if (!dialogEl.contains(e.target)) { dialogEl.setAttribute('hidden', ''); } |
| 54 | + }); |
| 55 | + |
| 56 | + const loginForm = document.getElementById('login'); |
| 57 | + const button = loginForm?.querySelector('button'); |
47 | 58 | const handler = (e) => {
|
48 |
| - e.preventDefault(); |
49 |
| - e.stopImmediatePropagation(); |
50 |
| - window.location.href = '3-multistep-form.html'; |
| 59 | + e.preventDefault(); |
| 60 | + e.stopImmediatePropagation(); |
| 61 | + // window.location.href = '3-multistep-form.html'; |
| 62 | + if (loginForm.checkValidity()) { |
| 63 | + setTimeout(() => { |
| 64 | + dialogEl.innerHTML = '<h1>Submitted!</h1>'; |
| 65 | + }, 100); |
| 66 | + } |
51 | 67 | };
|
52 | 68 | button?.addEventListener('click', handler, true);
|
53 |
| - clickForm?.addEventListener('submit', handler, true); |
54 |
| - }); |
55 |
| - </script> |
56 |
| -</div> |
57 |
| - |
| 69 | + loginForm?.addEventListener('submit', handler, true); |
| 70 | + }); |
| 71 | +</script> |
58 | 72 | </body>
|
59 | 73 |
|
60 | 74 | </html>
|
0 commit comments