|
3 | 3 | </script> |
4 | 4 |
|
5 | 5 | <script lang="ts"> |
6 | | - import { createEventDispatcher } from "svelte"; |
| 6 | + import type { Snippet } from "svelte"; |
7 | 7 | import { IconClear } from "@stackoverflow/stacks-icons/icons"; |
8 | 8 |
|
9 | 9 | import { Button, Icon } from "../../components"; |
10 | 10 | import { clickOutside, focusTrap } from "../../actions"; |
11 | 11 |
|
12 | | - /** |
13 | | - * The html id attribute for the modal (required) |
14 | | - * @type {string} |
15 | | - */ |
16 | | - export let id: string; |
17 | | -
|
18 | | - /** |
19 | | - * Boolean controlling the visibility of the modal |
20 | | - */ |
21 | | - export let visible = false; |
22 | | -
|
23 | | - /** |
24 | | - * The state of the modal |
25 | | - * @type {"" | "danger" | "celebration"} |
26 | | - */ |
27 | | - export let state: State = ""; |
28 | | -
|
29 | | - /** |
30 | | - * Additional CSS classes added to the element |
31 | | - */ |
32 | | - let className = ""; |
33 | | - export { className as class }; |
34 | | -
|
35 | | - /** |
36 | | - * Localized translation for the close button aria label |
37 | | - */ |
38 | | - export let i18nCloseButtonLabel = "Close"; |
39 | | -
|
40 | | - /** |
41 | | - * Boolean controlling whether or not the modal should close when the user clicks outside. |
42 | | - * This is an escape hatch, please consider whether use of this prop is absolutely necessary! |
43 | | - */ |
44 | | - export let preventCloseOnClickOutside = false; |
45 | | -
|
46 | | - /** |
47 | | - * Boolean controlling display of the modal close button. |
48 | | - * This is an escape hatch, please consider whether use of this prop is absolutely necessary! |
49 | | - */ |
50 | | - export let hideCloseButton = false; |
51 | | -
|
52 | | - $: modalClasses = getClasses(className); |
| 12 | + interface Props { |
| 13 | + /** |
| 14 | + * The html id attribute for the modal (required) |
| 15 | + */ |
| 16 | + id: string; |
| 17 | + /** |
| 18 | + * Boolean controlling the visibility of the modal |
| 19 | + */ |
| 20 | + visible?: boolean; |
| 21 | + /** |
| 22 | + * The state of the modal |
| 23 | + */ |
| 24 | + state?: State; |
| 25 | + /** |
| 26 | + * Additional CSS classes added to the element |
| 27 | + */ |
| 28 | + class?: string; |
| 29 | + /** |
| 30 | + * Localized translation for the close button aria label |
| 31 | + */ |
| 32 | + i18nCloseButtonLabel?: string; |
| 33 | + /** |
| 34 | + * Boolean controlling whether or not the modal should close when the user clicks outside. |
| 35 | + * This is an escape hatch, please consider whether use of this prop is absolutely necessary! |
| 36 | + */ |
| 37 | + preventCloseOnClickOutside?: boolean; |
| 38 | + /** |
| 39 | + * Boolean controlling display of the modal close button. |
| 40 | + * This is an escape hatch, please consider whether use of this prop is absolutely necessary! |
| 41 | + */ |
| 42 | + hideCloseButton?: boolean; |
| 43 | + /** |
| 44 | + * Callback fired when the modal is closed |
| 45 | + */ |
| 46 | + onclose?: () => void; |
| 47 | + /** |
| 48 | + * Content rendered in the modal header section |
| 49 | + */ |
| 50 | + header?: Snippet; |
| 51 | + /** |
| 52 | + * Content rendered in the modal body section |
| 53 | + */ |
| 54 | + body?: Snippet; |
| 55 | + /** |
| 56 | + * Content rendered in the modal footer section |
| 57 | + */ |
| 58 | + footer?: Snippet; |
| 59 | + } |
| 60 | +
|
| 61 | + let { |
| 62 | + id, |
| 63 | + visible = false, |
| 64 | + state = "", |
| 65 | + class: className = "", |
| 66 | + i18nCloseButtonLabel = "Close", |
| 67 | + preventCloseOnClickOutside = false, |
| 68 | + hideCloseButton = false, |
| 69 | + onclose, |
| 70 | + header, |
| 71 | + body, |
| 72 | + footer, |
| 73 | + }: Props = $props(); |
53 | 74 |
|
54 | 75 | const getClasses = (className: string) => { |
55 | 76 | let classes = "s-modal--dialog"; |
|
60 | 81 |
|
61 | 82 | return classes; |
62 | 83 | }; |
63 | | -
|
64 | | - const dispatch = createEventDispatcher<{ close: void }>(); |
| 84 | + const modalClasses = $derived(getClasses(className)); |
65 | 85 |
|
66 | 86 | const close = () => { |
67 | 87 | if (visible) { |
68 | 88 | visible = false; |
69 | | - dispatch("close"); |
| 89 | + onclose?.(); |
70 | 90 | } |
71 | 91 | }; |
72 | 92 |
|
|
77 | 97 | }; |
78 | 98 | </script> |
79 | 99 |
|
80 | | -<svelte:window on:keydown={keyPress} /> |
| 100 | +<svelte:window onkeydown={keyPress} /> |
81 | 101 |
|
82 | 102 | <!-- svelte-ignore a11y_no_noninteractive_element_to_interactive_role --> |
83 | 103 | <aside |
|
97 | 117 | onoutclick={() => !preventCloseOnClickOutside && close()} |
98 | 118 | > |
99 | 119 | <h1 id={`${id}-title`} class="s-modal--header"> |
100 | | - <!-- Content slotted in the Modal header section --> |
101 | | - <slot name="header" /> |
| 120 | + {@render header?.()} |
102 | 121 | </h1> |
103 | 122 | <div id={`${id}-description`} class="s-modal--body"> |
104 | | - <!-- Content slotted in the Modal body section --> |
105 | | - <slot name="body" /> |
| 123 | + {@render body?.()} |
106 | 124 | </div> |
107 | | - {#if $$slots.footer} |
| 125 | + {#if footer} |
108 | 126 | <div class="d-flex g8 s-modal--footer"> |
109 | | - <!-- Content slotted in the Modal footer section --> |
110 | | - <slot name="footer" /> |
| 127 | + {@render footer?.()} |
111 | 128 | </div> |
112 | 129 | {/if} |
113 | 130 | {#if !hideCloseButton} |
|
0 commit comments