inertiax-ui 0.0.13 → 0.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Modal.svelte +12 -8
- package/README.md +69 -0
- package/package.json +1 -1
package/Modal.svelte
CHANGED
|
@@ -5,30 +5,34 @@
|
|
|
5
5
|
import { cubicOut } from 'svelte/easing';
|
|
6
6
|
import { push } from './history'
|
|
7
7
|
|
|
8
|
-
export function modal(node) {
|
|
8
|
+
export function modal(node, props) {
|
|
9
9
|
node.addEventListener('click', (e) => {
|
|
10
10
|
e.preventDefault()
|
|
11
11
|
const href = node.getAttribute('href')
|
|
12
|
-
createModal({ src: href })
|
|
12
|
+
createModal({ src: href, ...props })
|
|
13
13
|
})
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export function createModal(props) {
|
|
17
|
+
const onclose = props.onclose || (() => {})
|
|
18
|
+
let modal = null
|
|
19
|
+
function close() {
|
|
20
|
+
unmount(modal, { outro: true })
|
|
21
|
+
onclose()
|
|
22
|
+
}
|
|
17
23
|
push(function(traverseBack) {
|
|
18
|
-
|
|
19
|
-
const modal = mount(Modal, {
|
|
24
|
+
modal = mount(Modal, {
|
|
20
25
|
target: document.body,
|
|
21
26
|
props: {
|
|
22
27
|
...props,
|
|
23
28
|
close: function(traverse = true) {
|
|
24
|
-
traverse ? traverseBack() :
|
|
29
|
+
traverse ? traverseBack() : close()
|
|
25
30
|
}
|
|
26
31
|
}
|
|
27
32
|
})
|
|
28
|
-
return
|
|
29
|
-
unmount(modal, { outro: true })
|
|
30
|
-
}
|
|
33
|
+
return close
|
|
31
34
|
})
|
|
35
|
+
return close
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
|
package/README.md
CHANGED
|
@@ -22,6 +22,15 @@ const modal = createModal({
|
|
|
22
22
|
})
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
`createModal` returns a `close` function you can call to close the modal programmatically:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
const modal = createModal({ src: '/profile/edit' })
|
|
29
|
+
|
|
30
|
+
// Later, close the modal
|
|
31
|
+
modal()
|
|
32
|
+
```
|
|
33
|
+
|
|
25
34
|
#### `modal` action
|
|
26
35
|
|
|
27
36
|
Inertia X UI also ships with a `modal` action. This is a small wrapper for `createModal` and passes the `href` attribute as the `src` prop.
|
|
@@ -34,6 +43,17 @@ Inertia X UI also ships with a `modal` action. This is a small wrapper for `crea
|
|
|
34
43
|
<a href="/profile/edit" use:modal>Edit profile</a>
|
|
35
44
|
```
|
|
36
45
|
|
|
46
|
+
You can also pass options like `onclose`:
|
|
47
|
+
|
|
48
|
+
```svelte
|
|
49
|
+
<script>
|
|
50
|
+
import { modal } from 'inertiax-ui'
|
|
51
|
+
import { router } from 'inertiax-svelte'
|
|
52
|
+
</script>
|
|
53
|
+
|
|
54
|
+
<a href="/profile/edit" use:modal={{ onclose: () => router.reload() }}>Edit profile</a>
|
|
55
|
+
```
|
|
56
|
+
|
|
37
57
|
### Closing a modal
|
|
38
58
|
|
|
39
59
|
The Modal component passes a `close` function down to its page component as a prop. You can call this function to close it. Behind the scenes, calling `close` will use the browsers Navigation API to traverse the history back to before the modal was opened, which in turn triggers callbacks that unmount the modal. Alternatively, you can call `close(false)` to close the modal without going back in history. This will prevent forward-navigation from re-opening the modal.
|
|
@@ -48,6 +68,55 @@ The Modal component passes a `close` function down to its page component as a pr
|
|
|
48
68
|
|
|
49
69
|
Note that there is no `close` function on the modal instance itself as components aren't usually able to unmount themselves.
|
|
50
70
|
|
|
71
|
+
|
|
72
|
+
### `onclose` callback
|
|
73
|
+
|
|
74
|
+
Pass an `onclose` callback to run custom logic when the modal closes. This fires regardless of how the modal was closed — via the close button, backdrop click, or browser back button.
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
import { createModal } from 'inertiax-ui'
|
|
78
|
+
import { router } from 'inertiax-svelte'
|
|
79
|
+
|
|
80
|
+
createModal({
|
|
81
|
+
src: '/profile/edit',
|
|
82
|
+
onclose: () => router.reload()
|
|
83
|
+
})
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Common use cases: reloading the parent page after an edit, resetting form state, or cleaning up side effects.
|
|
87
|
+
|
|
88
|
+
### Communicating with the parent
|
|
89
|
+
|
|
90
|
+
All props passed to `createModal` (except `src`) are forwarded to the page component rendered inside the modal. This lets you pass callbacks that the modal page can call to communicate back to the parent.
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
// In your parent component
|
|
94
|
+
import { createModal } from 'inertiax-ui'
|
|
95
|
+
|
|
96
|
+
createModal({
|
|
97
|
+
src: '/profile/edit',
|
|
98
|
+
onSave: (data) => {
|
|
99
|
+
console.log('Saved:', data)
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
```svelte
|
|
105
|
+
<!-- Inside the modal page (e.g. /profile/edit) -->
|
|
106
|
+
<script>
|
|
107
|
+
const { onSave, close } = $props()
|
|
108
|
+
|
|
109
|
+
let name = $state('')
|
|
110
|
+
</script>
|
|
111
|
+
|
|
112
|
+
<button onclick={() => { onSave({ name }); close() }}>Save</button>
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
// Also works with the modal action
|
|
117
|
+
<a href="/profile/edit" use:modal={{ onSave: (data) => handleSave(data) }}>Edit</a>
|
|
118
|
+
```
|
|
119
|
+
|
|
51
120
|
## Installation
|
|
52
121
|
|
|
53
122
|
To start using Inertia X UI, install the `inertiax-ui` package and import the CSS style you'd like to use.
|