Skip to content

Commit

Permalink
fix the fade transition at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Feb 12, 2023
1 parent 0fbb5b4 commit 3a91549
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 91 deletions.
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
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 {
in: open,
children,
onEnter,
onExited,
onClick,
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 })}
</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,
},
}}
>
<Fade in={open}>
Expand Down
8 changes: 5 additions & 3 deletions docs/data/material/components/modal/TransitionsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ export default function TransitionsModal() {
open={open}
onClose={handleClose}
closeAfterTransition
BackdropComponent={Backdrop}
BackdropProps={{
timeout: 500,
slots={{ backdrop: Backdrop }}
slotProps={{
backdrop: {
timeout: 500,
},
}}
>
<Fade in={open}>
Expand Down
8 changes: 5 additions & 3 deletions docs/data/material/components/modal/TransitionsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ export default function TransitionsModal() {
open={open}
onClose={handleClose}
closeAfterTransition
BackdropComponent={Backdrop}
BackdropProps={{
timeout: 500,
slots={{ backdrop: Backdrop }}
slotProps={{
backdrop: {
timeout: 500,
},
}}
>
<Fade in={open}>
Expand Down
1 change: 1 addition & 0 deletions docs/pages/material-ui/api/backdrop.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"description": "Array&lt;func<br>&#124;&nbsp;object<br>&#124;&nbsp;bool&gt;<br>&#124;&nbsp;func<br>&#124;&nbsp;object"
}
},
"TransitionComponent": { "type": { "name": "elementType" }, "default": "Fade" },
"transitionDuration": {
"type": {
"name": "union",
Expand Down
1 change: 1 addition & 0 deletions docs/translations/api-docs/backdrop/backdrop.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"slotProps": "The extra props for the slot components. You can override the existing props or add new ones.<br>This prop is an alias for the <code>componentsProps</code> prop, which will be deprecated in the future.",
"slots": "The components used for each slot inside.<br>This prop is an alias for the <code>components</code> prop, which will be deprecated in the future.",
"sx": "The system prop that allows defining system overrides as well as additional CSS styles. See the <a href=\"/system/getting-started/the-sx-prop/\">`sx` page</a> for more details.",
"TransitionComponent": "The component used for the transition. <a href=\"/material-ui/transitions/#transitioncomponent-prop\">Follow this guide</a> to learn more about the requirements for this component.",
"transitionDuration": "The duration for the transition, in milliseconds. You may specify a single timeout for all transitions, or individually with an object."
},
"classDescriptions": {
Expand Down
Loading

0 comments on commit 3a91549

Please sign in to comment.