Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | 176x 176x 176x 176x 176x 12x 12x 12x 12x 12x 12x 4x 4x 15x 15x 15x | import React from 'react';
import PropTypes from 'react-peek/prop-types';
import _ from 'lodash';
import { lucidClassNames } from '../../util/style-helpers';
import { omitProps, StandardProps } from '../../util/component-types';
import { getAbsoluteBoundingClientRect } from '../../util/dom-helpers';
const cx = lucidClassNames.bind('&-StickySection');
const { node, number, object, string } = PropTypes;
interface IStickySectionProps
extends StandardProps,
React.DetailedHTMLProps<
React.HTMLAttributes<HTMLDivElement>,
HTMLDivElement
> {
/** 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;
/** Top offset threshold before sticking to the top. The sticky content will
* display with this offset. */
topOffset?: number;
}
interface IContainerRect extends ClientRect {
scrollWidth: number;
frameLeft: number;
}
interface IStickySectionState {
isAboveFold: boolean;
containerRect: IContainerRect;
}
class StickySection extends React.Component<
IStickySectionProps,
IStickySectionState,
{}
> {
static displayName = 'StickySection';
static peek = {
description: `
\`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.
`,
categories: ['helpers'],
};
static propTypes = {
children: node`
any valid React children
`,
className: string`
Appended to the component-specific class names set on the root element.
`,
style: object`
Styles that are passed through to the root container.
`,
lowerBound: number`
Pixel value from the top of the document. When scrolled passed, the
sticky header is no longer sticky, and renders normally.
`,
viewportWidth: 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.
`,
topOffset: number`
Top offset threshold before sticking to the top. The sticky content will
display with this offset.
`,
};
private scrollContainer = React.createRef<HTMLDivElement>();
private stickySection = React.createRef<HTMLDivElement>();
private stickyFrame = React.createRef<HTMLDivElement>();
state = {
isAboveFold: false,
containerRect: {
bottom: 0,
height: 0,
left: 0,
right: 0,
top: 0,
width: 0,
frameLeft: 0,
scrollWidth: 0,
},
};
handleScroll = (): void => {
const { lowerBound, topOffset = 0 } = this.props;
const { isAboveFold, containerRect } = this.state;
const nextContainerRect = this.getContainerRect();
if (window.pageYOffset + topOffset >= 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,
});
}
if (
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 = (): IContainerRect => {
const containerRect = getAbsoluteBoundingClientRect(
this.scrollContainer.current as HTMLElement
);
const stickyRect = (this.stickySection
.current as HTMLElement).getBoundingClientRect();
const frameRect = (this.stickyFrame
.current as HTMLElement).getBoundingClientRect();
return {
bottom: containerRect.top + stickyRect.height,
height: stickyRect.height,
left: containerRect.left,
right: containerRect.left + stickyRect.width,
top: containerRect.top,
scrollWidth: (this.stickySection.current as HTMLElement).scrollWidth,
width: containerRect.width,
frameLeft: frameRect.left,
};
};
componentDidMount(): void {
setTimeout((): void => {
this.setState({
containerRect: this.getContainerRect(),
});
this.handleScroll();
}, 1);
window.addEventListener('scroll', this.handleScroll, true);
}
componentWillUnmount(): void {
window.removeEventListener('scroll', this.handleScroll, true);
}
render(): React.ReactNode {
const {
children,
className,
style,
topOffset = 0,
viewportWidth,
...passThroughs
} = this.props;
const { isAboveFold, containerRect } = this.state;
return (
<div
{...omitProps<IStickySectionProps>(
passThroughs,
undefined,
Object.keys(StickySection.propTypes)
)}
className={cx('&', className)}
style={{
...(isAboveFold
? {
height: containerRect.height,
visibility: 'hidden',
}
: {}),
...style,
}}
ref={this.scrollContainer}
>
<div
className={cx('&-sticky-frame')}
ref={this.stickyFrame}
style={{
...(isAboveFold
? {
visibility: 'visible',
position: 'fixed',
top: topOffset,
width: _.isNumber(viewportWidth)
? viewportWidth
: containerRect.width,
height: containerRect.height,
overflow: 'hidden',
}
: {}),
...style,
}}
>
<div
className={cx('&-sticky-section')}
ref={this.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;
|