Files
xenia-pos-local/packages/ui/src/components/InfoTip.jsx
T
bonaminandClaude Opus 5.5 dbb8d5f0f4 feat(i18n): @xenia/i18n translations, language resolution and venue-time formatting
Platform-v2 step 2.4 (APPS_PLATFORM D-11, D-12, §7.2, §8):
- i18next 26.7.2 + react-i18next 17.1.13 (exact); en/el locale files
  (common, dev); createI18n, I18nProvider, useLanguage, setUserLanguage
  (screen first, then PATCH /api/auth/me through an app-provided save)
- resolveLanguage: user -> device -> venue -> en
- useFormat(): dates/times always in the venue zone from bootstrap (24 h,
  DST-safe, business dates never shifted), relative times, money and
  numbers per language; parseNumber accepts a comma or a dot
- scripts/check-i18n.mjs guard: keys missing in en, placeholder mismatches,
  dates formatted without the venue zone in new code (fails); missing el (warns)
- @xenia/ui components read their texts from UiLabelsProvider; useUiLabels()
  supplies the translated set
- dev-only /i18n-sample in the manager; ui.mjs e2e checks it with the browser
  in New York; unit tests in run-all; lockfile keeps both apps' versions
- new pack rule CS-17

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 19:51:11 +02:00

93 lines
3.6 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
/**
* The "(i)" help tip next to a setting or label (D-12: every manager page gets them).
* Opens on click/tap (works on touch screens), closes on Escape, a click outside, or
* a second click. Placed above the button, or below when there's no room.
*
* <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 [open, setOpen] = useState(false)
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(o => !o)}
>
<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"
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>
)}
</>
)
}