-
-
Notifications
You must be signed in to change notification settings - Fork 477
/
Sidebar.jsx
174 lines (147 loc) · 4.35 KB
/
Sidebar.jsx
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
164
165
166
167
168
169
170
171
172
173
174
import {Component} from 'preact';
import cls from 'classnames';
import s from './Sidebar.css';
import Button from './Button';
import Icon from './Icon';
const toggleTime = parseInt(s.toggleTime);
export default class Sidebar extends Component {
static defaultProps = {
pinned: false,
position: 'left'
};
allowHide = true;
toggling = false;
hideContentTimeout = null;
width = null;
state = {
visible: true,
renderContent: true
};
componentDidMount() {
this.hideTimeoutId = setTimeout(() => this.toggleVisibility(false), 3000);
}
componentWillUnmount() {
clearTimeout(this.hideTimeoutId);
clearTimeout(this.hideContentTimeout);
}
render() {
const {position, pinned, children} = this.props;
const {visible, renderContent} = this.state;
const className = cls({
[s.container]: true,
[s.pinned]: pinned,
[s.left]: (position === 'left'),
[s.hidden]: !visible,
[s.empty]: !renderContent
});
return (
<div ref={this.saveNode}
className={className}
onClick={this.handleClick}
onMouseLeave={this.handleMouseLeave}>
{visible &&
<Button type="button"
title="Pin"
className={s.pinButton}
active={pinned}
toggle
onClick={this.handlePinButtonClick}>
<Icon name="pin" size={13}/>
</Button>
}
<Button type="button"
title={visible ? 'Hide' : 'Show sidebar'}
className={s.toggleButton}
onClick={this.handleToggleButtonClick}>
<Icon name="arrow-right" size={10} rotate={visible ? 180 : 0}/>
</Button>
{pinned && visible &&
<div className={s.resizer} onMouseDown={this.handleResizeStart}/>
}
<div className={s.content}
onMouseEnter={this.handleMouseEnter}
onMouseMove={this.handleMouseMove}>
{renderContent ? children : null}
</div>
</div>
);
}
handleClick = () => {
this.allowHide = false;
}
handleMouseEnter = () => {
if (!this.toggling && !this.props.pinned) {
clearTimeout(this.hideTimeoutId);
this.toggleVisibility(true);
}
};
handleMouseMove = () => {
this.allowHide = true;
}
handleMouseLeave = () => {
if (this.allowHide && !this.toggling && !this.props.pinned) {
this.toggleVisibility(false);
}
}
handleToggleButtonClick = () => {
this.toggleVisibility();
}
handlePinButtonClick = () => {
const pinned = !this.props.pinned;
this.width = pinned ? this.node.getBoundingClientRect().width : null;
this.updateNodeWidth();
this.props.onPinStateChange(pinned);
}
handleResizeStart = event => {
this.resizeInfo = {
startPageX: event.pageX,
initialWidth: this.width
};
document.body.classList.add('resizing', 'col');
document.addEventListener('mousemove', this.handleResize, true);
document.addEventListener('mouseup', this.handleResizeEnd, true);
}
handleResize = event => {
this.width = this.resizeInfo.initialWidth + (event.pageX - this.resizeInfo.startPageX);
this.updateNodeWidth();
}
handleResizeEnd = () => {
document.body.classList.remove('resizing', 'col');
document.removeEventListener('mousemove', this.handleResize, true);
document.removeEventListener('mouseup', this.handleResizeEnd, true);
this.props.onResize();
}
toggleVisibility(flag) {
clearTimeout(this.hideContentTimeout);
const {visible} = this.state;
const {onToggle, pinned} = this.props;
if (flag === undefined) {
flag = !visible;
} else if (flag === visible) {
return;
}
this.setState({visible: flag});
this.toggling = true;
setTimeout(() => {
this.toggling = false;
}, toggleTime);
if (pinned) {
this.updateNodeWidth(flag ? this.width : null);
}
if (flag || pinned) {
this.setState({renderContent: flag});
onToggle(flag);
} else if (!flag) {
// Waiting for the CSS animation to finish and hiding content
this.hideContentTimeout = setTimeout(() => {
this.hideContentTimeout = null;
this.setState({renderContent: false});
onToggle(false);
}, toggleTime);
}
}
saveNode = node => this.node = node;
updateNodeWidth(width = this.width) {
this.node.style.width = width ? `${width}px` : '';
}
}