Skip to content

Support fractional opacity modifiers for named shadow sizes#20302

Merged
RobinMalfait merged 4 commits into
tailwindlabs:mainfrom
koreahghg:fix/shadow-fractional-opacity-modifier
Jul 3, 2026
Merged

Support fractional opacity modifiers for named shadow sizes#20302
RobinMalfait merged 4 commits into
tailwindlabs:mainfrom
koreahghg:fix/shadow-fractional-opacity-modifier

Conversation

@koreahghg

Copy link
Copy Markdown
Contributor

Summary

shadow-*, text-shadow-*, drop-shadow-*, and inset-shadow-* accept a bare (non-arbitrary) opacity modifier like /50, but the named-size branch of all four utilities validates it with isPositiveInteger(candidate.modifier.value) instead of isValidOpacityValue(candidate.modifier.value) — the helper every other opacity/alpha modifier in the codebase uses (asColor, used by bg-*, text-*, border-*, ring-*, fill-*, stroke-*, decoration-*, accent-*, caret-*, outline-*, placeholder-*, divide-*).

This means a fractional modifier like /12.5 is silently ignored for a named size (shadow-sm/12.5 behaves exactly like shadow-sm, dropping the modifier), while the exact same /12.5 modifier works correctly on the color variant of the same utility (shadow-red-500/12.5color-mix(in oklab, var(--color-red-500) 12.5%, transparent)), since that path already goes through asColor.

drop-shadow-* is worse: its named-size branch has an extra guard (if (candidate.modifier && !alpha) return) that bails out of the entire utility when a modifier is present but couldn't be resolved to an alpha — so drop-shadow-sm/12.5 produces no CSS at all.

This isn't a case of fractional percentages being unsupported by design — the CHANGELOG entry that introduced shadow-*/<alpha> explicitly describes it as controlling shadow opacity, and the color branch of these same utilities already supports fractional values (shadow-red-500/2.25, /2.5, /2.75 are covered by existing tests). The named-size branch just never got the same treatment.

Repro (verified with pnpm --filter tailwindcss exec vitest run):

  • shadow-red-500/12.5color-mix(in oklab, var(--color-red-500) 12.5%, transparent) (correct)
  • shadow-sm/12.5 → identical output to plain shadow-sm (modifier silently dropped)
  • drop-shadow-sm/12.5no CSS generated at all
  • shadow-sm/50 (integer, control) → works correctly

Fix

Replaced isPositiveInteger(candidate.modifier.value) with isValidOpacityValue(candidate.modifier.value) in the four affected utility definitions in packages/tailwindcss/src/utilities.ts (shadow, text-shadow, drop-shadow, inset-shadow). No other changes were needed — once alpha resolves correctly, drop-shadow's existing if (candidate.modifier && !alpha) return guard naturally stops bailing out, since alpha is no longer undefined for valid fractional modifiers.

Test plan

  • Added a regression test in packages/tailwindcss/src/utilities.test.ts covering shadow-sm/12.5, text-shadow-sm/12.5, drop-shadow-sm/12.5, and inset-shadow-sm/12.5.
  • Verified via pnpm --filter tailwindcss exec vitest run that this test fails (modifier dropped / empty output) with the fix reverted, and passes with it applied.
  • Ran the full tailwindcss package test suite (pnpm --filter tailwindcss exec vitest run) — 4685 tests passing, no regressions.
  • Verified formatting on the changed files with npx prettier --check.

@koreahghg koreahghg requested a review from a team as a code owner July 2, 2026 15:49
@greptile-apps

greptile-apps Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Reviews (2): Last reviewed commit: "update changelog" | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b31a86ce-979b-42f6-96e8-8a18f892063a

📥 Commits

Reviewing files that changed from the base of the PR and between 65cbb31 and b9550ac.

📒 Files selected for processing (2)
  • CHANGELOG.md
  • packages/tailwindcss/src/utilities.test.ts
✅ Files skipped from review due to trivial changes (1)
  • CHANGELOG.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/tailwindcss/src/utilities.test.ts

Walkthrough

