All files / molecule/drawer/src index.js

60% Statements 21/35
54.54% Branches 18/33
66.66% Functions 6/9
61.53% Lines 16/26

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                              1x   1x                               5x 4x           5x 5x 4x   5x     5x           5x             5x             5x 5x   5x                                       1x 1x                                                        
import {forwardRef, useEffect} from 'react'
 
import cx from 'classnames'
import PropTypes from 'prop-types'
 
import useEventListener from '@s-ui/react-hooks/lib/useEventListener'
 
import MoleculeDrawerOverlay from './Overlay.js'
import {
  ANIMATION_DURATION as moleculeDrawerAnimationDuration,
  BASE_CLASS,
  PLACEMENTS as moleculeDrawerPlacements,
  SIZES as moleculeDrawerSizes
} from './settings.js'
 
const DRAWER_CONTENT_CLASS = `${BASE_CLASS}-content`
 
const MoleculeDrawer = forwardRef(
  (
    {
      animationDuration = moleculeDrawerAnimationDuration.FAST,
      children,
      isOpen = false,
      isPageScrollable = true,
      onOpen,
      onClose,
      placement = moleculeDrawerPlacements.LEFT,
      size = moleculeDrawerSizes.AUTO,
      target,
      closeOnOutsideClick = false
    },
    forwardedRef
  ) => {
    useEffect(() => {
      Iif (target !== undefined) {
        target.current.style.position = 'relative'
        target.current.style.overflow = 'hidden'
      }
    }, [target])
 
    useEffect(() => {
      if (isOpen && !isPageScrollable) blockScrollPage()
      else Iif (!isOpen && !isPageScrollable) enableScrollPage()
 
      if (!isPageScrollable) return () => enableScrollPage()
    }, [isOpen, isPageScrollable])
 
    const closeDrawer = event => {
      if (!isPageScrollable) enableScrollPage()
 
      onClose(event, {isOpen: false})
    }
 
    useEventListener('keydown', event => {
      if (isOpen === false) return
      if (event.key === 'Escape') {
        closeDrawer(event)
      }
    })
 
    useEventListener('mousedown', event => {
      if (isOpen === false || !forwardedRef || !closeOnOutsideClick) return
      if (!forwardedRef.current.contains(event.target)) {
        closeDrawer(event)
      }
    })
 
    const blockScrollPage = () => (document.body.style.overflow = 'hidden')
    const enableScrollPage = () => (document.body.style.overflow = 'auto')
 
    return (
      <div
        className={cx(
          DRAWER_CONTENT_CLASS,
          `${DRAWER_CONTENT_CLASS}--placement-${placement}`,
          `${DRAWER_CONTENT_CLASS}--size-${size}`,
          `${DRAWER_CONTENT_CLASS}--animationDuration-${animationDuration}`,
          `${DRAWER_CONTENT_CLASS}--state-${isOpen ? 'opened' : 'closed'}`,
          {
            [`${DRAWER_CONTENT_CLASS}--placement`]: typeof target === 'undefined'
          }
        )}
        ref={forwardedRef}
      >
        {children}
      </div>
    )
  }
)
 
MoleculeDrawer.displayName = 'MoleculeDrawer'
MoleculeDrawer.propTypes = {
  /** Duration in seconds for open/close animation */
  animationDuration: PropTypes.oneOf(Object.values(moleculeDrawerAnimationDuration)),
  /** content **/
  children: PropTypes.node,
  /** Tells if the drawer is open or not */
  isOpen: PropTypes.bool,
  /** false to prevent scroll body */
  isPageScrollable: PropTypes.bool,
  /** On open callback triggered after animation */
  onOpen: PropTypes.func,
  /** On close callback triggered after animation */
  onClose: PropTypes.func,
  /** Screen position where the drawer will be displayed */
  placement: PropTypes.oneOf(Object.values(moleculeDrawerPlacements)),
  /** Size of the drawer content */
  size: PropTypes.oneOf(Object.values(moleculeDrawerSizes)),
  /** DOM Element which wraps the component. **/
  target: PropTypes.node,
  /** Tells if drawer should be closed when clicked outside the drawer area, needs ref to be defined **/
  closeOnOutsideClick: PropTypes.bool
}
 
export default MoleculeDrawer
export {MoleculeDrawer, MoleculeDrawerOverlay}
export {moleculeDrawerPlacements}
export {moleculeDrawerSizes}
export {moleculeDrawerAnimationDuration}