All files / atom/spinner/src index.js

88.23% Statements 15/17
87.5% Branches 7/8
75% Functions 3/4
87.5% Lines 14/16

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                    1x                       3x 3x   3x 3x         3x   3x   3x         3x 3x 3x       3x                                 1x   1x                                                                          
import {forwardRef, useEffect, useRef, useState} from 'react'
 
import PropTypes from 'prop-types'
 
import Injector from '@s-ui/react-primitive-injector'
 
import SUILoader from './SUILoader/index.js'
import DefaultSpinner from './DefaultSpinner.js'
import {addParentClass, DELAY, getParentClassName, OVERLAY_TYPES, removeParentClass, SIZES, TYPES} from './settings.js'
 
const AtomSpinner = forwardRef(
  (
    {
      children = <DefaultSpinner />,
      isDelayed: isDelayedFromProps = false,
      loader = <SUILoader />,
      overlayType = OVERLAY_TYPES.LIGHT,
      size = SIZES.MEDIUM,
      type = TYPES.SECTION
    },
    forwardedRef
  ) => {
    const [isDelayed, setIsDelayed] = useState(isDelayedFromProps)
    const refSpinner = useRef()
 
    useEffect(() => {
      const parentClassName = getParentClassName({
        overlayType,
        size,
        type
      })
      const parentNodeClassList = refSpinner.current.parentNode.classList
 
      Eif (!isDelayed) addParentClass(parentNodeClassList)(parentClassName)
 
      const timer = setTimeout(() => {
        setIsDelayed(false)
        addParentClass(parentNodeClassList)(parentClassName)
      }, DELAY)
 
      return () => {
        clearTimeout(timer)
        removeParentClass(parentNodeClassList)(parentClassName)
      }
    }, [isDelayed, overlayType, size, type])
 
    return (
      <div ref={refSpinner} className="sui-AtomSpinner-content">
        <Injector
          isDelayed={isDelayed}
          loader={loader}
          overlayType={overlayType}
          ref={forwardedRef}
          size={size}
          type={type}
        >
          {children}
        </Injector>
      </div>
    )
  }
)
 
AtomSpinner.displayName = 'AtomSpinner'
 
AtomSpinner.propTypes = {
  /** Children with injected props */
  children: PropTypes.elementType,
 
  /** Makes the spinner appear after 500 ms */
  isDelayed: PropTypes.bool,
 
  /** Loader to be shown in the middle of the container */
  loader: PropTypes.elementType,
 
  /**
   * Possible options:
   * 'ACCENT'
   * 'DARK'
   * 'LIGHT'
   * 'PRIMARY'
   * 'TRANSPARENT'
   */
  overlayType: PropTypes.oneOf(Object.values(OVERLAY_TYPES)),
 
  /**
   * Possible options:
   * 'SMALL' and 'MEDIUM'
   */
  size: PropTypes.oneOf(Object.values(SIZES)),
 
  /**
   * Possible options:
   * 'FULL': The spinner fits the whole page container
   * 'SECTION': The spinner fits a specific site component
   */
  type: PropTypes.oneOf(Object.values(TYPES))
}
 
export {OVERLAY_TYPES as atomSpinnerOverlayTypes, TYPES as atomSpinnerTypes, SIZES as atomSpinnerSizes}
 
export default AtomSpinner