| 1 | /**
|
|---|
| 2 | * @typedef {object} ScreenValue
|
|---|
| 3 | * @property {number|undefined} min
|
|---|
| 4 | * @property {number|undefined} max
|
|---|
| 5 | * @property {string|undefined} raw
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | /**
|
|---|
| 9 | * @typedef {object} Screen
|
|---|
| 10 | * @property {string} name
|
|---|
| 11 | * @property {boolean} not
|
|---|
| 12 | * @property {ScreenValue[]} values
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * A function that normalizes the various forms that the screens object can be
|
|---|
| 17 | * provided in.
|
|---|
| 18 | *
|
|---|
| 19 | * Input(s):
|
|---|
| 20 | * - ['100px', '200px'] // Raw strings
|
|---|
| 21 | * - { sm: '100px', md: '200px' } // Object with string values
|
|---|
| 22 | * - { sm: { min: '100px' }, md: { max: '100px' } } // Object with object values
|
|---|
| 23 | * - { sm: [{ min: '100px' }, { max: '200px' }] } // Object with object array (multiple values)
|
|---|
| 24 | *
|
|---|
| 25 | * Output(s):
|
|---|
| 26 | * - [{ name: 'sm', values: [{ min: '100px', max: '200px' }] }] // List of objects, that contains multiple values
|
|---|
| 27 | *
|
|---|
| 28 | * @returns {Screen[]}
|
|---|
| 29 | */
|
|---|
| 30 | export function normalizeScreens(screens, root = true) {
|
|---|
| 31 | if (Array.isArray(screens)) {
|
|---|
| 32 | return screens.map((screen) => {
|
|---|
| 33 | if (root && Array.isArray(screen)) {
|
|---|
| 34 | throw new Error('The tuple syntax is not supported for `screens`.')
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | if (typeof screen === 'string') {
|
|---|
| 38 | return { name: screen.toString(), not: false, values: [{ min: screen, max: undefined }] }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | let [name, options] = screen
|
|---|
| 42 | name = name.toString()
|
|---|
| 43 |
|
|---|
| 44 | if (typeof options === 'string') {
|
|---|
| 45 | return { name, not: false, values: [{ min: options, max: undefined }] }
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | if (Array.isArray(options)) {
|
|---|
| 49 | return { name, not: false, values: options.map((option) => resolveValue(option)) }
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | return { name, not: false, values: [resolveValue(options)] }
|
|---|
| 53 | })
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | return normalizeScreens(Object.entries(screens ?? {}), false)
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * @param {Screen} screen
|
|---|
| 61 | * @returns {{result: false, reason: string} | {result: true, reason: null}}
|
|---|
| 62 | */
|
|---|
| 63 | export function isScreenSortable(screen) {
|
|---|
| 64 | if (screen.values.length !== 1) {
|
|---|
| 65 | return { result: false, reason: 'multiple-values' }
|
|---|
| 66 | } else if (screen.values[0].raw !== undefined) {
|
|---|
| 67 | return { result: false, reason: 'raw-values' }
|
|---|
| 68 | } else if (screen.values[0].min !== undefined && screen.values[0].max !== undefined) {
|
|---|
| 69 | return { result: false, reason: 'min-and-max' }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | return { result: true, reason: null }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * @param {'min' | 'max'} type
|
|---|
| 77 | * @param {Screen | 'string'} a
|
|---|
| 78 | * @param {Screen | 'string'} z
|
|---|
| 79 | * @returns {number}
|
|---|
| 80 | */
|
|---|
| 81 | export function compareScreens(type, a, z) {
|
|---|
| 82 | let aScreen = toScreen(a, type)
|
|---|
| 83 | let zScreen = toScreen(z, type)
|
|---|
| 84 |
|
|---|
| 85 | let aSorting = isScreenSortable(aScreen)
|
|---|
| 86 | let bSorting = isScreenSortable(zScreen)
|
|---|
| 87 |
|
|---|
| 88 | // These cases should never happen and indicate a bug in Tailwind CSS itself
|
|---|
| 89 | if (aSorting.reason === 'multiple-values' || bSorting.reason === 'multiple-values') {
|
|---|
| 90 | throw new Error(
|
|---|
| 91 | 'Attempted to sort a screen with multiple values. This should never happen. Please open a bug report.'
|
|---|
| 92 | )
|
|---|
| 93 | } else if (aSorting.reason === 'raw-values' || bSorting.reason === 'raw-values') {
|
|---|
| 94 | throw new Error(
|
|---|
| 95 | 'Attempted to sort a screen with raw values. This should never happen. Please open a bug report.'
|
|---|
| 96 | )
|
|---|
| 97 | } else if (aSorting.reason === 'min-and-max' || bSorting.reason === 'min-and-max') {
|
|---|
| 98 | throw new Error(
|
|---|
| 99 | 'Attempted to sort a screen with both min and max values. This should never happen. Please open a bug report.'
|
|---|
| 100 | )
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | // Let the sorting begin
|
|---|
| 104 | let { min: aMin, max: aMax } = aScreen.values[0]
|
|---|
| 105 | let { min: zMin, max: zMax } = zScreen.values[0]
|
|---|
| 106 |
|
|---|
| 107 | // Negating screens flip their behavior. Basically `not min-width` is `max-width`
|
|---|
| 108 | if (a.not) [aMin, aMax] = [aMax, aMin]
|
|---|
| 109 | if (z.not) [zMin, zMax] = [zMax, zMin]
|
|---|
| 110 |
|
|---|
| 111 | aMin = aMin === undefined ? aMin : parseFloat(aMin)
|
|---|
| 112 | aMax = aMax === undefined ? aMax : parseFloat(aMax)
|
|---|
| 113 | zMin = zMin === undefined ? zMin : parseFloat(zMin)
|
|---|
| 114 | zMax = zMax === undefined ? zMax : parseFloat(zMax)
|
|---|
| 115 |
|
|---|
| 116 | let [aValue, zValue] = type === 'min' ? [aMin, zMin] : [zMax, aMax]
|
|---|
| 117 |
|
|---|
| 118 | return aValue - zValue
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /**
|
|---|
| 122 | *
|
|---|
| 123 | * @param {PartialScreen> | string} value
|
|---|
| 124 | * @param {'min' | 'max'} type
|
|---|
| 125 | * @returns {Screen}
|
|---|
| 126 | */
|
|---|
| 127 | export function toScreen(value, type) {
|
|---|
| 128 | if (typeof value === 'object') {
|
|---|
| 129 | return value
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | return {
|
|---|
| 133 | name: 'arbitrary-screen',
|
|---|
| 134 | values: [{ [type]: value }],
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | function resolveValue({ 'min-width': _minWidth, min = _minWidth, max, raw } = {}) {
|
|---|
| 139 | return { min, max, raw }
|
|---|
| 140 | }
|
|---|