// — a badge that shows a value and, when clicked, opens a // popover under it holding the control that changes the value: a list of // checkboxes, a choice of modes, the entries behind a count. The control sits // next to the thing it changes instead of in a dialog away from it. // // A click on the badge toggles the popover; a click outside it or Escape // closes it (`Popover` does that). import { useRef, useState } from "preact/hooks"; import type { ComponentChildren } from "preact"; import { Badge, type BadgeTone } from "@hive/shared/badge.js"; import { Popover } from "../popover/Popover.js"; import "./BadgePopover.css"; export function BadgePopover({ label, value, icon, tone, title, ariaLabel, popoverLabel, children, }: { /** Dim prefix of the badge, e.g. "permissions". */ label?: ComponentChildren; /** What the badge says, e.g. "7 granted". */ value: ComponentChildren; /** A glyph before the value. */ icon?: ComponentChildren; tone?: BadgeTone; /** Tooltip on the badge: what clicking it does. */ title?: string; /** The badge's accessible name, when its text is not enough of one. */ ariaLabel?: string; /** `aria-label` of the floating box. */ popoverLabel: string; children: ComponentChildren; }) { const [open, setOpen] = useState(false); const anchorRef = useRef(null); return ( <> setOpen((o) => !o)} /> setOpen(false)} anchorRef={anchorRef} label={popoverLabel} >
{children}
); }