Add default option to @theme to support overriding default theme values from plugins/JS config files
#14327
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a new
defaultoption to@themeto make it possible for plugins/JS config files to override default theme values, and also ensures that the final set of CSS variables we output takes into account any theme values added by plugins/JS config files.Previously, if you were using the default theme but also had a JS config file that overrode any of those defaults like this:
…then utilities like
text-red-500would correctly usecolor: tomato, but the--color-red-500CSS variable would still be set to the default value:This feels like a straight-up bug — if
#ef4444is not part of your design system because you've overridden it, it shouldn't show up in your set of CSS variables anywhere.So this PR fixes this issue by making sure we don't print the final set of CSS variables until all of your plugins and config files have had a chance to update the theme.
The second issue is that we realized people have different expectations about how plugin/config theme values should interact with Tailwind's default theme vs. explicitly user-configured theme values.
Take this setup for instance:
If
tailwind.config.jsoverridesred-500to betomato, you'd expecttext-red-500to actually betomato, not the default#ef4444color.But in this setup:
…you'd expect
text-red-500to be#f00. This is despite the fact that currently in Tailwind there is no difference here — they are both just@themeblocks, one just happens to be coming from an imported file (@import "tailwindcss").So to resolve this ambiguity, I've added a
defaultoption to@themefor explicitly registering theme values as "defaults" that are safe to override with plugin/JS config theme values:Now
text-red-500would betomatohere as per the config file.This API is not something users are generally going to interact with — they will almost never want to use
defaultexplicitly. But in this PR I've updated the default theme we ship with to includedefaultso that it interacts in a more intuitive way with plugins and JS config files.Finally, this PR makes sure all theme values registered by plugins/configs are registered with
isReference: trueto make sure they do not end up in the final CSS at all.This is important to make sure that the super weird shit we used to do in configs in v3 doesn't get translated into nonsense variables that pollute your output (hello typography plugin I'm looking at you).
If we don't do this, you'll end up with CSS variables like this:
Preventing theme values registered in plugins/configs from outputting CSS values also serves the secondary purpose of nudging users to migrate to the CSS config if they do want CSS variables for their theme values.