# All files / src/Grid Grid.styles.ts

93.5% Statements 72/77
84.74% Branches 50/59
100% Functions 11/11
93.05% Lines 67/72

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
    2265x
    5x
    5x

    5x

    5x

    36x
    36x

    36x

    5x

    18x
    3x

    15x

    15x

    15x
    10x

    5x

    5x

    18x
    3x

    15x

    15x
    10x

    5x

    5x
    1x

    4x

    5x

    18x

    18x

    18x

    18x

    5x

    18x

    18x

    18x

    18x

    5x

    18x

    18x

    18x

    18x
    3x

    15x

    5x

    18x

    18x

    18x

    18x
    3x

    15x

    5x

    3x
    3x
    18x
    18x
    18x

    5x

    3x
    3x
    18x
    18x
    18x
    18x

    18x

    18x
    3x

    15x

     import styled, { css } from 'styled-components'
    import { spacing } from '../_styles/spacing'
    import {
      breakpointMinWidthValues,
      breakpointsSmallToLarge,
    } from './Grid.constants'
    import { GridBreakpoint, GridColProps, GridProps } from './Grid.types'

    const gridColumns = 12

    const getIsColStackActive = (
      targetBreakpoint: GridBreakpoint,
      colStackBreakpoint: GridBreakpoint
    ) => {
      const targetBrkIndex = breakpointsSmallToLarge.indexOf(targetBreakpoint)
      const stackBrkIndex = breakpointsSmallToLarge.indexOf(colStackBreakpoint)

      return targetBrkIndex <= stackBrkIndex
    }

    const getSpanCSS = (
      span: GridColProps['colWidth'],
      targetBreakpoint: GridBreakpoint,
      colStackBreakpoint: GridBreakpoint
    ) => {
      if (getIsColStackActive(targetBreakpoint, colStackBreakpoint)) {
        return css`
          width: 100%;
        `
      }

      const spanValue = typeof span !== 'object' ? span : span[targetBreakpoint]

      Iif (typeof spanValue === 'undefined') {
        return ''
      }

      if (spanValue === 'auto') {
        return css`
          flex: 1 1 0%;
          max-width: 100%;
        `
      }

      return css`
        flex: 0 0 auto;
        width: ${(spanValue / gridColumns) * 100}%;
      `
    }

    const getOffsetCSS = (
      $offset: GridColProps['colStart'],
      targetBreakpoint: GridBreakpoint,
      colStackBreakpoint: GridBreakpoint
    ) => {
      if (getIsColStackActive(targetBreakpoint, colStackBreakpoint)) {
        return ''
      }

      const offsetValue =
        typeof $offset === 'object' ? $offset[targetBreakpoint] : $offset

      if (typeof offsetValue === 'undefined' || offsetValue >= gridColumns) {
        return ''
      }

      const ratio = offsetValue / gridColumns

      if (ratio === 0) {
        return css`
          margin-left: 0;
        `
      }

      return css`
        margin-left: ${ratio * 100}%;
      `
    }

    const getColGutterHorizontal = (
      gutterX: GridProps['gutterX'],
      breakpoint: GridBreakpoint
    ) => {
      const gutterKey = typeof gutterX === 'object' ? gutterX[breakpoint] : gutterX

      Iif (typeof gutterKey === 'undefined') {
        return ''
      }

      const gutterCSS = css`
        padding-right: ${spacing[gutterKey] / 2}px;
        padding-left: ${spacing[gutterKey] / 2}px;
      `

      return gutterCSS
    }

    const getColGutterVertical = (
      gutterX: GridProps['gutterX'],
      breakpoint: GridBreakpoint
    ) => {
      const gutterKey = typeof gutterX === 'object' ? gutterX[breakpoint] : gutterX

      Iif (typeof gutterKey === 'undefined') {
        return ''
      }

      const gutterCSS = css`
        margin-top: ${spacing[gutterKey]}px;
      `

      return gutterCSS
    }

    const getRowGutterHorizontal = (
      gutterX: GridProps['gutterX'],
      breakpoint: GridBreakpoint
    ) => {
      const gutterKey = typeof gutterX === 'object' ? gutterX[breakpoint] : gutterX

      Iif (typeof gutterKey === 'undefined') {
        return ''
      }

      const gutterCSS = css`
        margin-right: ${spacing[gutterKey] / -2}px;
        margin-left: ${spacing[gutterKey] / -2}px;
      `

      if (breakpoint === breakpointsSmallToLarge[0]) {
        return gutterCSS
      }

      return css`
        @media (min-width: ${breakpointMinWidthValues[breakpoint]}) {
          ${gutterCSS}
        }
      `
    }

    const getRowGutterVertical = (
      gutterY: GridProps['gutterY'],
      breakpoint: GridBreakpoint
    ) => {
      const gutterKey = typeof gutterY === 'object' ? gutterY[breakpoint] : gutterY

      Iif (typeof gutterKey === 'undefined') {
        return ''
      }

      const gutterCSS = css`
        margin-top: ${spacing[gutterKey] * -1}px;
      `

      if (breakpoint === breakpointsSmallToLarge[0]) {
        return gutterCSS
      }

      return css`
        @media (min-width: ${breakpointMinWidthValues[breakpoint]}) {
          ${gutterCSS}
        }
      `
    }

    export const StyledGridRow = styled.div<{
      $gutterX: GridProps['gutterX']
      $gutterY: GridProps['gutterY']
    }>`
      ${({ $gutterX, $gutterY }) => {
        return breakpointsSmallToLarge.map((breakpoint) => {
          const gutterXCSS = getRowGutterHorizontal($gutterX, breakpoint)
          const gutterYCSS = getRowGutterVertical($gutterY, breakpoint)
          return css`
            ${gutterXCSS}

            // only adjust margins on first rows
            &:first-child {
              ${gutterYCSS}
            }
          `
        })
      }}

      display: flex;
      flex-wrap: wrap;
    `

    export const StyledGridCol = styled.div<{
      $offset?: GridColProps['colStart']
      $span?: GridColProps['colWidth']
      $gutterX: GridProps['gutterX']
      $gutterY: GridProps['gutterY']
      $colStackBreakpoint: GridBreakpoint
    }>`
      flex-shrink: 0;
      width: 100%;
      max-width: 100%;

      ${({ $span, $offset, $gutterX, $gutterY, $colStackBreakpoint }) => {
        return breakpointsSmallToLarge.map((breakpoint) => {
          const spanCSS = getSpanCSS($span, breakpoint, $colStackBreakpoint)
          const offsetCSS = getOffsetCSS($offset, breakpoint, $colStackBreakpoint)
          const gutterXCSS = getColGutterHorizontal($gutterX, breakpoint)
          const gutterYCSS = getColGutterVertical($gutterY, breakpoint)

          const resultCSS = css`
            ${offsetCSS}
            ${gutterXCSS}
            ${gutterYCSS}
            ${spanCSS}
          `

          if (breakpoint === breakpointsSmallToLarge[0]) {
            return resultCSS
          }

          return css`
            @media (min-width: ${breakpointMinWidthValues[breakpoint]}) {
              ${resultCSS}
            }
          `
        })
      }}
    `