mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
508e17bc11
The full screen chat is a modal dialog but left the focus on the trip behind it. It now takes the focus when shown, keeps Tab inside and hands the focus back when set aside or closed, through the helper MSheet now shares. The dropdown panels take the focus onto their first entry, close on Escape and give the focus back to their button; the history and menu buttons say whether their panel is open, and the open chat is announced as current.
61 lines
2.4 KiB
TypeScript
61 lines
2.4 KiB
TypeScript
import { ReactNode, useEffect, useLayoutEffect, useRef } from 'react'
|
|
|
|
interface MDropdownPanelProps {
|
|
open: boolean
|
|
onClose: () => void
|
|
/** Positioning classes (e.g. `top-[100px] end-4`); panel is fixed. */
|
|
className?: string
|
|
children: ReactNode
|
|
}
|
|
|
|
/**
|
|
* Floating popover panel (user menu, filter dropdowns): heavy glass, r20,
|
|
* closed by a transparent full-screen backdrop — no dim, unlike sheets.
|
|
* Layered above the scrolling content but below the bottom nav (z-40) and
|
|
* sheets (z-60), matching the design's stacking order. Opened, it takes the
|
|
* focus onto its first button and Escape closes it; closed, the focus goes
|
|
* back to the button it was opened from, unless a pick moved it on.
|
|
*/
|
|
export default function MDropdownPanel({ open, onClose, className = '', children }: MDropdownPanelProps) {
|
|
const panelRef = useRef<HTMLDivElement>(null)
|
|
const onCloseRef = useRef(onClose)
|
|
useLayoutEffect(() => {
|
|
onCloseRef.current = onClose
|
|
})
|
|
|
|
useEffect(() => {
|
|
if (!open) return
|
|
const opener = document.activeElement instanceof HTMLElement ? document.activeElement : null
|
|
panelRef.current?.querySelector<HTMLElement>('button:not(:disabled)')?.focus({ preventScroll: true })
|
|
// Captured and stopped, so the key closes the panel and nothing under it.
|
|
const onKey = (e: KeyboardEvent) => {
|
|
if (e.key !== 'Escape') return
|
|
e.stopPropagation()
|
|
onCloseRef.current()
|
|
}
|
|
document.addEventListener('keydown', onKey, true)
|
|
return () => {
|
|
document.removeEventListener('keydown', onKey, true)
|
|
// The focused button left with the panel: the focus fell back to the page, and goes to the opener.
|
|
const lost = document.activeElement === null || document.activeElement === document.body
|
|
if (lost && opener?.isConnected) opener.focus({ preventScroll: true })
|
|
}
|
|
}, [open])
|
|
|
|
if (!open) return null
|
|
|
|
return (
|
|
<>
|
|
{/* Invisible dismiss layer — decoration to a reader, which is why it carries
|
|
no role of its own; the panel's own controls are the keyboard path. */}
|
|
<div className="fixed inset-0 z-[35]" role="presentation" onClick={onClose} />
|
|
<div
|
|
ref={panelRef}
|
|
className={`m-pop-in fixed z-[36] w-[228px] rounded-[20px] border border-[color:var(--m-gbr)] bg-[color:var(--m-glass)] p-[6px] text-m-ink shadow-[0_24px_60px_-20px_rgba(0,0,0,.5)] backdrop-blur-[30px] backdrop-saturate-[1.8] ${className}`}
|
|
>
|
|
{children}
|
|
</div>
|
|
</>
|
|
)
|
|
}
|