Try 'magnetic button'
View code

Specification

Catalogue
No. 66 of 110
Interaction
Layout Animation
Element
Overlay
Preview
Live component
Imports
react 19.3.1
Added

Notes

A dialog that grows in, a side sheet and a bottom sheet that slide, all native.

Built on the browser's own dialog, so Escape, focus trapping and focus return work for free. The enter animation is pure CSS.

Dialog from 0.95 + fade, 200ms. Sheets slide 300ms, cubic-bezier(.22, 1, .36, 1). @starting-style, no JavaScript animation.

Code

React and plain CSS
"use client";

import { useRef, type MouseEvent, type SyntheticEvent } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import "./dialogs-sheets.css";

/** Opens a <dialog> with showModal and closes it after its exit transition. */
function useDialog() {
  const ref = useRef<HTMLDialogElement>(null);
  const open = () => ref.current?.showModal();
  const close = () => {
    const d = ref.current;
    if (!d || !d.open) return;
    if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
      d.close();
      return;
    }
    let finished = false;
    const done = () => {
      if (finished) return;
      finished = true;
      delete d.dataset.closing;
      d.close();
    };
    d.dataset.closing = "";
    d.addEventListener("transitionend", done, { once: true });
    window.setTimeout(done, 450);
  };
  const props = {
    ref,
    onCancel: (e: SyntheticEvent) => {
      e.preventDefault();
      close();
    },
    onClick: (e: MouseEvent<HTMLDialogElement>) => {
      if (e.target === e.currentTarget) close();
    },
  };
  return { open, close, props };
}

function DialogsDemo() {
  const modal = useDialog();
  const side = useDialog();
  const bottom = useDialog();
  return (
    <div className="dm">
      <div className="dm-actions">
        <button type="button" className="dm-btn" data-variant="solid" onClick={modal.open}>
          Open dialog
        </button>
        <button type="button" className="dm-btn" onClick={side.open}>
          Side sheet
        </button>
        <button type="button" className="dm-btn" onClick={bottom.open}>
          Bottom sheet
        </button>
      </div>
      <p className="dm-hint">Escape or a click outside closes them</p>

      <dialog className="dm-dialog modal" aria-labelledby="dlg-title" {...modal.props}>
        <div className="dm-dialog-body">
          <h3 id="dlg-title">Book a call</h3>
          <p>Pick a time that suits you and we will send a calendar invite.</p>
          <div className="dm-dialog-actions">
            <button type="button" className="dm-btn" onClick={modal.close}>
              Not now
            </button>
            <button type="button" className="dm-btn" data-variant="solid" onClick={modal.close}>
              Choose a time
            </button>
          </div>
        </div>
      </dialog>

      <dialog className="dm-dialog sheet" aria-label="Menu" {...side.props}>
        <div className="dm-dialog-body">
          <div className="dm-dialog-actions" style={{ marginTop: 0 }}>
            <button type="button" className="dm-btn" onClick={side.close}>
              Close
            </button>
          </div>
          <nav className="dm-sheet-links" aria-label="Sample menu">
            {["Services", "Work", "About", "Contact"].map((l) => (
              <a key={l} href="#">
                {l}
              </a>
            ))}
          </nav>
        </div>
      </dialog>

      <dialog className="dm-dialog bottom-sheet" aria-labelledby="sheet-title" {...bottom.props}>
        <div className="dm-dialog-body">
          <h3 id="sheet-title">Filter</h3>
          <div className="dm-chips">
            {["In stock", "Under £50", "New in", "Best rated", "Free delivery"].map((c) => (
              <span key={c} className="dm-chip">
                {c}
              </span>
            ))}
          </div>
          <div className="dm-dialog-actions">
            <button type="button" className="dm-btn" data-variant="solid" onClick={bottom.close}>
              Show results
            </button>
          </div>
        </div>
      </dialog>
    </div>
  );
}

export default function DialogsSheetsEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="Dialogs and sheets">
      <DialogsDemo />
    </PieceStage>
  );
}

More like this

Cookie choice that folds away

FeedbackOverlays and toasts

Height that glides

Layout AnimationCards and accordions

One tooltip that glides

HoverOverlays and toasts

Photo grid that opens into the photo

Layout AnimationMedia and galleries

FAQ that opens smoothly

Layout AnimationCards and accordions

Reviews that take turns

Layout AnimationCards and accordions