In a TailwindCSS variant - purely out of curiosity, since its usefulness is limited - I want to declare a dark: variant that works inside .dark, but gets overridden when inside a .light parent, and continues to behave this way for nested elements. I created a playground where green indicates that the variant is working as expected.
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<style type="text/tailwindcss">
@custom-variant dark (&:where(.dark, .dark *):not(.dark .light, .dark .light *));
</style>
<div class="dark text-red-500 dark:text-green-500">
DARK
<p class="text-red-500 dark:text-green-500">
DARK
<div class="light dark:text-red-600 text-green-600">
> LIGHT
<p class="dark:text-red-600 text-green-600">
> LIGHT
<div class="dark text-red-500 dark:text-green-500">
>> DARK
<p class="text-red-500 dark:text-green-500">
>> DARK
<div class="light dark:text-red-600 text-green-600">
>>> LIGHT
<p class="dark:text-red-600 text-green-600">
>>> LIGHT
<div class="dark text-red-500 dark:text-green-500">
>>>> DARK
<p class="text-red-500 dark:text-green-500">
>>>> DARK
<div class="light dark:text-red-600 text-green-600">
>>>>> LIGHT
<p class="dark:text-red-600 text-green-600">
>>>>> LIGHT
<div class="dark text-red-500 dark:text-green-500">
>>>>>> DARK
</div>
</p>
</div>
</p>
</div>
</p>
</div>
</p>
</div>
</p>
</div>
</p>
</div>
Understanding the task, this is really just a plain CSS question; the generated CSS looks like this:
Input
@custom-variant dark (&:where(.dark, .dark *):not(.dark .light, .dark .light *));
Output
.dark\:STYLE_NAME {
&:where(.dark, .dark *):not(.dark .light, .dark .light *) {
/* properties for STYLE_NAME */
}
}
Or extended example:
Input
@custom-variant dark {
/* default dark */
&:where(.dark, .dark *):not(.dark .light, .dark .light *) {
@slot;
}
/* dark apply inside light */
&:where(.dark .light .dark, .dark .light .dark *) {
@slot;
}
};
Output
.dark\:STYLE_NAME {
&:where(.dark, .dark *):not(.dark .light, .dark .light *) {
/* properties for STYLE_NAME */
}
&:where(.dark .light .dark, .dark .light .dark *) {
/* properties for STYLE_NAME */
}
}
I'm just looking for the proper selectors that target .dark and all its children, except .light and all of its child elements.