mirror of
https://github.com/bigskysoftware/htmx.git
synced 2025-09-27 13:01:03 +00:00
sweet alert example
This commit is contained in:
parent
d153929304
commit
8826e92517
@ -37,3 +37,4 @@ You can copy and paste them and then adjust them for your needs.
|
||||
| [Keyboard Shortcuts](/examples/keyboard-shortcuts) | Demonstrates how to create keyboard shortcuts for htmx enabled elements
|
||||
| [Sortable](/examples/sortable) | Demonstrates how to use htmx with the Sortable.js plugin to implement drag-and-drop reordering
|
||||
| [Updating Other Content](/examples/update-other-content) | Demonstrates how to update content beyond just the target elements
|
||||
| [Confirm](/examples/confirm) | Demonstrates how to implement a custom confirmation dialog with htmx
|
||||
|
62
www/examples/confirm.md
Normal file
62
www/examples/confirm.md
Normal file
@ -0,0 +1,62 @@
|
||||
---
|
||||
layout: demo_layout.njk
|
||||
---
|
||||
|
||||
## A Customized Confirmation UI
|
||||
|
||||
htmx supports the [`hx-confirm`](/attributes/hx-confirm) attribute to provide a simple mechanism for confirming a user action. This uses the default `confirm()` function in javascript which, while trusty, may not be consistent with your applications UX.
|
||||
|
||||
In this example we will see how to use [sweetalert2](https://sweetalert2.github.io) to implement a custom confirmation dialog.
|
||||
|
||||
```html
|
||||
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
||||
<button hx-trigger='confirmed'
|
||||
hx-get="/confirmed"
|
||||
_="on click
|
||||
call Swal.fire({title: 'Confirm', text:'Do you want to continue?'})
|
||||
if result.isConfirmed trigger confirmed">
|
||||
Click Me
|
||||
</button>
|
||||
```
|
||||
|
||||
The technique here is to make the button issue a request on the `confirmed` event, rather than a click.
|
||||
|
||||
We then add some hyperscript to invoke Sweet Alert 2 on a click, asking for confirmation. If the user confirms
|
||||
the dialog, we trigger the `confirmed` event, which then triggers the htmx request.
|
||||
|
||||
Note that we are taking advantage of the fact that hyperscript is (async-transparent)[https://hyperscript.org/docs/#async]
|
||||
and automatically resolves the Promise returned by `Swal.fire()`.
|
||||
|
||||
A VanillaJS implementation is left as an exercise for the reader. :)
|
||||
|
||||
{% include demo_ui.html.liquid %}
|
||||
|
||||
<script src="//cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
||||
|
||||
<script>
|
||||
|
||||
//=========================================================================
|
||||
// Fake Server Side Code
|
||||
//=========================================================================
|
||||
|
||||
// routes
|
||||
init("/demo", function(request, params){
|
||||
return initialUI();
|
||||
});
|
||||
|
||||
onGet("/confirmed", function (request, params) {
|
||||
return "Confirmed"
|
||||
});
|
||||
|
||||
// templates
|
||||
function initialUI() {
|
||||
return `<button hx-trigger='confirmed'
|
||||
hx-get="/confirmed"
|
||||
_="on click
|
||||
call Swal.fire({title: 'Confirm', text:'Do you want to continue?'})
|
||||
if result.isConfirmed trigger confirmed">
|
||||
Click Me
|
||||
</button>`;
|
||||
}
|
||||
|
||||
</script>
|
Loading…
x
Reference in New Issue
Block a user