Skip to content
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

Preserve shadow color when overriding shadow size #14458

Merged
merged 2 commits into from
Sep 19, 2024

Conversation

adamwathan
Copy link
Member

This PR changes how shadow color and shadow size utilities interact when used with variants.

Take this HTML:

<div class="shadow-lg shadow-red-500 hover:shadow-xl">
  <!-- … -->
</div>

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-xl for instance is rgb(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:

<div class="shadow-lg shadow-red-500 hover:shadow-xl hover:shadow-black/10">
  <!-- … -->
</div>

To make things more difficult, the default shadow color is not the same across shadow sizes. For shadow-sm it's black/5, and for shadow-2xl it's black/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-color variable like this:

<div class="shadow-lg shadow-red-500 hover:shadow-xl hover:[--tw-shadow-color:initial]">
  <!-- … -->
</div>

This relies on semi-private internals though, so perhaps we can introduce a utility for this, like shadow-default or shadow-initial that just unsets the shadow color.

@thecrypticace
Copy link
Contributor

Need to update the changelog but looks good otherwise

@adamwathan adamwathan merged commit 8d00534 into next Sep 19, 2024
3 checks passed
@adamwathan adamwathan deleted the feat/preserve-shadow-colors branch September 19, 2024 13:43
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 <[email protected]>
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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants