UNPKG

2.06 kBJavaScriptView Raw
1"use client";
2
3import React, { useCallback, useRef } from 'react';
4import Transition from 'react-transition-group/Transition';
5import useMergedRefs from '@restart/hooks/useMergedRefs';
6import safeFindDOMNode from './safeFindDOMNode';
7import { jsx as _jsx } from "react/jsx-runtime";
8// Normalizes Transition callbacks when nodeRef is used.
9const TransitionWrapper = /*#__PURE__*/React.forwardRef(({
10 onEnter,
11 onEntering,
12 onEntered,
13 onExit,
14 onExiting,
15 onExited,
16 addEndListener,
17 children,
18 childRef,
19 ...props
20}, ref) => {
21 const nodeRef = useRef(null);
22 const mergedRef = useMergedRefs(nodeRef, childRef);
23 const attachRef = r => {
24 mergedRef(safeFindDOMNode(r));
25 };
26 const normalize = callback => param => {
27 if (callback && nodeRef.current) {
28 callback(nodeRef.current, param);
29 }
30 };
31
32 /* eslint-disable react-hooks/exhaustive-deps */
33 const handleEnter = useCallback(normalize(onEnter), [onEnter]);
34 const handleEntering = useCallback(normalize(onEntering), [onEntering]);
35 const handleEntered = useCallback(normalize(onEntered), [onEntered]);
36 const handleExit = useCallback(normalize(onExit), [onExit]);
37 const handleExiting = useCallback(normalize(onExiting), [onExiting]);
38 const handleExited = useCallback(normalize(onExited), [onExited]);
39 const handleAddEndListener = useCallback(normalize(addEndListener), [addEndListener]);
40 /* eslint-enable react-hooks/exhaustive-deps */
41
42 return /*#__PURE__*/_jsx(Transition, {
43 ref: ref,
44 ...props,
45 onEnter: handleEnter,
46 onEntered: handleEntered,
47 onEntering: handleEntering,
48 onExit: handleExit,
49 onExited: handleExited,
50 onExiting: handleExiting,
51 addEndListener: handleAddEndListener,
52 nodeRef: nodeRef,
53 children: typeof children === 'function' ? (status, innerProps) =>
54 // TODO: Types for RTG missing innerProps, so need to cast.
55 children(status, {
56 ...innerProps,
57 ref: attachRef
58 }) : /*#__PURE__*/React.cloneElement(children, {
59 ref: attachRef
60 })
61 });
62});
63export default TransitionWrapper;
\No newline at end of file