inertiax-ui 0.0.14 → 0.0.16
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 +13 -8
- package/README.md +45 -1
- package/package.json +1 -1
package/Modal.svelte
CHANGED
|
@@ -15,27 +15,32 @@
|
|
|
15
15
|
|
|
16
16
|
export function createModal(props) {
|
|
17
17
|
const onclose = props.onclose || (() => {})
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
let modal = null
|
|
19
|
+
let traverseBack = null
|
|
20
|
+
function close(traverse = true) {
|
|
21
|
+
if (traverse && traverseBack) {
|
|
22
|
+
traverseBack()
|
|
23
|
+
} else {
|
|
21
24
|
unmount(modal, { outro: true })
|
|
22
25
|
onclose()
|
|
23
26
|
}
|
|
27
|
+
}
|
|
28
|
+
push(function(tb) {
|
|
29
|
+
traverseBack = tb
|
|
24
30
|
modal = mount(Modal, {
|
|
25
31
|
target: document.body,
|
|
26
32
|
props: {
|
|
27
33
|
...props,
|
|
28
|
-
close
|
|
29
|
-
traverse ? traverseBack() : close()
|
|
30
|
-
}
|
|
34
|
+
close
|
|
31
35
|
}
|
|
32
36
|
})
|
|
33
|
-
return close
|
|
37
|
+
return () => close(false)
|
|
34
38
|
})
|
|
39
|
+
return close
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
|
|
38
|
-
|
|
43
|
+
function css(node, { delay = 0 }) {
|
|
39
44
|
// duration on desktop is 300, on mobile 400
|
|
40
45
|
const duration = window.innerWidth > 768 ? 300 : 400;
|
|
41
46
|
return {
|
package/README.md
CHANGED
|
@@ -22,6 +22,18 @@ const modal = createModal({
|
|
|
22
22
|
})
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
`createModal` returns a `close` function you can call to close the modal programmatically. Call `close()` or `close(true)` to navigate back in history then unmount. Call `close(false)` to unmount without touching history:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
const closeModal = createModal({ src: '/profile/edit' })
|
|
29
|
+
|
|
30
|
+
// Navigate back, then unmount
|
|
31
|
+
closeModal()
|
|
32
|
+
|
|
33
|
+
// Or just unmount, skip history
|
|
34
|
+
closeModal(false)
|
|
35
|
+
```
|
|
36
|
+
|
|
25
37
|
#### `modal` action
|
|
26
38
|
|
|
27
39
|
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.
|
|
@@ -57,7 +69,7 @@ The Modal component passes a `close` function down to its page component as a pr
|
|
|
57
69
|
<button onclick={close}>Close</button>
|
|
58
70
|
```
|
|
59
71
|
|
|
60
|
-
Note that
|
|
72
|
+
Note that `createModal` also returns a `close` function you can call to close the modal programmatically from the parent.
|
|
61
73
|
|
|
62
74
|
|
|
63
75
|
### `onclose` callback
|
|
@@ -76,6 +88,38 @@ createModal({
|
|
|
76
88
|
|
|
77
89
|
Common use cases: reloading the parent page after an edit, resetting form state, or cleaning up side effects.
|
|
78
90
|
|
|
91
|
+
### Communicating with the parent
|
|
92
|
+
|
|
93
|
+
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.
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
// In your parent component
|
|
97
|
+
import { createModal } from 'inertiax-ui'
|
|
98
|
+
|
|
99
|
+
createModal({
|
|
100
|
+
src: '/profile/edit',
|
|
101
|
+
onSave: (data) => {
|
|
102
|
+
console.log('Saved:', data)
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
```svelte
|
|
108
|
+
<!-- Inside the modal page (e.g. /profile/edit) -->
|
|
109
|
+
<script>
|
|
110
|
+
const { onSave, close } = $props()
|
|
111
|
+
|
|
112
|
+
let name = $state('')
|
|
113
|
+
</script>
|
|
114
|
+
|
|
115
|
+
<button onclick={() => { onSave({ name }); close() }}>Save</button>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
// Also works with the modal action
|
|
120
|
+
<a href="/profile/edit" use:modal={{ onSave: (data) => handleSave(data) }}>Edit</a>
|
|
121
|
+
```
|
|
122
|
+
|
|
79
123
|
## Installation
|
|
80
124
|
|
|
81
125
|
To start using Inertia X UI, install the `inertiax-ui` package and import the CSS style you'd like to use.
|