Skip to content
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

feat: Nest modal support #14320

Merged
merged 17 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 8 additions & 4 deletions packages/react/src/components/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,16 @@ const Modal = React.forwardRef(function Modal(
return (
<div
{...rest}
onKeyDown={handleKeyDown}
onMouseDown={handleMousedown}
onBlur={handleBlur}
className={modalClasses}
ref={ref}
role="presentation"
ref={ref}>
onKeyDown={handleKeyDown}>
<div
className={`${prefix}--modal-background`}
onMouseDown={handleMousedown}
onBlur={handleBlur}
aria-hidden
/>
tay1orjones marked this conversation as resolved.
Show resolved Hide resolved
{/* Non-translatable: Focus-wrap code makes this `<span>` not actually read by screen readers */}
<span
ref={startTrap}
Expand Down
92 changes: 69 additions & 23 deletions packages/react/src/components/Modal/Modal.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import React, { useState, useRef } from 'react';
import ReactDOM from 'react-dom';
import { action } from '@storybook/addon-actions';
import Modal from './Modal';
import Button from '../Button';
Expand All @@ -23,6 +22,7 @@ import {
StructuredListRow,
StructuredListCell,
} from '../StructuredList';
import { Portal } from '../Portal';

export default {
title: 'Components/Modal',
Expand Down Expand Up @@ -358,30 +358,26 @@ Playground.argTypes = {
},
};

export const WithStateManager = () => {
/**
* Simple state manager for modals.
*/
const ModalStateManager = ({
renderLauncher: LauncherContent,
children: ModalContent,
}) => {
const [open, setOpen] = useState(false);
return (
<>
{!ModalContent || typeof document === 'undefined'
? null
: ReactDOM.createPortal(
<ModalContent open={open} setOpen={setOpen} />,
document.body
)}
{LauncherContent && <LauncherContent open={open} setOpen={setOpen} />}
</>
);
};
/**
* Simple state manager for modals.
*/
const ModalStateManager = ({
renderLauncher: LauncherContent,
children: ModalContent,
}) => {
const [open, setOpen] = useState(false);
return (
<>
<Portal>
<ModalContent open={open} setOpen={setOpen} />
</Portal>
{LauncherContent && <LauncherContent open={open} setOpen={setOpen} />}
</>
);
};

export const WithStateManager = () => {
const button = useRef();

return (
<ModalStateManager
renderLauncher={({ setOpen }) => (
Expand Down Expand Up @@ -420,6 +416,56 @@ export const WithStateManager = () => {
);
};

export const Nested = () => {
const button = useRef();

const ModalOne = ({ children }) => (
<ModalStateManager
renderLauncher={({ setOpen }) => (
<Button ref={button} onClick={() => setOpen(true)}>
Launch modal one
</Button>
)}>
{({ open, setOpen }) => (
<Modal
id="modal1"
modalHeading="Modal One"
passiveModal
open={open}
onRequestClose={() => setOpen(false)}>
{children}
</Modal>
)}
</ModalStateManager>
);

const ModalTwo = ({ children }) => (
<ModalStateManager
renderLauncher={({ setOpen }) => (
<Button ref={button} onClick={() => setOpen(true)}>
Launch modal two
</Button>
)}>
{({ open, setOpen }) => (
<Modal
id="modal2"
modalHeading="Modal Two"
passiveModal
open={open}
onRequestClose={() => setOpen(false)}>
{children}
</Modal>
)}
</ModalStateManager>
);

return (
<ModalOne>
<ModalTwo />
</ModalOne>
);
};

export const PassiveModal = () => {
return (
<Modal
Expand Down
14 changes: 8 additions & 6 deletions packages/styles/scss/components/modal/_modal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,10 @@
.#{$prefix}--modal {
position: fixed;
z-index: z('modal');
top: 0;
left: 0;
display: flex;
width: 100vw;
height: 100vh;
tay1orjones marked this conversation as resolved.
Show resolved Hide resolved
align-items: center;
justify-content: center;
background-color: $overlay;
content: '';
inset: 0;
opacity: 0;
transition: opacity $duration-moderate-02 motion(exit, expressive),
visibility 0ms linear $duration-moderate-02;
Expand All @@ -50,6 +45,13 @@
}
}

.#{$prefix}--modal-background {
position: fixed;
z-index: -1;
background-color: $overlay;
inset: 0;
}

// V11: Question for design: do we have an updated tokens for fields that exist on `layer`?
.#{$prefix}--pagination,
.#{$prefix}--pagination__control-buttons,
Expand Down