refactor: popover a11y aria attributes

This commit is contained in:
Aman Harwara
2023-08-14 14:42:33 +05:30
parent e1c5d52dbe
commit 4865e3ba28
31 changed files with 110 additions and 88 deletions

View File

@@ -35,7 +35,8 @@ const PositionedPopoverContent = ({
}: PopoverContentProps) => {
const [popoverElement, setPopoverElement] = useState<HTMLDivElement | null>(null)
const popoverRect = useAutoElementRect(popoverElement)
const anchorElementRect = useAutoElementRect(anchorElement, {
const resolvedAnchorElement = anchorElement && 'current' in anchorElement ? anchorElement.current : anchorElement
const anchorElementRect = useAutoElementRect(resolvedAnchorElement, {
updateOnWindowResize: true,
})
const anchorPointRect = DOMRect.fromRect({
@@ -75,7 +76,7 @@ const PositionedPopoverContent = ({
usePopoverCloseOnClickOutside({
popoverElement,
anchorElement,
anchorElement: resolvedAnchorElement,
togglePopover,
childPopovers,
hideOnClickInModal,
@@ -122,13 +123,14 @@ const PositionedPopoverContent = ({
} as CSSProperties
}
ref={mergeRefs([setPopoverElement, addCloseMethod])}
id={'popover/' + id}
data-popover={id}
onKeyDown={(event) => {
if (event.key === KeyboardKey.Escape) {
event.stopPropagation()
togglePopover?.()
if (anchorElement) {
anchorElement.focus()
if (resolvedAnchorElement) {
resolvedAnchorElement.focus()
}
}
}}