-
Notifications
You must be signed in to change notification settings - Fork 60
/
index.js
121 lines (111 loc) · 3.46 KB
/
index.js
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
import React, { Component, Children, createRef } from "react"
import PropTypes from "prop-types"
import { Flipped } from "react-flip-toolkit"
import {
DropdownRoot,
Caret,
DropdownBackground,
AltBackground,
InvertedDiv
} from "./Components"
import FadeContents from "./FadeContents"
const getFirstDropdownSectionHeight = el => {
if (
!el ||
!el.querySelector ||
!el.querySelector("*[data-first-dropdown-section]")
)
return 0
return el.querySelector("*[data-first-dropdown-section]").offsetHeight
}
const updateAltBackground = ({
altBackground,
prevDropdown,
currentDropdown
}) => {
const prevHeight = getFirstDropdownSectionHeight(prevDropdown)
const currentHeight = getFirstDropdownSectionHeight(currentDropdown)
const immediateSetTranslateY = (el, translateY) => {
el.style.transform = `translateY(${translateY}px)`
el.style.transition = "transform 0s"
requestAnimationFrame(() => (el.style.transitionDuration = ""))
}
if (prevHeight) {
// transition the grey ("alt") background from its previous height to its current height
immediateSetTranslateY(altBackground, prevHeight)
requestAnimationFrame(() => {
altBackground.style.transform = `translateY(${currentHeight}px)`
})
} else {
// just immediately set the background to the appropriate height
// since we don't have a stored value
immediateSetTranslateY(altBackground, currentHeight)
}
}
class DropdownContainer extends Component {
static propTypes = {
children: PropTypes.node.isRequired,
animatingOut: PropTypes.bool,
direction: PropTypes.oneOf(["left", "right"]),
duration: PropTypes.number
}
currentDropdownEl = createRef()
prevDropdownEl = createRef()
componentDidMount() {
updateAltBackground({
altBackground: this.altBackgroundEl,
prevDropdown: this.prevDropdownEl.current,
currentDropdown: this.currentDropdownEl.current,
duration: this.props.duration
})
}
render() {
const { children, direction, animatingOut, duration } = this.props
const [currentDropdown, prevDropdown] = Children.toArray(children)
return (
<DropdownRoot
direction={direction}
animatingOut={animatingOut}
duration={duration}
>
<Flipped flipId="dropdown-caret">
<Caret />
</Flipped>
<Flipped flipId="dropdown">
<DropdownBackground>
<Flipped inverseFlipId="dropdown">
<InvertedDiv>
<AltBackground
ref={el => (this.altBackgroundEl = el)}
duration={duration}
/>
<FadeContents
direction={direction}
duration={duration}
ref={this.currentDropdownEl}
>
{currentDropdown}
</FadeContents>
</InvertedDiv>
</Flipped>
<Flipped inverseFlipId="dropdown" scale>
<InvertedDiv absolute>
{prevDropdown && (
<FadeContents
animatingOut
direction={direction}
duration={duration}
ref={this.prevDropdownEl}
>
{prevDropdown}
</FadeContents>
)}
</InvertedDiv>
</Flipped>
</DropdownBackground>
</Flipped>
</DropdownRoot>
)
}
}
export default DropdownContainer