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

Sort breakpoints? #9425

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 21 additions & 1 deletion src/corePlugins.js
@@ -1,6 +1,7 @@
import fs from 'fs'
import * as path from 'path'
import postcss from 'postcss'
import postcssValueParser from 'postcss-value-parser'
import createUtilityPlugin from './util/createUtilityPlugin'
import buildMediaQuery from './util/buildMediaQuery'
import escapeClassName from './util/escapeClassName'
Expand Down Expand Up @@ -208,10 +209,29 @@ export let variantPlugins = {
},

screenVariants: ({ theme, addVariant }) => {
let id = Symbol('screens')
let values = Object.values(theme('screens', {}))
let sort = !(
values.every((value) => typeof value === 'string') &&
new Set(values.map((value) => postcssValueParser.unit(value)?.unit ?? value)).size === 1
)
? () => 0
: (a, z) => {
// Compare min-width screens
if (a.length === 1 && z.length === 1) {
let [{ min: aMin }] = a
let [{ min: zMin }] = z
return parseInt(aMin) - parseInt(zMin)
}

// Not a screen we want to even try and compare
return 0
}

for (let screen of normalizeScreens(theme('screens'))) {
let query = buildMediaQuery(screen)

addVariant(screen.name, `@media ${query}`)
addVariant(screen.name, `@media ${query}`, { id, sort, value: screen.values })
}
},

Expand Down
36 changes: 35 additions & 1 deletion tests/responsive-and-variants-atrules.test.js
@@ -1,7 +1,7 @@
import fs from 'fs'
import path from 'path'

import { run, css } from './util/run'
import { run, css, html } from './util/run'

test('responsive and variants atrules', () => {
let config = {
Expand Down Expand Up @@ -79,3 +79,37 @@ test('responsive and variants atrules', () => {
expect(result.css).toMatchFormattedCss(expected)
})
})

it('should sort breakpoints intelligentally', () => {
let config = {
content: [{ raw: html`<div class="xs:italic md:underline"></div>` }],
theme: {
extend: {
screens: {
xs: '123px',
},
},
},
corePlugins: { preflight: false },
}

let input = css`
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
@media (min-width: 123px) {
.xs\:italic {
font-style: italic;
}
}

@media (min-width: 768px) {
.md\:underline {
text-decoration-line: underline;
}
}
`)
})
})