All files / src Sticky.js

29.73% Statements 11/37
0% Branches 0/20
44.44% Functions 4/9
29.73% Lines 11/37

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        1x                                                   1x                                                               2x   2x   2x                                                     1x 1x             1x                                                                                                                                                     1x   1x                             1x      
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
/* eslint complexity: [2, 4] */
const StickyContent = ({
  children,
  className,
  isStuck,
  isAnchored,
  stickToBottom,
  setRef,
  ...props
}) => {
  /* eslint-disable no-nested-ternary */
  const stickyClass = isStuck
    ? stickToBottom
      ? 'rev-Sticky-content--stuck-bottom'
      : 'rev-Sticky-content--stuck-top'
    : isAnchored
      ? 'rev-Sticky-content--anchored'
      : ''
  const itemClassName = classNames(className, 'rev-Sticky-content', stickyClass)
 
  return (
    <div ref={setRef} className={itemClassName} {...props}>
      {children}
    </div>
  )
}
 
StickyContent.propTypes = {
  children: PropTypes.node,
  className: PropTypes.string,
  isStuck: PropTypes.bool,
  isAnchored: PropTypes.bool,
  stickToBottom: PropTypes.bool,
  setRef: PropTypes.func,
}
 
class Sticky extends Component {
  static propTypes = {
    className: PropTypes.string,
    children: PropTypes.node,
    contentClassName: PropTypes.string,
    isStuck: PropTypes.bool,
    isAnchored: PropTypes.bool,
    stickToBottom: PropTypes.bool,
    setRef: PropTypes.func,
    setContentRef: PropTypes.func,
  }
 
  render() {
    const {
      children,
      className,
      contentClassName,
      setRef,
      setContentRef,
      isStuck,
      isAnchored,
      stickToBottom,
      ...props
    } = this.props
 
    const containerClassName = classNames(className, 'rev-Sticky')
 
    return (
      <div ref={setRef} className={containerClassName} {...props}>
        <StickyContent
          className={contentClassName}
          isAnchored={isAnchored}
          isStuck={isStuck}
          stickToBottom={stickToBottom}
          setRef={setContentRef}
        >
          {children}
        </StickyContent>
      </div>
    )
  }
}
 
class StatefulSticky extends Component {
  static propTypes = {
    isOpen: PropTypes.bool,
    onBackgroundClick: PropTypes.func,
    className: PropTypes.string,
    children: PropTypes.node,
    stickToBottom: PropTypes.bool,
    offset: PropTypes.bool,
  }
 
  constructor(props) {
    super(props)
    this.state = {
      isStuck: false,
      isAnchored: false,
    }
  }
 
  componentDidMount() {
    window.addEventListener('scroll', this.setContentState.bind(this))
  }
 
  /* eslint complexity: [2, 8] */
  setContentState() {
    /* eslint-disable no-unused-vars */
    const contentTop = this.stickyContent.getBoundingClientRect().top
    const contentBottom = this.stickyContent.getBoundingClientRect().bottom
 
    const containerTop = this.stickyContainer.getBoundingClientRect().top
    const containerBottom = this.stickyContainer.getBoundingClientRect().bottom
 
    let stickyStart, stickyStop, stickyFlag, anchorFlag
 
    if (this.props.stickToBottom) {
      stickyStart = this.props.offset
        ? containerBottom -
          this.parsePxValue(this.props.offset) -
          window.innerHeight
        : containerBottom - window.innerHeight
      stickyStop =
        containerTop + this.stickyContent.offsetHeight - window.innerHeight
      stickyFlag = stickyStart >= 0 && stickyStop <= 0
      anchorFlag = stickyStart <= 0
    } else {
      stickyStart = this.props.offset
        ? containerTop + this.parsePxValue(this.props.offset)
        : containerTop
      stickyStop = containerBottom - this.stickyContent.offsetHeight
      stickyFlag = stickyStart <= 0 && stickyStop >= 0
      anchorFlag = stickyStop <= 0
    }
 
    if (stickyFlag) {
      const sideBorders =
        this.parsePxValue(this.stickyContainer.style.borderRightWidth) +
        this.parsePxValue(this.stickyContainer.style.borderLeftWidth)
 
      // this is to force the fixed div holding the sticky content
      // to not break out of the sticky container since fixed
      // positioning breaks elements out of document flow
      this.stickyContent.style.width = `${(
        this.stickyContainer.offsetWidth - sideBorders
      ).toString()}px`
 
      this.setState({
        isStuck: true,
        isAnchored: false,
      })
    } else if (anchorFlag) {
      this.setState({
        isStuck: false,
        isAnchored: true,
      })
    } else {
      this.setState({
        isStuck: false,
        isAnchored: false,
      })
    }
  }
 
  parsePxValue(value) {
    return parseInt(value.replace('px', ''), 10)
  }
 
  setContainerRef(container) {
    this.stickyContainer = container
  }
 
  setContentRef(content) {
    this.stickyContent = content
  }
 
  render() {
    const {children, className, offset, stickToBottom, ...props} = this.props
 
    return (
      <Sticky
        setRef={this.setContainerRef.bind(this)}
        setContentRef={this.setContentRef.bind(this)}
        isStuck={this.state.isStuck}
        isAnchored={this.state.isAnchored}
        stickToBottom={stickToBottom}
        {...props}
      >
        {children}
      </Sticky>
    )
  }
}
 
Sticky.Stateful = StatefulSticky
 
export default Sticky