UNPKG

2.68 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
5const _excluded = ["children", "className", "lastTransitionedPropertyOnExit", "enterClassName", "exitClassName"];
6import * as React from 'react';
7import PropTypes from 'prop-types';
8import clsx from 'clsx';
9import { useTransitionStateManager } from '../useTransition';
10import { jsx as _jsx } from "react/jsx-runtime";
11/**
12 * A utility component that hooks up to the Base UI transitions API and
13 * applies a CSS transition to its children when necessary.
14 *
15 * Demos:
16 *
17 * - [Transitions](https://mui.com/base-ui/react-transitions/)
18 *
19 * API:
20 *
21 * - [CssTransition API](https://mui.com/base-ui/react-transitions/components-api/#css-transition)
22 */
23const CssTransition = /*#__PURE__*/React.forwardRef(function CssTransition(props, forwardedRef) {
24 const {
25 children,
26 className,
27 lastTransitionedPropertyOnExit,
28 enterClassName,
29 exitClassName
30 } = props,
31 other = _objectWithoutPropertiesLoose(props, _excluded);
32 const {
33 requestedEnter,
34 onExited
35 } = useTransitionStateManager();
36 const [isEntering, setIsEntering] = React.useState(false);
37
38 // The `isEntering` state (which is used to determine the right CSS class to apply)
39 // is updated slightly (one animation frame) after the `requestedEnter` state is updated.
40 // Thanks to this, elements that are mounted will have their enter transition applied
41 // (if the `enterClassName` was applied when the element was mounted, the transition would not be fired).
42 React.useEffect(() => {
43 if (requestedEnter) {
44 requestAnimationFrame(() => {
45 setIsEntering(true);
46 });
47 } else {
48 setIsEntering(false);
49 }
50 }, [requestedEnter]);
51 const handleTransitionEnd = React.useCallback(event => {
52 if (!requestedEnter && (lastTransitionedPropertyOnExit == null || event.propertyName === lastTransitionedPropertyOnExit)) {
53 onExited();
54 }
55 }, [onExited, requestedEnter, lastTransitionedPropertyOnExit]);
56 return /*#__PURE__*/_jsx("div", _extends({
57 onTransitionEnd: handleTransitionEnd,
58 className: clsx(className, isEntering ? enterClassName : exitClassName)
59 }, other, {
60 ref: forwardedRef,
61 children: children
62 }));
63});
64process.env.NODE_ENV !== "production" ? CssTransition.propTypes = {
65 children: PropTypes.node,
66 className: PropTypes.string,
67 enterClassName: PropTypes.string,
68 exitClassName: PropTypes.string,
69 lastTransitionedPropertyOnEnter: PropTypes.string,
70 lastTransitionedPropertyOnExit: PropTypes.string
71} : void 0;
72export { CssTransition };
\No newline at end of file