Trying out native dialog modals

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

I wrote an article on creating your modal instead of using a plugin.
I reposted it the other day on Twitter, and Jhey made a good comment that it could quickly be done with the dialog element these days.
I made a Astro version powered by dialog but haven't written down the plain version for you all. So in this article, we'll create a modal with the dialog element.
The main element we'll be working with today is the dialog element. This is an HTML element that, by default, is a dialog (not a modal).
It can take an open state, indicating whether it's open or not.
<dialog open>
<p>Hello from the dialog</p>
</dialog>
The code above will render the dialog directly on our page like this.

You'll notice that it's not a modal and doesn't have a backdrop this way.
We usually want to open the dialog with some action.
Luckily for us, the dialog comes with a JavaScript API. If you give your dialogs a specific ID, we can target them.
<dialog id="mydialog">
<p>Hello I only show on click</p>
</dialog>
We can then use JavaScript to open up the modal like this.
window.mydialog.show();
However, I'd recommend using the more explicit showModal function.
This one makes it a modal, allowing us to have a backdrop and centering the dialog on the page.
window.mydialog.showModal();
It's, of course, pretty rare to show a dialog directly in the browser so let's only show it on button click.
<button onclick="window.mydialog.showModal();">show modal</button>
<dialog id="mydialog">
<p>Hello I only show on click</p>
</dialog>
There are two ways of closing the modal. The first one is to use the close function on the dialog like so:
<button onclick='window.mydialog.close();'>close modal</button>
Alternatively, you can use an HTML way of closing the dialog. You can put a form with the method dialog inside the dialog element.
<form method="dialog">
<button>Close</button>
</form>
The last part of why the dialog is cool is that you can style some custom elements.
We get a new pseudo-element called ::backdrop to style the backdrop the modal has.
dialog::backdrop {
background: rgba(255, 0, 0, 0.1);
}
We should now get a pinkish backdrop color when we open the modal.
Then, of course, you can style every element of the dialog as you see fit.
It's good to note that to this day, there are still some accessibility concerns raised.
But from trying it out, most browsers these days also got the full support of the focus within the dialog, and it's looking to be a full-blown accessible solution.
The dialog element, exposed APIs, and styling options are a great way to create modals these days.
I'm sure to be trying them out on a project very soon.
If you want to play with the dialog element, I've created this CodePen for you to try out.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter