Preserve shadow color when overriding shadow size#14458
Merged
Conversation
thecrypticace
approved these changes
Sep 18, 2024
Contributor
|
Need to update the changelog but looks good otherwise |
3648134 to
2cfcbfa
Compare
adamwathan
added a commit
that referenced
this pull request
Sep 20, 2024
This PR complements #14458 by adding new `shadow-initial` and `inset-shadow-initial` utilities that make it possible to "undo" a custom shadow color and revert to the default shadow color for the current shadow size. For example, in this example the shadow will be red on hover even though the default color for the `shadow` utility is `rgb(0 0 0 / 5%)`: ```html <div class="shadow-sm shadow-red-500 hover:shadow"> <!-- … --> </div> ``` This is usually what you want, but if you actually want `hover:shadow` to apply its default color, you need to know what the color is and add it yourself: ```html <div class="shadow-sm shadow-red-500 hover:shadow hover:shadow-black/5"> <!-- … --> </div> ``` Using `shadow-initial`, you can achieve the same thing without having to know what the original color was: ```html <div class="shadow-sm shadow-red-500 hover:shadow hover:shadow-initial"> <!-- … --> </div> ``` The `inset-shadow-initial` utility does the same thing for the `inset-shadow-*` utilities. --------- Co-authored-by: Adam Wathan <4323180+adamwathan@users.noreply.github.com>
adamwathan
added a commit
that referenced
this pull request
Sep 20, 2024
This PR complements #14458 by adding new `shadow-initial` and `inset-shadow-initial` utilities that make it possible to "undo" a custom shadow color and revert to the default shadow color for the current shadow size. For example, in this example the shadow will be red on hover even though the default color for the `shadow` utility is `rgb(0 0 0 / 5%)`: ```html <div class="shadow-sm shadow-red-500 hover:shadow"> <!-- … --> </div> ``` This is usually what you want, but if you actually want `hover:shadow` to apply its default color, you need to know what the color is and add it yourself: ```html <div class="shadow-sm shadow-red-500 hover:shadow hover:shadow-black/5"> <!-- … --> </div> ``` Using `shadow-initial`, you can achieve the same thing without having to know what the original color was: ```html <div class="shadow-sm shadow-red-500 hover:shadow hover:shadow-initial"> <!-- … --> </div> ``` The `inset-shadow-initial` utility does the same thing for the `inset-shadow-*` utilities. --------- Co-authored-by: Adam Wathan <4323180+adamwathan@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 changes how shadow color and shadow size utilities interact when used with variants.
Take this HTML:
Currently this shadow would be red by default, but revert to the default semi-transparent black color on hover.
This PR changes this behavior such that the shadow remains red on hover, and only the shadow size changes.
We deliberately didn't do this originally because making things behave this way makes it very difficult to get the default shadow color back once you've changed it. The default color for
shadow-xlfor instance isrgb(0 0 0 / 0.1), and the only way to get that color back after changing it is to know that value and explicitly bring it back:To make things more difficult, the default shadow color is not the same across shadow sizes. For
shadow-smit'sblack/5, and forshadow-2xlit'sblack/25.In practice though you basically never need to bring back the default shadow color, so I'm reconsidering this trade-off in v4, and think I prefer this new behavior where the color is preserved but you have to bring back the default color if you actually need it.
A simple workaround if you don't know the color is to reset the
--tw-shadow-colorvariable like this:This relies on semi-private internals though, so perhaps we can introduce a utility for this, like
shadow-defaultorshadow-initialthat just unsets the shadow color.