Try 'magnetic button'
View code

Number 35: Popover that closes faster than it opens

Specification

Catalogue
No. 35 of 110
Interaction
Press
Element
Overlay, Button
Preview
Live component
Imports
react 19.3.1
Added

Notes

A small menu that answers in 150ms and gets out of the way in 110ms.

Opening answers a request; closing should not make anyone wait. It hides only after its fade, so nothing invisible can be clicked.

Open 150ms ease-out. Close 110ms ease-in, lifts 6px, shrinks to 0.97. visibility hidden after the fade.

Code

React and plain CSS
"use client";

import { useEffect, useRef, useState } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "@/components/pieces/kit/pop.css";
import "./quick-popover.css";

function QuickPopoverDemo() {
  const [open, setOpen] = useState(false);
  const wrap = useRef<HTMLDivElement>(null);
  const trigger = useRef<HTMLButtonElement>(null);

  useEffect(() => {
    if (!open) return;
    const onDown = (e: PointerEvent) => {
      if (wrap.current && e.target instanceof Node && !wrap.current.contains(e.target)) setOpen(false);
    };
    const onKey = (e: KeyboardEvent) => {
      if (e.key === "Escape") {
        setOpen(false);
        trigger.current?.focus();
      }
    };
    document.addEventListener("pointerdown", onDown);
    document.addEventListener("keydown", onKey);
    return () => {
      document.removeEventListener("pointerdown", onDown);
      document.removeEventListener("keydown", onKey);
    };
  }, [open]);

  const choose = () => {
    setOpen(false);
    trigger.current?.focus();
  };

  return (
    <div className="dm" style={{ alignContent: "start", paddingTop: 48 }}>
      <div className="dm-pop-wrap" ref={wrap}>
        <button ref={trigger} type="button" className="dm-btn" aria-expanded={open} aria-haspopup="menu" onClick={() => setOpen((o) => !o)}>
          Share
          <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
            <path d="m6 9 6 6 6-6" />
          </svg>
        </button>
        <div className="dm-pop pop" role="menu" data-closed={!open || undefined} inert={!open || undefined}>
          {["Copy link", "Send by email", "Download PDF"].map((l) => (
            <button key={l} type="button" role="menuitem" onClick={choose}>
              {l}
            </button>
          ))}
        </div>
      </div>
    </div>
  );
}

// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
  { click: ".dm-btn", text: "Share", wait: 1600 },
  { click: '[role="menuitem"]', nth: 0, wait: 800 },
];

export default function QuickPopoverEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="Popover that closes faster than it opens" tour={TOUR}>
      <QuickPopoverDemo />
    </PieceStage>
  );
}

More like this

Delete that asks where you clicked

Press, FeedbackButtons and toggles

Button that shows its own progress

Press, LoadingButtons and toggles

Lit buttons and panels

PressButtons and toggles

Liquid metal ring

Hover, PressButtons and toggles

Cookie choice that folds away

FeedbackOverlays and toasts

Currency swap that trades places

Press, Layout AnimationForms and inputs