Tooltip and popover

A small toolbar with both: a dark hint that shows on hover and on keyboard focus, and a floating card that opens on a click and closes on Escape or a click outside.

Q4 launch planEdited 2 minutes ago

Copy link C

Options

Screen
Colour

Code

import { cloneElement, useEffect, useId, useRef, useState } from "react";
import "./tooltip.css";

/* A hint for one control. It shows on hover and on keyboard focus.

   peek shows it without a hover, as an example.

   <Tooltip text="Copy link"><button aria-label="Copy link">…</button></Tooltip> */
export function Tooltip({ text, keys, peek = false, children }) {
  const id = useId();
  return (
    <span className="dh-tip" data-peek={peek ? "" : undefined}>
      {cloneElement(children, { "aria-describedby": id })}
      <span className="dh-tip-bubble" role="tooltip" id={id}>
        {text} {keys && <kbd>{keys}</kbd>}
      </span>
    </span>
  );
}

/* A small panel under a button. It closes on Escape or a click outside.

   <Popover label="Share" title="Share this page">…</Popover> */
export function Popover({ label, title, note, defaultOpen = false, children }) {
  const [open, setOpen] = useState(defaultOpen);
  const root = useRef(null);
  const button = useRef(null);
  const id = useId();

  useEffect(() => {
    if (!open) return;
    const away = (event) => {
      if (!root.current.contains(event.target)) setOpen(false);
    };
    document.addEventListener("pointerdown", away);
    return () => document.removeEventListener("pointerdown", away);
  }, [open]);

  const keys = (event) => {
    if (event.key !== "Escape" || !open) return;
    setOpen(false);
    button.current.focus();
  };

  return (
    <span className="dh-pop" ref={root} onKeyDown={keys}>
      <button
        className="dh-tip-btn"
        type="button"
        data-tone="primary"
        ref={button}
        aria-expanded={open}
        aria-controls={id}
        onClick={() => setOpen(!open)}
      >
        {label}
      </button>
      <div className="dh-pop-panel" id={id} role="dialog" aria-label={title} data-open={open ? "" : undefined}>
        <strong>{title}</strong>
        {children}
        {note && <footer>{note}</footer>}
      </div>
    </span>
  );
}

export function Example() {
  const link = "example.com/q4-plan";
  const [label, setLabel] = useState("Copy");
  const [peek, setPeek] = useState(true);

  /* The label changes on the click, before the clipboard answers. */
  const copy = () => {
    setLabel("Copied");
    const failed = () => setLabel("Copy failed");
    try {
      navigator.clipboard.writeText(link).catch(failed);
    } catch {
      failed();
    }
    setTimeout(() => setLabel("Copy"), 1600);
  };

  return (
    <div className="dh-tip-demo" onPointerEnter={() => setPeek(false)}>
      <div className="dh-tip-bar">
        <i aria-hidden="true" />
        <p>
          <b>Q4 launch plan</b>
          <span>Edited 2 minutes ago</span>
        </p>
        <Tooltip text="Copy link" keys="C" peek={peek}>
          <button className="dh-tip-btn" type="button" data-icon="" aria-label="Copy link">
            <svg viewBox="0 0 20 20" aria-hidden="true">
              <path d="M7.500 2A2.500 2.500 0 0 0 5 4.500V5h6.500A3.500 3.500 0 0 1 15 8.500V15h.500a2.500 2.500 0 0 0 2.500-2.500v-8A2.500 2.500 0 0 0 15.500 2h-8Z" />
              <path d="M2 8.500A2.500 2.500 0 0 1 4.500 6h7A2.500 2.500 0 0 1 14 8.500v7a2.500 2.500 0 0 1-2.500 2.500h-7A2.500 2.500 0 0 1 2 15.500v-7Z" />
            </svg>
          </button>
        </Tooltip>
        <Popover
          label="Share"
          title="Share this page"
          note={
            <>
              Anyone with the link <em>Can view</em>
            </>
          }
          defaultOpen
        >
          <p>Send the link to anyone who should see it.</p>
          <div className="dh-pop-row">
            <input type="text" value={link} readOnly aria-label="Link" />
            <button className="dh-pop-copy" type="button" onClick={copy}>
              {label}
            </button>
          </div>
        </Popover>
      </div>
    </div>
  );
}

Q4 launch planEdited 2 minutes ago

Copy link C