Skip to content

Commit 9e3700c

Browse files
authored
add function presets (tailwindlabs#2680)
1 parent d5df569 commit 9e3700c

2 files changed

Lines changed: 117 additions & 2 deletions

File tree

__tests__/customConfig.test.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,56 @@ test('the default config can be overridden using the presets key', () => {
261261
})
262262
})
263263

264+
test('presets can be functions', () => {
265+
return postcss([
266+
tailwind({
267+
presets: [
268+
() => ({
269+
theme: {
270+
extend: {
271+
minHeight: {
272+
24: '24px',
273+
},
274+
},
275+
},
276+
corePlugins: ['minHeight'],
277+
variants: { minHeight: [] },
278+
}),
279+
],
280+
theme: {
281+
extend: { minHeight: { 48: '48px' } },
282+
},
283+
}),
284+
])
285+
.process(
286+
`
287+
@tailwind utilities
288+
`,
289+
{ from: undefined }
290+
)
291+
.then((result) => {
292+
const expected = `
293+
.min-h-0 {
294+
min-height: 0;
295+
}
296+
.min-h-24 {
297+
min-height: 24px;
298+
}
299+
.min-h-48 {
300+
min-height: 48px;
301+
}
302+
.min-h-full {
303+
min-height: 100%;
304+
}
305+
.min-h-screen {
306+
min-height: 100vh;
307+
}
308+
`
309+
310+
expect(result.css).toMatchCss(expected)
311+
})
312+
})
313+
264314
test('the default config can be removed by using an empty presets key in a preset', () => {
265315
return postcss([
266316
tailwind({
@@ -367,3 +417,68 @@ test('presets can have their own presets', () => {
367417
expect(result.css).toMatchCss(expected)
368418
})
369419
})
420+
421+
test('function presets can be mixed with object presets', () => {
422+
return postcss([
423+
tailwind({
424+
presets: [
425+
() => ({
426+
presets: [],
427+
theme: {
428+
colors: { red: '#dd0000' },
429+
},
430+
}),
431+
{
432+
presets: [
433+
() => ({
434+
presets: [],
435+
theme: {
436+
colors: {
437+
transparent: 'transparent',
438+
red: '#ff0000',
439+
},
440+
},
441+
}),
442+
],
443+
theme: {
444+
extend: {
445+
colors: {
446+
black: 'black',
447+
red: '#ee0000',
448+
},
449+
backgroundColor: (theme) => theme('colors'),
450+
},
451+
},
452+
corePlugins: ['backgroundColor'],
453+
},
454+
],
455+
theme: {
456+
extend: { colors: { white: 'white' } },
457+
},
458+
}),
459+
])
460+
.process(
461+
`
462+
@tailwind utilities
463+
`,
464+
{ from: undefined }
465+
)
466+
.then((result) => {
467+
const expected = `
468+
.bg-transparent {
469+
background-color: transparent;
470+
}
471+
.bg-red {
472+
background-color: #ee0000;
473+
}
474+
.bg-black {
475+
background-color: black;
476+
}
477+
.bg-white {
478+
background-color: white;
479+
}
480+
`
481+
482+
expect(result.css).toMatchCss(expected)
483+
})
484+
})

src/util/getAllConfigs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import defaultConfig from '../../stubs/defaultConfig.stub.js'
22
import { flagEnabled } from '../featureFlags'
3-
import { flatMap, get } from 'lodash'
3+
import { flatMap, get, isFunction } from 'lodash'
44

55
export default function getAllConfigs(config) {
66
const configs = flatMap([...get(config, 'presets', [defaultConfig])].reverse(), (preset) => {
7-
return getAllConfigs(preset)
7+
return getAllConfigs(isFunction(preset) ? preset() : preset)
88
})
99

1010
const features = {

0 commit comments

Comments
 (0)