Skip to content

Usage

<script src="/src/js/modal-init.js"></script>

<dialog class="modal">
  <form method="dialog">
    <button class="button theme">Close</button>
  </form>

  <div class="content">
    <div class="l-stack">
      <h2>Some Content</h2>
      <p>Some text</p>
      <a href="/">Call to action</a>
    </div>
  </div>
</dialog>

<button class="button theme showModal" style="margin-inline: auto;">Open Modal</button>

Todo

Document the approaches available of preventing scrolling when the modal is open. How to prevent layout shift when scrollbar disappears (more evident when the background content is visible)

The Dialog Element

This implimentation of the modal component uses the dialog element.

You can set the color of the background behind the modal using the ::backdrop psuedo-selector. However, it does not currently inherit custom properties.

Scrolling

User scroll should be prevented when the modal is open. This implimentation uses the body-scroll-lock package, which handles scroll locking for a variety of browsers and device types.

Keyboard Navigation

This implimentation provides focus trapping to keep the tabbing context within the modal.

Future Additions

  • document the styles that are included (ex. positioning)