Skip to content

feat(Modal): add new render options for modal #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/ImperativeTransition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ export function useTransition({
return ref;
}

export interface ImperativeTransitionProps extends TransitionProps {
export interface ImperativeTransitionProps
extends Omit<TransitionProps, 'appear' | 'mountOnEnter' | 'unmountOnExit'> {
transition: TransitionHandler;
appear: true;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These types conflict with the new code since they are used as an intersection with the regular transition props. Since they aren't used in the code, can we remove?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are here bc the run transition version doesn't support other values

mountOnEnter: true;
unmountOnExit: true;
}

/**
Expand Down
59 changes: 43 additions & 16 deletions src/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface RenderModalDialogProps {
style: React.CSSProperties | undefined;
className: string | undefined;
tabIndex: number;
role: string;
role: string | undefined;
ref: React.RefCallback<Element>;
'aria-modal': boolean | undefined;
}
Expand Down Expand Up @@ -184,6 +184,27 @@ export interface BaseModalProps extends TransitionCallbacks {
restoreFocusOptions?: {
preventScroll: boolean;
};

/**
* Lazy mount the dialog element when the Modal is shown.
*
* @default true
*/
mountDialogOnEnter?: boolean | undefined;

/**
* Unmount the dialog element (remove it from the DOM) when the modal is no longer visible.
*
* @default true
*/
unmountDialogOnExit?: boolean | undefined;

/**
* Render modal in a portal.
*
* @default true
*/
portal?: boolean | undefined;
}

export interface ModalProps extends BaseModalProps {
Expand Down Expand Up @@ -251,6 +272,9 @@ const Modal: React.ForwardRefExoticComponent<
enforceFocus = true,
restoreFocus = true,
restoreFocusOptions,
mountDialogOnEnter = true,
unmountDialogOnExit = true,
portal = true,
renderDialog,
renderBackdrop = (props: RenderModalBackdropProps) => <div {...props} />,
manager: providedManager,
Expand Down Expand Up @@ -349,10 +373,10 @@ const Modal: React.ForwardRefExoticComponent<
// Show logic when:
// - show is `true` _and_ `container` has resolved
useEffect(() => {
if (!show || !container) return;
if (!show || (!container && portal)) return;

handleShow();
}, [show, container, /* should never change: */ handleShow]);
}, [show, container, portal, /* should never change: */ handleShow]);

// Hide cleanup logic when:
// - `exited` switches to true
Expand Down Expand Up @@ -419,15 +443,15 @@ const Modal: React.ForwardRefExoticComponent<
onExited?.(...args);
};

if (!container) {
if (!container && portal) {
return null;
}

const dialogProps = {
role,
role: show ? role : undefined,
ref: modal.setDialogRef,
// apparently only works on the dialog role element
'aria-modal': role === 'dialog' ? true : undefined,
'aria-modal': show && role === 'dialog' ? true : undefined,
...rest,
style,
className,
Expand All @@ -446,8 +470,8 @@ const Modal: React.ForwardRefExoticComponent<
transition as TransitionComponent,
runTransition,
{
unmountOnExit: true,
mountOnEnter: true,
unmountOnExit: unmountDialogOnExit,
mountOnEnter: mountDialogOnEnter,
appear: true,
in: !!show,
onExit,
Expand Down Expand Up @@ -480,15 +504,18 @@ const Modal: React.ForwardRefExoticComponent<
);
}

return (
return portal && container ? (
ReactDOM.createPortal(
<>
{backdropElement}
{dialog}
</>,
container,
)
) : (
<>
{ReactDOM.createPortal(
<>
{backdropElement}
{dialog}
</>,
container,
)}
{backdropElement}
{dialog}
</>
);
},
Expand Down
25 changes: 25 additions & 0 deletions test/ModalSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,31 @@ describe('<Modal>', () => {
);
});

it('should not render in a portal when `portal` is false', () => {
render(
<div data-testid="container">
<Modal show portal={false}>
<strong>Message</strong>
</Modal>
</div>,
);

expect(
screen.getByTestId('container').contains(screen.getByRole('dialog')),
).toBeTruthy();
});

it('should render the dialog when mountDialogOnEnter and mountDialogOnEnter are false when not shown', () => {
render(
<Modal mountDialogOnEnter={false} unmountDialogOnExit={false}>
<strong>Message</strong>
</Modal>,
{ container: attachTo },
);

expect(screen.getByText('Message')).toBeTruthy();
});

describe('Focused state', () => {
let focusableContainer: HTMLElement;

Expand Down