| 1 | import { useCallback, useEffect, useRef, useState } from 'react';
|
|---|
| 2 | import { noop } from '../util/DataUtils';
|
|---|
| 3 | import { resolveDefaultProps } from '../util/resolveDefaultProps';
|
|---|
| 4 | import { useAnimationManager } from './useAnimationManager';
|
|---|
| 5 | import { getTransitionVal } from './util';
|
|---|
| 6 | import { Global } from '../util/Global';
|
|---|
| 7 | var defaultProps = {
|
|---|
| 8 | begin: 0,
|
|---|
| 9 | duration: 1000,
|
|---|
| 10 | easing: 'ease',
|
|---|
| 11 | isActive: true,
|
|---|
| 12 | canBegin: true,
|
|---|
| 13 | onAnimationEnd: () => {},
|
|---|
| 14 | onAnimationStart: () => {}
|
|---|
| 15 | };
|
|---|
| 16 | export function CSSTransitionAnimate(outsideProps) {
|
|---|
| 17 | var props = resolveDefaultProps(outsideProps, defaultProps);
|
|---|
| 18 | var {
|
|---|
| 19 | animationId,
|
|---|
| 20 | from,
|
|---|
| 21 | to,
|
|---|
| 22 | attributeName,
|
|---|
| 23 | isActive: isActiveProp,
|
|---|
| 24 | canBegin,
|
|---|
| 25 | duration,
|
|---|
| 26 | easing,
|
|---|
| 27 | begin,
|
|---|
| 28 | onAnimationEnd,
|
|---|
| 29 | onAnimationStart: onAnimationStartFromProps,
|
|---|
| 30 | children
|
|---|
| 31 | } = props;
|
|---|
| 32 | var isActive = isActiveProp === 'auto' ? !Global.isSsr : isActiveProp;
|
|---|
| 33 | var animationManager = useAnimationManager(animationId + attributeName, props.animationManager);
|
|---|
| 34 | var [style, setStyle] = useState(() => {
|
|---|
| 35 | if (!isActive) {
|
|---|
| 36 | return to;
|
|---|
| 37 | }
|
|---|
| 38 | return from;
|
|---|
| 39 | });
|
|---|
| 40 | var initialized = useRef(false);
|
|---|
| 41 | var onAnimationStart = useCallback(() => {
|
|---|
| 42 | setStyle(from);
|
|---|
| 43 | onAnimationStartFromProps();
|
|---|
| 44 | }, [from, onAnimationStartFromProps]);
|
|---|
| 45 | useEffect(() => {
|
|---|
| 46 | if (!isActive || !canBegin) {
|
|---|
| 47 | return noop;
|
|---|
| 48 | }
|
|---|
| 49 | initialized.current = true;
|
|---|
| 50 | var unsubscribe = animationManager.subscribe(setStyle);
|
|---|
| 51 | animationManager.start([onAnimationStart, begin, to, duration, onAnimationEnd]);
|
|---|
| 52 | return () => {
|
|---|
| 53 | animationManager.stop();
|
|---|
| 54 | if (unsubscribe) {
|
|---|
| 55 | unsubscribe();
|
|---|
| 56 | }
|
|---|
| 57 | onAnimationEnd();
|
|---|
| 58 | };
|
|---|
| 59 | }, [isActive, canBegin, duration, easing, begin, onAnimationStart, onAnimationEnd, animationManager, to, from]);
|
|---|
| 60 | if (!isActive) {
|
|---|
| 61 | /*
|
|---|
| 62 | * With isActive=false, the component always renders with the final style, immediately,
|
|---|
| 63 | * and ignores all other props.
|
|---|
| 64 | * Also there is no transition applied.
|
|---|
| 65 | */
|
|---|
| 66 | return children({
|
|---|
| 67 | [attributeName]: to
|
|---|
| 68 | });
|
|---|
| 69 | }
|
|---|
| 70 | if (!canBegin) {
|
|---|
| 71 | return children({
|
|---|
| 72 | [attributeName]: from
|
|---|
| 73 | });
|
|---|
| 74 | }
|
|---|
| 75 | if (initialized.current) {
|
|---|
| 76 | var transition = getTransitionVal([attributeName], duration, easing);
|
|---|
| 77 | return children({
|
|---|
| 78 | transition,
|
|---|
| 79 | [attributeName]: style
|
|---|
| 80 | });
|
|---|
| 81 | }
|
|---|
| 82 | return children({
|
|---|
| 83 | [attributeName]: from
|
|---|
| 84 | });
|
|---|
| 85 | } |
|---|