Repository navigation
fix(preset): resolve the alpha placeholder of theme() calls without opacity - #11
Merged
Merged
Conversation
…pacity
`theme('colors.primary.400')` in CSS (transformerDirectives) left
`/ %alpha` behind, because `transformThemeString` of @unocss/rule-utils
only resolves alpha placeholders when the call has an opacity, so the
declaration was dropped. `postprocess` cannot reach it: `theme()` is
expanded in the CSS source, not by the generator.
The preset now ships a `pre` transformer that gives `theme()` calls of
its own colours without an opacity an explicit `/1` before the
directives transformer expands them. Calls with an opacity, other
colours and non-CSS files are left alone.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
✅ Deploy Preview for proj-airi-chromatic ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
`typeof import('magic-string')` does not resolve from packages/preset on
a clean install (the preset does not depend on magic-string), which
failed CI's typecheck. Derive the type from `SourceCodeTransformer`
instead.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.
Summary
In CSS processed by
transformerDirectives,theme('colors.primary.400')produced an invalid colour ending in/ %alpha), so browsers dropped the declaration. Found while migrating AIRI's hand-made tints to chromatic colours, which currently have to writetheme('colors.primary.400 / 100%').Cause
/ %alpha). Opacity utilities likebg-primary-500/50need it, because the nestedcalc()insideoklch()cannot be parsed to insert an opacity otherwise.transformThemeStringof@unocss/rule-utils(66.7.5), which backstheme(), only resolves the placeholder when the call has an opacity. Without one it returns the raw string.postprocesscannot fix it:theme()is expanded in the CSS source by the directives transformer, not by the generator.Fix
The preset now ships a source transformer:
enforce: 'pre', CSS ids only. It runs before the directives transformer, which uses the default pass.theme('colors.<one of the preset's colours>[.<shade>]')calls without an opacity totheme('colors.… /1'), so the directives transformer resolves the placeholder to1. That is the value UnoCSS uses for an omitted opacity elsewhere (colorToString).colors.red.500) and non-CSS files are untouched.postwould not work: in a build, Vite's CSS plugin has already extracted the CSS by then.This can be removed once UnoCSS resolves the placeholder for
theme()without an explicit opacity. An upstream fix intransformThemeStringwould be a one-liner and would also help other themes that use%alpha/<alpha-value>.Test plan
shared.theme.test.tsruns the merged transformers in the order@unocss/viteuses (pre → default → post). It covers single / double quotes,DEFAULT, every configured colour, an explicit opacity, a default value, thev1model, other colours, and non-CSS files.theme('colors.primary.400')rule in the playground is served as a valid colour and computes tooklch(0.729 0.207 344)in Chrome. The rule was removed afterwards.pnpm lint,pnpm typecheck,pnpm build,pnpm test:run(31 tests).🤖 Generated with Claude Code