UNPKG

6.96 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import { Transition } from 'react-transition-group';
6import { elementAcceptingRef } from '@mui/utils';
7import useTheme from '../styles/useTheme';
8import { reflow, getTransitionProps } from '../transitions/utils';
9import useForkRef from '../utils/useForkRef';
10import { jsx as _jsx } from "react/jsx-runtime";
11var styles = {
12 entering: {
13 opacity: 1
14 },
15 entered: {
16 opacity: 1
17 }
18};
19/**
20 * The Fade transition is used by the [Modal](/material-ui/react-modal/) component.
21 * It uses [react-transition-group](https://github.com/reactjs/react-transition-group) internally.
22 */
23
24var Fade = /*#__PURE__*/React.forwardRef(function Fade(props, ref) {
25 var theme = useTheme();
26 var defaultTimeout = {
27 enter: theme.transitions.duration.enteringScreen,
28 exit: theme.transitions.duration.leavingScreen
29 };
30
31 var addEndListener = props.addEndListener,
32 _props$appear = props.appear,
33 appear = _props$appear === void 0 ? true : _props$appear,
34 _children = props.children,
35 easing = props.easing,
36 inProp = props.in,
37 onEnter = props.onEnter,
38 onEntered = props.onEntered,
39 onEntering = props.onEntering,
40 onExit = props.onExit,
41 onExited = props.onExited,
42 onExiting = props.onExiting,
43 style = props.style,
44 _props$timeout = props.timeout,
45 timeout = _props$timeout === void 0 ? defaultTimeout : _props$timeout,
46 _props$TransitionComp = props.TransitionComponent,
47 TransitionComponent = _props$TransitionComp === void 0 ? Transition : _props$TransitionComp,
48 other = _objectWithoutProperties(props, ["addEndListener", "appear", "children", "easing", "in", "onEnter", "onEntered", "onEntering", "onExit", "onExited", "onExiting", "style", "timeout", "TransitionComponent"]);
49
50 var enableStrictModeCompat = true;
51 var nodeRef = React.useRef(null);
52 var foreignRef = useForkRef(_children.ref, ref);
53 var handleRef = useForkRef(nodeRef, foreignRef);
54
55 var normalizedTransitionCallback = function normalizedTransitionCallback(callback) {
56 return function (maybeIsAppearing) {
57 if (callback) {
58 var node = nodeRef.current; // onEnterXxx and onExitXxx callbacks have a different arguments.length value.
59
60 if (maybeIsAppearing === undefined) {
61 callback(node);
62 } else {
63 callback(node, maybeIsAppearing);
64 }
65 }
66 };
67 };
68
69 var handleEntering = normalizedTransitionCallback(onEntering);
70 var handleEnter = normalizedTransitionCallback(function (node, isAppearing) {
71 reflow(node); // So the animation always start from the start.
72
73 var transitionProps = getTransitionProps({
74 style: style,
75 timeout: timeout,
76 easing: easing
77 }, {
78 mode: 'enter'
79 });
80 node.style.webkitTransition = theme.transitions.create('opacity', transitionProps);
81 node.style.transition = theme.transitions.create('opacity', transitionProps);
82
83 if (onEnter) {
84 onEnter(node, isAppearing);
85 }
86 });
87 var handleEntered = normalizedTransitionCallback(onEntered);
88 var handleExiting = normalizedTransitionCallback(onExiting);
89 var handleExit = normalizedTransitionCallback(function (node) {
90 var transitionProps = getTransitionProps({
91 style: style,
92 timeout: timeout,
93 easing: easing
94 }, {
95 mode: 'exit'
96 });
97 node.style.webkitTransition = theme.transitions.create('opacity', transitionProps);
98 node.style.transition = theme.transitions.create('opacity', transitionProps);
99
100 if (onExit) {
101 onExit(node);
102 }
103 });
104 var handleExited = normalizedTransitionCallback(onExited);
105
106 var handleAddEndListener = function handleAddEndListener(next) {
107 if (addEndListener) {
108 // Old call signature before `react-transition-group` implemented `nodeRef`
109 addEndListener(nodeRef.current, next);
110 }
111 };
112
113 return /*#__PURE__*/_jsx(TransitionComponent, _extends({
114 appear: appear,
115 in: inProp,
116 nodeRef: enableStrictModeCompat ? nodeRef : undefined,
117 onEnter: handleEnter,
118 onEntered: handleEntered,
119 onEntering: handleEntering,
120 onExit: handleExit,
121 onExited: handleExited,
122 onExiting: handleExiting,
123 addEndListener: handleAddEndListener,
124 timeout: timeout
125 }, other, {
126 children: function children(state, childProps) {
127 return /*#__PURE__*/React.cloneElement(_children, _extends({
128 style: _extends({
129 opacity: 0,
130 visibility: state === 'exited' && !inProp ? 'hidden' : undefined
131 }, styles[state], style, _children.props.style),
132 ref: handleRef
133 }, childProps));
134 }
135 }));
136});
137process.env.NODE_ENV !== "production" ? Fade.propTypes
138/* remove-proptypes */
139= {
140 // ----------------------------- Warning --------------------------------
141 // | These PropTypes are generated from the TypeScript type definitions |
142 // | To update them edit the d.ts file and run "yarn proptypes" |
143 // ----------------------------------------------------------------------
144
145 /**
146 * Add a custom transition end trigger. Called with the transitioning DOM
147 * node and a done callback. Allows for more fine grained transition end
148 * logic. Note: Timeouts are still used as a fallback if provided.
149 */
150 addEndListener: PropTypes.func,
151
152 /**
153 * Perform the enter transition when it first mounts if `in` is also `true`.
154 * Set this to `false` to disable this behavior.
155 * @default true
156 */
157 appear: PropTypes.bool,
158
159 /**
160 * A single child content element.
161 */
162 children: elementAcceptingRef.isRequired,
163
164 /**
165 * The transition timing function.
166 * You may specify a single easing or a object containing enter and exit values.
167 */
168 easing: PropTypes.oneOfType([PropTypes.shape({
169 enter: PropTypes.string,
170 exit: PropTypes.string
171 }), PropTypes.string]),
172
173 /**
174 * If `true`, the component will transition in.
175 */
176 in: PropTypes.bool,
177
178 /**
179 * @ignore
180 */
181 onEnter: PropTypes.func,
182
183 /**
184 * @ignore
185 */
186 onEntered: PropTypes.func,
187
188 /**
189 * @ignore
190 */
191 onEntering: PropTypes.func,
192
193 /**
194 * @ignore
195 */
196 onExit: PropTypes.func,
197
198 /**
199 * @ignore
200 */
201 onExited: PropTypes.func,
202
203 /**
204 * @ignore
205 */
206 onExiting: PropTypes.func,
207
208 /**
209 * @ignore
210 */
211 style: PropTypes.object,
212
213 /**
214 * The duration for the transition, in milliseconds.
215 * You may specify a single timeout for all transitions, or individually with an object.
216 * @default {
217 * enter: theme.transitions.duration.enteringScreen,
218 * exit: theme.transitions.duration.leavingScreen,
219 * }
220 */
221 timeout: PropTypes.oneOfType([PropTypes.number, PropTypes.shape({
222 appear: PropTypes.number,
223 enter: PropTypes.number,
224 exit: PropTypes.number
225 })])
226} : void 0;
227export default Fade;
\No newline at end of file