And that's all that's needed to get that first interaction. And in the dialogue, we have a command close button that helps us close the dialogue. And because it's all HTML, we can use CSS to target specific things to customize the look and the behavior of certain things. So we can style the backdrop. So if we wanted to make that green, for example, we can easily do that. But we can also do things like more advanced things like animations. Like we can animate the dialogue in and out, especially even if we dynamically add the DOM element to it, we can use discrete animations and starting styles to define where the transition should start in any given case. And in this case, we want to scale it in and from opacity zero to one, and we can easily do that for in and out, which is really powerful. And if you haven't looked at dialogues because of these restraints, this is all newly available since 2024, so quite a while. And all major browsers definitely check it out again and give it another try.
A very similar pattern is popovers, where the main difference is with popover elements, they still pop up, but they shouldn't block us from interacting with the rest of the page. For that, it's not a dedicated element. It's just a popover attribute that can put on any element, but the pattern is the same. We give it a unique identifier and then we have a popover target attribute on the button, tells the browser if I click on this button, that's what I want to open. But by default, it just opens it in the middle of the page, which isn't really what you want with popovers. Popovers, you want to position relative to their trigger. And we can do that with CSS anchors. So that's been finally newly available since earlier this year. So again, definitely check it out if you haven't. It basically allows us to on the trigger elements, in this case the button, we can give the button an anchor name. And then in the popover itself, we can say position absolute and position relative to that anchor. And then we can use the position area to say like where relative to the anchor we want to position it. So right now, bottom center, if we change that to top, it goes top center. And it deals with all of the collision magic for us. So calculating whether there's enough space, otherwise push it in a little bit or flip it to the other side. The browser does all of this for us, but we have full control via CSS, which is pretty cool.
When we talk about popovers, the next thing that you probably think of is tooltips. Because they're very similar behavior, just in different interactions. So usually tooltips you want to trigger when you hover over an element, and you can't do that with the popover API. So for that, this is very experimental.
Comments