All files / molecule/notification/src index.js

75.51% Statements 37/49
72.09% Branches 31/43
58.33% Functions 7/12
78.26% Lines 36/46

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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227                                                1x               1x                                       5x 5x   5x 5x   5x 3x     5x   5x 3x 3x         5x         5x   3x         5x 3x   3x 3x 3x     3x 3x 3x       5x   5x         5x       5x       5x   5x       5x 5x                                       5x   5x         5x       1x   1x                                                                                                                                                    
import {memo, useCallback, useEffect, useRef, useState} from 'react'
import {createPortal} from 'react-dom'
 
import cx from 'classnames'
import PropTypes from 'prop-types'
 
import Button from '@s-ui/react-atom-button'
import IconClose from '@s-ui/react-icons/lib/Close'
 
import {
  ALIGN_ITEMS,
  AUTO_CLOSE,
  AUTO_CLOSE_TIME,
  BRDS_SIZE,
  BUTTONS_MAX,
  CLASS,
  EMPTY_METHOD,
  ICONS,
  POSITION,
  TRANSITION_DELAY,
  TYPES,
  VARIATIONS
} from './settings.js'
 
const getContainer = ref => {
  const hasRef = ref !== undefined
  if (hasRef) {
    return ref.current
  }
  return document.body
}
 
const MoleculeNotification = memo(
  ({
    alignItems = ALIGN_ITEMS.FLEX_START,
    autoClose: autoCloseTiming = AUTO_CLOSE.short,
    onClose = EMPTY_METHOD,
    effect = true,
    buttons,
    children,
    icon,
    position = POSITION.relative,
    roundedCorners = null,
    showCloseButton = true,
    showLeftIconMobile = false,
    text,
    type = TYPES.info,
    variation = VARIATIONS.negative,
    show: showFromProps = true,
    overrideContainer = false,
    targetRef = undefined
  }) => {
    const [show, setShow] = useState(showFromProps)
    const [delay, setDelay] = useState(false)
 
    const transitionTimeout = useRef()
    const autoCloseTimeout = useRef()
 
    useEffect(() => {
      setShow(showFromProps)
    }, [showFromProps])
 
    const autoCloseTimeInSeconds = AUTO_CLOSE_TIME[autoCloseTiming] || AUTO_CLOSE_TIME.manual
 
    const removeDelay = show => {
      const delay = show ? 1 : TRANSITION_DELAY
      transitionTimeout.current = setTimeout(() => {
        setDelay(false)
      }, delay)
    }
 
    const handleClose = useCallback(() => {
      setShow(false)
      onClose()
    }, [onClose])
 
    const triggerAutoClose = useCallback(
      time => {
        autoCloseTimeout.current = setTimeout(handleClose, time)
      },
      [handleClose]
    )
 
    useEffect(() => {
      Eif (show && autoCloseTimeInSeconds) triggerAutoClose(autoCloseTimeInSeconds)
 
      Eif (effect) {
        setDelay(true)
        removeDelay(show)
      }
 
      return () => {
        clearTimeout(autoCloseTimeout.current)
        clearTimeout(transitionTimeout.current)
      }
    }, [show, effect, triggerAutoClose, autoCloseTimeInSeconds])
 
    const getButtons = () => buttons.slice(0, BUTTONS_MAX).map((button, i) => <Button key={i} {...button} />)
 
    const wrapperClassName = cx(`${CLASS} ${CLASS}--${type} ${CLASS}--${position} ${CLASS}--${variation}`, {
      [`${CLASS}-effect--${position}`]: effect,
      [`${CLASS}-effect--hide`]: effect && delay,
      [`${CLASS}-roundedCorners--${roundedCorners}`]: roundedCorners
    })
    const innerWrapperClassName = cx({
      [`${CLASS}-children`]: children,
      [`${CLASS}-text`]: text
    })
    const iconLeftClassName = cx(`${CLASS}-iconLeft`, {
      [`${CLASS}-iconLeft--show`]: showLeftIconMobile
    })
 
    const contentClassName = `${CLASS}-content ${CLASS}-content--alignItems-${alignItems}`
 
    Iif (!show && !delay) {
      return null
    }
 
    const render = () => {
      return (
        <div className={wrapperClassName}>
          <div className={contentClassName}>
            <div className={iconLeftClassName}>
              <span className={`${CLASS}-icon`}>{icon || ICONS[type]}</span>
            </div>
            <div className={innerWrapperClassName}>{children || text}</div>
            {showCloseButton && (
              <div className={`${CLASS}-iconClose`} onClick={handleClose}>
                <span className={`${CLASS}-icon`}>
                  <IconClose />
                </span>
              </div>
            )}
          </div>
          {buttons && <div className={`${CLASS}-buttonsContainer`}>{getButtons()}</div>}
        </div>
      )
    }
 
    const notificationEl = render()
 
    Iif (overrideContainer) {
      const container = getContainer(targetRef)
      return container ? createPortal(notificationEl, container) : notificationEl
    }
 
    return notificationEl
  }
)
 
MoleculeNotification.displayName = 'MoleculeNotification'
 
MoleculeNotification.propTypes = {
  /** Vertical alignment for content */
  alignItems: PropTypes.oneOf(Object.keys(ALIGN_ITEMS)),
 
  /** Auto close time: 'short' (3s), 'medium' (6s), 'long' (9s), 'manual' or null (disabled) */
  autoClose: PropTypes.string,
 
  /** Array of props to sui-atom-buttons. Max: 3 buttons */
  buttons: PropTypes.array,
 
  /** Notification content */
  children: PropTypes.node,
 
  /** Transition enabled */
  effect: PropTypes.bool,
 
  /** Icon to be added on the left of the content */
  icon: PropTypes.node,
 
  /** On close callback */
  onClose: PropTypes.func,
 
  /** Positions: 'top', 'bottom', 'relative' */
  position: PropTypes.string,
 
  /**
   * Border Radius sizes:
   * 'xl',
   * 'l',
   * 'm' (default),
   * 's',
   * 'xs',
   */
  roundedCorners: PropTypes.oneOf(Object.values(BRDS_SIZE)),
 
  /** Show / hide notification */
  show: PropTypes.bool,
 
  /** Show / hide close button */
  showCloseButton: PropTypes.bool,
 
  /** Show / hide left icon on mobile */
  showLeftIconMobile: PropTypes.bool,
 
  /** Content text. Deprecated, use children instead. */
  text: PropTypes.string,
 
  /** Notification type: 'info', 'success', 'warning', 'error', 'system'. */
  type: PropTypes.PropTypes.oneOf(Object.keys(TYPES)),
 
  /** Color variation of the notification: 'positive' with washed out colors, 'negative' with bold colors */
  variation: PropTypes.oneOf(Object.keys(VARIATIONS)),
 
  /** Allows to change the container of the notifaction. This can be specified on targetRef prop, otherwise will be use the div "notification-react-portal".  */
  overrideContainer: PropTypes.bool,
 
  /** Used along with overrideContainer let specify a dom elment to render the notification. */
  targetRef: PropTypes.string
}
 
export {
  POSITION,
  POSITION as moleculeNotificationPosition,
  ALIGN_ITEMS as moleculeNotificationAlignItems,
  AUTO_CLOSE,
  AUTO_CLOSE as moleculeNotificationAutoClose,
  TYPES,
  TYPES as moleculeNotificationTypes,
  VARIATIONS,
  VARIATIONS as moleculeNotificationVariations,
  BRDS_SIZE,
  BRDS_SIZE as moleculeNotificationBorderSizes
}
export default MoleculeNotification