import { useEffect, useRef } from 'react'; import { useRequestAnimationFrame } from '../animation/RequestAnimationFrameContext'; import TrapFocusBehavior from '../behaviors/TrapFocusBehavior'; import Box from '../Box'; import Button from '../Button'; import { useDefaultLabelContext } from '../contexts/DefaultLabelProvider'; import Flex from '../Flex'; import { ESCAPE } from '../keyCodes'; import InternalPopover from '../Popover/InternalPopover'; import Text from '../Text'; type Props = { anchor: HTMLElement | null | undefined; message?: string; onDismiss: () => void; primaryAction?: { accessibilityLabel?: string; text?: string; onClick?: (arg1: { event: | React.MouseEvent | React.MouseEvent | React.KeyboardEvent | React.KeyboardEvent; }) => void; }; secondaryAction?: { accessibilityLabel?: string; text?: string; onClick?: (arg1: { event: | React.MouseEvent | React.MouseEvent | React.KeyboardEvent | React.KeyboardEvent; }) => void; }; subtext?: string; }; export default function ConfirmationPopover({ anchor, message, subtext, primaryAction, secondaryAction, onDismiss, }: Props) { const confirmationButtonRef = useRef(null); const { onExternalDismiss } = useRequestAnimationFrame(); const { dismissConfirmationMessage: messageDefault, dismissConfirmationSubtext: subtextDefault, dismissConfirmationPrimaryActionText: primaryActionTextDefault, dismissConfirmationSecondaryActionText: secondaryActionTextDefault, dismissConfirmationPrimaryActionTextLabel: primaryActionTextLabelDefault, dismissConfirmationSecondaryActionTextLabel: secondaryActionTextLabelDefault, } = useDefaultLabelContext('OverlayPanel'); useEffect(() => { confirmationButtonRef?.current?.focus(); }, [confirmationButtonRef]); // Handle onDismiss triggering from ESC keyup event useEffect(() => { function handleKeyDown(event: React.KeyboardEvent) { if (event.keyCode === ESCAPE) event.stopPropagation(); } // @ts-expect-error - TS2769 - No overload matches this call. window.addEventListener('keydown', handleKeyDown); return function cleanup() { // @ts-expect-error - TS2769 - No overload matches this call. window.removeEventListener('keydown', handleKeyDown); }; }, []); return ( onDismiss()} role="dialog" shouldFocus showCaret={false} size="md" > {message ?? messageDefault} {subtext ?? subtextDefault}