72 lines
2.5 KiB
JavaScript
Executable File
72 lines
2.5 KiB
JavaScript
Executable File
const _excluded = ["onEnter", "onEntering", "onEntered", "onExit", "onExiting", "onExited", "addEndListener", "children"];
|
|
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
|
|
import { cloneElement, useCallback, useRef } from 'react';
|
|
import useMergedRefs from '@restart/hooks/useMergedRefs';
|
|
import { getReactVersion } from './utils';
|
|
/**
|
|
* Normalizes RTG transition callbacks with nodeRef to better support
|
|
* strict mode.
|
|
*
|
|
* @param props Transition props.
|
|
* @returns Normalized transition props.
|
|
*/
|
|
export default function useRTGTransitionProps(_ref) {
|
|
let {
|
|
onEnter,
|
|
onEntering,
|
|
onEntered,
|
|
onExit,
|
|
onExiting,
|
|
onExited,
|
|
addEndListener,
|
|
children
|
|
} = _ref,
|
|
props = _objectWithoutPropertiesLoose(_ref, _excluded);
|
|
const {
|
|
major
|
|
} = getReactVersion();
|
|
const childRef = major >= 19 ? children.props.ref : children.ref;
|
|
const nodeRef = useRef(null);
|
|
const mergedRef = useMergedRefs(nodeRef, typeof children === 'function' ? null : childRef);
|
|
const normalize = callback => param => {
|
|
if (callback && nodeRef.current) {
|
|
callback(nodeRef.current, param);
|
|
}
|
|
};
|
|
|
|
/* eslint-disable react-hooks/exhaustive-deps */
|
|
const handleEnter = useCallback(normalize(onEnter), [onEnter]);
|
|
const handleEntering = useCallback(normalize(onEntering), [onEntering]);
|
|
const handleEntered = useCallback(normalize(onEntered), [onEntered]);
|
|
const handleExit = useCallback(normalize(onExit), [onExit]);
|
|
const handleExiting = useCallback(normalize(onExiting), [onExiting]);
|
|
const handleExited = useCallback(normalize(onExited), [onExited]);
|
|
const handleAddEndListener = useCallback(normalize(addEndListener), [addEndListener]);
|
|
/* eslint-enable react-hooks/exhaustive-deps */
|
|
|
|
return Object.assign({}, props, {
|
|
nodeRef
|
|
}, onEnter && {
|
|
onEnter: handleEnter
|
|
}, onEntering && {
|
|
onEntering: handleEntering
|
|
}, onEntered && {
|
|
onEntered: handleEntered
|
|
}, onExit && {
|
|
onExit: handleExit
|
|
}, onExiting && {
|
|
onExiting: handleExiting
|
|
}, onExited && {
|
|
onExited: handleExited
|
|
}, addEndListener && {
|
|
addEndListener: handleAddEndListener
|
|
}, {
|
|
children: typeof children === 'function' ? (status, innerProps) =>
|
|
// TODO: Types for RTG missing innerProps, so need to cast.
|
|
children(status, Object.assign({}, innerProps, {
|
|
ref: mergedRef
|
|
})) : /*#__PURE__*/cloneElement(children, {
|
|
ref: mergedRef
|
|
})
|
|
});
|
|
} |