UNPKG

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