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.
Options
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>
);
}/* A tooltip and a popover on one small toolbar. The tooltip is a dark hint
that shows on hover and on keyboard focus, with no script. The popover is a
floating card that opens on a click. */
.dh-tip-demo {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ring: 0 0 0 1px var(--line), 0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
width: 400px;
max-width: 100%;
padding: 36px 0 180px;
color: var(--dh-fg, #171717);
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-tip-bar {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 10px 10px 12px;
border-radius: 12px;
background: var(--dh-raised, #fff);
box-shadow: var(--ring);
}
/* The page's picture: a small accent tile that thins out into dots. */
.dh-tip-bar > i {
flex: none;
width: 32px;
height: 32px;
border-radius: 8px;
background: linear-gradient(color-mix(in srgb, var(--accent) 82%, #000), var(--accent));
-webkit-mask-image: linear-gradient(#000 30%, transparent), radial-gradient(circle, #000 1.1px, transparent 1.4px);
mask-image: linear-gradient(#000 30%, transparent), radial-gradient(circle, #000 1.1px, transparent 1.4px);
-webkit-mask-size: auto, 4px 4px;
mask-size: auto, 4px 4px;
}
.dh-tip-bar > p {
display: grid;
flex: 1;
min-width: 0;
margin: 0 0 0 2px;
}
.dh-tip-bar b {
overflow: hidden;
font-weight: 500;
line-height: 18px;
letter-spacing: -0.01em;
text-overflow: ellipsis;
white-space: nowrap;
}
.dh-tip-bar p span {
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
line-height: 16px;
}
.dh-tip,
.dh-pop {
position: relative;
display: inline-flex;
flex: none;
}
.dh-tip-btn,
.dh-pop-copy {
display: inline-flex;
align-items: center;
justify-content: center;
height: 32px;
padding: 0 12px;
border: 0;
border-radius: 8px;
background: var(--dh-raised, #fff);
color: inherit;
font: inherit;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
box-shadow: var(--ring);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-tip-btn[data-icon] {
width: 32px;
padding: 0;
color: var(--dh-muted, #5c5c5c);
}
.dh-tip-btn[data-tone="primary"] {
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
color: var(--dh-on-accent, #fff);
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
}
.dh-tip-btn svg {
width: 14px;
height: 14px;
fill: currentColor;
}
.dh-tip-btn:active,
.dh-pop-copy:active {
transform: scale(0.97);
}
.dh-tip-demo :is(button, input):focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 1px;
}
/* The tooltip sits above its button and never takes the pointer. */
.dh-tip-bubble {
position: absolute;
bottom: calc(100% + 8px);
left: 50%;
display: flex;
align-items: center;
gap: 6px;
height: 26px;
padding: 0 4px 0 8px;
border-radius: 7px;
background: var(--dh-fg, #171717);
color: var(--dh-bg, #f7f7f6);
font-size: 12px;
font-weight: 500;
letter-spacing: -0.01em;
white-space: nowrap;
pointer-events: none;
opacity: 0;
transform: translateX(-50%) translateY(2px) scale(0.97);
transform-origin: 50% 100%;
box-shadow: 0 1px 2px rgb(0 0 0 / 0.2), 0 8px 16px -8px rgb(0 0 0 / 0.4);
transition:
opacity 120ms cubic-bezier(0.23, 1, 0.32, 1),
transform 120ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-tip-bubble kbd {
display: grid;
place-items: center;
min-width: 18px;
height: 18px;
padding: 0 4px;
border-radius: 4px;
background: color-mix(in srgb, var(--dh-bg, #f7f7f6) 18%, transparent);
font: 500 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
/* data-peek shows the hint before anyone has pointed at the toolbar. */
.dh-tip:focus-within .dh-tip-bubble,
.dh-tip[data-peek] .dh-tip-bubble {
opacity: 1;
transform: translateX(-50%);
transition-duration: 150ms;
}
@media (hover: hover) and (pointer: fine) {
.dh-tip:hover .dh-tip-bubble {
opacity: 1;
transform: translateX(-50%);
transition-duration: 150ms;
transition-delay: 300ms;
}
}
/* The popover hangs from the right edge of its button. */
.dh-pop-panel {
position: absolute;
top: calc(100% + 16px);
right: 0;
z-index: 1;
overflow: hidden;
width: 264px;
padding: 12px;
border-radius: 12px;
background: var(--dh-raised, #fff);
box-shadow: var(--ring), 0 14px 28px -14px rgb(0 0 0 / 0.28);
opacity: 0;
visibility: hidden;
transform: scale(0.97);
transform-origin: 100% 0;
transition:
opacity 120ms cubic-bezier(0.23, 1, 0.32, 1),
transform 120ms cubic-bezier(0.23, 1, 0.32, 1),
visibility 0s 120ms;
}
.dh-pop-panel[data-open] {
opacity: 1;
visibility: visible;
transform: none;
transition-duration: 150ms, 150ms, 0s;
transition-delay: 0s;
}
.dh-pop-panel strong {
display: block;
font-size: 14px;
font-weight: 500;
letter-spacing: -0.015em;
}
.dh-pop-panel p {
margin: 0 0 10px;
color: var(--dh-muted, #5c5c5c);
}
.dh-pop-row {
display: flex;
gap: 8px;
}
.dh-pop-row input {
flex: 1;
min-width: 0;
height: 28px;
padding: 0 8px;
border: 0;
border-radius: 7px;
background: var(--dh-well, #ededeb);
color: var(--dh-muted, #5c5c5c);
font: 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.dh-pop-copy {
flex: none;
height: 28px;
padding: 0 10px;
border-radius: 7px;
}
.dh-pop-panel footer {
display: flex;
align-items: center;
justify-content: space-between;
margin: 12px -12px -12px;
padding: 8px 12px;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
line-height: 16px;
}
.dh-pop-panel em {
padding: 1px 5px;
border-radius: 4px;
background: color-mix(in srgb, var(--accent) 12%, transparent);
color: color-mix(in srgb, var(--accent) 80%, var(--dh-fg, #171717));
font-size: 11px;
font-style: normal;
font-weight: 500;
}
@media (prefers-reduced-motion: reduce) {
.dh-tip-bubble,
.dh-tip:focus-within .dh-tip-bubble,
.dh-tip[data-peek] .dh-tip-bubble {
transform: translateX(-50%);
}
.dh-pop-panel,
.dh-pop-panel[data-open] {
transform: none;
}
.dh-tip-btn,
.dh-pop-copy {
transition: none;
}
}<div class="dh-tip-demo">
<div class="dh-tip-bar">
<i aria-hidden="true"></i>
<p><b>Q4 launch plan</b><span>Edited 2 minutes ago</span></p>
<span class="dh-tip" data-peek>
<button class="dh-tip-btn" type="button" data-icon aria-label="Copy link" aria-describedby="dh-tip-copy">
<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>
<span class="dh-tip-bubble" role="tooltip" id="dh-tip-copy">Copy link <kbd>C</kbd></span>
</span>
<span class="dh-pop">
<button class="dh-tip-btn" type="button" data-tone="primary" aria-expanded="true" aria-controls="dh-pop-share">Share</button>
<div class="dh-pop-panel" id="dh-pop-share" role="dialog" aria-label="Share this page" data-open>
<strong>Share this page</strong>
<p>Send the link to anyone who should see it.</p>
<div class="dh-pop-row">
<input type="text" value="example.com/q4-plan" readonly aria-label="Link" />
<button class="dh-pop-copy" type="button">Copy</button>
</div>
<footer>Anyone with the link <em>Can view</em></footer>
</div>
</span>
</div>
</div>/* A tooltip and a popover on one small toolbar. The tooltip is a dark hint
that shows on hover and on keyboard focus, with no script. The popover is a
floating card that opens on a click. */
.dh-tip-demo {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ring: 0 0 0 1px var(--line), 0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
width: 400px;
max-width: 100%;
padding: 36px 0 180px;
color: var(--dh-fg, #171717);
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-tip-bar {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 10px 10px 12px;
border-radius: 12px;
background: var(--dh-raised, #fff);
box-shadow: var(--ring);
}
/* The page's picture: a small accent tile that thins out into dots. */
.dh-tip-bar > i {
flex: none;
width: 32px;
height: 32px;
border-radius: 8px;
background: linear-gradient(color-mix(in srgb, var(--accent) 82%, #000), var(--accent));
-webkit-mask-image: linear-gradient(#000 30%, transparent), radial-gradient(circle, #000 1.1px, transparent 1.4px);
mask-image: linear-gradient(#000 30%, transparent), radial-gradient(circle, #000 1.1px, transparent 1.4px);
-webkit-mask-size: auto, 4px 4px;
mask-size: auto, 4px 4px;
}
.dh-tip-bar > p {
display: grid;
flex: 1;
min-width: 0;
margin: 0 0 0 2px;
}
.dh-tip-bar b {
overflow: hidden;
font-weight: 500;
line-height: 18px;
letter-spacing: -0.01em;
text-overflow: ellipsis;
white-space: nowrap;
}
.dh-tip-bar p span {
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
line-height: 16px;
}
.dh-tip,
.dh-pop {
position: relative;
display: inline-flex;
flex: none;
}
.dh-tip-btn,
.dh-pop-copy {
display: inline-flex;
align-items: center;
justify-content: center;
height: 32px;
padding: 0 12px;
border: 0;
border-radius: 8px;
background: var(--dh-raised, #fff);
color: inherit;
font: inherit;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
box-shadow: var(--ring);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-tip-btn[data-icon] {
width: 32px;
padding: 0;
color: var(--dh-muted, #5c5c5c);
}
.dh-tip-btn[data-tone="primary"] {
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
color: var(--dh-on-accent, #fff);
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
}
.dh-tip-btn svg {
width: 14px;
height: 14px;
fill: currentColor;
}
.dh-tip-btn:active,
.dh-pop-copy:active {
transform: scale(0.97);
}
.dh-tip-demo :is(button, input):focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 1px;
}
/* The tooltip sits above its button and never takes the pointer. */
.dh-tip-bubble {
position: absolute;
bottom: calc(100% + 8px);
left: 50%;
display: flex;
align-items: center;
gap: 6px;
height: 26px;
padding: 0 4px 0 8px;
border-radius: 7px;
background: var(--dh-fg, #171717);
color: var(--dh-bg, #f7f7f6);
font-size: 12px;
font-weight: 500;
letter-spacing: -0.01em;
white-space: nowrap;
pointer-events: none;
opacity: 0;
transform: translateX(-50%) translateY(2px) scale(0.97);
transform-origin: 50% 100%;
box-shadow: 0 1px 2px rgb(0 0 0 / 0.2), 0 8px 16px -8px rgb(0 0 0 / 0.4);
transition:
opacity 120ms cubic-bezier(0.23, 1, 0.32, 1),
transform 120ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-tip-bubble kbd {
display: grid;
place-items: center;
min-width: 18px;
height: 18px;
padding: 0 4px;
border-radius: 4px;
background: color-mix(in srgb, var(--dh-bg, #f7f7f6) 18%, transparent);
font: 500 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
/* data-peek shows the hint before anyone has pointed at the toolbar. */
.dh-tip:focus-within .dh-tip-bubble,
.dh-tip[data-peek] .dh-tip-bubble {
opacity: 1;
transform: translateX(-50%);
transition-duration: 150ms;
}
@media (hover: hover) and (pointer: fine) {
.dh-tip:hover .dh-tip-bubble {
opacity: 1;
transform: translateX(-50%);
transition-duration: 150ms;
transition-delay: 300ms;
}
}
/* The popover hangs from the right edge of its button. */
.dh-pop-panel {
position: absolute;
top: calc(100% + 16px);
right: 0;
z-index: 1;
overflow: hidden;
width: 264px;
padding: 12px;
border-radius: 12px;
background: var(--dh-raised, #fff);
box-shadow: var(--ring), 0 14px 28px -14px rgb(0 0 0 / 0.28);
opacity: 0;
visibility: hidden;
transform: scale(0.97);
transform-origin: 100% 0;
transition:
opacity 120ms cubic-bezier(0.23, 1, 0.32, 1),
transform 120ms cubic-bezier(0.23, 1, 0.32, 1),
visibility 0s 120ms;
}
.dh-pop-panel[data-open] {
opacity: 1;
visibility: visible;
transform: none;
transition-duration: 150ms, 150ms, 0s;
transition-delay: 0s;
}
.dh-pop-panel strong {
display: block;
font-size: 14px;
font-weight: 500;
letter-spacing: -0.015em;
}
.dh-pop-panel p {
margin: 0 0 10px;
color: var(--dh-muted, #5c5c5c);
}
.dh-pop-row {
display: flex;
gap: 8px;
}
.dh-pop-row input {
flex: 1;
min-width: 0;
height: 28px;
padding: 0 8px;
border: 0;
border-radius: 7px;
background: var(--dh-well, #ededeb);
color: var(--dh-muted, #5c5c5c);
font: 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.dh-pop-copy {
flex: none;
height: 28px;
padding: 0 10px;
border-radius: 7px;
}
.dh-pop-panel footer {
display: flex;
align-items: center;
justify-content: space-between;
margin: 12px -12px -12px;
padding: 8px 12px;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
line-height: 16px;
}
.dh-pop-panel em {
padding: 1px 5px;
border-radius: 4px;
background: color-mix(in srgb, var(--accent) 12%, transparent);
color: color-mix(in srgb, var(--accent) 80%, var(--dh-fg, #171717));
font-size: 11px;
font-style: normal;
font-weight: 500;
}
@media (prefers-reduced-motion: reduce) {
.dh-tip-bubble,
.dh-tip:focus-within .dh-tip-bubble,
.dh-tip[data-peek] .dh-tip-bubble {
transform: translateX(-50%);
}
.dh-pop-panel,
.dh-pop-panel[data-open] {
transform: none;
}
.dh-tip-btn,
.dh-pop-copy {
transition: none;
}
}/* The tooltip needs no script, apart from hiding the one shown as an example
once someone points at the toolbar. The rest opens and closes the popover,
and makes its Copy button work. */
document.querySelectorAll(".dh-tip-demo").forEach((demo) => {
demo.addEventListener(
"pointerenter",
() => demo.querySelectorAll("[data-peek]").forEach((tip) => tip.removeAttribute("data-peek")),
{ once: true },
);
});
document.querySelectorAll(".dh-pop").forEach((pop) => {
const button = pop.querySelector(".dh-tip-btn");
const panel = pop.querySelector(".dh-pop-panel");
const copy = pop.querySelector(".dh-pop-copy");
const set = (open) => {
panel.toggleAttribute("data-open", open);
button.setAttribute("aria-expanded", String(open));
};
button.addEventListener("click", () => {
const open = !panel.hasAttribute("data-open");
set(open);
if (open) panel.querySelector("input").select();
});
document.addEventListener("pointerdown", (event) => {
if (!pop.contains(event.target)) set(false);
});
pop.addEventListener("keydown", (event) => {
if (event.key !== "Escape" || !panel.hasAttribute("data-open")) return;
set(false);
button.focus();
});
/* The label changes on the click, before the clipboard answers. */
copy.addEventListener("click", () => {
copy.textContent = "Copied";
const failed = () => (copy.textContent = "Copy failed");
try {
navigator.clipboard.writeText(panel.querySelector("input").value).catch(failed);
} catch {
failed();
}
setTimeout(() => (copy.textContent = "Copy"), 1600);
});
});Tooltip and popover is not in the Figma file yet.
Use the React or HTML code for now.