All files / atom/panel/src ImagePanel.js

100% Statements 11/11
85.71% Branches 12/14
100% Functions 3/3
100% Lines 11/11

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                            1x                       7x 7x   7x                           1x 7x 7x         1x 7x             1x   1x                                                      
import cx from 'classnames'
import PropTypes from 'prop-types'
 
import PolymorphicElement from '@s-ui/react-primitive-polymorphic-element'
 
import {
  ALPHA,
  BORDER_RADIUS,
  DEFAULT_ALPHA,
  ELEVATION,
  HORIZONTAL_ALIGNMENTS,
  VERTICAL_ALIGNMENTS
} from './constants.js'
 
const getClassNames = function ({
  verticalAlign,
  horizontalAlign,
  resized,
  overlayColor,
  overlayAlpha = ALPHA[DEFAULT_ALPHA],
  color,
  rounded,
  elevation,
  isFullWidth,
  isFullHeight
}) {
  const BASE_CLASS = 'sui-atom-panel'
  const IMAGE_PANEL_CLASS = `${BASE_CLASS}-image`
 
  return cx(
    BASE_CLASS,
    rounded !== BORDER_RADIUS.NONE && `${BASE_CLASS}--rounded-${rounded}`,
    `${IMAGE_PANEL_CLASS}--vertical-${verticalAlign}`,
    `${IMAGE_PANEL_CLASS}--horizontal-${horizontalAlign}`,
    overlayColor && `${BASE_CLASS}--${overlayColor}-overlay-${overlayAlpha}`,
    `${BASE_CLASS}-color--${color}`,
    resized && `${IMAGE_PANEL_CLASS}--resized`,
    isFullWidth && `${BASE_CLASS}--fullWidth`,
    isFullHeight && `${BASE_CLASS}--fullHeight`,
    elevation !== ELEVATION.NONE && `${BASE_CLASS}--elevation-${elevation}`
  )
}
 
const getStyles = function ({src}) {
  const url = `url(${src})`
  return {
    backgroundImage: url
  }
}
 
const ImagePanel = function ({as = 'div', id, children, ...props}) {
  return (
    <PolymorphicElement as={as} id={id} className={getClassNames(props)} style={getStyles(props)}>
      {children}
    </PolymorphicElement>
  )
}
 
ImagePanel.displayName = 'ImagePanel'
 
ImagePanel.propTypes = {
  as: PropTypes.elementType,
  id: PropTypes.string,
  children: PropTypes.node,
  isFullWidth: PropTypes.bool,
  isFullHeight: PropTypes.bool,
  /**
   * Background color while loading the image
   */
  color: PropTypes.string,
  resized: PropTypes.bool,
  /**
   * Background position x
   */
  horizontalAlign: PropTypes.oneOf(Object.values(HORIZONTAL_ALIGNMENTS)),
  overlayAlpha: PropTypes.string,
  overlayColor: PropTypes.string,
  /**
   * Background position y
   */
  verticalAlign: PropTypes.oneOf(Object.values(VERTICAL_ALIGNMENTS)),
  rounded: PropTypes.oneOf(Object.values(BORDER_RADIUS)),
  elevation: PropTypes.oneOf(Object.values(ELEVATION))
}
 
export default ImagePanel
export {HORIZONTAL_ALIGNMENTS, VERTICAL_ALIGNMENTS}