import React, { type HTMLAttributes } from 'react' import { FocusScope } from '@react-aria/focus' import { DismissButton, useOverlay } from '@react-aria/overlays' import { type OverrideClassName } from '~components/types/OverrideClassName' import { useSelectContext } from '../../context' import { type SingleSelectOption } from '../../types' export type OverlayProps = OverrideClassName> & { children: React.ReactNode popoverRef?: React.RefObject } export const Overlay = ({ children, classNameOverride, popoverRef, ...restProps }: OverlayProps): JSX.Element => { const { state } = useSelectContext() // Handle events that should cause the menu to close, // e.g. blur, clicking outside, or pressing the escape key. const overlayRef = React.useRef(null) const { overlayProps } = useOverlay( { isDismissable: true, isOpen: state.isOpen, onClose: state.close }, popoverRef ?? overlayRef, ) // Wrap in so that focus is restored back to the trigger when the menu is closed // and auto focus on the first focusable item after loading. (disable eslint no-autofocus error for it) // In addition, add hidden components at the start and end of the list // to allow screen reader users to dismiss the popup easily. return ( {/* eslint-disable-next-line jsx-a11y/no-autofocus */} {children} ) } Overlay.displayName = 'Overlay'