-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using both selectors and media-queries not working as expected. #24
Comments
Thanks for the report! I can reproduce this. |
This is happening because recent versions of Tailwind CSS reserve special meaning for the // possibly const plugin = require("tailwindcss/plugin"); instead, but you can switch from .cjs to .js and use ESM syntax in modern Tailwind versions
import plugin from 'tailwindcss/plugin';
/** @type {import('tailwindcss').Config} */
// possibly module.exports = instead, but you can switch from .cjs to .js and use ESM syntax in modern Tailwind versions
export default {
darkMode: ['variant', ['@media (prefers-color-scheme: dark) { :root & }', ':root[data-theme=dark] &']],
theme: {
// ... your customizations here
extend: {}
},
// variants: section no longer needed in v3
plugins: [
plugin(({ addVariant }) => {
addVariant('light', ['@media (prefers-color-scheme: light) { :root & }', ':root[data-theme=light] &']);
})
]
} This works for your current configuration without having to refactor the HTML. A note on potential future needs, though: because their |
An alternative option that takes more work to implement: It is also an option not to use the built-in // if you continue to use my plugin
themes: {
// example names
"dark-theme": { ... },
"light-theme": { ... }
}
// if you use the approach bypassing this plugin
addVariant('theme-dark', ['@media (prefers-color-scheme: dark) { :root & }', ':root[data-theme=dark] &']);
addVariant('theme-light', ['@media (prefers-color-scheme: light) { :root & }', ':root[data-theme=light] &']); and go back and change all occurrences of |
Implementing fallback without my plugin would be done by changing the order of the // theme-dark will apply as long as the light theme hasn't been activated by `prefers-color-scheme: light` or by `[data-theme=light]`
addVariant('theme-dark', ':root &');
// but this can override it (it will have enough specificity in the CSS)
addVariant('theme-light', [
'@media (prefers-color-scheme: light) { :root:not([data-theme=dark]) & }',
'[data-theme=light] &'
]); (where here, and in the messages before this, |
Regardless of these workarounds, this plugin and its documentation need to be updated to address this issue. Thanks again for the report. |
Disabling darkMode in the tailwind config is actually possible using thanks for looking into this! |
I quite like the syntax and provided media query exports of this plugin, and would enjoy continued use of it! At this time, I am circumventing the issue by adding two auto themes // Color Schemes
themeVariants({
group: 'schemes',
themes: {
// Basic Dark / Light Themes
dark: {
selector: ":is([data-theme='dark'], :has(>[data-theme='dark']))"
},
light: {
selector: ":is([data-theme='light'], :has(>[data-theme='light']))"
},
'auto-light': {
selector: ":is([data-theme='auto'], :has([data-theme='auto']))",
mediaQuery: prefersLight
},
'auto-dark': {
selector: ":is([data-theme='auto'], :has([data-theme='auto']))",
mediaQuery: prefersDark
}
// // Inverted Colors (make sure there's no compatibility issues with inverted colors)
// 'colors-inverted': {
// mediaQuery: colorsInverted
// }
},
baseSelector: '*'
// fallback: 'auto-dark'
}), It is a little... verbose, but it was the solution I came up with the quickest, which is what mattered at this time. The main annoyance is providing all theme variants whenever I style an element. again, thanks for getting back to me, I'll give the built-in variant config a try. |
Ok so I like your workaround approach but I did still wanna keep the syntax from your plugin sooo I will add attribution in the readme in the next commit, there's already a little comment at the top of the index file |
Seen as this repo seems stale, I would be down to maintain a spiritual successor of this plugin. I don't see a future where I stop working in ts/js soon. Let me know! |
In the docs on the website, there's a table specifying that if a selector matches, it takes precedence over any media query.
The thing is, in my project, my themes are not behaving as described above.
My config is like such:
The generated CSS (for example on the light theme), looks like this:
and behaves as you would expect from the generated css; BOTH the media query AND the selector [data-theme='light'] have to match for the background to turn green.
The text was updated successfully, but these errors were encountered: