File size: 3,501 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import React, { useEffect, useState } from 'react';
import { storiesOf } from '@storybook/react';

import TransitionGroup from '../src/TransitionGroup';

import CSSTransitionGroupFixture from './CSSTransitionGroupFixture';
import NestedTransition from './NestedTransition';
import StoryFixture from './StoryFixture';
import Fade, { FADE_TIMEOUT } from './transitions/CSSFadeForTransitionGroup';

storiesOf('Css Transition Group', module)
  .add('Animates on all', () => (
    <CSSTransitionGroupFixture
      description={`
        Should animate when items are added to the list but not when they are
        removed or on initial appear
      `}
      appear
      items={['Item number: 1']}
    >
      <Fade />
    </CSSTransitionGroupFixture>
  ))
  .add('Animates on enter', () => (
    <CSSTransitionGroupFixture
      description={`
        Should animate when items are added to the list but not when they are
        removed or on initial appear
      `}
      exit={false}
      timeout={{ enter: FADE_TIMEOUT }}
      items={['Item number: 1']}
    >
      <Fade />
    </CSSTransitionGroupFixture>
  ))
  .add('Animates on exit', () => (
    <CSSTransitionGroupFixture
      description={`
        Should animate when items are removed to the list but not when they are
        added or on initial appear
      `}
      items={['Item number: 1', 'Item number: 2', 'Item number: 3']}
    >
      <Fade enter={false} timeout={{ exit: FADE_TIMEOUT }} />
    </CSSTransitionGroupFixture>
  ))
  .add('Animates on appear', () => (
    <CSSTransitionGroupFixture
      description={`
        Should animate when items first mount but not when added or removed
      `}
      appear
      items={['Item number: 1', 'Item number: 2', 'Item number: 3']}
    >
      <Fade exit={false} enter={false} />
    </CSSTransitionGroupFixture>
  ))
  .add('Dynamic props', () => (
    <StoryFixture
      description={`
        Updates to children should not break animations
      `}
    >
      <DynamicTransition />
    </StoryFixture>
  ))
  .add('Re-entering while leaving', () => (
    <StoryFixture
      description={`
        Should animate on enter even while exiting
      `}
    >
      <ReEnterTransition />
    </StoryFixture>
  ))
  .add('Nested Transitions', () => <NestedTransition />);

class DynamicTransition extends React.Component {
  state = { count: 0 };
  handleClick = () => {
    this.setState({ hide: !this.state.hide });
  };

  componentDidMount() {
    this.interval = setInterval(() => {
      this.setState({ count: this.state.count + 1 });
    }, 700);
  }
  componentWillUnmount() {
    clearInterval(this.interval);
  }

  render() {
    const { hide, count } = this.state;
    return (
      <div>
        <button onClick={this.handleClick}>Toggle item</button>
        <TransitionGroup timeout={FADE_TIMEOUT}>
          {!hide && <Fade key="item">Changing! {count}</Fade>}
        </TransitionGroup>
      </div>
    );
  }
}

function ReEnterTransition() {
  const [hide, setHide] = useState(false);

  useEffect(() => {
    if (hide) {
      setTimeout(() => {
        console.log('re-entering!');
        setHide(false);
      }, 0.5 * FADE_TIMEOUT);
    }
  }, [hide]);

  return (
    <div>
      <button
        onClick={() => {
          setHide(true);
        }}
      >
        Remove and re-add
      </button>
      <TransitionGroup timeout={FADE_TIMEOUT}>
        {!hide && <Fade key="item">I'm entering!</Fade>}
      </TransitionGroup>
    </div>
  );
}