Skip to content

Commit

Permalink
Merge pull request #5858 from matuzalemsteles/drop-down-inert
Browse files Browse the repository at this point in the history
fix(@clayui/drop-down): fixes bug with chrome blocking element focus when clicking outside the menu
  • Loading branch information
matuzalemsteles authored Aug 20, 2024
2 parents 164e1fa + d2610ef commit 12a89bb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 34 deletions.
1 change: 1 addition & 0 deletions packages/clay-drop-down/src/DropDown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ function ClayDropDown<T>({
navigationProps.onKeyDown(event);
}}
ref={menuElementRef}
suppress={[triggerElementRef, menuElementRef]}
triggerRef={triggerElementRef}
width={menuWidth}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

exports[`ClayDropDownWithDrilldown renders 1`] = `
<body>
<div
aria-hidden="true"
data-aria-hidden="true"
>
<div>
<div
class="dropdown"
>
Expand Down Expand Up @@ -216,10 +213,7 @@ exports[`ClayDropDownWithDrilldown renders 1`] = `

exports[`ClayDropDownWithDrilldown renders dividers 1`] = `
<body>
<div
aria-hidden="true"
data-aria-hidden="true"
>
<div>
<div
class="dropdown"
>
Expand Down
20 changes: 4 additions & 16 deletions packages/clay-drop-down/src/__tests__/__snapshots__/index.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

exports[`ClayDropDown renders dropdown menu when clicked 1`] = `
<body>
<div
aria-hidden="true"
data-aria-hidden="true"
>
<div>
<div
class="dropdown"
>
Expand Down Expand Up @@ -77,10 +74,7 @@ exports[`ClayDropDown renders dropdown menu when clicked 1`] = `

exports[`ClayDropDown renders with groups 1`] = `
<body>
<div
aria-hidden="true"
data-aria-hidden="true"
>
<div>
<div
class="dropdown"
>
Expand Down Expand Up @@ -224,10 +218,7 @@ exports[`ClayDropDown renders with groups 1`] = `

exports[`ClayDropDown renders with icons 1`] = `
<body>
<div
aria-hidden="true"
data-aria-hidden="true"
>
<div>
<div
class="dropdown"
>
Expand Down Expand Up @@ -344,10 +335,7 @@ exports[`ClayDropDown renders with icons 1`] = `

exports[`ClayDropDown renders with search input 1`] = `
<body>
<div
aria-hidden="true"
data-aria-hidden="true"
>
<div>
<div
class="dropdown"
>
Expand Down
27 changes: 17 additions & 10 deletions packages/clay-shared/src/Overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {Undo} from 'aria-hidden';

type Props = {
children: React.ReactElement;
inert?: boolean;
isCloseOnInteractOutside?: boolean;
isKeyboardDismiss?: boolean;
isModal?: boolean;
Expand All @@ -35,6 +36,7 @@ const overlayStack: Array<React.RefObject<Element>> = [];
*/
export function Overlay({
children,
inert,
isCloseOnInteractOutside = false,
isKeyboardDismiss = false,
isModal = false,
Expand Down Expand Up @@ -116,6 +118,11 @@ export function Overlay({
onHide('blur');
},
onInteractStart: (event) => {
if (unsuppressCallbackRef.current) {
unsuppressCallbackRef.current();
unsuppressCallbackRef.current = null;
}

if (overlayStack[overlayStack.length - 1] === menuRef && isModal) {
event.stopPropagation();
event.preventDefault();
Expand Down Expand Up @@ -148,20 +155,20 @@ export function Overlay({
// Inert is a new native feature to better handle DOM arias that are not
// assertive to a SR or that should ignore any user interaction.
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/inert
if (isModal && supportsInert()) {
if ((isModal || inert) && supportsInert()) {
unsuppressCallbackRef.current = suppressOthers(elements);

return () => {
if (unsuppressCallbackRef.current) {
unsuppressCallbackRef.current();
}
unsuppressCallbackRef.current = null;
};
} else {
return hideOthers(elements);
unsuppressCallbackRef.current = hideOthers(elements);
}

return () => {
if (unsuppressCallbackRef.current) {
unsuppressCallbackRef.current();
}
unsuppressCallbackRef.current = null;
};
}
}, [isModal, isOpen]);
}, [isModal, inert, isOpen]);

return (
<ClayPortal className={menuClassName} subPortalRef={portalRef}>
Expand Down

0 comments on commit 12a89bb

Please sign in to comment.