Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add dark mode variant option
  • Loading branch information
thecrypticace committed Jan 5, 2024
commit 1544ef576d48b9b20bbee14deb6e6bf8206ed242
14 changes: 14 additions & 0 deletions src/corePlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
},

Expand Down
23 changes: 23 additions & 0 deletions tests/dark-mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 2 additions & 0 deletions types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]>
Expand Down