This change updates opacity modifier handling in shadow, text-shadow, drop-shadow, and inset-shadow so non-arbitrary modifiers are validated with isValidOpacityValue instead of isPositiveInteger, allowing fractional values such as /12.5. The test suite now covers the generated CSS for each affected utility with fractional modifiers, and the changelog records the new support.

Changes

Cohort / File(s) Summary
Shadow opacity modifier fix
packages/tailwindcss/src/utilities.ts
Replaced integer-only validation with opacity-value validation for alpha parsing in all four shadow-related utilities
packages/tailwindcss/src/utilities.test.ts Added snapshot coverage for drop-shadow/12.5, text-shadow-sm/12.5, shadow-sm/12.5, and inset-shadow-sm/12.5
CHANGELOG.md Added an unreleased note for fractional opacity modifier support

Sequence Diagram(s)

Not applicable.

Related PRs: None identified

Suggested labels: bug, css

Suggested reviewers: philipp-spiess, thecrypticace

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: fractional opacity support for named shadow sizes.
Description check ✅ Passed The description accurately and thoroughly describes the same shadow opacity fix and added tests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/tailwindcss/src/utilities.ts (1)

4617-4625: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider extracting the duplicated modifier-alpha resolution.

The exact same if (arbitrary) ... else if (isValidOpacityValue(...)) ... block is now repeated identically across drop-shadow, text-shadow, shadow, and inset-shadow. This duplication is precisely how the original bug happened — one of the five copies (this one) silently diverged to isPositiveInteger while its siblings kept the correct helper. A small shared helper, e.g. resolveAlphaModifier(candidate.modifier): string | undefined, would remove the risk of this class of drift in the future.

♻️ Example helper extraction
+function resolveAlphaModifier(modifier: CandidateModifier | null): string | undefined {
+  if (!modifier) return undefined
+  if (modifier.kind === 'arbitrary') return modifier.value
+  if (isValidOpacityValue(modifier.value)) return `${modifier.value}%`
+  return undefined
+}
+
 utilities.functional('drop-shadow', (candidate) => {
-  let alpha: string | undefined
-
-  if (candidate.modifier) {
-    if (candidate.modifier.kind === 'arbitrary') {
-      alpha = candidate.modifier.value
-    } else {
-      if (isValidOpacityValue(candidate.modifier.value)) {
-        alpha = `${candidate.modifier.value}%`
-      }
-    }
-  }
+  let alpha = resolveAlphaModifier(candidate.modifier)

Same replacement applies to the text-shadow, shadow, and inset-shadow blocks.

Also applies to: 5426-5434, 5572-5580, 5698-5706


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 224abfc2-a8dc-4493-945f-678cf88fd0d8

📥 Commits

Reviewing files that changed from the base of the PR and between 04588b1 and 65cbb31.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • packages/tailwindcss/src/utilities.test.ts
  • packages/tailwindcss/src/utilities.ts

@Ziiyodullayevv Ziiyodullayevv left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great addition for fractional opacity modifiers on shadow utilities. The implementation:

  1. The test coverage is comprehensive - testing shadow-sm/12.5, text-shadow-sm/12.5, drop-shadow-sm/12.5, and inset-shadow-sm/12.5
  2. The inline snapshot tests show the expected CSS output clearly
  3. The changelog entry is well-written

This brings shadow utilities in line with other opacity modifiers in the codebase. The fix using isValidOpacityValue instead of isPositiveInteger is the right approach.

@koreahghg

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestion! Agreed the duplication is exactly how the original bug slipped in. I'd rather keep this PR scoped to just the bug fix though, and leave extracting a shared resolveAlphaModifier helper for a separate follow-up PR — happy to open that if there's interest.

@RobinMalfait RobinMalfait enabled auto-merge (squash) July 3, 2026 12:29

@RobinMalfait RobinMalfait left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Made some small changes.


No need to introduce follow up PRs for introducing abstractions 👍

@RobinMalfait RobinMalfait merged commit 2683903 into tailwindlabs:main Jul 3, 2026
10 checks passed
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.

3 participants