All files / atom/backToTop/src index.js

80.95% Statements 17/21
53.33% Branches 8/15
80% Functions 4/5
80.95% Lines 17/21

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                        1x                             4x   4x 4x 4x 1x 1x 1x 1x       4x 4x 4x 3x       4x                 4x                           1x   1x                                                          
import {forwardRef, useCallback, useEffect} from 'react'
import {createPortal} from 'react-dom'
 
import PropTypes from 'prop-types'
 
import {getTarget} from '@s-ui/js/lib/react'
import useControlledState from '@s-ui/react-hooks/lib/useControlledState'
 
import BackToTopButton from './BackToTopButton.js'
import calcBackToTopEngine, {isDocumentElement} from './calcBackToTopEngine.js'
import {SCROLL_BEHAVIOR, STYLES} from './config.js'
 
const AtomBackToTop = forwardRef(
  (
    {
      iconTop: IconTop,
      isVisible,
      minHeight,
      onScroll,
      onIsVisibleToggle,
      refContainer = window.document,
      scrollBehavior = SCROLL_BEHAVIOR.SMOOTH,
      style = STYLES.DARK,
      textTop
    },
    forwardedRef
  ) => {
    const [show, setShow] = useControlledState(isVisible)
 
    useEffect(() => {
      const container = getTarget(refContainer)
      const handleScroll = event => {
        const [nextShow, properties] = calcBackToTopEngine(refContainer, minHeight)
        setShow(nextShow)
        typeof onScroll === 'function' && onScroll(event, {...properties, show})
        Iif (typeof onIsVisibleToggle === 'function' && show !== nextShow) {
          onIsVisibleToggle(event, {...properties, show: nextShow})
        }
      }
      container.removeEventListener('scroll', handleScroll)
      container.addEventListener('scroll', handleScroll)
      return () => {
        container.removeEventListener('scroll', handleScroll)
      }
    }, [minHeight, refContainer, show, setShow, onIsVisibleToggle, onScroll])
 
    const scrollToTop = useCallback(() => {
      const container = getTarget(refContainer)
      const isDocument = isDocumentElement(container)
      ;(isDocument ? window : container).scrollTo({
        top: 0,
        behavior: scrollBehavior
      })
    }, [refContainer, scrollBehavior])
 
    return createPortal(
      <BackToTopButton
        textTop={textTop}
        iconTop={IconTop && <IconTop />}
        show={show}
        scrollToTop={scrollToTop}
        style={style}
        ref={forwardedRef}
      />,
      refContainer === document ? refContainer.body : refContainer
    )
  }
)
 
AtomBackToTop.displayName = 'AtomBackToTop'
 
AtomBackToTop.propTypes = {
  /** Icon (component) to be displayed **/
  iconTop: PropTypes.oneOfType([PropTypes.element, PropTypes.func]), // eslint-disable-line react/no-unused-prop-types
  /** controlled visible value **/
  isVisible: PropTypes.bool, // eslint-disable-line react/no-unused-prop-types
  /** Minimum height (in pixels) to show button */
  minHeight: PropTypes.number, // eslint-disable-line react/no-unused-prop-types
  /** scroll event handler **/
  onScroll: PropTypes.func, // eslint-disable-line react/no-unused-prop-types
  /** on isVisible inner state changes handler **/
  onIsVisibleToggle: PropTypes.func, // eslint-disable-line react/no-unused-prop-types
  /** Container to be scrolled. Can be a selector, or a React ref object */
  refContainer: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), // eslint-disable-line react/no-unused-prop-types
  /** scroll to top effect behavior */
  scrollBehavior: PropTypes.oneOf(Object.values(SCROLL_BEHAVIOR)), // eslint-disable-line react/no-unused-prop-types
  /**
   * Styles
   *  DARK → 'dark'
   *  LIGHT →'light'
   */
  style: PropTypes.oneOf(Object.values(STYLES)), // eslint-disable-line react/no-unused-prop-types
  /** Text to be displayed */
  textTop: PropTypes.string // eslint-disable-line react/no-unused-prop-types
}
 
export default AtomBackToTop
 
export {STYLES as backToTopStyles}
export {SCROLL_BEHAVIOR as backToTopScrollBehavior}