All files / components/StickySection StickySection.jsx

89.29% Statements 25/28
63.33% Branches 19/30
100% Functions 1/1
89.29% Lines 25/28
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200            120x           120x               120x                                                       12x                 2x         2x   2x   2x 2x 1x                       2x 1x         2x                   2x             3x 2x 2x     2x                         1x 1x         1x       1x                   19x         19x   19x                                                                                                      
import React from 'react';
import _ from 'lodash';
import { lucidClassNames } from '../../util/style-helpers';
import { createClass, omitProps } from '../../util/component-types';
import { getAbsoluteBoundingClientRect } from '../../util/dom-helpers';
 
const cx = lucidClassNames.bind('&-StickySection');
const {
	node,
	number,
	object,
	string,
} = React.PropTypes;
 
/**
 * {"categories": ["helpers"]}
 *
 * `StickySection` can be wrapped around any content to make it _stick_ to the
 * top edge of the screen when a user scrolls beyond its initial location.
 */
const StickySection = createClass({
	displayName: 'StickySection',
	propTypes: {
		/**
		 * any valid React children
		 */
		children: node,
		/**
		 * Appended to the component-specific class names set on the root element.
		 */
		className: string,
		/**
		 * Styles that are passed through to the root container.
		 */
		style: object,
		/**
		 * Pixel value from the top of the document. When scrolled passed, the
		 * sticky header is no longer sticky, and renders normally.
		 */
		lowerBound: number,
		/**
		 * Width of section when it sticks to the top edge of the screen. When
		 * omitted, it defaults to the last width of the section.
		 */
		viewportWidth: number,
	},
 
	getInitialState() {
		return {
			isAboveFold: false,
			containerRect: {},
		};
	},
 
	handleScroll() {
		const {
			lowerBound,
		} = this.props;
 
		const {
			isAboveFold,
			containerRect,
		} = this.state;
 
		const nextContainerRect = this.getContainerRect();
 
		Eif (window.pageYOffset >= nextContainerRect.top) {
			if (!isAboveFold) {
				this.setState({
					isAboveFold: true,
				});
			}
		} else {
			if (isAboveFold) {
				this.setState({
					isAboveFold: false,
				});
			}
		}
 
		if (_.isNumber(lowerBound) && window.pageYOffset >= lowerBound) {
			this.setState({
				isAboveFold: false,
			});
		}
 
		Eif (
			containerRect.bottom !== nextContainerRect.bottom
			|| containerRect.height !== nextContainerRect.height
			|| containerRect.left !== nextContainerRect.left
			|| containerRect.right !== nextContainerRect.right
			|| containerRect.top !== nextContainerRect.top
			|| containerRect.width !== nextContainerRect.width
			|| containerRect.scrollWidth !== nextContainerRect.scrollWidth
			|| containerRect.frameLeft !== nextContainerRect.frameLeft
		) {
			this.setState({
				containerRect: nextContainerRect,
			});
		}
	},
 
	getContainerRect() {
		const containerRect = getAbsoluteBoundingClientRect(this.refs.scrollContainer);
		const stickyRect = this.refs.stickySection.getBoundingClientRect();
		const frameRect = this.refs.stickyFrame.getBoundingClientRect();
 
 
		return {
			bottom: containerRect.top + stickyRect.height,
			height: stickyRect.height,
			left: containerRect.left,
			right: containerRect.left + stickyRect.width,
			top: containerRect.top,
			scrollWidth: this.refs.stickySection.scrollWidth,
			width: containerRect.width,
			frameLeft: frameRect.left,
		};
	},
 
	componentDidMount() {
		setTimeout(() => {
			this.setState({
				containerRect: this.getContainerRect(),
			});
			this.handleScroll();
		}, 1);
		window.addEventListener('scroll', this.handleScroll, true);
	},
 
	componentWillUnmount() {
		window.removeEventListener('scroll', this.handleScroll, true);
	},
 
	render() {
		const {
			children,
			className,
			style,
			viewportWidth,
			...passThroughs,
		} = this.props;
 
		const {
			isAboveFold,
			containerRect,
		} = this.state;
 
		return (
			<div
				{...omitProps(passThroughs, StickySection)}
				className={cx('&', className)}
				style={{
					...(isAboveFold ? {
						height: containerRect.height,
					} : {}),
					...style,
				}}
				ref='scrollContainer'
			>
				<div
					className={cx('&-sticky-frame')}
					ref='stickyFrame'
					style={{
						...(isAboveFold ? {
							position: 'fixed',
							top: 0,
							width: (_.isNumber(viewportWidth) ? viewportWidth : containerRect.width),
							height: containerRect.height,
							overflow: 'hidden',
						} : {}),
						...style,
					}}
				>
					<div
						className={cx('&-sticky-section')}
						ref='stickySection'
						style={{
							...(isAboveFold ? {
								position: 'absolute',
								top: 0,
								left: (containerRect.left - containerRect.frameLeft) || 0,
								width: containerRect.scrollWidth,
								height: containerRect.height,
							} : {
								position: 'relative',
							}),
							...style,
						}}
					>
						{children}
					</div>
				</div>
			</div>
		);
	},
});
 
export default StickySection;