All files Modal.js

97.22% Statements 35/36
77.08% Branches 37/48
100% Functions 14/14
97.06% Lines 33/34

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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273                5x                 6x                 3x         12x       3x               6x                       3x             6x         6x                   6x             3x       3x                     3x         3x     6x 3x                   3x           3x   6x       6x             3x         3x                                         7x   7x 3x 4x 1x     7x     6x                                                                                                         3x                         3x 14x             3x                                                
import React from 'react'
import styled from 'styled-components'
import PropTypes from 'prop-types'
import { Transition } from 'react-transition-group'
import { color, width, height } from 'styled-system'
import { DialogOverlay, DialogContent } from '@reach/dialog'
import { Box, CloseButton, deprecatedPropType, Flex } from 'pcln-design-system'
 
const OVERLAY_ANIMATION = (transitionState) => `
  opacity: 0;
  transition: opacity .5s cubic-bezier(0.50, 0.00, 0.25, 1.00);
  ${transitionState === 'entering' ? `opacity: 0;` : ''}
  ${transitionState === 'entered' ? `opacity: 1;` : ''}
  ${transitionState === 'exiting' ? `opacity: 0;` : ''}
  ${transitionState === 'exited' ? `opacity: 0;` : ''}
`
 
const DIALOG_ANIMATION = (transitionState) => `
  transform: scale(0.5);
  transition: transform .5s cubic-bezier(0.50, 0.00, 0.25, 1.00);
  ${transitionState === 'entering' ? `transform: scale(0.5);` : ''}
  ${transitionState === 'entered' ? `transform: scale(1);` : ''}
  ${transitionState === 'exiting' ? `transform: scale(0.5);` : ''}
  ${transitionState === 'exited' ? `transform: scale(0.5);` : ''}
`
 
const getAnimation = ({
  transitionstate,
  defaultAnimation,
  customAnimation = null,
}) =>
  typeof customAnimation === 'function'
    ? customAnimation(transitionstate)
    : defaultAnimation(transitionstate)
 
const Overlay = styled(DialogOverlay)`
  background-color: rgba(0, 0, 0, 0.7);
  bottom: 0;
  left: 0;
  overflow-y: auto;
  position: fixed;
  right: 0;
  top: 0;
  ${(props) => `
    z-index: ${props.zindex || 100};
    font-family: ${props.theme.font};
    line-height: ${props.theme.lineHeights?.standard};
    font-weight: ${props.theme.fontWeights?.medium};
  `}
  * {
    box-sizing: border-box;
  }
  ${getAnimation};
`
 
const Dialog = styled(DialogContent)`
  ${color}
  ${width}
  ${height}
  position: relative;
  margin-left: auto;
  margin-right: auto;
  box-shadow: ${(props) => props.theme.boxShadows[3]};
  &:focus {
    outline: none;
  }
  ${(props) =>
    !props.fullScreen
      ? `
    max-height: 100vh;
    max-width: calc(100vw - 32px);
  `
      : `
    width: 100vw;
    height: 100vh;
  `}
  ${(props) =>
    props.enableoverflow &&
    `
    max-height: none;
  `}
  ${getAnimation}
`
 
const HeaderWrapper = styled.div`
  flex-shrink: 0;
`
 
const FloatCloseButton = styled(CloseButton)`
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 2;
 
  svg {
    vertical-align: top;
  }
 
  &:focus {
    background-color: ${(props) => props.theme.colors.borderGray};
    outline: none;
  }
`
 
const ContentWrapper = styled(Box)`
  position: relative;
  ${(props) => {
    if (!props.enableoverflow) {
      return `
        overflow-y: auto;
        overflow-x: hidden;
        height: 100%;
        -webkit-overflow-scrolling: touch;
      `
    }
  }}
`
 
const OverlayWrapper = styled.div`
  display: table;
  height: 100%;
  width: 100%;
`
 
