Files
xenia-pos-local/packages/ui/src/components/Popover.jsx
T
bonaminandClaude Opus 5.5 aaa0914778 feat(signage): look rows that wrap, one colour field with opacity, badges' text, several widgets per stack part, pages up to 2200 px (owner review 4)
- Menu text look: each field names its part ("Headings size"), rows wrap in aligned columns on
  alternating bands; names align left / right; line and letter spacing as lists (Auto default);
  each field changes only itself (followers frozen before the other changes); badge colour +
  badge text colour (auto: readableOn); "Reset the look" with a question.
- ColourField everywhere in signage: a Popover panel (new @xenia/ui Popover, portal, kept in the
  window) with 12 presets, picker, opacity (#rrggbbaa) and typed code; HEX accepts 8 digits.
- Stacks: Cell.widgets (a stored `widget` still reads), several widgets per part laid across the
  stack's direction; extras by path stack.part.place; Add a widget adds, each removable / ordered.
- Page rule: --x-page-max is 2200px (no longer follows the window height).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 12:53:14 +02:00

66 lines
2.7 KiB
React

import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'
import { cx } from '../internal/cx.js'
import { Portal } from '../internal/Portal.jsx'
const GAP = 6
const MARGIN = 8
/**
* A small floating panel under a button (2026-11-21): a colour picker, an "Add…" menu. Drawn over
* everything (portal, fixed), like the Dropdown's list: below its button, above when there's no
* room, and always kept inside the window, so it never makes a page or a section scroll sideways.
* Closes on a click outside (the button excluded) and on Escape.
*
* const btn = useRef(null)
* <Button ref={btn} onClick={() => setOpen(o => !o)}>Add</Button>
* <Popover open={open} anchor={btn} onClose={() => setOpen(false)} role="menu">…</Popover>
*/
export function Popover({ open, anchor, onClose, className, style, children, role = 'dialog', ...rest }) {
const panel = useRef(null)
const [pos, setPos] = useState(null)
const place = useCallback(() => {
const b = anchor?.current?.getBoundingClientRect()
const p = panel.current?.getBoundingClientRect()
if (!b || !p) return
const below = window.innerHeight - b.bottom - GAP - MARGIN
const above = b.top - GAP - MARGIN
const up = p.height > below && above > below
const left = Math.min(Math.max(MARGIN, b.left), Math.max(MARGIN, window.innerWidth - MARGIN - p.width))
const top = up ? Math.max(MARGIN, b.top - GAP - p.height) : b.bottom + GAP
setPos({ left, top, maxHeight: Math.max(160, up ? above : below) })
}, [anchor])
useLayoutEffect(() => { if (open) place(); else setPos(null) }, [open, place])
useEffect(() => {
if (!open) return undefined
const away = e => {
if (panel.current?.contains(e.target) || anchor?.current?.contains(e.target)) return
onClose?.()
}
const esc = e => { if (e.key === 'Escape') { e.stopPropagation(); onClose?.() } }
const onScroll = e => { if (!panel.current?.contains(e.target)) place() }
document.addEventListener('pointerdown', away)
window.addEventListener('keydown', esc)
window.addEventListener('resize', place)
window.addEventListener('scroll', onScroll, true)
return () => {
document.removeEventListener('pointerdown', away)
window.removeEventListener('keydown', esc)
window.removeEventListener('resize', place)
window.removeEventListener('scroll', onScroll, true)
}
}, [open, anchor, onClose, place])
if (!open) return null
return (
<Portal>
<div ref={panel} role={role} className={cx('x-popover', className)} {...rest}
style={{ ...style, ...(pos ? { left: pos.left, top: pos.top, maxHeight: pos.maxHeight } : { left: 0, top: 0, visibility: 'hidden' }) }}>
{children}
</div>
</Portal>
)
}