Transitions
Transitions help to make a UI expressive and easy to use.
Material UI provides transitions that can be used to introduce some basicmotion to your applications.
Collapse
Expand from the start edge of the child element.Use theorientation prop if you need a horizontal collapse.ThecollapsedSize prop can be used to set the minimum width/height when not expanded.
Fade
Fade in from transparent to opaque.
Grow
Expands outwards from the center of the child element, while also fading in from transparent to opaque.
The second example demonstrates how to change thetransform-origin, and conditionally appliesthetimeout prop to change the entry speed.
Slide
Slide in from the edge of the screen.Thedirection prop controls which edge of the screen the transition starts from.
The Transition component'smountOnEnter prop prevents the child component from being mounteduntilin istrue.This prevents the relatively positioned component from scrolling into viewfrom its off-screen position.Similarly, theunmountOnExit prop removes the component from the DOM after it has been transition off-screen.
Slide relative to a container
The Slide component also acceptscontainer prop, which is a reference to a DOM node.If this prop is set, the Slide component will slide from the edge of that DOM node.
Zoom
Expand outwards from the center of the child element.
This example also demonstrates how to delay the enter transition.
Child requirement
- Forward the style: To better support server rendering, Material UI provides a
styleprop to the children of some transition components (Fade, Grow, Zoom, Slide).Thestyleprop must be applied to the DOM for the animation to work as expected. - Forward the ref: The transition components require the first child element to forward its ref to the DOM node. For more details about ref, check outCaveat with refs
- Single element: The transition components require only one child element (
React.Fragmentis not allowed).
// The `props` object contains a `style` prop.// You need to provide it to the `div` element as shown here.const MyComponent= React.forwardRef(function(props, ref){return(<divref={ref}{...props}> Fade</div>);});exportdefaultfunctionMain(){return(<Fade>{/* MyComponent must be the only child */}<MyComponent/></Fade>);}TransitionGroup
To animate a component when it is mounted or unmounted, you can use theTransitionGroup component fromreact-transition-group.As components are added or removed, thein prop is toggled automatically byTransitionGroup.
- 🍏 Apple
- 🍌 Banana
- 🍍 Pineapple
TransitionComponent prop
Some Material UI components use these transitions internally. These accept aTransitionComponent prop to customize the default transition.You can use any of the above components or your own.It should respect the following conditions:
- Accepts an
inprop. This corresponds to the open/close state. - Call the
onEntercallback prop when the enter transition starts. - Call the
onExitedcallback prop when the exit transition is completed.These two callbacks allow to unmount the children when in a closed state and fully transitioned.
For more information on creating a custom transition, visit thereact-transition-groupTransition documentation.You can also visit the dedicated sections of some of the components:
Performance & SEO
The content of transition component is mounted by default even ifin={false}.This default behavior has server-side rendering and SEO in mind.If you render expensive component trees inside your transition it might be a good idea to change this default behavior by enabling theunmountOnExit prop:
<Fadein={false}unmountOnExit/>As with any performance optimization this is not a silver bullet.Be sure to identify bottlenecks first and then try out these optimization strategies.