@itrocks/modal 0.1.2 → 0.2.0
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/README.md +40 -0
- package/modal.d.ts +1 -0
- package/modal.js +17 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -178,3 +178,43 @@ if (form) {
|
|
|
178
178
|
dedicated "Close" link using `href="about:blank"`.
|
|
179
179
|
- Projects that want a lightweight, dependency-free modal behavior and
|
|
180
180
|
default styling that can be easily overridden with custom CSS.
|
|
181
|
+
|
|
182
|
+
## Asynchronous forms
|
|
183
|
+
|
|
184
|
+
Add `data-modal-close="manual"` to keep a form open during submission. After the server confirms success,
|
|
185
|
+
call `closeModal(element: Element): void`; it removes the closest `#modal` ancestor.
|
|
186
|
+
A failed request can leave the form open and display an error.
|
|
187
|
+
Repeated `modalForm(form)` calls do not register duplicate handlers, and cancellation links prevent native navigation.
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
import { closeModal } from '@itrocks/modal/modal.js'
|
|
191
|
+
import { modalForm } from '@itrocks/modal/modal.js'
|
|
192
|
+
|
|
193
|
+
form.dataset.modalClose = 'manual'
|
|
194
|
+
modalForm(form)
|
|
195
|
+
form.addEventListener('submit', async event => {
|
|
196
|
+
event.preventDefault()
|
|
197
|
+
const response = await fetch(form.action, { method: 'POST', body: new FormData(form) })
|
|
198
|
+
if (response.ok) closeModal(form)
|
|
199
|
+
})
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Cancellation links with an explicit `target` delegate to `xtarget`; non-targeted cancellation links close the modal
|
|
203
|
+
through `modalForm`. For a targeted close without network access, use:
|
|
204
|
+
|
|
205
|
+
```html
|
|
206
|
+
<a href="about:blank" target="#modal:replace">Cancel</a>
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Usage with the it.rocks framework
|
|
210
|
+
|
|
211
|
+
The [it.rocks framework](https://github.com/itrocks-ts/framework) activates `xtarget` and its modifier plugin.
|
|
212
|
+
Targeted cancellation links therefore work without additional `xtarget` setup.
|
|
213
|
+
Custom modal forms still use `modalForm` for the form behaviour described above.
|
|
214
|
+
|
|
215
|
+
## Usage outside the it.rocks framework
|
|
216
|
+
|
|
217
|
+
For targeted cancellation, activate [`xtarget`](https://github.com/itrocks-ts/xtarget) and its modifier plugin
|
|
218
|
+
in your browser entry. Non-targeted cancellation only needs `modalForm`.
|
|
219
|
+
Call `modalForm` for newly inserted forms, or load `@itrocks/modal/build.js` with
|
|
220
|
+
[`build`](https://github.com/itrocks-ts/build) to activate matching dynamic forms automatically.
|
package/modal.d.ts
CHANGED
package/modal.js
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
|
+
const activated = new WeakSet();
|
|
2
|
+
export function closeModal(element) {
|
|
3
|
+
element.closest('#modal')?.remove();
|
|
4
|
+
}
|
|
1
5
|
export function modalForm(form) {
|
|
2
6
|
const modal = form.closest('#modal');
|
|
3
|
-
if (!modal)
|
|
7
|
+
if (!modal || activated.has(form))
|
|
4
8
|
return;
|
|
5
|
-
|
|
9
|
+
activated.add(form);
|
|
10
|
+
form.addEventListener('submit', () => {
|
|
11
|
+
if (form.dataset.modalClose !== 'manual')
|
|
12
|
+
setTimeout(() => modal.remove());
|
|
13
|
+
});
|
|
6
14
|
form.querySelectorAll('a[href="about:blank"]').forEach(anchor => {
|
|
7
|
-
|
|
15
|
+
// Targeted cancellation is handled by xtarget, including its target modifiers.
|
|
16
|
+
if (anchor.hasAttribute('target'))
|
|
17
|
+
return;
|
|
18
|
+
anchor.addEventListener('click', event => {
|
|
19
|
+
event.preventDefault();
|
|
20
|
+
closeModal(anchor);
|
|
21
|
+
});
|
|
8
22
|
});
|
|
9
23
|
}
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"description": "Lightweight modal with default styling and auto-close on form submit or link click",
|
|
7
7
|
"devDependencies": {
|
|
8
8
|
"sass": "^1.95",
|
|
9
|
-
"typescript": "^
|
|
9
|
+
"typescript": "^7.0"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"LICENSE",
|
|
@@ -50,5 +50,5 @@
|
|
|
50
50
|
"scripts": {
|
|
51
51
|
"build": "tsc && sass --no-source-map modal.scss:modal.css"
|
|
52
52
|
},
|
|
53
|
-
"version": "0.
|
|
53
|
+
"version": "0.2.0"
|
|
54
54
|
}
|