-
Notifications
You must be signed in to change notification settings - Fork 180
/
sidebar.tsx
115 lines (96 loc) · 3.18 KB
/
sidebar.tsx
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
import React, {Component, HTMLAttributes} from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import {Waypoint} from 'react-waypoint';
import styles from './content-layout.css';
const ABOVE = 'above';
const INSIDE = 'inside';
export interface SidebarProps extends HTMLAttributes<HTMLElement> {
right?: boolean | null | undefined
containerClassName?: string | null | undefined
fixedClassName?: string | null | undefined
contentNode?: HTMLElement | null | undefined
}
export default class Sidebar extends Component<SidebarProps> {
static propTypes = {
right: PropTypes.bool,
children: PropTypes.node,
className: PropTypes.string,
containerClassName: PropTypes.string,
fixedClassName: PropTypes.string,
contentNode: PropTypes.object
};
state = {
topIsOutside: true,
bottomIsOutside: true,
sidebarVisibleHeight: undefined
};
sidebarNode?: HTMLElement | null;
handleTopWaypoint = ({currentPosition}: Waypoint.CallbackArgs) => {
this.setState({topIsOutside: currentPosition === ABOVE});
};
handleBottomWaypoint = ({currentPosition, waypointTop}: Waypoint.CallbackArgs) => {
this.setState({
sidebarVisibleHeight: waypointTop,
bottomIsOutside: currentPosition !== INSIDE
});
};
shouldUseFixation() {
const {contentNode} = this.props;
const {sidebarNode} = this;
if (!contentNode || !sidebarNode) {
return false;
}
return contentNode.offsetHeight >= sidebarNode.offsetHeight;
}
shouldFixateBottom() {
const {topIsOutside, bottomIsOutside} = this.state;
return !bottomIsOutside && topIsOutside && this.shouldUseFixation();
}
sidebarRef = (node: HTMLElement | null) => {
this.sidebarNode = node;
};
render() {
const {right, children, className, containerClassName,
fixedClassName, contentNode, ...restProps} = this.props;
const {topIsOutside, bottomIsOutside, sidebarVisibleHeight} = this.state;
const shouldFixateTop = bottomIsOutside && topIsOutside && this.shouldUseFixation();
const shouldFixateBottom = this.shouldFixateBottom();
const containerClasses = classNames(styles.sidebarContainer, containerClassName, {
[styles.sidebarContainerRight]: right
});
const classes = classNames(styles.sidebar, className, {
[styles.sidebarRight]: right,
[styles.sidebarFixedTop]: shouldFixateTop,
[styles.sidebarFixedBottom]: shouldFixateBottom,
[fixedClassName ?? '']: shouldFixateTop || shouldFixateBottom
});
const style = {
maxHeight: shouldFixateBottom && sidebarVisibleHeight ? `${sidebarVisibleHeight}px` : undefined
};
return (
<aside
className={containerClasses}
ref={this.sidebarRef}
>
<Waypoint
onEnter={this.handleTopWaypoint}
onLeave={this.handleTopWaypoint}
/>
<div
{...restProps}
style={style}
className={classes}
>
{children}
</div>
<div className={styles.bottomMarker}>
<Waypoint
onEnter={this.handleBottomWaypoint}
onLeave={this.handleBottomWaypoint}
/>
</div>
</aside>
);
}
}