Debian Codegraph @

frontend/src/modals/ModalBase.svelte

raw
1 <!--
2 @component
3 A modal dialog.
4
5 This tries to follow https://www.w3.org/TR/wai-aria-practices-1.1/#dialog_modal.
6 -->
7 <script lang="ts">
8 import type { Snippet } from "svelte";
9 import type { Action } from "svelte/action";
10
11 import { attemptFocus, getFocusableElements } from "../lib/focus";
12 import { closeOverlay } from "../stores/url";
13
14 interface Props {
15 shown: boolean;
16 focus?: string;
17 closeHandler?: () => void;
18 children: Snippet;
19 }
20
21 let { shown, focus, closeHandler = closeOverlay, children }: Props = $props();
22
23 /**
24 * A Svelte action to handle focus within a modal.
25 */
26 const handleFocus: Action = (el) => {
27 const keydown = (ev: KeyboardEvent) => {
28 if (ev.key === "Tab") {
29 const focusable = getFocusableElements(el);
30 const first = focusable[0];
31 const last = focusable[focusable.length - 1];
32 if (ev.shiftKey && document.activeElement === first && last) {
33 ev.preventDefault();
34 attemptFocus(last);
35 } else if (!ev.shiftKey && document.activeElement === last && first) {
36 ev.preventDefault();
37 attemptFocus(first);
38 }
39 } else if (ev.key === "Escape") {
40 ev.preventDefault();
41 closeHandler();
42 }
43 };
44 document.addEventListener("keydown", keydown);
45
46 const selectorFocusEl = focus != null ? el.querySelector(focus) : undefined;
47 const focusEl = selectorFocusEl ?? getFocusableElements(el)[0];
48 if (focusEl) {
49 attemptFocus(focusEl);
50 }
51
52 return {
53 destroy: () => {
54 document.removeEventListener("keydown", keydown);
55 },
56 };
57 };
58 </script>
59
60 {#if shown}
61 <div class="overlay">
62 <div class="background" onclick={closeHandler} aria-hidden="true"></div>
63 <div class="content" use:handleFocus role="dialog" aria-modal="true">
64 {@render children()}
65 <button type="button" class="muted close" onclick={closeHandler}>
66 x
67 </button>
68 </div>
69 </div>
70 {/if}
71
72 <style>
73 :global(body:has(.overlay)) {
74 overflow: hidden;
75 }
76
77 .background {
78 position: fixed;
79 inset: 0;
80 width: 100%;
81 height: 100%;
82 cursor: pointer;
83 background: var(--overlay-wrapper-background);
84 }
85
86 .overlay {
87 position: fixed;
88 inset: 0;
89 z-index: var(--z-index-overlay);
90 display: flex;
91 align-items: start;
92 justify-content: center;
93 width: 100vw;
94 height: 100vh;
95 overflow: auto;
96 }
97
98 .content {
99 position: relative;
100 display: flex;
101 width: 100%;
102 max-width: 767px;
103 padding: 1em;
104 margin: 0.5em;
105 margin-top: 10vh;
106 background: var(--background);
107 box-shadow: var(--box-shadow-overlay);
108 }
109
110 .close {
111 position: absolute;
112 top: 1em;
113 right: 1em;
114 width: 2em;
115 height: 2em;
116 margin: 0;
117 line-height: 1em;
118 color: var(--text-color-lighter);
119 }
120
121 .content :global(form),
122 .content > :global(div) {
123 width: 100%;
124 }
125
126 @media (width <= 767px) {
127 /* Show the modal full-screen on mobile. */
128 .overlay {
129 height: 100%;
130 }
131
132 .background {
133 /* Ensure that modal overflow gets a white background. */
134 background: var(--background);
135 }
136
137 .content {
138 height: 100%;
139 margin: 0;
140 box-shadow: unset;
141 }
142 }
143 </style>