121 lines
4.2 KiB
TypeScript
121 lines
4.2 KiB
TypeScript
import { useEffect, useId, useRef, useState } from 'react'
|
|
import type { KeyboardEvent, RefObject } from 'react'
|
|
import { Ellipsis, UserMinus } from 'lucide-react'
|
|
import { useAttendeeDetail } from './AttendeeDetailProvider'
|
|
|
|
const ITEM_CLASS =
|
|
'flex min-h-11 w-full items-center gap-2.5 rounded-lg px-3 text-left text-sm font-medium text-primary transition-colors hover:bg-danger-soft focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent'
|
|
|
|
/** Menú "⋯" de la ficha del miembro.
|
|
*
|
|
* Por ahora solo guarda "Quitar del grupo": el backend no tiene forma de
|
|
* regenerar el token de ausencias, así que la opción no se muestra antes que
|
|
* exista el endpoint (ver `apps/backend/prisma/models/domain.prisma`, campo
|
|
* `notifyToken`). Cuando exista, es un `<button role="menuitem">` más en la
|
|
* lista de abajo.
|
|
*
|
|
* La etiqueta va en `text-primary` y el ícono lleva el `text-danger`: el rojo
|
|
* de `danger` sobre blanco queda en 3.7:1 y no llega a AA para texto normal. */
|
|
export function AttendeeActionsMenu({
|
|
triggerRef,
|
|
}: {
|
|
/** Se lo pasa la pantalla para que el diálogo de confirmación le devuelva el
|
|
* foco cuando se cierra. */
|
|
triggerRef: RefObject<HTMLButtonElement | null>
|
|
}) {
|
|
const { openRemoveDialog } = useAttendeeDetail()
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
const menuRef = useRef<HTMLDivElement>(null)
|
|
const itemRef = useRef<HTMLButtonElement>(null)
|
|
const menuId = useId()
|
|
|
|
useEffect(() => {
|
|
if (isOpen) itemRef.current?.focus()
|
|
}, [isOpen])
|
|
|
|
useEffect(() => {
|
|
if (!isOpen) return
|
|
const handlePointerDown = (event: PointerEvent) => {
|
|
const target = event.target as Node
|
|
if (menuRef.current?.contains(target) || triggerRef.current?.contains(target)) return
|
|
setIsOpen(false)
|
|
}
|
|
document.addEventListener('pointerdown', handlePointerDown)
|
|
return () => document.removeEventListener('pointerdown', handlePointerDown)
|
|
}, [isOpen, triggerRef])
|
|
|
|
const close = (restoreFocus = false) => {
|
|
setIsOpen(false)
|
|
if (restoreFocus) triggerRef.current?.focus()
|
|
}
|
|
|
|
const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
|
|
if (event.key === 'Escape') {
|
|
event.stopPropagation()
|
|
close(true)
|
|
return
|
|
}
|
|
if (event.key === 'Tab') {
|
|
close()
|
|
return
|
|
}
|
|
if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return
|
|
|
|
event.preventDefault()
|
|
const items = Array.from(
|
|
menuRef.current?.querySelectorAll<HTMLElement>('[role="menuitem"]') ?? [],
|
|
)
|
|
if (items.length === 0) return
|
|
const current = items.indexOf(document.activeElement as HTMLElement)
|
|
const next =
|
|
event.key === 'Home'
|
|
? 0
|
|
: event.key === 'End'
|
|
? items.length - 1
|
|
: (current + (event.key === 'ArrowDown' ? 1 : -1) + items.length) % items.length
|
|
items[next]?.focus()
|
|
}
|
|
|
|
return (
|
|
<div className="relative shrink-0">
|
|
<button
|
|
ref={triggerRef}
|
|
type="button"
|
|
onClick={() => setIsOpen((value) => !value)}
|
|
aria-haspopup="menu"
|
|
aria-expanded={isOpen}
|
|
aria-controls={isOpen ? menuId : undefined}
|
|
aria-label="Más acciones"
|
|
className="inline-flex size-11 items-center justify-center rounded-xl text-primary transition-colors hover:bg-primary-soft active:scale-[0.96] motion-reduce:active:scale-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent"
|
|
>
|
|
<Ellipsis className="size-5" aria-hidden="true" />
|
|
</button>
|
|
|
|
{isOpen ? (
|
|
<div
|
|
ref={menuRef}
|
|
id={menuId}
|
|
role="menu"
|
|
aria-label="Acciones del miembro"
|
|
onKeyDown={handleKeyDown}
|
|
className="absolute right-0 top-full z-40 mt-1 w-60 rounded-xl bg-primary-soft p-1.5 shadow-lg"
|
|
>
|
|
<button
|
|
ref={itemRef}
|
|
type="button"
|
|
role="menuitem"
|
|
className={ITEM_CLASS}
|
|
onClick={() => {
|
|
close()
|
|
openRemoveDialog()
|
|
}}
|
|
>
|
|
<UserMinus className="size-4 shrink-0 text-danger" aria-hidden="true" />
|
|
Quitar del grupo
|
|
</button>
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
)
|
|
}
|