Skip to content

Usage

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

<div
  class="l-center"
  style="--cr-max-width: none; --cr-align: center; margin: var(--vertical-rhythm) var(--global-padding) 0 0"
>
  <button class="off-canvas-btn-open theme button" style="margin-inline: auto"
    >Open</button
  >
</div>

<div class="off-canvas theme container">
  <div
    class="l-stack"
    style="padding: var(--vertical-rhythm) var(--global-padding)"
  >
    <button class="off-canvas-btn-close theme button">Close</button>

    <div>
      <ul>
        <li><a href="/">Link 1</a></li>
        <li><a href="/">Link 2</a></li>
        <li><a href="/">Link 3</a></li>
      </ul>
    </div>
  </div>
</div>

Useful for mobile navigation.

Todos

Elaborate on disabling scrolling documentation below. How to prevent layout shift when scrollbar disappears (more evident when the background content is visible)

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.

Accessibility Affordances

  • Checks whether prefers-reduced-motion is enabled and sets the transition duration to 0.
  • Traps tab focus to the canvas content while open.
  • Pressing the Escape key will close the component.