|
| 1 | +import React, { Component } from 'react' |
| 2 | +import { findDOMNode } from 'react-dom' |
| 3 | + |
| 4 | +import Loadable from 'react-loadable' |
| 5 | + |
| 6 | +let intersectionObserver |
| 7 | +let trackedElements = {} |
| 8 | + |
| 9 | +if (window && window.IntersectionObserver) { |
| 10 | + intersectionObserver = new window.IntersectionObserver((entries, observer) => { |
| 11 | + entries.forEach((entry) => { |
| 12 | + if (entry.intersectionRatio > 0 && trackedElements[entry.target]) { |
| 13 | + const component = trackedElements[entry.target] |
| 14 | + |
| 15 | + component.visibilityHandler() |
| 16 | + } |
| 17 | + }) |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +function createLoadableVisibilityComponent (opts, Loadable) { |
| 22 | + let preloaded = false |
| 23 | + const visibilityHandlers = [] |
| 24 | + |
| 25 | + const LoadableComponent = Loadable(opts) |
| 26 | + |
| 27 | + return class LoadableVisibilityComponent extends Component { |
| 28 | + static preload() { |
| 29 | + if (!preloaded) { |
| 30 | + preloaded = true |
| 31 | + visibilityHandlers.forEach((handler) => handler()) |
| 32 | + } |
| 33 | + |
| 34 | + LoadableComponent.preload() |
| 35 | + } |
| 36 | + |
| 37 | + constructor(props) { |
| 38 | + super(props) |
| 39 | + |
| 40 | + if (!preloaded) { |
| 41 | + visibilityHandlers.push(this.visibilityHandler) |
| 42 | + } |
| 43 | + |
| 44 | + this.state = { |
| 45 | + visible: preloaded, |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + attachRef = (element) => { |
| 50 | + this.loadingRef = element |
| 51 | + |
| 52 | + if (element) { |
| 53 | + trackedElements[element] = this |
| 54 | + intersectionObserver.observe(element) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + componentWillUnmount() { |
| 59 | + if (this.loadingRef) { |
| 60 | + intersectionObserver.unobserve(this.loadingRef) |
| 61 | + delete trackedElements[this.loadingRef] |
| 62 | + } |
| 63 | + |
| 64 | + const handlerIndex = visibilityHandlers.indexOf(this.visibilityHandler) |
| 65 | + |
| 66 | + if (handlerIndex >= 0) { |
| 67 | + visibilityHandlers.splice(handlerIndex, 1) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + visibilityHandler = () => { |
| 72 | + const node = findDOMNode(this) |
| 73 | + |
| 74 | + intersectionObserver.unobserve(node) |
| 75 | + delete trackedElements[node] |
| 76 | + |
| 77 | + this.setState({ |
| 78 | + visible: true |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + render() { |
| 83 | + if (this.state.visible) { |
| 84 | + return <LoadableComponent {...this.props} /> |
| 85 | + } else if (opts.loading) { |
| 86 | + return <span ref={this.attachRef}> |
| 87 | + {React.createElement(opts.loading, { |
| 88 | + isLoading: true, |
| 89 | + })} |
| 90 | + </span> |
| 91 | + } else { |
| 92 | + return <span ref={this.attachRef} /> |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +function LoadableVisibility (opts) { |
| 99 | + if (intersectionObserver) { |
| 100 | + return createLoadableVisibilityComponent(opts, Loadable) |
| 101 | + } else { |
| 102 | + return Loadable(opts) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +function LoadableVisibilityMap (opts) { |
| 107 | + if (intersectionObserver) { |
| 108 | + return createLoadableVisibilityComponent(opts, Loadable.Map) |
| 109 | + } else { |
| 110 | + return Loadable.Map(opts) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +LoadableVisibility.Map = LoadableVisibilityMap |
| 115 | + |
| 116 | +module.exports = LoadableVisibility |
0 commit comments