Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! fixup! Re-implement Modal component usi…
Browse files Browse the repository at this point in the history
…ng HTMLDialogElement (#461)
  • Loading branch information
bedrich-schindler committed Sep 24, 2024
1 parent 6348886 commit 4f137dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/components/Modal/Modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const preRender = (
export const Modal = ({
allowCloseOnBackdropClick,
allowCloseOnEscapeKey,
allowPrimaryActionOnEnterKey,
autoFocus,
children,
closeButtonRef,
Expand All @@ -59,7 +60,7 @@ export const Modal = ({
dialogRef.current.showModal();
}, []);

useModalFocus(autoFocus, dialogRef, primaryButtonRef);
useModalFocus(allowPrimaryActionOnEnterKey, autoFocus, dialogRef, primaryButtonRef);
useModalScrollPrevention(preventScrollUnderneath);

const onCancel = useCallback(
Expand Down Expand Up @@ -112,6 +113,7 @@ export const Modal = ({
Modal.defaultProps = {
allowCloseOnBackdropClick: true,
allowCloseOnEscapeKey: true,
allowPrimaryActionOnEnterKey: true,
autoFocus: true,
children: null,
closeButtonRef: null,
Expand All @@ -131,6 +133,10 @@ Modal.propTypes = {
* If `true`, the `Modal` can be closed by pressing the Escape key.
*/
allowCloseOnEscapeKey: PropTypes.bool,
/**
* If `true`, the `Modal` can be submitted by pressing the Enter key.
*/
allowPrimaryActionOnEnterKey: PropTypes.bool,
/**
* If `true`, focus the first input element in the `Modal`, or primary button (referenced by the `primaryButtonRef`
* prop), or other focusable element when the `Modal` is opened. If there are none or `autoFocus` is set to `false`,
Expand Down
5 changes: 4 additions & 1 deletion src/components/Modal/_hooks/useModalFocus.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect } from 'react';

export const useModalFocus = (
allowPrimaryActionOnEnterKey,
autoFocus,
dialogRef,
primaryButtonRef,
Expand Down Expand Up @@ -53,7 +54,8 @@ export const useModalFocus = (

const keyPressHandler = (e) => {
if (
e.key === 'Enter'
allowPrimaryActionOnEnterKey
&& e.key === 'Enter'
&& e.target.nodeName !== 'BUTTON'
&& e.target.nodeName !== 'TEXTAREA'
&& e.target.nodeName !== 'A'
Expand Down Expand Up @@ -112,6 +114,7 @@ export const useModalFocus = (
return () => window.document.removeEventListener('keydown', keyPressHandler, false);
},
[
allowPrimaryActionOnEnterKey,
autoFocus,
dialogRef,
primaryButtonRef,
Expand Down

0 comments on commit 4f137dd

Please sign in to comment.