|
1 | 1 | import React, {Component} from 'react'; |
2 | 2 |
|
3 | | -const expandAnimationStyles = { |
4 | | - open: { |
5 | | - text: { |
6 | | - transition: 'width 0.75s cubic-bezier(0.000, 0.795, 0.000, 1.000)', |
7 | | - width: 300, |
8 | | - }, |
9 | | - frame: { |
10 | | - transition: 'width 0.75s cubic-bezier(0.000, 0.795, 0.000, 1.000)', |
11 | | - width: 300, |
12 | | - position: 'absolute', |
13 | | - left: 0, |
14 | | - top:-25, |
15 | | - marginLeft: -150 |
16 | | - } |
17 | | - }, |
18 | | - closed: { |
19 | | - text: { |
20 | | - transition: 'width 0.75s cubic-bezier(0.000, 0.795, 0.000, 1.000)', |
21 | | - width: 0, |
22 | | - }, |
23 | | - frame: { |
24 | | - transition: 'width 0.75s cubic-bezier(0.000, 0.795, 0.000, 1.000)', |
25 | | - width: 0, |
26 | | - } |
27 | | - }, |
28 | | - |
29 | | -}; |
30 | | -const translationAnimationStyles = { |
31 | | - open: { |
32 | | - text: { |
33 | | - width: 0, |
34 | | - }, |
35 | | - frame: { |
36 | | - transform: 'translateX(-150px)', |
37 | | - transition: 'transform 0.5s ease' |
38 | | - } |
39 | | - }, |
40 | | - closed: { |
41 | | - text: { |
42 | | - width: 0, |
43 | | - }, |
44 | | - frame: { |
45 | | - transition: 'transform 0.5s ease' |
46 | | - } |
47 | | - } |
| 3 | +const animationStyle = { |
| 4 | + transition: 'width 0.75s cubic-bezier(0.000, 0.795, 0.000, 1.000)' |
48 | 5 | }; |
49 | 6 |
|
50 | 7 | const makeExpanding = (Target) => { |
51 | 8 | return class extends Component { |
52 | 9 | constructor(props) { |
53 | 10 | super(props); |
54 | | - this.state = { |
55 | | - isOpen: false, |
56 | | - animationStyles: [expandAnimationStyles.closed] |
57 | | - }; |
| 11 | + this.state = {isOpen: false}; |
58 | 12 | } |
59 | 13 |
|
60 | 14 | onClick = () => { |
61 | 15 | let newState = !this.state.isOpen; |
62 | | - let newAnimationStyles; |
63 | | - |
64 | | - if (newState) newAnimationStyles = [translationAnimationStyles.open, expandAnimationStyles.open]; |
65 | | - else newAnimationStyles = [expandAnimationStyles.closed, translationAnimationStyles.closed]; |
66 | | - |
67 | 16 | this.setState({ |
68 | | - isOpen: newState, |
69 | | - animationStyles: newAnimationStyles |
| 17 | + isOpen: newState |
70 | 18 | }); |
71 | 19 | }; |
72 | | - handleTransitionEnd = ({target, propertyName, elapsedTime}) => { |
73 | | - console.log(`${target.innerHTML}:${this.animationWrapper.innerHTML}`); |
74 | | - if (propertyName === 'transform' || propertyName === 'width') { |
75 | | - let animationStyles = this.state.animationStyles; |
76 | | - if (animationStyles.length > 1) |
77 | | - animationStyles.splice(0, 1); |
78 | | - |
79 | | - this.setState({animationStyles: animationStyles}); |
80 | | - } |
81 | | - }; |
82 | 20 |
|
83 | 21 | render() { |
84 | | - const refCallback = (animationWrapper) => { |
85 | | - this.animationWrapper = animationWrapper; |
86 | | - }; |
87 | 22 | return ( |
88 | | - <div ref={refCallback} onTransitionEnd={this.handleTransitionEnd} > |
89 | | - <Target isOpen={this.state.isOpen} |
90 | | - clickHandler={this.onClick} |
91 | | - additionalStyles={this.state.animationStyles[0]} |
92 | | - /> |
93 | | - |
94 | | - </div> |
| 23 | + <Target isOpen={this.state.isOpen} |
| 24 | + clickHandler={this.onClick} |
| 25 | + additionalStyles={{text:animationStyle, frame:animationStyle}} /> |
95 | 26 | ); |
96 | 27 | } |
97 | 28 | } |
|
0 commit comments