Skip to content

Commit

Permalink
Remap existing true value to ignore close button
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Sep 8, 2023
1 parent f4efa25 commit 1f71011
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/components/src/modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ import type { ModalProps } from './types';
// Used to count the number of open modals.
let openModalCount = 0;

/**
* Returns the first tabbable element that is not a close button.
* See: https://github.com/WordPress/gutenberg/issues/54106.
* @param tabbables HTMLElement[] an array of tabbable elements.
* @return HTMLElement the first tabbable element that is not a close button.
*/
function focusFirstNonCloseButtonElement( tabbables: HTMLElement[] ) {
return tabbables.find(
// Ignore the `Close` button.
// See: https://github.com/WordPress/gutenberg/issues/54106.
( tabbableNode ) => tabbableNode?.ariaLabel !== 'Close'
);
}
Expand All @@ -55,7 +59,7 @@ function UnforwardedModal(
bodyOpenClassName = 'modal-open',
role = 'dialog',
title = null,
focusOnMount = focusFirstNonCloseButtonElement,
focusOnMount = true,
shouldCloseOnEsc = true,
shouldCloseOnClickOutside = true,
isDismissible = true,
Expand Down Expand Up @@ -83,7 +87,13 @@ function UnforwardedModal(
const headingId = title
? `components-modal-header-${ instanceId }`
: aria.labelledby;
const focusOnMountRef = useFocusOnMount( focusOnMount );

// Modals should ignore the `Close` button which is the first focusable element.
// Remap `true` to select the next focusable element instead.
const focusOnMountRef = useFocusOnMount(
focusOnMount === true ? focusFirstNonCloseButtonElement : focusOnMount
);

const constrainedTabbingRef = useConstrainedTabbing();
const focusReturnRef = useFocusReturn();
const focusOutsideProps = useFocusOutside( onRequestClose );
Expand Down

0 comments on commit 1f71011

Please sign in to comment.