Files
xenia-pos-local/packages/ui/src/components/InfoTip.jsx
T
bonaminandClaude Opus 5.5 99145e83da feat(manager): help tips open after 1.5 s of hover, at once on click
Owner 2026-11-23 (plan 6F.1a): InfoTip waits INFOTIP_HOVER_MS (1500 ms) with the pointer on the i
before opening (leaving earlier cancels); a click or tap still opens it at once and keeps it open.
ui e2e checks the delay, the cancel and the click, and now runs without any backend (a Docker stack
left on :8000 in Greek made the i18n sample fail).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 06:34:55 +02:00

121 lines
5.1 KiB
React

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>Alert after failures <InfoTip>How many failed print attempts before managers are notified. 0 turns it off.</InfoTip></label>
*
* 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 (
<>
<button
ref={button}
type="button"
className={cx('x-infotip', open && 'x-infotip--open', className)}
aria-label={label ?? labels.moreInfo}
aria-expanded={open}
aria-describedby={open ? id : undefined}
onClick={() => setOpen(!pinned)}
onPointerEnter={e => { if (e.pointerType === 'mouse') enter() }}
onPointerLeave={e => { if (e.pointerType === 'mouse') leave() }}
>
<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true">
<circle cx="8" cy="8" r="7" fill="none" stroke="currentColor" strokeWidth="1.5" />
<circle cx="8" cy="4.75" r="1" fill="currentColor" />
<path d="M8 7.25v4.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />
</svg>
</button>
{open && (
<Portal>
<div
ref={tip}
id={id}
role="tooltip"
onPointerEnter={stay}
onPointerLeave={leave}
className={cx('x-infotip__popover', pos && (pos.above ? 'x-infotip__popover--above' : 'x-infotip__popover--below'))}
style={pos ? { top: pos.top, left: pos.left, '--x-arrow-x': `${pos.arrow}px` } : { top: 0, left: 0, visibility: 'hidden' }}
>
{children}
</div>
</Portal>
)}
</>
)
}