All files helpers.ts

100% Statements 50/50
100% Branches 12/12
100% Functions 5/5
100% Lines 49/49

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 178208x                   4x 38x 38x 38x 38x 38x   38x 38x 38x   38x                                                     4x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x 104x   104x   104x 66x           66x                       38x                 38x 38x     38x 38x     38x 38x     38x           38x                                                   4x 104x 104x 104x 104x 104x 104x   104x           104x 38x     104x                         4x 20x                    
const toDurationString = (duration: number): string => `${duration}ms`
 
interface GetScale {
  height: number
  innerHeight: number
  innerWidth: number
  width: number
  zoomMargin: number
}
 
export const getScale = ({
  height,
  innerHeight,
  innerWidth,
  width,
  zoomMargin
}: GetScale): number => {
  const scaleX = innerWidth / (width + zoomMargin)
  const scaleY = innerHeight / (height + zoomMargin)
  const scale = Math.min(scaleX, scaleY)
 
  return scale
}
 
interface GetModalContentStyle {
  height: number
  innerHeight: number
  innerWidth: number
  isLoaded: boolean
  isUnloading: boolean
  left: number
  originalTransform: string | null
  top: number
  transitionDuration: number
  width: number
  zoomMargin: number
}
 
type GetModalContentStyleReturnType = {
  height: number
  left: number
  top: number
  transform: string
  WebkitTransform: string
  transitionDuration: string
  width: number
}
 
export const getModalContentStyle = ({
  height,
  innerHeight,
  innerWidth,
  isLoaded,
  isUnloading,
  left,
  originalTransform,
  top,
  transitionDuration,
  width,
  zoomMargin
}: GetModalContentStyle): GetModalContentStyleReturnType => {
  const transitionDurationString = toDurationString(transitionDuration)
 
  if (!isLoaded || isUnloading) {
    const initTransform = [
      `scale(1)`,
      `translate(0, 0)`,
      ...(originalTransform ? [originalTransform] : [])
    ].join(' ')
 
    return {
      height,
      left,
      top,
      transform: initTransform,
      WebkitTransform: initTransform,
      transitionDuration: transitionDurationString,
      width
    }
  }
 
  // Get amount to scale item
  const scale = getScale({
    height,
    innerWidth,
    innerHeight,
    width,
    zoomMargin
  })
 
  // Get the the coords for center of the viewport
  const viewportX = innerWidth / 2
  const viewportY = innerHeight / 2
 
  // Get the coords for center of the parent item
  const childCenterX = left + width / 2
  const childCenterY = top + height / 2
 
  // Get offset amounts for item coords to be centered on screen
  const translateX = (viewportX - childCenterX) / scale
  const translateY = (viewportY - childCenterY) / scale
 
  // Build transform style, including any original transform
  const transform = [
    `scale(${scale})`,
    `translate(${translateX}px, ${translateY}px)`,
    ...(originalTransform ? [originalTransform] : [])
  ].join(' ')
 
  return {
    height,
    left,
    top,
    transform,
    WebkitTransform: transform,
    transitionDuration: transitionDurationString,
    width
  }
}
 
interface GetModalOverlayStyle {
  isLoaded: boolean
  isUnloading: boolean
  overlayBgColorEnd: string
  overlayBgColorStart: string
  transitionDuration: number
  zoomZindex: number
}
 
type GetModalOverlayStyleReturnType = {
  backgroundColor: string
  transitionDuration: string
  zIndex: number
}
 
export const getModalOverlayStyle = ({
  isLoaded,
  isUnloading,
  overlayBgColorEnd,
  overlayBgColorStart,
  transitionDuration,
  zoomZindex
}: GetModalOverlayStyle): GetModalOverlayStyleReturnType => {
  const style = {
    backgroundColor: overlayBgColorStart,
    transitionDuration: toDurationString(transitionDuration),
    zIndex: zoomZindex
  }
 
  if (isLoaded && !isUnloading) {
    style.backgroundColor = overlayBgColorEnd
  }
 
  return style
}
 
// @TODO: test
// if parentRef.current is not available yet,
// we can fall back to these defaults
type GetBoundingClientRectReturnType = {
  height: number
  left: number
  top: number
  width: number
}
 
export const pseudoParentEl = {
  getBoundingClientRect: (): GetBoundingClientRectReturnType => ({
    height: 0,
    left: 0,
    top: 0,
    width: 0
  }),
  style: {
    transform: null
  }
}