UNPKG

6.42 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, childProps) => {
118 return /*#__PURE__*/React.cloneElement(children, {
119 style: {
120 transform: 'scale(0)',
121 visibility: state === 'exited' && !inProp ? 'hidden' : undefined,
122 ...styles[state],
123 ...style,
124 ...children.props.style
125 },
126 ref: handleRef,
127 ...childProps
128 });
129 }
130 });
131});
132process.env.NODE_ENV !== "production" ? Zoom.propTypes /* remove-proptypes */ = {
133 // ┌────────────────────────────── Warning ──────────────────────────────┐
134 // │ These PropTypes are generated from the TypeScript type definitions. │
135 // │ To update them, edit the d.ts file and run `pnpm proptypes`. │
136 // └─────────────────────────────────────────────────────────────────────┘
137 /**
138 * Add a custom transition end trigger. Called with the transitioning DOM
139 * node and a done callback. Allows for more fine grained transition end
140 * logic. Note: Timeouts are still used as a fallback if provided.
141 */
142 addEndListener: PropTypes.func,
143 /**
144 * Perform the enter transition when it first mounts if `in` is also `true`.
145 * Set this to `false` to disable this behavior.
146 * @default true
147 */
148 appear: PropTypes.bool,
149 /**
150 * A single child content element.
151 */
152 children: elementAcceptingRef.isRequired,
153 /**
154 * The transition timing function.
155 * You may specify a single easing or a object containing enter and exit values.
156 */
157 easing: PropTypes.oneOfType([PropTypes.shape({
158 enter: PropTypes.string,
159 exit: PropTypes.string
160 }), PropTypes.string]),
161 /**
162 * If `true`, the component will transition in.
163 */
164 in: PropTypes.bool,
165 /**
166 * @ignore
167 */
168 onEnter: PropTypes.func,
169 /**
170 * @ignore
171 */
172 onEntered: PropTypes.func,
173 /**
174 * @ignore
175 */
176 onEntering: PropTypes.func,
177 /**
178 * @ignore
179 */
180 onExit: PropTypes.func,
181 /**
182 * @ignore
183 */
184 onExited: PropTypes.func,
185 /**
186 * @ignore
187 */
188 onExiting: PropTypes.func,
189 /**
190 * @ignore
191 */
192 style: PropTypes.object,
193 /**
194 * The duration for the transition, in milliseconds.
195 * You may specify a single timeout for all transitions, or individually with an object.
196 * @default {
197 * enter: theme.transitions.duration.enteringScreen,
198 * exit: theme.transitions.duration.leavingScreen,
199 * }
200 */
201 timeout: PropTypes.oneOfType([PropTypes.number, PropTypes.shape({
202 appear: PropTypes.number,
203 enter: PropTypes.number,
204 exit: PropTypes.number
205 })])
206} : void 0;
207export default Zoom;
\No newline at end of file