import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from 'react' import { cx } from '../internal/cx.js' import { Portal } from '../internal/Portal.jsx' import { useLabels } from './labels.js' const GAP = 8 const MARGIN = 8 /** How long the pointer rests on the "i" before its tip opens (owner 2026-11-23, plan 6F.1a). * A click or a tap opens it at once. */ export const INFOTIP_HOVER_MS = 1500 /** * The "(i)" help tip next to a setting or label (D-12: every manager page gets them). * Opens when the pointer has rested on it for 1.5 s (INFOTIP_HOVER_MS; leaving earlier cancels it), and * at once on click / tap (it then stays open: touch screens). Closes on Escape, a click outside, a * second click, or the pointer leaving the "i" and the tip (a hover-opened tip). Placed above the * button, or below when there's no room. The same component in the manager, the waiter app and the * terminal (6F.1a). * * * * label overrides the button's accessible name (default: UiLabelsProvider's "moreInfo"). side: 'top' | 'bottom'. */ export function InfoTip({ children, label, side = 'top', className }) { const labels = useLabels() const [pinned, setPinned] = useState(false) const [hover, setHover] = useState(false) const leaveTimer = useRef(null) const enterTimer = useRef(null) const open = pinned || hover const setOpen = v => { clearTimeout(enterTimer.current); setPinned(v); if (!v) setHover(false) } // the pointer on the "i": open after the delay; on the open tip: keep it open const enter = () => { clearTimeout(leaveTimer.current) if (hover) return clearTimeout(enterTimer.current) enterTimer.current = setTimeout(() => setHover(true), INFOTIP_HOVER_MS) } const stay = () => clearTimeout(leaveTimer.current) const leave = () => { clearTimeout(enterTimer.current); clearTimeout(leaveTimer.current) leaveTimer.current = setTimeout(() => setHover(false), 120) } useEffect(() => () => { clearTimeout(leaveTimer.current); clearTimeout(enterTimer.current) }, []) const [pos, setPos] = useState(null) const button = useRef(null) const tip = useRef(null) const id = useId() const place = useCallback(() => { const b = button.current?.getBoundingClientRect() const t = tip.current?.getBoundingClientRect() if (!b || !t) return const roomAbove = b.top - GAP - MARGIN >= t.height const roomBelow = window.innerHeight - b.bottom - GAP - MARGIN >= t.height const above = side === 'top' ? (roomAbove || !roomBelow) : !(roomBelow || !roomAbove) const top = above ? b.top - GAP - t.height : b.bottom + GAP const left = Math.min(Math.max(MARGIN, b.left + b.width / 2 - t.width / 2), window.innerWidth - MARGIN - t.width) setPos({ top, left, above, arrow: b.left + b.width / 2 - left }) }, [side]) useLayoutEffect(() => { if (open) place() }, [open, place]) useEffect(() => { if (!open) return const close = () => setOpen(false) const onKey = e => { if (e.key === 'Escape') { e.stopPropagation(); close(); button.current?.focus() } } const onDown = e => { if (button.current?.contains(e.target) || tip.current?.contains(e.target)) return close() } document.addEventListener('keydown', onKey, true) document.addEventListener('pointerdown', onDown) window.addEventListener('resize', place) window.addEventListener('scroll', place, true) return () => { document.removeEventListener('keydown', onKey, true) document.removeEventListener('pointerdown', onDown) window.removeEventListener('resize', place) window.removeEventListener('scroll', place, true) } }, [open, place]) return ( <> {open && ( )} ) }