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 screen function #4318

Merged
merged 1 commit into from May 11, 2021
Merged
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
10 changes: 10 additions & 0 deletions src/lib/evaluateTailwindFunctions.js
Expand Up @@ -2,6 +2,7 @@ import _ from 'lodash'
import didYouMean from 'didyoumean'
import transformThemeValue from '../util/transformThemeValue'
import parseValue from 'postcss-value-parser'
import buildMediaQuery from '../util/buildMediaQuery'

function findClosestExistingPath(theme, path) {
const parts = _.toPath(path)
Expand Down Expand Up @@ -163,6 +164,15 @@ export default function (config) {

return value
},
screen: (node, screen) => {
screen = _.trim(screen, `'"`)

if (config.theme.screens[screen] === undefined) {
throw node.error(`The '${screen}' screen does not exist in your theme.`)
}

return buildMediaQuery(config.theme.screens[screen])
},
}
return (root) => {
root.walk((node) => {
Expand Down
Expand Up @@ -401,3 +401,129 @@ test('transition-duration values are joined when an array', () => {
expect(result.warnings().length).toBe(0)
})
})

test('basic screen function calls are expanded', () => {
const input = `
@media screen(sm) {
.foo {}
}
`

const output = `
@media (min-width: 600px) {
.foo {}
}
`

return run(input, {
theme: { screens: { sm: '600px' } },
}).then((result) => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})

test('screen function supports max-width screens', () => {
const input = `
@media screen(sm) {
.foo {}
}
`

const output = `
@media (max-width: 600px) {
.foo {}
}
`

return run(input, {
theme: { screens: { sm: { max: '600px' } } },
}).then((result) => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})

test('screen function supports min-width screens', () => {
const input = `
@media screen(sm) {
.foo {}
}
`

const output = `
@media (min-width: 600px) {
.foo {}
}
`

return run(input, {
theme: { screens: { sm: { min: '600px' } } },
}).then((result) => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})

test('screen function supports min-width and max-width screens', () => {
const input = `
@media screen(sm) {
.foo {}
}
`

const output = `
@media (min-width: 600px) and (max-width: 700px) {
.foo {}
}
`

return run(input, {
theme: { screens: { sm: { min: '600px', max: '700px' } } },
}).then((result) => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})

test('screen function supports raw screens', () => {
const input = `
@media screen(mono) {
.foo {}
}
`

const output = `
@media monochrome {
.foo {}
}
`

return run(input, {
theme: { screens: { mono: { raw: 'monochrome' } } },
}).then((result) => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})

test('screen arguments can be quoted', () => {
const input = `
@media screen('sm') {
.foo {}
}
`

const output = `
@media (min-width: 600px) {
.foo {}
}
`

return run(input, {
theme: { screens: { sm: '600px' } },
}).then((result) => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})