2
2
3
3
A simple, flexible, zero-dependency modal manager for Svelte.
4
4
5
- [ View documentation] ( https://svelte-modals.mattjennings.io )
5
+ [ View documentation] ( https://svelte-modals.mattjennin.gs )
6
6
7
7
## Getting Started
8
8
9
- ```
9
+ ### Installation
10
+
11
+ ``` bash
10
12
npm install svelte-modals
11
13
```
12
14
13
- Add ` Modals ` somewhere in your app. This is where the modals will render.
15
+ ### Add \< Modals /> to your app
16
+
17
+ All opened modals will be rendered inside ` <Modals /> ` . If you're using SvelteKit, ` +layout.svelte ` would be appropriate
18
+ place to put it. Otherwise, wherever you want the modals to be rendered.
14
19
15
20
``` svelte
16
21
<script>
17
- import { Modals, closeModal } from 'svelte-modals'
22
+ import { Modals } from 'svelte-modals'
18
23
</script>
19
24
20
25
<Modals>
21
- <div slot="backdrop" class="backdrop" on:click={closeModal} />
26
+ <!-- shown when any modal is opened -->
27
+ {#snippet backdrop({ close })}
28
+ <div class="backdrop" onclick={() => close()} />
29
+ {/snippet}
22
30
</Modals>
23
31
24
32
<style>
@@ -33,29 +41,33 @@ Add `Modals` somewhere in your app. This is where the modals will render.
33
41
</style>
34
42
```
35
43
36
- Create your Modal component
44
+ ### Create your Modal component
37
45
38
- ``` html
39
- <script >
40
- import { closeModal } from ' svelte-modals'
41
-
42
- // provided by Modals
43
- export let isOpen
46
+ Let's create a basic modal component:
44
47
45
- export let title
46
- export let message
48
+ ``` svelte
49
+ <script>
50
+ const {
51
+ // provided by <Modals />
52
+ isOpen,
53
+ close,
54
+
55
+ // your props
56
+ title,
57
+ message
58
+ } = $props()
47
59
</script>
48
60
49
61
{#if isOpen}
50
- <div role =" dialog" class =" modal" >
51
- <div class =" contents" >
52
- <h2 >{title}</h2 >
53
- <p >{message}</p >
54
- <div class =" actions" >
55
- <button on:click =" {closeModal}" >OK</button >
62
+ <div role="dialog" class="modal">
63
+ <div class="contents">
64
+ <h2>{title}</h2>
65
+ <p>{message}</p>
66
+ <div class="actions">
67
+ <button onclick={() => close()}>OK</button>
68
+ </div>
56
69
</div>
57
70
</div>
58
- </div >
59
71
{/if}
60
72
61
73
<style>
@@ -75,7 +87,6 @@ Create your Modal component
75
87
76
88
.contents {
77
89
min-width: 240px;
78
- border-radius : 6px ;
79
90
padding: 16px;
80
91
background: white;
81
92
display: flex;
@@ -92,6 +103,7 @@ Create your Modal component
92
103
p {
93
104
text-align: center;
94
105
margin-top: 16px;
106
+ border-radius: 6px;
95
107
}
96
108
97
109
.actions {
@@ -102,17 +114,19 @@ Create your Modal component
102
114
</style>
103
115
```
104
116
105
- Open it
117
+ ### Try it out
106
118
107
- ``` html
119
+ Import ` modals ` anywhere in your app to open or close your modals
120
+
121
+ ``` svelte
108
122
<script>
109
- import { openModal } from ' svelte-modals/legacy '
110
- import Modal from ' ./Modal .svelte'
123
+ import { modals } from 'svelte-modals'
124
+ import MyModal from '$lib/components/MyModal .svelte'
111
125
112
126
function handleClick() {
113
- openModal (Modal , { title: ' Alert' , message: ' This is an alert' })
127
+ modals.open(MyModal , { title: 'Alert', message: 'This is an alert' })
114
128
}
115
129
</script>
116
130
117
- <button on:click = " {handleClick}" >Open Modal</button >
131
+ <button onclick= {handleClick}>Open Modal</button>
118
132
```
0 commit comments