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

[docs] Fix modal transition demos #36137

Merged
merged 3 commits into from
Feb 18, 2023
Merged
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
2 changes: 1 addition & 1 deletion docs/data/base/components/modal/NestedModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ function ChildModal() {
<React.Fragment>
<Button onClick={handleOpen}>Open Child Modal</Button>
<Modal
hideBackdrop
open={open}
onClose={handleClose}
aria-labelledby="child-modal-title"
aria-describedby="child-modal-description"
slots={{ backdrop: Backdrop }}
>
<Box sx={[style, { width: '200px' }]}>
<h2 id="child-modal-title">Text in a child modal</h2>
Expand Down
2 changes: 1 addition & 1 deletion docs/data/base/components/modal/NestedModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ function ChildModal() {
<React.Fragment>
<Button onClick={handleOpen}>Open Child Modal</Button>
<Modal
hideBackdrop
Copy link
Member Author

@oliviertassinari oliviertassinari Feb 12, 2023

Choose a reason for hiding this comment

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

The UX is broken if we disable the backdrop.

I suspect this prop is here because we wanted to test that it was behaving correctly.

open={open}
onClose={handleClose}
aria-labelledby="child-modal-title"
aria-describedby="child-modal-description"
slots={{ backdrop: Backdrop }}
>
<Box sx={[style, { width: '200px' }]}>
<h2 id="child-modal-title">Text in a child modal</h2>
Expand Down
24 changes: 8 additions & 16 deletions docs/data/base/components/modal/SpringModal.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { Box, styled } from '@mui/system';
import ModalUnstyled from '@mui/base/ModalUnstyled';
import Button from '@mui/base/ButtonUnstyled';
import { useSpring, animated } from '@react-spring/web';

const BackdropUnstyled = React.forwardRef((props, ref) => {
const { open, className, ...other } = props;
return (
<div
className={clsx({ 'MuiBackdrop-open': open }, className)}
ref={ref}
{...other}
/>
);
const { open, ...other } = props;
return <Fade ref={ref} in={open} {...other} />;
});

BackdropUnstyled.propTypes = {
className: PropTypes.string.isRequired,
open: PropTypes.bool,
open: PropTypes.bool.isRequired,
};

const Modal = styled(ModalUnstyled)`
Expand Down Expand Up @@ -52,12 +44,12 @@ const Fade = React.forwardRef(function Fade(props, ref) {
to: { opacity: open ? 1 : 0 },
onStart: () => {
if (open && onEnter) {
onEnter();
onEnter(null, true);
}
},
onRest: () => {
if (!open && onExited) {
onExited();
onExited(null, true);
}
},
});
Expand All @@ -70,8 +62,8 @@ const Fade = React.forwardRef(function Fade(props, ref) {
});

Fade.propTypes = {
children: PropTypes.element,
in: PropTypes.bool.isRequired,
children: PropTypes.element.isRequired,
in: PropTypes.bool,
onEnter: PropTypes.func,
onExited: PropTypes.func,
};
Expand Down Expand Up @@ -107,7 +99,7 @@ export default function SpringModal() {
<Fade in={open}>
<Box sx={style}>
<h2 id="spring-modal-title">Text in a modal</h2>
<span id="spring-modal-description" style={{ marginTop: '16px' }}>
<span id="spring-modal-description" style={{ marginTop: 16 }}>
Duis mollis, est non commodo luctus, nisi erat porttitor ligula.
</span>
</Box>
Expand Down
28 changes: 11 additions & 17 deletions docs/data/base/components/modal/SpringModal.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import * as React from 'react';
import clsx from 'clsx';
import { Box, styled, Theme } from '@mui/system';
import ModalUnstyled from '@mui/base/ModalUnstyled';
import Button from '@mui/base/ButtonUnstyled';
import { useSpring, animated } from '@react-spring/web';

const BackdropUnstyled = React.forwardRef<
HTMLDivElement,
{ open?: boolean; className: string }
{ children: React.ReactElement; open: boolean }
>((props, ref) => {
const { open, className, ...other } = props;
return (
<div
className={clsx({ 'MuiBackdrop-open': open }, className)}
ref={ref}
{...other}
/>
);
const { open, ...other } = props;
return <Fade ref={ref} in={open} {...other} />;
});

const Modal = styled(ModalUnstyled)`
Expand All @@ -43,10 +36,11 @@ const Backdrop = styled(BackdropUnstyled)`
`;

interface FadeProps {
children?: React.ReactElement;
in: boolean;
onEnter?: () => {};
onExited?: () => {};
children: React.ReactElement;
in?: boolean;
onClick?: any;
onEnter?: (node: HTMLElement, isAppearing: boolean) => void;
onExited?: (node: HTMLElement, isAppearing: boolean) => void;
}

const Fade = React.forwardRef<HTMLDivElement, FadeProps>(function Fade(props, ref) {
Expand All @@ -56,12 +50,12 @@ const Fade = React.forwardRef<HTMLDivElement, FadeProps>(function Fade(props, re
to: { opacity: open ? 1 : 0 },
onStart: () => {
if (open && onEnter) {
onEnter();
onEnter(null as any, true);
}
},
onRest: () => {
if (!open && onExited) {
onExited();
onExited(null as any, true);
}
},
});
Expand Down Expand Up @@ -104,7 +98,7 @@ export default function SpringModal() {
<Fade in={open}>
<Box sx={style}>
<h2 id="spring-modal-title">Text in a modal</h2>
<span id="spring-modal-description" style={{ marginTop: '16px' }}>
<span id="spring-modal-description" style={{ marginTop: 16 }}>
Duis mollis, est non commodo luctus, nisi erat porttitor ligula.
</span>
</Box>
Expand Down
16 changes: 6 additions & 10 deletions docs/data/base/components/modal/TransitionsModal.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { Box, styled } from '@mui/system';
import ModalUnstyled from '@mui/base/ModalUnstyled';
import Fade from '@mui/material/Fade';
import Button from '@mui/base/ButtonUnstyled';

const BackdropUnstyled = React.forwardRef((props, ref) => {
const { open, className, ...other } = props;
const { open, ...other } = props;
return (
<div
className={clsx({ 'MuiBackdrop-open': open }, className)}
ref={ref}
{...other}
/>
<Fade in={open}>
<div ref={ref} {...other} />
</Fade>
);
});

BackdropUnstyled.propTypes = {
className: PropTypes.string.isRequired,
open: PropTypes.bool,
};

Expand Down Expand Up @@ -73,10 +69,10 @@ export default function TransitionsModal() {
closeAfterTransition
slots={{ backdrop: Backdrop }}
>
<Fade in={open} timeout={300}>
<Fade in={open}>
<Box sx={style}>
<h2 id="transition-modal-title">Text in a modal</h2>
<span id="transition-modal-description" style={{ marginTop: '16px' }}>
<span id="transition-modal-description" style={{ marginTop: 16 }}>
Duis mollis, est non commodo luctus, nisi erat porttitor ligula.
</span>
</Box>
Expand Down
28 changes: 12 additions & 16 deletions docs/data/base/components/modal/TransitionsModal.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import * as React from 'react';
import clsx from 'clsx';
import { Box, styled, Theme } from '@mui/system';
import ModalUnstyled from '@mui/base/ModalUnstyled';
import Fade from '@mui/material/Fade';
import Button from '@mui/base/ButtonUnstyled';

const BackdropUnstyled = React.forwardRef<
HTMLDivElement,
{ open?: boolean; className: string }
>((props, ref) => {
const { open, className, ...other } = props;
return (
<div
className={clsx({ 'MuiBackdrop-open': open }, className)}
ref={ref}
{...other}
/>
);
});
const BackdropUnstyled = React.forwardRef<HTMLDivElement, { open?: boolean }>(
(props, ref) => {
const { open, ...other } = props;
return (
<Fade in={open}>
<div ref={ref} {...other} />
</Fade>
);
},
);

const Modal = styled(ModalUnstyled)`
position: fixed;
Expand Down Expand Up @@ -70,10 +66,10 @@ export default function TransitionsModal() {
closeAfterTransition
slots={{ backdrop: Backdrop }}
>
<Fade in={open} timeout={300}>
<Fade in={open}>
<Box sx={style}>
<h2 id="transition-modal-title">Text in a modal</h2>
<span id="transition-modal-description" style={{ marginTop: '16px' }}>
<span id="transition-modal-description" style={{ marginTop: 16 }}>
Duis mollis, est non commodo luctus, nisi erat porttitor ligula.
</span>
</Box>
Expand Down
1 change: 0 additions & 1 deletion docs/data/material/components/modal/NestedModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function ChildModal() {
<React.Fragment>
<Button onClick={handleOpen}>Open Child Modal</Button>
<Modal
hideBackdrop
open={open}
onClose={handleClose}
aria-labelledby="child-modal-title"
Expand Down
1 change: 0 additions & 1 deletion docs/data/material/components/modal/NestedModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function ChildModal() {
<React.Fragment>
<Button onClick={handleOpen}>Open Child Modal</Button>
<Modal
hideBackdrop
open={open}
onClose={handleClose}
aria-labelledby="child-modal-title"
Expand Down
30 changes: 21 additions & 9 deletions docs/data/material/components/modal/SpringModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,44 @@ import Typography from '@mui/material/Typography';
import { useSpring, animated } from '@react-spring/web';

const Fade = React.forwardRef(function Fade(props, ref) {
const { in: open, children, onEnter, onExited, ...other } = props;
const {
children,
in: open,
onClick,
onEnter,
onExited,
ownerState,
...other
} = props;
const style = useSpring({
from: { opacity: 0 },
to: { opacity: open ? 1 : 0 },
onStart: () => {
if (open && onEnter) {
onEnter();
onEnter(null, true);
}
},
onRest: () => {
if (!open && onExited) {
onExited();
onExited(null, true);
}
},
});

return (
<animated.div ref={ref} style={style} {...other}>
{children}
{React.cloneElement(children, { onClick })}
</animated.div>
);
});

Fade.propTypes = {
children: PropTypes.element,
in: PropTypes.bool.isRequired,
children: PropTypes.element.isRequired,
in: PropTypes.bool,
onClick: PropTypes.any,
onEnter: PropTypes.func,
onExited: PropTypes.func,
ownerState: PropTypes.any,
};

const style = {
Expand Down Expand Up @@ -64,9 +74,11 @@ export default function SpringModal() {
open={open}
onClose={handleClose}
closeAfterTransition
BackdropComponent={Backdrop}
BackdropProps={{
timeout: 500,
slots={{ backdrop: Backdrop }}
slotProps={{
backdrop: {
TransitionComponent: Fade,
},
}}
>
<Fade in={open}>
Expand Down
34 changes: 23 additions & 11 deletions docs/data/material/components/modal/SpringModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,42 @@ import Typography from '@mui/material/Typography';
import { useSpring, animated } from '@react-spring/web';

interface FadeProps {
children?: React.ReactElement;
in: boolean;
onEnter?: () => {};
onExited?: () => {};
children: React.ReactElement;
in?: boolean;
onClick?: any;
onEnter?: (node: HTMLElement, isAppearing: boolean) => void;
onExited?: (node: HTMLElement, isAppearing: boolean) => void;
ownerState?: any;
}

const Fade = React.forwardRef<HTMLDivElement, FadeProps>(function Fade(props, ref) {
const { in: open, children, onEnter, onExited, ...other } = props;
const {
children,
in: open,
onClick,
onEnter,
onExited,
ownerState,
...other
} = props;
const style = useSpring({
from: { opacity: 0 },
to: { opacity: open ? 1 : 0 },
onStart: () => {
if (open && onEnter) {
onEnter();
onEnter(null as any, true);
}
},
onRest: () => {
if (!open && onExited) {
onExited();
onExited(null as any, true);
}
},
});

return (
<animated.div ref={ref} style={style} {...other}>
{children}
{React.cloneElement(children, { onClick })}
Copy link
Member Author

Choose a reason for hiding this comment

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

the click needs to be registered on the element that is position fixed to work.

</animated.div>
);
});
Expand Down Expand Up @@ -63,9 +73,11 @@ export default function SpringModal() {
open={open}
onClose={handleClose}
closeAfterTransition
BackdropComponent={Backdrop}
BackdropProps={{
timeout: 500,
slots={{ backdrop: Backdrop }}
slotProps={{
backdrop: {
TransitionComponent: Fade,
},
Comment on lines +76 to +80
Copy link
Member Author

Choose a reason for hiding this comment

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

For a consistent animation, use the Fade component for both the modal body and modal backdrop.

}}
>
<Fade in={open}>
Expand Down
Loading