|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the CC-BY-4.0 license found |
| 5 | + * in the LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @emails react-core |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +import React, {Component} from 'react'; |
| 13 | +import Section from './Section'; |
| 14 | + |
| 15 | +class ScrollSyncSection extends Component { |
| 16 | + constructor(props, context) { |
| 17 | + super(props, context); |
| 18 | + |
| 19 | + this.state = { |
| 20 | + activeItemId: '', |
| 21 | + itemTopOffsets: [], |
| 22 | + }; |
| 23 | + |
| 24 | + this.calculateItemTopOffsets = this.calculateItemTopOffsets.bind(this); |
| 25 | + this.handleResize = this.handleResize.bind(this); |
| 26 | + this.handleScroll = this.handleScroll.bind(this); |
| 27 | + } |
| 28 | + |
| 29 | + componentDidMount() { |
| 30 | + this.calculateItemTopOffsets(); |
| 31 | + |
| 32 | + window.addEventListener('resize', this.handleResize); |
| 33 | + window.addEventListener('scroll', this.handleScroll); |
| 34 | + } |
| 35 | + |
| 36 | + componentWillUnmount() { |
| 37 | + window.removeEventListener('resize', this.handleResize); |
| 38 | + window.removeEventListener('scroll', this.handleScroll); |
| 39 | + } |
| 40 | + |
| 41 | + calculateItemTopOffsets() { |
| 42 | + const {section} = this.props; |
| 43 | + |
| 44 | + const itemIds = _getItemIds(section.items); |
| 45 | + this.setState({ |
| 46 | + itemTopOffsets: _getElementTopOffsetsById(itemIds), |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + handleResize() { |
| 51 | + this.calculateItemTopOffsets(); |
| 52 | + this.handleScroll(); |
| 53 | + } |
| 54 | + |
| 55 | + handleScroll() { |
| 56 | + const {itemTopOffsets} = this.state; |
| 57 | + const item = itemTopOffsets.find((itemTopOffset, i) => { |
| 58 | + const nextItemTopOffset = itemTopOffsets[i + 1]; |
| 59 | + if (nextItemTopOffset) { |
| 60 | + return ( |
| 61 | + window.scrollY >= itemTopOffset.offsetTop && |
| 62 | + window.scrollY < nextItemTopOffset.offsetTop |
| 63 | + ); |
| 64 | + } |
| 65 | + return window.scrollY >= itemTopOffset.offsetTop; |
| 66 | + }); |
| 67 | + this.setState({ |
| 68 | + activeItemId: item ? item.id : '', |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + render() { |
| 73 | + const {activeItemId} = this.state; |
| 74 | + return <Section isScrollSync activeItemId={activeItemId} {...this.props} />; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +const _getItemIds = items => |
| 79 | + items |
| 80 | + .map(item => { |
| 81 | + let subItemIds = []; |
| 82 | + if (item.subitems) { |
| 83 | + subItemIds = item.subitems.map(subitem => subitem.id); |
| 84 | + } |
| 85 | + return [item.id, ...subItemIds]; |
| 86 | + }) |
| 87 | + .reduce((prev, current) => prev.concat(current)); |
| 88 | + |
| 89 | +const _getElementTopOffsetsById = ids => |
| 90 | + ids |
| 91 | + .map(id => { |
| 92 | + const element = document.getElementById(id); |
| 93 | + if (!element) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + return { |
| 97 | + id, |
| 98 | + offsetTop: element.offsetTop, |
| 99 | + }; |
| 100 | + }) |
| 101 | + .filter(item => item); |
| 102 | + |
| 103 | +export default ScrollSyncSection; |
0 commit comments