import { Button } from "@repo/shadcn-ui/components/button"; import { Popover, PopoverContent, PopoverTitle, PopoverTrigger, } from "@repo/shadcn-ui/components/popover"; import type { ReactNode } from "react"; import { useContext, useEffect, useId, useRef } from "react"; import { createPortal } from "react-dom"; import { DialogHelpContext, DialogHelpLabelsContext } from "../lib/dialog-help-context"; import type { DialogHelpPresentation } from "../lib/dialog-help-presentation"; import { CircleHelpIcon } from "../lib/icons"; /** Slot declarativo: los portales conservan contexto, traducciones y una Ășnica copia del contenido. */ export function DialogHelp({ title, children, presentation = "auto", }: { title: string; children: ReactNode; presentation?: DialogHelpPresentation; }) { const dialog = useContext(DialogHelpContext); const labels = useContext(DialogHelpLabelsContext); const contentId = useId(); const titleId = useId(); const headingRef = useRef(null); const wasOpen = useRef(false); const triggerRef = dialog?.triggerRef; const open = dialog?.open ?? false; const mode = dialog?.mode; // El destino cambia al redimensionar. El foco debe salir del formulario antes de ocultarlo. useEffect(() => { if (open && mode !== "popover") { headingRef.current?.focus(); } else if (!open && wasOpen.current) { triggerRef?.current?.focus(); } wasOpen.current = open; }, [open, mode, triggerRef]); if (!(dialog?.actionsHost && dialog.contentHost)) { return null; } const close = () => { dialog.setPresentation(null); dialog.triggerRef.current?.focus(); }; const trigger = ( ); return ( <> {createPortal( mode === "popover" || (!open && presentation === "compact") ? ( dialog.setPresentation(next ? presentation : null)} open={open && mode === "popover"} > {title} {children} ) : ( trigger ), dialog.actionsHost )} {createPortal( open && mode !== "popover" ? (

{title}

{children}
) : null, dialog.contentHost )} ); }