const DialogWrapper = styled(Box)`
  display: table-cell;
  vertical-align: ${({ verticalAlignment }) => verticalAlignment};
  position: relative;
  height: 100%;
  ${(props) =>
    props.enableoverflow &&
    `
    padding-top: 24px;
    padding-bottom: 24px;
  `};
`
 
const DialogInnerWrapper = styled(Flex)`
  position: relative;
  height: 100%;
`
 
const Modal = ({
  ariaLabel,
  ariaLabelledBy,
  bg,
  children,
  className,
  dialogAnimation,
  disableCloseButton,
  enableOverflow,
  fullScreen,
  header,
  height,
  imgMode,
  isOpen,
  onClose,
  overlayAnimation,
  timeout,
  verticalAlignment,
  width,
  zIndex,
}) => {
  let dialogHeight = height
 
  if (enableOverflow) {
    dialogHeight = null
  } else if (fullScreen) {
    dialogHeight = [1]
  }
 
  return (
    <Transition in={isOpen} unmountOnExit timeout={timeout}>
      {(state) => (
        <Overlay
          onDismiss={onClose}
          zindex={zIndex}
          transitionstate={state}
          initialFocusRef={null}
          defaultAnimation={OVERLAY_ANIMATION}
          customAnimation={overlayAnimation}
        >
          <OverlayWrapper>
            <DialogWrapper
              enableoverflow={enableOverflow}
              verticalAlignment={verticalAlignment}
            >
              <Dialog
                width={fullScreen ? 1 : width}
                bg={bg}
                height={dialogHeight}
                transitionstate={state}
                className={className}
                enableoverflow={enableOverflow ? 'true' : null}
                defaultAnimation={DIALOG_ANIMATION}
                customAnimation={dialogAnimation}
                fullScreen={fullScreen}
                data-testid='dialog-content'
                aria-label={ariaLabel}
                aria-labelledby={ariaLabelledBy}
              >
                <DialogInnerWrapper flexDirection='column'>
                  {header && <HeaderWrapper>{header}</HeaderWrapper>}
                  <ContentWrapper
                    p={imgMode ? 0 : 16}
                    header={header}
                    enableoverflow={enableOverflow}
                  >
                    {enableOverflow && !disableCloseButton && (
                      <FloatCloseButton
                        data-testid='pcln-modal-close'
                        header={header}
                        onClick={onClose}
                      />
                    )}
                    {children}
                  </ContentWrapper>
                </DialogInnerWrapper>
              </Dialog>
            </DialogWrapper>
          </OverlayWrapper>
        </Overlay>
      )}
    </Transition>
  )
}
 
Modal.defaultProps = {
  bg: 'white',
  dialogAnimation: null,
  disableCloseButton: false,
  enableOverflow: false,
  header: null,
  height: 420,
  isOpen: false,
  overlayAnimation: null,
  timeout: 500,
  verticalAlignment: 'middle',
}
 
const validateAriaProps = (props, propName, componentName) => {
  Iif (!props.ariaLabel && !props.ariaLabelledBy) {
    return new Error(
      `A <${componentName}> must have either an 'ariaLabel' or 'ariaLabelledBy' prop.`
    )
  }
}
 
Modal.propTypes = {
  ...width.propTypes,
  ...height.propTypes,
  ariaLabel: validateAriaProps,
  ariaLabelledBy: validateAriaProps,
  bg: deprecatedPropType('color'),
  className: PropTypes.string,
  dialogAnimation: PropTypes.func,
  disableCloseButton: PropTypes.bool,
  enableOverflow: PropTypes.bool,
  fullScreen: PropTypes.bool,
  header: PropTypes.any,
  headerBg: PropTypes.string,
  imgMode: PropTypes.bool,
  isOpen: PropTypes.bool,
  onClose: PropTypes.func,
  overlayAnimation: PropTypes.func,
  timeout: PropTypes.number,
  title: PropTypes.string,
  verticalAlignment: PropTypes.string,
  zIndex: PropTypes.number,
}
 
export default Modal