UNPKG

@material-ui/core

Version:

Quickly build beautiful React apps. Material-UI is a simple and customizable component library to build faster, beautiful, and more accessible React applications. Follow your own design system, or start with Material Design.

209 lines (189 loc) 5.85 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; const _excluded = ["appear", "children", "easing", "in", "onEnter", "onEntered", "onEntering", "onExit", "onExited", "onExiting", "style", "timeout", "TransitionComponent"]; import * as React from 'react'; import PropTypes from 'prop-types'; import { Transition } from 'react-transition-group'; import { elementAcceptingRef } from '@material-ui/utils'; import { duration } from '../styles/createTransitions'; import useTheme from '../styles/useTheme'; import { reflow, getTransitionProps } from '../transitions/utils'; import useForkRef from '../utils/useForkRef'; import { jsx as _jsx } from "react/jsx-runtime"; const styles = { entering: { opacity: 1 }, entered: { opacity: 1 } }; const defaultTimeout = { enter: duration.enteringScreen, exit: duration.leavingScreen }; /** * The Fade transition is used by the [Modal](/components/modal/) component. * It uses [react-transition-group](https://github.com/reactjs/react-transition-group) internally. */ const Fade = /*#__PURE__*/React.forwardRef(function Fade(props, ref) { const { appear = true, children, easing, in: inProp, onEnter, onEntered, onEntering, onExit, onExited, onExiting, style, timeout = defaultTimeout, // eslint-disable-next-line react/prop-types TransitionComponent = Transition } = props, other = _objectWithoutPropertiesLoose(props, _excluded); const theme = useTheme(); const enableStrictModeCompat = true; const nodeRef = React.useRef(null); const foreignRef = useForkRef(children.ref, ref); const handleRef = useForkRef(nodeRef, foreignRef); const normalizedTransitionCallback = callback => maybeIsAppearing => { if (callback) { const node = nodeRef.current; // onEnterXxx and onExitXxx callbacks have a different arguments.length value. if (maybeIsAppearing === undefined) { callback(node); } else { callback(node, maybeIsAppearing); } } }; const handleEntering = normalizedTransitionCallback(onEntering); const handleEnter = normalizedTransitionCallback((node, isAppearing) => { reflow(node); // So the animation always start from the start. const transitionProps = getTransitionProps({ style, timeout, easing }, { mode: 'enter' }); node.style.webkitTransition = theme.transitions.create('opacity', transitionProps); node.style.transition = theme.transitions.create('opacity', transitionProps); if (onEnter) { onEnter(node, isAppearing); } }); const handleEntered = normalizedTransitionCallback(onEntered); const handleExiting = normalizedTransitionCallback(onExiting); const handleExit = normalizedTransitionCallback(node => { const transitionProps = getTransitionProps({ style, timeout, easing }, { mode: 'exit' }); node.style.webkitTransition = theme.transitions.create('opacity', transitionProps); node.style.transition = theme.transitions.create('opacity', transitionProps); if (onExit) { onExit(node); } }); const handleExited = normalizedTransitionCallback(onExited); return /*#__PURE__*/_jsx(TransitionComponent, _extends({ appear: appear, in: inProp, nodeRef: enableStrictModeCompat ? nodeRef : undefined, onEnter: handleEnter, onEntered: handleEntered, onEntering: handleEntering, onExit: handleExit, onExited: handleExited, onExiting: handleExiting, timeout: timeout }, other, { children: (state, childProps) => { return /*#__PURE__*/React.cloneElement(children, _extends({ style: _extends({ opacity: 0, visibility: state === 'exited' && !inProp ? 'hidden' : undefined }, styles[state], style, children.props.style), ref: handleRef }, childProps)); } })); }); process.env.NODE_ENV !== "production" ? Fade.propTypes /* remove-proptypes */ = { // ----------------------------- Warning -------------------------------- // | These PropTypes are generated from the TypeScript type definitions | // | To update them edit the d.ts file and run "yarn proptypes" | // ---------------------------------------------------------------------- /** * Perform the enter transition when it first mounts if `in` is also `true`. * Set this to `false` to disable this behavior. * @default true */ appear: PropTypes.bool, /** * A single child content element. */ children: elementAcceptingRef, /** * The transition timing function. * You may specify a single easing or a object containing enter and exit values. */ easing: PropTypes.oneOfType([PropTypes.shape({ enter: PropTypes.string, exit: PropTypes.string }), PropTypes.string]), /** * If `true`, the component will transition in. */ in: PropTypes.bool, /** * @ignore */ onEnter: PropTypes.func, /** * @ignore */ onEntered: PropTypes.func, /** * @ignore */ onEntering: PropTypes.func, /** * @ignore */ onExit: PropTypes.func, /** * @ignore */ onExited: PropTypes.func, /** * @ignore */ onExiting: PropTypes.func, /** * @ignore */ style: PropTypes.object, /** * The duration for the transition, in milliseconds. * You may specify a single timeout for all transitions, or individually with an object. * @default { * enter: duration.enteringScreen, * exit: duration.leavingScreen, * } */ timeout: PropTypes.oneOfType([PropTypes.number, PropTypes.shape({ appear: PropTypes.number, enter: PropTypes.number, exit: PropTypes.number })]) } : void 0; export default Fade;