-
Notifications
You must be signed in to change notification settings - Fork 1
/
Inbox.stories.js
163 lines (143 loc) · 3.32 KB
/
Inbox.stories.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React from 'react'
import {storiesOf} from '@storybook/react'
import {StatsGraph} from '@helpscout/stats'
import styled from '@helpscout/fancy'
import {createSpec, faker} from '@helpscout/helix'
import withMotion from '../src/withMotion'
const stories = storiesOf('Inbox', module)
const ItemSpec = createSpec({
title: faker.lorem.sentence(),
excerpt: faker.lorem.paragraph(),
})
stories.add('Example', () => {
class Example extends React.Component {
state = {
items: ItemSpec.generate(5),
}
add = () => {
this.setState({
items: [...this.state.items, ItemSpec.generate()],
})
}
remove = () => {
this.setState({
items: this.state.items.slice(0, -1),
})
}
render() {
return (
<div>
<StatsGraph />
<button onClick={this.add}>Add Link</button>
<button onClick={this.remove}>Remove Link</button>
<hr />
<List>
{this.state.items.map((item, index) => (
<AnimatedListItem key={item.title} {...item} />
))}
</List>
</div>
)
}
}
return <Example />
})
class ListItem extends React.PureComponent {
state = {
expanded: false,
}
toggle = () => {
this.setState({
expanded: !this.state.expanded,
})
}
render() {
return (
<Item key={this.props.title} onClick={this.toggle}>
<ItemInner>
{this.props.title}
<AnimatedExcerpt expanded={this.state.expanded}>
<div className="content">{this.props.excerpt}</div>
</AnimatedExcerpt>
</ItemInner>
</Item>
)
}
}
const List = styled('div')`
font-family: sans-serif;
position: relative;
max-width: 320px;
max-height: 320px;
overflow-y: auto;
border: 1px solid #eee;
box-sizing: border-box;
overflow-x: hidden;
`
const Item = styled('div')`
box-sizing: border-box;
background: white;
cursor: pointer;
text-transform: capitalize;
border-bottom: 1px solid #eee;
overflow: hidden;
`
const ItemInner = styled('div')`
padding: 8px 10px;
`
const Excerpt = styled('div')`
box-sizing: border-box;
overflow: hidden;
height: 10px;
position: relative;
color: #888;
font-size: 14px;
padding: 10px 0;
&::before {
content: '';
position: absolute;
bottom: 0;
width: 100%;
background: linear-gradient(rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));
height: 20px;
}
`
const AnimatedListItem = withMotion({
isAnimateOnInitialMount: false,
componentDidMount: ({node, animate}) => {
animate({
keyframes: [
{
height: [0, node.clientHeight],
opacity: [0, 1],
},
],
}).finished.then(() => {
node.style.height = 'auto'
})
},
componentWillUnmount: ({node, animate}) => {
node.style.height = `${node.clientHeight}px`
return animate({
keyframes: [
{
opacity: [1, 0],
translateX: '-100%',
},
{
height: 0,
},
],
duration: 300,
easing: 'linear',
}).finished
},
})(ListItem)
const AnimatedExcerpt = withMotion({
componentDidUpdate: ({node, animate, props}) => {
const {clientHeight: height} = node.querySelector('.content')
animate({
height: props.expanded ? height + 40 : 10,
})
},
})(Excerpt)