Skip to content

Commit

Permalink
fixup! fixup! fixup! Re-implement Modal component using HTMLDialogE…
Browse files Browse the repository at this point in the history
…lement (#461)
  • Loading branch information
bedrich-schindler committed Jan 14, 2025
1 parent 61bcff4 commit fe13200
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/components/Modal/Modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import PropTypes from 'prop-types';
import React, {
useCallback,
useEffect,
useImperativeHandle,
useRef,
} from 'react';
import { createPortal } from 'react-dom';
Expand Down Expand Up @@ -47,29 +48,32 @@ export const Modal = ({
autoFocus,
children,
closeButtonRef,
dialogRef,
portalId,
position,
preventScrollUnderneath,
primaryButtonRef,
size,
...restProps
}) => {
const dialogRef = useRef();
const internalDialogRef = useRef();

useEffect(() => {
dialogRef.current.showModal();
internalDialogRef.current.showModal();
}, []);

useModalFocus(autoFocus, dialogRef, primaryButtonRef);
useImperativeHandle(dialogRef, () => internalDialogRef.current);

useModalFocus(autoFocus, internalDialogRef, primaryButtonRef);
useModalScrollPrevention(preventScrollUnderneath);

const onCancel = useCallback(
(e) => dialogOnCancelHandler(e, closeButtonRef),
[closeButtonRef],
);
const onClick = useCallback(
(e) => dialogOnClickHandler(e, closeButtonRef, dialogRef, allowCloseOnBackdropClick),
[allowCloseOnBackdropClick, closeButtonRef, dialogRef],
(e) => dialogOnClickHandler(e, closeButtonRef, internalDialogRef, allowCloseOnBackdropClick),
[allowCloseOnBackdropClick, closeButtonRef, internalDialogRef],
);
const onClose = useCallback(
(e) => dialogOnCloseHandler(e, closeButtonRef),
Expand Down Expand Up @@ -100,7 +104,7 @@ export const Modal = ({
if (portalId === null) {
return preRender(
children,
dialogRef,
internalDialogRef,
position,
size,
events,
Expand All @@ -111,7 +115,7 @@ export const Modal = ({
return createPortal(
preRender(
children,
dialogRef,
internalDialogRef,
position,
size,
events,
Expand All @@ -128,6 +132,7 @@ Modal.defaultProps = {
autoFocus: true,
children: null,
closeButtonRef: null,
dialogRef: null,
portalId: null,
position: 'center',
preventScrollUnderneath: window.document.body,
Expand Down Expand Up @@ -172,6 +177,13 @@ Modal.propTypes = {
// eslint-disable-next-line react/forbid-prop-types
current: PropTypes.any,
}),
/**
* Reference to dialog element
*/
dialogRef: PropTypes.shape({
// eslint-disable-next-line react/forbid-prop-types
current: PropTypes.any,
}),
/**
* If set, modal is rendered in the React Portal with that ID.
*/
Expand Down

0 comments on commit fe13200

Please sign in to comment.