Add dark mode variant option

This commit is contained in:
Jordan Pittman 2023-12-21 10:34:09 -05:00
parent a5ae318a14
commit 1544ef576d
3 changed files with 39 additions and 0 deletions

View file

@ -225,12 +225,26 @@ export let variantPlugins = {
'Change `darkMode` to `media` or remove it entirely.',
'https://tailwindcss.com/docs/upgrade-guide#remove-dark-mode-configuration',
])
} else if (mode === 'variant' && className === '.dark') {
mode = false
log.warn('darkmode-variant-without-selector', [
'darkMode: "variant" was used without a provided selector.',
'Change to `darkMode: ["variant", ".your-selector &"]` ',
])
} else if (mode === 'variant' && !className.includes('&')) {
mode = false
log.warn('darkmode-variant-without-ampersand', [
'A custom dark mode selector was used without an `&` — a `&` is required to correctly scope the custom selector.',
'Change to `darkMode: ["variant", ".your-selector &"]` ',
])
}
if (mode === 'class') {
addVariant('dark', `:is(:where(${className}) &)`)
} else if (mode === 'media') {
addVariant('dark', '@media (prefers-color-scheme: dark)')
} else if (mode === 'variant') {
addVariant('dark', className)
}
},

View file

@ -119,3 +119,26 @@ it('should default to the `media` mode when mode is set to `false`', () => {
`)
})
})
it('should allow customization of the dark mode variant', () => {
let config = {
darkMode: ['variant', '&:not(.light *)'],
content: [{ raw: html`<div class="dark:font-bold"></div>` }],
corePlugins: { preflight: false },
}
let input = css`
@tailwind base;
@tailwind components;
@tailwind utilities;
`
return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
${defaults}
.dark\:font-bold:not(.light *) {
font-weight: 700;
}
`)
})
})

2
types/config.d.ts vendored
View file

@ -75,6 +75,8 @@ type DarkModeConfig =
| 'class'
// Use the `class` strategy with a custom class instead of `.dark`.
| ['class', string]
// Use the `variant` strategy, which allows you to completely customize the selector
| ['variant', string]
type Screen = { raw: string } | { min: string } | { max: string } | { min: string; max: string }
type ScreensConfig = string[] | KeyValuePair<string, string | Screen | Screen[]>