3

Basic Dialog Usage and Gotchas To Watch For

 1 year ago
source link: https://frontendmasters.com/blog/basic-dialog-usage-and-gotchas-to-watch-for/?ref=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
dialog-thumb.jpg?fit=1000%2C500&ssl=1

Basic Dialog Usage and Gotchas To Watch For

February 5, 2024

The <dialog> element in HTML is tremendous. We’ve got support across the board now, so using it is a smart plan. Just with basic usage, you get a centered modal dialog experience that comes up when you call it, a dimmed background, focus trapped within it, closes with the ESC key, and focus returning where it came from. You can style it all entirely predictably in CSS. Those things range from a little bit of a pain to downright hard to pull off if left to our own implementations. Now we get them all for free as they say.

You don’t automatically get a close button, so here’s a basic implementation that adds that.

<button class="show-dialog-button">Show Dialog</button>

<dialog id="dialog">
  <div class="dialog-title">
    Hi, I'm a dialog.
  </div>
  <button aria-label="Close Dialog" class="close-dialog-button">
    <svg ...>
  </button>
</dialog> HTML, XML 

Now we need two click handlers, one for opening and one for closing.

const showDialogButton = document.querySelector(".show-dialog-button");
const closeDialogButton = document.querySelector(".close-dialog-button");
const dialog = document.querySelector("dialog");

showDialogButton.addEventListener("click", () => {
  dialog.showModal();
});

closeDialogButton.addEventListener("click", () => {
  dialog.close();
}); JavaScript 

I think the naming of things here almost qualifies as a gotcha. So it’s showModal() eh? Why not showDialog(), since, ya know, it’s for the <dialog> element? So to close it it must be closeDialog() or hideDialog() surely, right? No. Just close(). Oh well I’m sure that was bikeshedded to death and there are probably ✨ reasons.

That code above is it really, that’s largely functional. And that, friends, is extremely cool. Here’s a quick live demo:

Here’s another little gotcha! Where do you want to position that close button?

I would think probably in the top right or top left of the dialog. So you might…

dialog {
  position: relative;

  .close-button {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
  }
}
 CSS 

Danger danger!

By setting the <dialog> to position: relative;, something we naturally do when I’m thinking about using position: absolute; on a child element, we’ve introduced a potentially gnarly UX bug. Desktop browsers will now scroll the page all the way to the top when the dialog is opened, and you’ll see the dialog open and centered in the viewport. But on iOS, the window will not scroll up. This leads to a situation where you can open the dialog and… not see it at all.

The problem there is that, by default, the <dialog> is position: fixed;, which means it would show up no matter where the page is scrolled to (even without force-scrolling to the top). But we’ve (ok ok, I’ve) accidentally overridden it with relative creating this unexpected behavior.

It’s a little tempting to look into locking the scroll position while the dialog is open, perhaps with a simple :has()-based selector, but I’m not sure how much I care if the page can scroll while it’s open.

There is other little gotcha’s to think about as well, like the fact that content within dialogs are not find-on-page-able until opened. Which kinda makes sense, but content within details elements are, so it’s just something you need to know about and probably not accidentally hide content within you want available always. I’d listen to Scott O’Hara, myself.

If you want to see a <dialog> in production use, look no further. Posts right here on Boost have a “Take Quiz” button the sidebar that opens a dialog with a custom Web Component that the Frontend Masters team have built to help build you a custom course path.

Any other gotchas you’ve found with the dialog element?

Oh hey ya know what, if someone were to make like 8-10 really cool designs for the <dialog> and the ::backdrop, that would make for a pretty sweet guest post, I’d say.


Recommend

  • 10
    • www.wavebeem.com 4 years ago
    • Cache

    JS gotchas with this and new

    JS gotchas with this and newBrian Mock Home Blog Resume JS gotchas with this and n...

  • 10
    • www.mikechambers.com 4 years ago
    • Cache

    Flash / Arduino Tips, Tricks and Gotchas

    This is a ongoing post where I will post tips, tricks and gotchas that I learn while developing with Flash and the Arduino. I will update this from time to time with more information. Make sure you are using the correct BAUD Rate

  • 7

    Tips and Gotchas for Using key with v-for in Vue.js 3Written by Daniel Kelly When working with v-for in Vue it is typically recommended to provide a special key attribute. Something like this: <di...

  • 6
    • dandreamsofcoding.com 3 years ago
    • Cache

    Java quirks and interview gotchas

    Java quirks and interview gotchas Interviewers are a diverse lot. Some care about this, others about that, each has her own set of biases, and short of being perfect, there’s really no way to please everyone. The worst is when y...

  • 7

    Recently we ran into a couple of bugs on our main fairly large monorepo which were caused my the same package existing twice but having different versions. This made me search for some dependency checking tools, and while there are existing o...

  • 6
    • khalidabuhakmeh.com 3 years ago
    • Cache

    ASP.NET Docker Gotchas and Workarounds

    November 16, 2021 ASP.NET Docker Gotchas and Workarounds ...

  • 5

    I’ve updated Undirect, a Chrome plugin that prevents annoying redirects on the google search page, because it had stopped working. I ran into a few surpr...

  • 4
    • snakeycode.wordpress.com 3 years ago
    • Cache

    Django-Compressor Tricks and Gotchas | Snakey Code

    Django-Compressor Tricks and Gotchas Although it looks simple enough, getting Django-Compressor working is a little tricky. Here are my notes. Do Not Nest {% Compress %} Blocks This one comes from

  • 8
    • www.sqlite.org 2 years ago
    • Cache

    Quirks, Caveats, and Gotchas In SQLite

    Quirks, Caveats, and Gotchas In SQLite ► Table Of Contents 1. Overview The SQL language is a "standard". Even...

  • 3

    MongoDB V4.2 EOL Is Coming: How To Upgrade Now and Watch Out for the Gotchas! April 13, 2023 Kimberly Wilkins

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK