Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dimension prefix when using dynamic min-* and max-* (resolved sorting) #11225

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
76ed3dc
Revert prepare of v3.3
RobinMalfait Mar 28, 2023
fbbba6f
Revert prepare of v3.3
RobinMalfait Mar 28, 2023
6660f4b
Merge branch 'tailwindlabs:master' into master
brandonmcconnell Apr 11, 2023
910345f
Revert prepare of v3.3
RobinMalfait Mar 28, 2023
88e4f4c
Update CODE_OF_CONDUCT.md (#10999)
vjdevra Apr 13, 2023
dae5fbc
Revert "Update CODE_OF_CONDUCT.md (#10999)"
thecrypticace Apr 14, 2023
a44ca25
Merge branch 'tailwindlabs:master' into master
brandonmcconnell Apr 18, 2023
cd29de9
Revert prepare of v3.3
RobinMalfait Mar 28, 2023
076555a
Update CODE_OF_CONDUCT.md (#10999)
vjdevra Apr 13, 2023
4c94cd4
Revert "Update CODE_OF_CONDUCT.md (#10999)"
thecrypticace Apr 14, 2023
ea4e1cd
Update resolve to version 1.22.3
depfu[bot] Apr 21, 2023
e4a37ce
[oxide] Expose experimental Rust parser setup (#11116)
RobinMalfait Apr 27, 2023
7f555c4
use older version of centos/nodejs-12
RobinMalfait Apr 27, 2023
759a8c2
Update @swc/core to version 1.3.56
depfu[bot] May 2, 2023
4893cad
Update vite to version 4.3.4
depfu[bot] May 3, 2023
e4ddda6
Update postcss-selector-parser to version 6.0.12
depfu[bot] May 6, 2023
cdbcefc
Update cssnano to version 6.0.1
depfu[bot] May 7, 2023
e0135f0
Merge branch 'tailwindlabs:master' into master
brandonmcconnell May 9, 2023
884efa0
Merge remote-tracking branch 'origin/master'
brandonmcconnell May 13, 2023
3e1d1fb
Support `@import "tailwindcss"` using top-level `index.css` file (#11…
RobinMalfait May 10, 2023
f249485
Add support for dimension prefix when using dynamic `min-*` and `max-*`
brandonmcconnell May 11, 2023
cf32d97
Reset changelog
brandonmcconnell May 11, 2023
265b484
Account for dimension prefix in sorting
brandonmcconnell May 13, 2023
abca6af
Reset diffed files to origin state
brandonmcconnell May 13, 2023
f183490
Sort by dimension before numeric value
brandonmcconnell May 13, 2023
bf49fdd
Simplify and consolidate the `compareScreens` return statements
brandonmcconnell May 13, 2023
c196aea
Fix sorting bug by destructuring dimensions separately from values
brandonmcconnell May 13, 2023
39d59b7
Clean up and add comments
brandonmcconnell May 13, 2023
f388378
Shuffle the test string order
brandonmcconnell May 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/util/buildMediaQuery.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { splitDimensionPrefix } from './splitDimensionPrefix'

export default function buildMediaQuery(screens) {
screens = Array.isArray(screens) ? screens : [screens]

Expand All @@ -8,9 +10,12 @@ export default function buildMediaQuery(screens) {
return screen.raw
}

let [minDimension, minValue] = splitDimensionPrefix(screen.min)
let [maxDimension, maxValue] = splitDimensionPrefix(screen.max)

return [
screen.min && `(min-width: ${screen.min})`,
screen.max && `(max-width: ${screen.max})`,
minValue && `(min-${minDimension}: ${minValue})`,
maxValue && `(max-${maxDimension}: ${maxValue})`,
]
.filter(Boolean)
.join(' and ')
Expand Down
27 changes: 21 additions & 6 deletions src/util/normalizeScreens.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { splitDimensionPrefix } from './splitDimensionPrefix'

/**
* @typedef {object} ScreenValue
* @property {number|undefined} min
Expand Down Expand Up @@ -108,14 +110,27 @@ export function compareScreens(type, a, z) {
if (a.not) [aMin, aMax] = [aMax, aMin]
if (z.not) [zMin, zMax] = [zMax, zMin]

aMin = aMin === undefined ? aMin : parseFloat(aMin)
aMax = aMax === undefined ? aMax : parseFloat(aMax)
zMin = zMin === undefined ? zMin : parseFloat(zMin)
zMax = zMax === undefined ? zMax : parseFloat(zMax)
// Split each mediq query value into its respective dimension and value (e.g. `width` and `100px`)
let [aMinDimension, aMinValue] = splitDimensionPrefix(aMin)
let [aMaxDimension, aMaxValue] = splitDimensionPrefix(aMax)
let [zMinDimension, zMinValue] = splitDimensionPrefix(zMin)
let [zMaxDimension, zMaxValue] = splitDimensionPrefix(zMax)

// Determine which dimension to compare (e.g. `min-width/height` vs. `max-width/height`)
let [aDimension, zDimension] =
type === 'min' ? [aMinDimension, zMinDimension] : [aMaxDimension, zMaxDimension]

// Invert the values if we're comparing max values to use descending order
let [aValue, zValue] = type === 'min' ? [aMinValue, zMinValue] : [zMaxValue, aMaxValue]

// Compare dimensions (e.g. "height" or "width" alphabetically)
const dimensionComparison = aDimension.localeCompare(zDimension)

let [aValue, zValue] = type === 'min' ? [aMin, zMin] : [zMax, aMax]
// Compare dimensions (e.g. "100px" and "200px" -> `100 - 200`)
const valueComparison = parseFloat(aValue) - parseFloat(zValue)

return aValue - zValue
// Sort by dimension first, then by value (if dimensions are the same)
return dimensionComparison || valueComparison
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/util/splitDimensionPrefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { splitVariantPrefix } from './splitVariantPrefix'

/**
* @param {string} value
* @returns {['height' | 'width', string]}
*/
export function splitDimensionPrefix(value) {
const [prefix, extractedValue] = splitVariantPrefix(value)
const dimension = prefix === 'h' ? 'height' : 'width'
return [dimension, extractedValue]
}
9 changes: 9 additions & 0 deletions src/util/splitVariantPrefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @param {string} value
* @returns {[string, string]}
*/
export function splitVariantPrefix(value) {
if (typeof value !== 'string') return ['', value]
let parts = value.split(':')
return ['', ...parts].slice(-2)
}
194 changes: 194 additions & 0 deletions tests/min-max-screen-variants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,200 @@ crosscheck(() => {
})
})

it('supports min-* and max-* variants with or without arbitrary dimension prefixes', () => {
let config = {
content: [
{
raw: html`
<div
class="font-bold min-[100px]:font-bold max-[100px]:font-bold min-[w:100px]:font-bold max-[w:100px]:font-bold min-[h:100px]:font-bold max-[h:100px]:font-bold"
></div>
`,
},
],
corePlugins: { preflight: false },
theme: {
screens: defaultScreens,
},
}

let input = css`
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
.font-bold {
font-weight: 700;
}
@media (max-height: 100px) {
.max-\[h\:100px\]\:font-bold {
font-weight: 700;
}
}
@media (max-width: 100px) {
.max-\[100px\]\:font-bold,
.max-\[w\:100px\]\:font-bold {
font-weight: 700;
}
}
@media (min-height: 100px) {
.min-\[h\:100px\]\:font-bold {
font-weight: 700;
}
}
@media (min-width: 100px) {
.min-\[100px\]\:font-bold,
.min-\[w\:100px\]\:font-bold {
font-weight: 700;
}
}
`)
})
})

it('supports min-* and max-* variants being chained together with or without arbitrary dimension prefixes', () => {
let config = {
content: [
{
raw: html`
<div
class="min-[100px]:min-[w:100px]:min-[h:100px]:max-[100px]:max-[w:100px]:max-[h:100px]:font-bold"
></div>
`,
},
],
corePlugins: { preflight: false },
theme: {
screens: defaultScreens,
},
}

let input = css`
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
@media (min-width: 100px) {
@media (min-width: 100px) {
@media (min-height: 100px) {
@media (max-width: 100px) {
@media (max-width: 100px) {
@media (max-height: 100px) {
.min-\[100px\]\:min-\[w\:100px\]\:min-\[h\:100px\]\:max-\[100px\]\:max-\[w\:100px\]\:max-\[h\:100px\]\:font-bold {
font-weight: 700;
}
}
}
}
}
}
}
`)
})
})

it('supports proper sorting of min-* and max-* variants with arbitrary dimension prefixes', () => {
let config = {
content: [
{
raw: html`
<div
class="
min-[3px]:font-bold min-[1px]:font-bold min-[2px]:font-bold
min-[h:3px]:font-bold min-[h:1px]:font-bold min-[h:2px]:font-bold
min-[w:3px]:font-bold min-[w:1px]:font-bold min-[w:2px]:font-bold
max-[3px]:font-bold max-[1px]:font-bold max-[2px]:font-bold
max-[h:3px]:font-bold max-[h:1px]:font-bold max-[h:2px]:font-bold
max-[w:3px]:font-bold max-[w:1px]:font-bold max-[w:2px]:font-bold
"
></div>
`,
},
],
corePlugins: { preflight: false },
theme: {
screens: defaultScreens,
},
}

let input = css`
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
@media (max-height: 3px) {
.max-\[h\:3px\]\:font-bold {
font-weight: 700;
}
}
@media (max-height: 2px) {
.max-\[h\:2px\]\:font-bold {
font-weight: 700;
}
}
@media (max-height: 1px) {
.max-\[h\:1px\]\:font-bold {
font-weight: 700;
}
}
@media (max-width: 3px) {
.max-\[3px\]\:font-bold,
.max-\[w\:3px\]\:font-bold {
font-weight: 700;
}
}
@media (max-width: 2px) {
.max-\[2px\]\:font-bold,
.max-\[w\:2px\]\:font-bold {
font-weight: 700;
}
}
@media (max-width: 1px) {
.max-\[1px\]\:font-bold,
.max-\[w\:1px\]\:font-bold {
font-weight: 700;
}
}
@media (min-height: 1px) {
.min-\[h\:1px\]\:font-bold {
font-weight: 700;
}
}
@media (min-height: 2px) {
.min-\[h\:2px\]\:font-bold {
font-weight: 700;
}
}
@media (min-height: 3px) {
.min-\[h\:3px\]\:font-bold {
font-weight: 700;
}
}
@media (min-width: 1px) {
.min-\[1px\]\:font-bold,
.min-\[w\:1px\]\:font-bold {
font-weight: 700;
}
}
@media (min-width: 2px) {
.min-\[2px\]\:font-bold,
.min-\[w\:2px\]\:font-bold {
font-weight: 700;
}
}
@media (min-width: 3px) {
.min-\[3px\]\:font-bold,
.min-\[w\:3px\]\:font-bold {
font-weight: 700;
}
}
`)
})
})

it('warns when using min variants with complex screen configs', async () => {
let config = {
content: [
Expand Down