-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! fixup! Re-implement
Modal
component using HTMLDialogElement (#…
…461)
- Loading branch information
1 parent
bf704ca
commit 61bcff4
Showing
4 changed files
with
91 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,37 @@ | ||
export const dialogOnKeyDownHandler = ( | ||
e, | ||
closeButtonRef, | ||
primaryButtonRef, | ||
allowCloseOnEscapeKey, | ||
allowPrimaryActionOnEnterKey, | ||
) => { | ||
// When `allowCloseOnEscapeKey` is set to `false`, prevent closing the modal using the Escape key. | ||
// Prevent closing the modal using the Escape key when one of the following conditions is met: | ||
// 1. The close button is not present | ||
// 2. The close button is disabled | ||
// 3. `allowCloseOnEscapeKey` is set to `false` | ||
if ( | ||
e.key === 'Escape' | ||
&& !allowCloseOnEscapeKey | ||
&& ( | ||
closeButtonRef?.current == null | ||
|| closeButtonRef?.current?.disabled === true | ||
|| !allowCloseOnEscapeKey | ||
) | ||
) { | ||
e.preventDefault(); | ||
} | ||
|
||
// When the close button is disabled, prevent closing the modal using the Escape key. | ||
// Trigger the primary action when the Enter key is pressed and the following conditions are met: | ||
// 1. The primary button is present | ||
// 2. The primary button is not disabled | ||
// 3. `allowPrimaryActionOnEnterKey` is set to `true` | ||
// 4. The focused element is an input or select (text area is omitted as Enter key is used for new line) | ||
if ( | ||
e.key === 'Escape' | ||
&& closeButtonRef?.current != null | ||
&& closeButtonRef?.current?.disabled === true | ||
e.key === 'Enter' | ||
&& primaryButtonRef?.current != null | ||
&& primaryButtonRef?.current?.disabled === false | ||
&& allowPrimaryActionOnEnterKey | ||
&& ['INPUT', 'SELECT'].includes(e.target.nodeName) | ||
) { | ||
e.preventDefault(); | ||
primaryButtonRef.current.click(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters