File size: 3,295 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { BreakpointEntry, SystemContext } from "./types"
import { toPx, toRem } from "./unit-conversion"

const capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1)

export function createBreakpoints(
  breakpoints: Record<string, string>,
): SystemContext["breakpoints"] {
  const sorted = sort(breakpoints)
  const values = Object.fromEntries(sorted)

  function get(name: string) {
    return values[name]
  }

  function only(name: string) {
    return build(get(name))
  }

  function getRanges() {
    const breakpoints: string[] = Object.keys(values)
    const permuations = getPermutations(breakpoints)

    const results = breakpoints
      .flatMap((name) => {
        const value = get(name)

        const down: [string, string] = [
          `${name}Down`,
          build({ max: adjust(value.min) }),
        ]

        const up: [string, string] = [name, build({ min: value.min })]
        const _only: [string, string] = [`${name}Only`, only(name)]

        return [up, _only, down]
      })
      .filter(([, value]) => value !== "")
      .concat(
        permuations.map(([min, max]) => {
          const minValue = get(min)
          const maxValue = get(max)
          return [
            `${min}To${capitalize(max)}`,
            build({ min: minValue.min, max: adjust(maxValue.min) }),
          ]
        }),
      )

    return Object.fromEntries(results)
  }

  function toConditions() {
    const ranges = getRanges()
    return Object.fromEntries(Object.entries(ranges))
  }

  const conditions = toConditions()

  const getCondition = (key: string) => {
    return conditions[key]
  }

  function keys() {
    return ["base", ...Object.keys(values)]
  }

  function up(name: string) {
    return build({ min: get(name).min })
  }

  function down(name: string) {
    return build({ max: adjust(get(name).min) })
  }

  return {
    values: Object.values(values),
    only,
    keys,
    conditions,
    getCondition,
    up,
    down,
  }
}

type Entries = [string, BreakpointEntry][]

function adjust(value: string | null | undefined) {
  const computedMax = parseFloat(toPx(value!) ?? "") - 0.04
  return toRem(`${computedMax}px`) as string
}

function sort(breakpoints: Record<string, string>): Entries {
  const entries = Object.entries(breakpoints).sort(([, minA], [, minB]) => {
    return parseInt(minA, 10) < parseInt(minB, 10) ? -1 : 1
  })

  return entries.map(([name, min], index, entries) => {
    let max: string | null = null

    if (index <= entries.length - 1) {
      max = entries[index + 1]?.[1]
    }

    if (max != null) {
      max = adjust(max)
    }

    return [name, { name, min: toRem(min), max }]
  })
}

function getPermutations(values: string[]) {
  const result: [string, string][] = []

  values.forEach((current, index) => {
    let idx = index
    idx++
    let next = values[idx]

    while (next) {
      result.push([current, next])
      idx++
      next = values[idx]
    }
  })

  return result
}

function build({
  min,
  max,
}: {
  min?: string | null | undefined
  max?: string | null | undefined
}) {
  if (min == null && max == null) return ""
  return [
    "@media screen",
    min && `(min-width: ${min})`,
    max && `(max-width: ${max})`,
  ]
    .filter(Boolean)
    .join(" and ")
}