@ramstack/alpinegear-dialog 1.4.3 → 1.4.4
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 +365 -394
- package/alpinegear-dialog.esm.js +190 -190
- package/alpinegear-dialog.esm.min.js +1 -1
- package/alpinegear-dialog.js +190 -190
- package/alpinegear-dialog.min.js +1 -1
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -1,394 +1,365 @@
|
|
|
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
|
|
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
|
|
52
|
-
|
|
53
|
-
Alpine.plugin(
|
|
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
|
|
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
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
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.
|
|
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.
|
|
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 without a single line of script.
|
|
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 packages
|
|
353
|
+
This package is part of **[AlpineGear](https://github.com/rameel/ramstack.alpinegear.js)** —
|
|
354
|
+
a collection of utilities and directives for [Alpine.js](https://alpinejs.dev).
|
|
355
|
+
|
|
356
|
+
You can find the full list of related packages and their documentation here:
|
|
357
|
+
https://github.com/rameel/ramstack.alpinegear.js
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
## Contributions
|
|
361
|
+
Bug reports and contributions are welcome.
|
|
362
|
+
|
|
363
|
+
## License
|
|
364
|
+
This package is released as open source under the **MIT License**.
|
|
365
|
+
See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
|