Skip to content

Commit 7991646

Browse files
authored
Add screen function (#4318)
1 parent 07bfce3 commit 7991646

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

src/lib/evaluateTailwindFunctions.js

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import _ from 'lodash'
22
import didYouMean from 'didyoumean'
33
import transformThemeValue from '../util/transformThemeValue'
44
import parseValue from 'postcss-value-parser'
5+
import buildMediaQuery from '../util/buildMediaQuery'
56

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

164165
return value
165166
},
167+
screen: (node, screen) => {
168+
screen = _.trim(screen, `'"`)
169+
170+
if (config.theme.screens[screen] === undefined) {
171+
throw node.error(`The '${screen}' screen does not exist in your theme.`)
172+
}
173+
174+
return buildMediaQuery(config.theme.screens[screen])
175+
},
166176
}
167177
return (root) => {
168178
root.walk((node) => {

tests/themeFunction.test.js tests/evaluateTailwindFunctions.test.js

+126
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,129 @@ test('transition-duration values are joined when an array', () => {
401401
expect(result.warnings().length).toBe(0)
402402
})
403403
})
404+
405+
test('basic screen function calls are expanded', () => {
406+
const input = `
407+
@media screen(sm) {
408+
.foo {}
409+
}
410+
`
411+
412+
const output = `
413+
@media (min-width: 600px) {
414+
.foo {}
415+
}
416+
`
417+
418+
return run(input, {
419+
theme: { screens: { sm: '600px' } },
420+
}).then((result) => {
421+
expect(result.css).toMatchCss(output)
422+
expect(result.warnings().length).toBe(0)
423+
})
424+
})
425+
426+
test('screen function supports max-width screens', () => {
427+
const input = `
428+
@media screen(sm) {
429+
.foo {}
430+
}
431+
`
432+
433+
const output = `
434+
@media (max-width: 600px) {
435+
.foo {}
436+
}
437+
`
438+
439+
return run(input, {
440+
theme: { screens: { sm: { max: '600px' } } },
441+
}).then((result) => {
442+
expect(result.css).toMatchCss(output)
443+
expect(result.warnings().length).toBe(0)
444+
})
445+
})
446+
447+
test('screen function supports min-width screens', () => {
448+
const input = `
449+
@media screen(sm) {
450+
.foo {}
451+
}
452+
`
453+
454+
const output = `
455+
@media (min-width: 600px) {
456+
.foo {}
457+
}
458+
`
459+
460+
return run(input, {
461+
theme: { screens: { sm: { min: '600px' } } },
462+
}).then((result) => {
463+
expect(result.css).toMatchCss(output)
464+
expect(result.warnings().length).toBe(0)
465+
})
466+
})
467+
468+
test('screen function supports min-width and max-width screens', () => {
469+
const input = `
470+
@media screen(sm) {
471+
.foo {}
472+
}
473+
`
474+
475+
const output = `
476+
@media (min-width: 600px) and (max-width: 700px) {
477+
.foo {}
478+
}
479+
`
480+
481+
return run(input, {
482+
theme: { screens: { sm: { min: '600px', max: '700px' } } },
483+
}).then((result) => {
484+
expect(result.css).toMatchCss(output)
485+
expect(result.warnings().length).toBe(0)
486+
})
487+
})
488+
489+
test('screen function supports raw screens', () => {
490+
const input = `
491+
@media screen(mono) {
492+
.foo {}
493+
}
494+
`
495+
496+
const output = `
497+
@media monochrome {
498+
.foo {}
499+
}
500+
`
501+
502+
return run(input, {
503+
theme: { screens: { mono: { raw: 'monochrome' } } },
504+
}).then((result) => {
505+
expect(result.css).toMatchCss(output)
506+
expect(result.warnings().length).toBe(0)
507+
})
508+
})
509+
510+
test('screen arguments can be quoted', () => {
511+
const input = `
512+
@media screen('sm') {
513+
.foo {}
514+
}
515+
`
516+
517+
const output = `
518+
@media (min-width: 600px) {
519+
.foo {}
520+
}
521+
`
522+
523+
return run(input, {
524+
theme: { screens: { sm: '600px' } },
525+
}).then((result) => {
526+
expect(result.css).toMatchCss(output)
527+
expect(result.warnings().length).toBe(0)
528+
})
529+
})

0 commit comments

Comments
 (0)