Skip to content

Commit

Permalink
Add screen function (#4318)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed May 11, 2021
1 parent 07bfce3 commit 7991646
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
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)
})
})

0 comments on commit 7991646

Please sign in to comment.