@ramstack/alpinegear-dialog 1.3.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 +394 -0
- package/alpinegear-dialog.esm.js +194 -0
- package/alpinegear-dialog.esm.min.js +1 -0
- package/alpinegear-dialog.js +199 -0
- package/alpinegear-dialog.min.js +1 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
# @ramstack/alpinegear-dialog
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@ramstack/alpinegear-dialog)
|
|
4
|
+
[](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE)
|
|
5
|
+
|
|
6
|
+
`@ramstack/alpinegear-dialog` is a **headless dialog directive for Alpine.js**, built on top of the native HTML `<dialog>` element.
|
|
7
|
+
|
|
8
|
+
It allows you to describe dialog behavior declaratively, without coupling logic to JavaScript code.
|
|
9
|
+
This makes it especially suitable for **progressive enhancement** and **seamless integration with htmx**.
|
|
10
|
+
|
|
11
|
+
The plugin provides a small set of composable directives that together form a dialog "component",
|
|
12
|
+
while leaving markup, layout, and styling entirely up to you.
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
* Declarative dialog composition using Alpine directives
|
|
17
|
+
* Supports **modal** and **non-modal** dialogs
|
|
18
|
+
* Built on the native `<dialog>` element
|
|
19
|
+
* Value-based close semantics
|
|
20
|
+
* Promise-based API for imperative control
|
|
21
|
+
* Value-scoped events for htmx integration
|
|
22
|
+
* Completely headless (no markup or styling constraints)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
### Using CDN
|
|
28
|
+
|
|
29
|
+
Include the plugin **before** Alpine.js:
|
|
30
|
+
|
|
31
|
+
```html
|
|
32
|
+
<!-- alpine.js plugin -->
|
|
33
|
+
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-dialog@1/alpinegear-dialog.min.js" defer></script>
|
|
34
|
+
|
|
35
|
+
<!-- alpine.js -->
|
|
36
|
+
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" defer></script>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Using NPM
|
|
40
|
+
|
|
41
|
+
Install the package:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npm install --save @ramstack/alpinegear-dialog
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Initialize the plugin:
|
|
48
|
+
|
|
49
|
+
```js
|
|
50
|
+
import Alpine from "alpinejs";
|
|
51
|
+
import Dialog from "@ramstack/alpinegear-dialog";
|
|
52
|
+
|
|
53
|
+
Alpine.plugin(Dialog);
|
|
54
|
+
Alpine.start();
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
### Basic Example
|
|
60
|
+
|
|
61
|
+
```html
|
|
62
|
+
<div x-dialog:modal>
|
|
63
|
+
<button x-dialog:trigger>Update</button>
|
|
64
|
+
|
|
65
|
+
<dialog x-dialog:panel>
|
|
66
|
+
Are you sure you want to continue?
|
|
67
|
+
|
|
68
|
+
<div>
|
|
69
|
+
<button x-dialog:action value="yes">Yes</button>
|
|
70
|
+
<button x-dialog:action value="no">No</button>
|
|
71
|
+
<button x-dialog:action>Cancel</button>
|
|
72
|
+
</div>
|
|
73
|
+
</dialog>
|
|
74
|
+
</div>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Dialogs are composed using the following directives:
|
|
78
|
+
|
|
79
|
+
* `x-dialog` — dialog root and scope provider (`x-dialog:modal` enables modal behavior)
|
|
80
|
+
* `x-dialog:trigger` — element that opens the dialog
|
|
81
|
+
* `x-dialog:panel` — the dialog panel (must be a `<dialog>` element)
|
|
82
|
+
* `x-dialog:action` — closes the dialog and optionally provides a return value
|
|
83
|
+
|
|
84
|
+
### Dialog Modes
|
|
85
|
+
|
|
86
|
+
The root `x-dialog` directive supports two display modes:
|
|
87
|
+
|
|
88
|
+
* **Non-modal dialog** (default)
|
|
89
|
+
* **Modal dialog**, enabled via `x-dialog:modal`
|
|
90
|
+
|
|
91
|
+
### Actions and return values
|
|
92
|
+
|
|
93
|
+
The `x-dialog:action` directive closes the dialog when activated.
|
|
94
|
+
|
|
95
|
+
* The `value` attribute defines the dialog's return value
|
|
96
|
+
* If `value` is omitted, an empty string (`""`) is used as the return value
|
|
97
|
+
|
|
98
|
+
The return value is propagated through both **events** and the **Promise-based API**.
|
|
99
|
+
|
|
100
|
+
### Forms in Dialogs
|
|
101
|
+
|
|
102
|
+
Dialogs can contain forms and fully rely on the browser's native form handling.
|
|
103
|
+
|
|
104
|
+
```html
|
|
105
|
+
<div x-dialog:modal>
|
|
106
|
+
<button x-dialog:trigger>Update details</button>
|
|
107
|
+
|
|
108
|
+
<dialog x-dialog:panel>
|
|
109
|
+
<form method="dialog">
|
|
110
|
+
<label>
|
|
111
|
+
Name:
|
|
112
|
+
<input name="username" required />
|
|
113
|
+
</label>
|
|
114
|
+
|
|
115
|
+
<div>
|
|
116
|
+
<button value="update">Update</button>
|
|
117
|
+
<button formnovalidate>Cancel</button>
|
|
118
|
+
</div>
|
|
119
|
+
</form>
|
|
120
|
+
</dialog>
|
|
121
|
+
</div>
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
#### Notes
|
|
125
|
+
|
|
126
|
+
* `x-dialog:action` is **optional** inside `<form method="dialog">`
|
|
127
|
+
* Native form validation applies automatically
|
|
128
|
+
* The dialog closes only if validation succeeds
|
|
129
|
+
* `formnovalidate` allows closing the dialog without triggering validation
|
|
130
|
+
|
|
131
|
+
In practice, the dialog behaves exactly like a standard HTML `<dialog>` with a form.
|
|
132
|
+
|
|
133
|
+
## Events
|
|
134
|
+
|
|
135
|
+
All events are dispatched from the `x-dialog` root element.
|
|
136
|
+
|
|
137
|
+
### `open`
|
|
138
|
+
|
|
139
|
+
* Fired when the dialog is opened
|
|
140
|
+
* Non-cancelable, does not bubble
|
|
141
|
+
|
|
142
|
+
### `toggle`
|
|
143
|
+
|
|
144
|
+
* Fired whenever the dialog open state changes
|
|
145
|
+
* `event.detail.state` contains the new state (`true` / `false`)
|
|
146
|
+
* Non-cancelable, does not bubble
|
|
147
|
+
|
|
148
|
+
### `beforeclose`
|
|
149
|
+
|
|
150
|
+
* Fired **before** the dialog is closed
|
|
151
|
+
* Cancelable, does not bubble
|
|
152
|
+
* `event.detail.value` contains the proposed return value
|
|
153
|
+
|
|
154
|
+
If this event is canceled, the dialog remains **open**.
|
|
155
|
+
|
|
156
|
+
### `close:[value]`
|
|
157
|
+
|
|
158
|
+
* Fired after the dialog is closed
|
|
159
|
+
* Value-scoped event
|
|
160
|
+
* Event name is normalized to lowercase
|
|
161
|
+
* `event.detail.value` contains the return value
|
|
162
|
+
|
|
163
|
+
Example:
|
|
164
|
+
`value="Yes"` >> `close:yes`
|
|
165
|
+
|
|
166
|
+
### `close`
|
|
167
|
+
|
|
168
|
+
* Fired after the dialog is fully closed
|
|
169
|
+
* `event.detail.value` contains the return value
|
|
170
|
+
|
|
171
|
+
### Event Example
|
|
172
|
+
|
|
173
|
+
```html
|
|
174
|
+
<div x-dialog:modal
|
|
175
|
+
@open="console.log('open')"
|
|
176
|
+
@beforeclose="console.log('beforeclose', $event.detail.value)"
|
|
177
|
+
@close:yes="console.log('User confirmed')"
|
|
178
|
+
@close="console.log('Dialog closed')">
|
|
179
|
+
|
|
180
|
+
<button x-dialog:trigger>Update</button>
|
|
181
|
+
|
|
182
|
+
<dialog x-dialog:panel>
|
|
183
|
+
Are you sure you want to continue?
|
|
184
|
+
|
|
185
|
+
<div>
|
|
186
|
+
<button x-dialog:action value="yes">Yes</button>
|
|
187
|
+
<button x-dialog:action value="no">No</button>
|
|
188
|
+
<button x-dialog:action>Cancel</button>
|
|
189
|
+
</div>
|
|
190
|
+
</dialog>
|
|
191
|
+
</div>
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## HTMX Integration
|
|
195
|
+
|
|
196
|
+
Value-scoped close events make integration with **htmx** straightforward and JavaScript-free.
|
|
197
|
+
|
|
198
|
+
```html
|
|
199
|
+
<div x-dialog:modal
|
|
200
|
+
hx-trigger="close:yes"
|
|
201
|
+
hx-delete="/account/5">
|
|
202
|
+
|
|
203
|
+
<button x-dialog:trigger>Deactivate account</button>
|
|
204
|
+
|
|
205
|
+
<dialog x-dialog:panel>
|
|
206
|
+
Are you sure you wish to deactivate your account?
|
|
207
|
+
|
|
208
|
+
<div>
|
|
209
|
+
<button x-dialog:action value="yes">Yes</button>
|
|
210
|
+
<button x-dialog:action>Cancel</button>
|
|
211
|
+
</div>
|
|
212
|
+
</dialog>
|
|
213
|
+
</div>
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## Nested Dialogs
|
|
217
|
+
|
|
218
|
+
Nesting `x-dialog` components directly in the DOM is **not supported** and results in **undefined behavior**.
|
|
219
|
+
|
|
220
|
+
This limitation is intentional and follows native HTML constraints:
|
|
221
|
+
|
|
222
|
+
* The `<dialog>` element does not define consistent behavior for nested dialogs
|
|
223
|
+
* HTML forms cannot be safely nested
|
|
224
|
+
* Buttons inside nested dialogs may be treated as part of an outer form
|
|
225
|
+
* Validation and submission semantics become unpredictable across browsers
|
|
226
|
+
|
|
227
|
+
For these reasons, we intentionally do not attempt to emulate or implement workarounds for nested dialog behavior.
|
|
228
|
+
|
|
229
|
+
### Recommended pattern
|
|
230
|
+
|
|
231
|
+
A common use case that appears to require nested dialogs is **confirming a destructive or cancel action**
|
|
232
|
+
while a dialog is already open (for example, canceling a form with unsaved data).
|
|
233
|
+
|
|
234
|
+
Instead of nesting dialogs, the recommended approach is to **guard the close operation** using a secondary dialog.
|
|
235
|
+
|
|
236
|
+
```html
|
|
237
|
+
<div x-dialog:modal @beforeclose="confirm"
|
|
238
|
+
x-data="{
|
|
239
|
+
email: '',
|
|
240
|
+
password: '',
|
|
241
|
+
confirm(e) {
|
|
242
|
+
if (!e.detail.value && (this.email || this.password)) {
|
|
243
|
+
e.preventDefault();
|
|
244
|
+
|
|
245
|
+
this.$refs.discardconfirm.show().then(result => {
|
|
246
|
+
if (result === 'yes') {
|
|
247
|
+
e.target.close('create');
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}">
|
|
253
|
+
|
|
254
|
+
<button x-dialog:trigger>Create</button>
|
|
255
|
+
|
|
256
|
+
<dialog x-dialog:panel closedby="closerequest">
|
|
257
|
+
<form method="dialog">
|
|
258
|
+
<h3>Create an account</h3>
|
|
259
|
+
|
|
260
|
+
<label>
|
|
261
|
+
Email:
|
|
262
|
+
<input x-model="email" type="email" required />
|
|
263
|
+
</label>
|
|
264
|
+
|
|
265
|
+
<label>
|
|
266
|
+
Password:
|
|
267
|
+
<input x-model="password" type="password" required />
|
|
268
|
+
</label>
|
|
269
|
+
|
|
270
|
+
<div class="actions">
|
|
271
|
+
<button value="create">Create</button>
|
|
272
|
+
<button formnovalidate>Cancel</button>
|
|
273
|
+
</div>
|
|
274
|
+
</form>
|
|
275
|
+
</dialog>
|
|
276
|
+
</div>
|
|
277
|
+
|
|
278
|
+
<div x-dialog:modal x-ref="discardconfirm">
|
|
279
|
+
<dialog x-dialog:panel closedby="any">
|
|
280
|
+
You have unsaved changes. Discard them?
|
|
281
|
+
|
|
282
|
+
<button x-dialog:action value="yes">Yes</button>
|
|
283
|
+
<button x-dialog:action autofocus>No</button>
|
|
284
|
+
</dialog>
|
|
285
|
+
</div>
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### Important Note on `beforeclose`
|
|
289
|
+
|
|
290
|
+
The `beforeclose` event is dispatched **synchronously**.
|
|
291
|
+
|
|
292
|
+
As a result, the decision to cancel the close operation **must be made synchronously during event dispatch**.
|
|
293
|
+
If the handler returns a `Promise` or performs asynchronous work before calling `preventDefault()`,
|
|
294
|
+
the event dispatch will already have completed and the dialog will close regardless.
|
|
295
|
+
|
|
296
|
+
For this reason:
|
|
297
|
+
|
|
298
|
+
* `beforeclose` handlers **must not rely on `async / await`**
|
|
299
|
+
* `event.preventDefault()` **must be called synchronously**
|
|
300
|
+
* Any asynchronous confirmation logic must occur *after* the close has been canceled
|
|
301
|
+
|
|
302
|
+
In the example above, the flow is:
|
|
303
|
+
|
|
304
|
+
1. `beforeclose` is dispatched synchronously
|
|
305
|
+
2. The handler immediately calls `event.preventDefault()`
|
|
306
|
+
3. The close operation is canceled
|
|
307
|
+
4. A secondary dialog is shown using `show().then(...)`
|
|
308
|
+
5. If the user confirms, the original dialog is closed programmatically
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
## Properties and Methods
|
|
312
|
+
|
|
313
|
+
All properties and methods are available within the `x-dialog` scope.
|
|
314
|
+
|
|
315
|
+
In addition, the same API is exposed on the **root DOM element** to which the `x-dialog` directive is applied.
|
|
316
|
+
This allows imperative control via `x-ref` when needed.
|
|
317
|
+
|
|
318
|
+
```js
|
|
319
|
+
const result = await this.$refs.dialog.show();
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
```js
|
|
323
|
+
const el = document.getElementById("dialog");
|
|
324
|
+
const result = await el.show();
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### `open` (readonly)
|
|
328
|
+
|
|
329
|
+
A boolean representing the dialog state:
|
|
330
|
+
* `true` — dialog is open; otherwise `false`
|
|
331
|
+
|
|
332
|
+
### `show(): Promise<string>`
|
|
333
|
+
|
|
334
|
+
Displays the dialog using the configured display mode (modal or non-modal).
|
|
335
|
+
|
|
336
|
+
Returns a `Promise<string>` that resolves when the dialog is closed.
|
|
337
|
+
The resolved value is the dialog's return value.
|
|
338
|
+
|
|
339
|
+
### `close(returnValue?: string): void`
|
|
340
|
+
|
|
341
|
+
Closes the dialog programmatically.
|
|
342
|
+
|
|
343
|
+
* `returnValue` — string returned by the dialog
|
|
344
|
+
* Closing can be prevented by canceling `beforeclose`
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
## Source Code
|
|
348
|
+
You can find the source code for this plugin on GitHub:
|
|
349
|
+
|
|
350
|
+
https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/dialog
|
|
351
|
+
|
|
352
|
+
## Related projects
|
|
353
|
+
|
|
354
|
+
**[@ramstack/alpinegear-main](https://www.npmjs.com/package/@ramstack/alpinegear-main)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/main))<br>
|
|
355
|
+
Provides a combined plugin that includes several useful directives.
|
|
356
|
+
This package aggregates multiple individual plugins, offering a convenient all-in-one bundle.
|
|
357
|
+
Included directives: `x-bound`, `x-format`, `x-fragment`, `x-match`, `x-template`, and `x-when`.
|
|
358
|
+
|
|
359
|
+
**[@ramstack/alpinegear-bound](https://www.npmjs.com/package/@ramstack/alpinegear-bound)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/bound))<br>
|
|
360
|
+
Provides the `x-bound` directive, which allows for two-way binding of input elements and their associated data properties.
|
|
361
|
+
It works similarly to the binding provided by [Svelte](https://svelte.dev/docs/element-directives#bind-property)
|
|
362
|
+
and also supports synchronizing values between two `Alpine.js` data properties.
|
|
363
|
+
|
|
364
|
+
**[@ramstack/alpinegear-template](https://www.npmjs.com/package/@ramstack/alpinegear-template)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/template))<br>
|
|
365
|
+
Provides the `x-template` directive, which allows you to define a template once anywhere in the DOM and reference it by its ID.
|
|
366
|
+
|
|
367
|
+
**[@ramstack/alpinegear-fragment](https://www.npmjs.com/package/@ramstack/alpinegear-fragment)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/fragment))<br>
|
|
368
|
+
Provides the `x-fragment` directive, which allows for fragment-like behavior similar to what's available in frameworks
|
|
369
|
+
like `Vue.js` or `React`, where multiple root elements can be grouped together.
|
|
370
|
+
|
|
371
|
+
**[@ramstack/alpinegear-match](https://www.npmjs.com/package/@ramstack/alpinegear-match)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/match))<br>
|
|
372
|
+
Provides the `x-match` directive, which functions similarly to the `switch` statement in many programming languages,
|
|
373
|
+
allowing you to conditionally render elements based on matching cases.
|
|
374
|
+
|
|
375
|
+
**[@ramstack/alpinegear-when](https://www.npmjs.com/package/@ramstack/alpinegear-when)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/when))<br>
|
|
376
|
+
Provides the `x-when` directive, which allows for conditional rendering of elements similar to `x-if`, but supports multiple root elements.
|
|
377
|
+
|
|
378
|
+
**[@ramstack/alpinegear-destroy](https://www.npmjs.com/package/@ramstack/alpinegear-destroy)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/destroy))<br>
|
|
379
|
+
Provides the `x-destroy` directive, which is the opposite of `x-init` and allows you to hook into the cleanup phase
|
|
380
|
+
of any element, running a callback when the element is removed from the DOM.
|
|
381
|
+
|
|
382
|
+
**[@ramstack/alpinegear-hotkey](https://www.npmjs.com/package/@ramstack/alpinegear-hotkey)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/hotkey))<br>
|
|
383
|
+
Provides the `x-hotkey` directive, which allows you to easily handle keyboard shortcuts within your Alpine.js components or application.
|
|
384
|
+
|
|
385
|
+
**[@ramstack/alpinegear-router](https://www.npmjs.com/package/@ramstack/alpinegear-router)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/router))<br>
|
|
386
|
+
Provides the `x-router` and `x-route` directives, which enable client-side navigation and routing functionality within your Alpine.js application.
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
## Contributions
|
|
390
|
+
Bug reports and contributions are welcome.
|
|
391
|
+
|
|
392
|
+
## License
|
|
393
|
+
This package is released as open source under the **MIT License**.
|
|
394
|
+
See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
const warn = (...args) => console.warn("alpinegear.js:", ...args);
|
|
2
|
+
const is_dialog = el => el.matches("dialog");
|
|
3
|
+
|
|
4
|
+
const listen = (target, type, listener, options) => {
|
|
5
|
+
target.addEventListener(type, listener, options);
|
|
6
|
+
return () => target.removeEventListener(type, listener, options);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const closest = (el, callback) => {
|
|
10
|
+
while (el && !callback(el)) {
|
|
11
|
+
el = (el._x_teleportBack ?? el).parentElement;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return el;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function plugin({ bind, directive }) {
|
|
18
|
+
directive("dialog", (el, { value }, { cleanup }) => {
|
|
19
|
+
const get_dialog_info = () => closest(el, n => n._r_dialog)?._r_dialog;
|
|
20
|
+
|
|
21
|
+
value ||= "";
|
|
22
|
+
|
|
23
|
+
if (!get_dialog_info() && value !== "modal" && value !== "") {
|
|
24
|
+
warn(`x-dialog:${value} is missing a parent x-dialog`);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (value === "panel") {
|
|
29
|
+
process_panel();
|
|
30
|
+
}
|
|
31
|
+
else if (value === "trigger") {
|
|
32
|
+
process_trigger();
|
|
33
|
+
}
|
|
34
|
+
else if (value === "action") {
|
|
35
|
+
process_action();
|
|
36
|
+
}
|
|
37
|
+
else if (value === "modal" || !value) {
|
|
38
|
+
process_dialog();
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
warn(`Unknown x-dialog:${value} directive`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function process_dialog() {
|
|
45
|
+
el._r_dialog = {
|
|
46
|
+
owner: el,
|
|
47
|
+
panel: null,
|
|
48
|
+
modal: value === "modal"
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
Object.defineProperty(el, "open", {
|
|
52
|
+
get() {
|
|
53
|
+
return !!el._r_dialog?.panel.open;
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
Object.assign(el, {
|
|
58
|
+
show() {
|
|
59
|
+
return dialog_show();
|
|
60
|
+
},
|
|
61
|
+
close(value) {
|
|
62
|
+
dialog_close(value);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
bind(el, {
|
|
67
|
+
"x-data"() {
|
|
68
|
+
return {
|
|
69
|
+
open: false,
|
|
70
|
+
show() {
|
|
71
|
+
return dialog_show();
|
|
72
|
+
},
|
|
73
|
+
close(value) {
|
|
74
|
+
dialog_close(value);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function process_panel() {
|
|
82
|
+
if (get_dialog_info().panel) {
|
|
83
|
+
warn("x-dialog:panel is already present. Only the last one will be used.");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!is_dialog(el)) {
|
|
87
|
+
warn("x-dialog:panel should be used on a <dialog> element");
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const owner = get_dialog_info().owner;
|
|
92
|
+
get_dialog_info().panel = el;
|
|
93
|
+
|
|
94
|
+
bind(el, {
|
|
95
|
+
"x-init"() {
|
|
96
|
+
this.open = el.open;
|
|
97
|
+
},
|
|
98
|
+
"@toggle"(e) {
|
|
99
|
+
(this.open = el.open) && dispatch(owner, "open");
|
|
100
|
+
dispatch(owner, "toggle", { state: e.newState });
|
|
101
|
+
},
|
|
102
|
+
"@cancel.prevent"() {
|
|
103
|
+
dialog_close();
|
|
104
|
+
},
|
|
105
|
+
//
|
|
106
|
+
// https://issues.chromium.org/issues/346597066
|
|
107
|
+
// HTMLDialogElement's "cancel" event is not cancelable when "ESC" key is pressed several times
|
|
108
|
+
//
|
|
109
|
+
"@keydown.escape.prevent.stop"() {
|
|
110
|
+
//
|
|
111
|
+
// https://bugs.webkit.org/show_bug.cgi?id=284592
|
|
112
|
+
// Safari still lacks native support for the "closedby" attribute on <dialog>
|
|
113
|
+
//
|
|
114
|
+
if (["any", "closerequest"].includes(el.getAttribute("closedby"))) {
|
|
115
|
+
//
|
|
116
|
+
// "requestClose" fires a "cancel" event before firing the "close" event
|
|
117
|
+
//
|
|
118
|
+
el.requestClose();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
cleanup(
|
|
124
|
+
//
|
|
125
|
+
// Listening to the "submit" event on the document element, ensuring (to some extent)
|
|
126
|
+
// that our handler executes last among all handlers listening for this event.
|
|
127
|
+
// This allows us to determine whether the event was canceled by someone else.
|
|
128
|
+
//
|
|
129
|
+
listen(document, "submit", e => {
|
|
130
|
+
if (e.target.method === "dialog" && closest(e.target, n => n === el) && !e.defaultPrevented) {
|
|
131
|
+
//
|
|
132
|
+
// Prevent the dialog from closing immediately,
|
|
133
|
+
// as we need to trigger our own custom events first.
|
|
134
|
+
//
|
|
135
|
+
e.preventDefault();
|
|
136
|
+
|
|
137
|
+
dialog_close(e.submitter?.value);
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function process_trigger() {
|
|
144
|
+
bind(el, {
|
|
145
|
+
"@click.prevent": "show"
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function process_action() {
|
|
150
|
+
if (!closest(el, is_dialog)) {
|
|
151
|
+
warn("x-dialog:action is missing a parent x-dialog:panel");
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
el.form || bind(el, {
|
|
156
|
+
"@click.prevent"() {
|
|
157
|
+
dialog_close(el.value);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function dialog_show() {
|
|
163
|
+
const { panel, modal } = get_dialog_info();
|
|
164
|
+
|
|
165
|
+
if (panel) {
|
|
166
|
+
return new Promise(resolve => {
|
|
167
|
+
listen(panel, "close", () => resolve(panel.returnValue), { once: true });
|
|
168
|
+
panel[modal ? "showModal" : "show"]();
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return Promise.resolve();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function dialog_close(value) {
|
|
176
|
+
value ??= "";
|
|
177
|
+
|
|
178
|
+
const { owner, panel } = get_dialog_info();
|
|
179
|
+
const detail = { value };
|
|
180
|
+
|
|
181
|
+
if (dispatch(owner, "beforeclose", detail, { cancelable: true })) {
|
|
182
|
+
value && dispatch(owner, "close:" + value.toLowerCase(), detail);
|
|
183
|
+
dispatch(owner, "close", detail);
|
|
184
|
+
panel.close(value);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function dispatch(el, name, detail = {}, options = {}) {
|
|
189
|
+
return el.dispatchEvent(new CustomEvent(name, { detail, ...options }));
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export { plugin as dialog };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=(...e)=>console.warn("alpinegear.js:",...e),o=e=>e.matches("dialog"),n=(e,o,n,t)=>(e.addEventListener(o,n,t),()=>e.removeEventListener(o,n,t)),t=(e,o)=>{for(;e&&!o(e);)e=(e._x_teleportBack??e).parentElement;return e};function a({bind:a,directive:l}){l("dialog",(l,{value:i},{cleanup:s})=>{const r=()=>t(l,e=>e._r_dialog)?._r_dialog;function c(){const{panel:e,modal:o}=r();return e?new Promise(t=>{n(e,"close",()=>t(e.returnValue),{once:!0}),e[o?"showModal":"show"]()}):Promise.resolve()}function d(e){e??="";const{owner:o,panel:n}=r(),t={value:e};p(o,"beforeclose",t,{cancelable:!0})&&(e&&p(o,"close:"+e.toLowerCase(),t),p(o,"close",t),n.close(e))}function p(e,o,n={},t={}){return e.dispatchEvent(new CustomEvent(o,{detail:n,...t}))}i||="",r()||"modal"===i||""===i?"panel"===i?function(){if(!o(l))return void e("x-dialog:panel should be used on a <dialog> element");const i=r().owner;r().panel=l,a(l,{"x-init"(){this.open=l.open},"@toggle"(e){(this.open=l.open)&&p(i,"open"),p(i,"toggle",{state:e.newState})},"@cancel.prevent"(){d()},"@keydown.escape.prevent.stop"(){["any","closerequest"].includes(l.getAttribute("closedby"))&&l.requestClose()}}),s(n(document,"submit",e=>{"dialog"===e.target.method&&t(e.target,e=>e===l)&&!e.defaultPrevented&&(e.preventDefault(),d(e.submitter?.value))}))}():"trigger"===i?a(l,{"@click.prevent":"show"}):"action"===i?t(l,o)?l.form||a(l,{"@click.prevent"(){d(l.value)}}):e("x-dialog:action is missing a parent x-dialog:panel"):"modal"!==i&&i||(l._r_dialog={owner:l,panel:null,modal:"modal"===i},Object.defineProperty(l,"open",{get:()=>!!l._r_dialog?.panel.open}),Object.assign(l,{show:()=>c(),close(e){d(e)}}),a(l,{"x-data":()=>({open:!1,show:()=>c(),close(e){d(e)}})})):e(`x-dialog:${i} is missing a parent x-dialog`)})}export{a as dialog};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const warn = (...args) => console.warn("alpinegear.js:", ...args);
|
|
5
|
+
const is_dialog = el => el.matches("dialog");
|
|
6
|
+
|
|
7
|
+
const listen = (target, type, listener, options) => {
|
|
8
|
+
target.addEventListener(type, listener, options);
|
|
9
|
+
return () => target.removeEventListener(type, listener, options);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const closest = (el, callback) => {
|
|
13
|
+
while (el && !callback(el)) {
|
|
14
|
+
el = (el._x_teleportBack ?? el).parentElement;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return el;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function plugin({ bind, directive }) {
|
|
21
|
+
directive("dialog", (el, { value }, { cleanup }) => {
|
|
22
|
+
const get_dialog_info = () => closest(el, n => n._r_dialog)?._r_dialog;
|
|
23
|
+
|
|
24
|
+
value ||= "";
|
|
25
|
+
|
|
26
|
+
if (!get_dialog_info() && value !== "modal" && value !== "") {
|
|
27
|
+
warn(`x-dialog:${value} is missing a parent x-dialog`);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (value === "panel") {
|
|
32
|
+
process_panel();
|
|
33
|
+
}
|
|
34
|
+
else if (value === "trigger") {
|
|
35
|
+
process_trigger();
|
|
36
|
+
}
|
|
37
|
+
else if (value === "action") {
|
|
38
|
+
process_action();
|
|
39
|
+
}
|
|
40
|
+
else if (value === "modal" || !value) {
|
|
41
|
+
process_dialog();
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
warn(`Unknown x-dialog:${value} directive`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function process_dialog() {
|
|
48
|
+
el._r_dialog = {
|
|
49
|
+
owner: el,
|
|
50
|
+
panel: null,
|
|
51
|
+
modal: value === "modal"
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
Object.defineProperty(el, "open", {
|
|
55
|
+
get() {
|
|
56
|
+
return !!el._r_dialog?.panel.open;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
Object.assign(el, {
|
|
61
|
+
show() {
|
|
62
|
+
return dialog_show();
|
|
63
|
+
},
|
|
64
|
+
close(value) {
|
|
65
|
+
dialog_close(value);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
bind(el, {
|
|
70
|
+
"x-data"() {
|
|
71
|
+
return {
|
|
72
|
+
open: false,
|
|
73
|
+
show() {
|
|
74
|
+
return dialog_show();
|
|
75
|
+
},
|
|
76
|
+
close(value) {
|
|
77
|
+
dialog_close(value);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function process_panel() {
|
|
85
|
+
if (get_dialog_info().panel) {
|
|
86
|
+
warn("x-dialog:panel is already present. Only the last one will be used.");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!is_dialog(el)) {
|
|
90
|
+
warn("x-dialog:panel should be used on a <dialog> element");
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const owner = get_dialog_info().owner;
|
|
95
|
+
get_dialog_info().panel = el;
|
|
96
|
+
|
|
97
|
+
bind(el, {
|
|
98
|
+
"x-init"() {
|
|
99
|
+
this.open = el.open;
|
|
100
|
+
},
|
|
101
|
+
"@toggle"(e) {
|
|
102
|
+
(this.open = el.open) && dispatch(owner, "open");
|
|
103
|
+
dispatch(owner, "toggle", { state: e.newState });
|
|
104
|
+
},
|
|
105
|
+
"@cancel.prevent"() {
|
|
106
|
+
dialog_close();
|
|
107
|
+
},
|
|
108
|
+
//
|
|
109
|
+
// https://issues.chromium.org/issues/346597066
|
|
110
|
+
// HTMLDialogElement's "cancel" event is not cancelable when "ESC" key is pressed several times
|
|
111
|
+
//
|
|
112
|
+
"@keydown.escape.prevent.stop"() {
|
|
113
|
+
//
|
|
114
|
+
// https://bugs.webkit.org/show_bug.cgi?id=284592
|
|
115
|
+
// Safari still lacks native support for the "closedby" attribute on <dialog>
|
|
116
|
+
//
|
|
117
|
+
if (["any", "closerequest"].includes(el.getAttribute("closedby"))) {
|
|
118
|
+
//
|
|
119
|
+
// "requestClose" fires a "cancel" event before firing the "close" event
|
|
120
|
+
//
|
|
121
|
+
el.requestClose();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
cleanup(
|
|
127
|
+
//
|
|
128
|
+
// Listening to the "submit" event on the document element, ensuring (to some extent)
|
|
129
|
+
// that our handler executes last among all handlers listening for this event.
|
|
130
|
+
// This allows us to determine whether the event was canceled by someone else.
|
|
131
|
+
//
|
|
132
|
+
listen(document, "submit", e => {
|
|
133
|
+
if (e.target.method === "dialog" && closest(e.target, n => n === el) && !e.defaultPrevented) {
|
|
134
|
+
//
|
|
135
|
+
// Prevent the dialog from closing immediately,
|
|
136
|
+
// as we need to trigger our own custom events first.
|
|
137
|
+
//
|
|
138
|
+
e.preventDefault();
|
|
139
|
+
|
|
140
|
+
dialog_close(e.submitter?.value);
|
|
141
|
+
}
|
|
142
|
+
})
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function process_trigger() {
|
|
147
|
+
bind(el, {
|
|
148
|
+
"@click.prevent": "show"
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function process_action() {
|
|
153
|
+
if (!closest(el, is_dialog)) {
|
|
154
|
+
warn("x-dialog:action is missing a parent x-dialog:panel");
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
el.form || bind(el, {
|
|
159
|
+
"@click.prevent"() {
|
|
160
|
+
dialog_close(el.value);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function dialog_show() {
|
|
166
|
+
const { panel, modal } = get_dialog_info();
|
|
167
|
+
|
|
168
|
+
if (panel) {
|
|
169
|
+
return new Promise(resolve => {
|
|
170
|
+
listen(panel, "close", () => resolve(panel.returnValue), { once: true });
|
|
171
|
+
panel[modal ? "showModal" : "show"]();
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return Promise.resolve();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function dialog_close(value) {
|
|
179
|
+
value ??= "";
|
|
180
|
+
|
|
181
|
+
const { owner, panel } = get_dialog_info();
|
|
182
|
+
const detail = { value };
|
|
183
|
+
|
|
184
|
+
if (dispatch(owner, "beforeclose", detail, { cancelable: true })) {
|
|
185
|
+
value && dispatch(owner, "close:" + value.toLowerCase(), detail);
|
|
186
|
+
dispatch(owner, "close", detail);
|
|
187
|
+
panel.close(value);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function dispatch(el, name, detail = {}, options = {}) {
|
|
192
|
+
return el.dispatchEvent(new CustomEvent(name, { detail, ...options }));
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
document.addEventListener("alpine:init", () => { Alpine.plugin(plugin); });
|
|
198
|
+
|
|
199
|
+
})();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(){"use strict";const e=(...e)=>console.warn("alpinegear.js:",...e),n=e=>e.matches("dialog"),o=(e,n,o,t)=>(e.addEventListener(n,o,t),()=>e.removeEventListener(n,o,t)),t=(e,n)=>{for(;e&&!n(e);)e=(e._x_teleportBack??e).parentElement;return e};function a({bind:a,directive:l}){l("dialog",(l,{value:i},{cleanup:s})=>{const r=()=>t(l,e=>e._r_dialog)?._r_dialog;function c(){const{panel:e,modal:n}=r();return e?new Promise(t=>{o(e,"close",()=>t(e.returnValue),{once:!0}),e[n?"showModal":"show"]()}):Promise.resolve()}function d(e){e??="";const{owner:n,panel:o}=r(),t={value:e};p(n,"beforeclose",t,{cancelable:!0})&&(e&&p(n,"close:"+e.toLowerCase(),t),p(n,"close",t),o.close(e))}function p(e,n,o={},t={}){return e.dispatchEvent(new CustomEvent(n,{detail:o,...t}))}i||="",r()||"modal"===i||""===i?"panel"===i?function(){if(!n(l))return void e("x-dialog:panel should be used on a <dialog> element");const i=r().owner;r().panel=l,a(l,{"x-init"(){this.open=l.open},"@toggle"(e){(this.open=l.open)&&p(i,"open"),p(i,"toggle",{state:e.newState})},"@cancel.prevent"(){d()},"@keydown.escape.prevent.stop"(){["any","closerequest"].includes(l.getAttribute("closedby"))&&l.requestClose()}}),s(o(document,"submit",e=>{"dialog"===e.target.method&&t(e.target,e=>e===l)&&!e.defaultPrevented&&(e.preventDefault(),d(e.submitter?.value))}))}():"trigger"===i?a(l,{"@click.prevent":"show"}):"action"===i?t(l,n)?l.form||a(l,{"@click.prevent"(){d(l.value)}}):e("x-dialog:action is missing a parent x-dialog:panel"):"modal"!==i&&i||(l._r_dialog={owner:l,panel:null,modal:"modal"===i},Object.defineProperty(l,"open",{get:()=>!!l._r_dialog?.panel.open}),Object.assign(l,{show:()=>c(),close(e){d(e)}}),a(l,{"x-data":()=>({open:!1,show:()=>c(),close(e){d(e)}})})):e(`x-dialog:${i} is missing a parent x-dialog`)})}document.addEventListener("alpine:init",()=>{Alpine.plugin(a)})}();
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ramstack/alpinegear-dialog",
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "A headless, unstyled directive-based dialog (modal) component for Alpine.js, built on the native <dialog> element",
|
|
5
|
+
"author": "Rameel Burhan",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/rameel/ramstack.alpinegear.js.git",
|
|
10
|
+
"directory": "src/plugins/dialog"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"alpine.js",
|
|
14
|
+
"alpinejs",
|
|
15
|
+
"dialog",
|
|
16
|
+
"modal",
|
|
17
|
+
"headless-ui",
|
|
18
|
+
"component",
|
|
19
|
+
"alpinejs-directive",
|
|
20
|
+
"alpinejs-plugin",
|
|
21
|
+
"alpinejs-component",
|
|
22
|
+
"htmx"
|
|
23
|
+
],
|
|
24
|
+
"main": "alpinegear-dialog.js",
|
|
25
|
+
"module": "alpinegear-dialog.esm.js"
|
|
26
|
+
}
|