All files / molecule/modal/src/Content index.js

44.44% Statements 8/18
0% Branches 0/8
33.33% Functions 1/3
47.05% Lines 8/17

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          1x             2x 2x 2x         2x           2x                     2x                     1x                
import {useRef} from 'react'
 
import cx from 'classnames'
import PropTypes from 'prop-types'
 
const MoleculeModalContent = ({
  className: classNameProp,
  enableContentScroll,
  withoutIndentation,
  isOverflowVisible,
  ...others
}) => {
  const baseClassName = 'sui-MoleculeModalContent'
  const contentRef = useRef()
  const className = cx(baseClassName, {
    [`${baseClassName}--without-indentation`]: withoutIndentation,
    [`${baseClassName}--visible-overflow`]: isOverflowVisible
  })
 
  const preventScrollIfNeeded = ev => {
    const {clientHeight, scrollHeight} = contentRef.current
    const noScroll = scrollHeight <= clientHeight
    if (!enableContentScroll && noScroll) ev.preventDefault()
  }
 
  const avoidOverscroll = () => {
    const {offsetHeight, scrollTop, scrollHeight} = contentRef.current
    const currentScroll = scrollTop + offsetHeight
 
    if (scrollTop === 0) {
      contentRef.current.scrollTop = 1
    } else if (currentScroll >= scrollHeight) {
      contentRef.current.scrollTop = scrollTop - 1
    }
  }
 
  return (
    <div
      className={className}
      ref={contentRef}
      onTouchStart={avoidOverscroll}
      onTouchMove={preventScrollIfNeeded}
      {...others}
    />
  )
}
 
MoleculeModalContent.propTypes = {
  className: PropTypes.string,
  enableContentScroll: PropTypes.bool,
  withoutIndentation: PropTypes.bool,
  isOverflowVisible: PropTypes.bool
}
 
export default MoleculeModalContent