Make --theme(…) return CSS variables#17036
Conversation
packages/@tailwindcss-postcss/src/__snapshots__/index.test.ts.snap
Outdated
Show resolved
Hide resolved
| } | ||
|
|
||
| function theme(designSystem: DesignSystem, path: string, ...fallback: string[]) { | ||
| function theme(designSystem: DesignSystem, source: AstNode, path: string, ...fallback: string[]) { |
There was a problem hiding this comment.
We're passing through the source now where we found the value that contains the function invocation.
|
|
||
| getVariantOrder(): Map<Variant, number> | ||
| resolveThemeValue(path: string): string | undefined | ||
| resolveThemeValue(path: string, forceInline?: boolean): string | undefined |
There was a problem hiding this comment.
I have to make this optional because of IntelliSense unfortunately. Also decided to make a boolean here since don't leak the ThemeOption enum beyond internal APIs right now.
| --default-font-feature-settings: var(--font-sans--font-feature-settings); | ||
| --default-font-variation-settings: var(--font-sans--font-variation-settings); | ||
| --default-mono-font-family: var(--font-mono); | ||
| --default-mono-font-feature-settings: var(--font-mono--font-feature-settings); | ||
| --default-mono-font-variation-settings: var(--font-mono--font-variation-settings); |
There was a problem hiding this comment.
Now that the --default-font-feature-settings theme variable is defined as:
@theme {
--default-font-feature-settings: --theme(--font-sans--font-feature-settings, initial);
}The value is resolved to initial (and thus omitted) because we know that --font-sans--font-feature-settings is not defined in the theme. Note that this implies that values referenced via --theme(…) can not be defined at runtime only and NEED to be in the @theme block. I think this is a reasonable thing to do since we also error when it's not in the @theme block (unless a fallback is specified like it is here).
f03d2cc to
d51babe
Compare
c32833a to
2f50ecd
Compare
| // Inject the fallback… | ||
| // | ||
| // - …as the value if the value returned is `initial` | ||
| // - …expect any `initial` fallbacks on `var(…)`, `theme(…)`, or `--theme(…)` |
There was a problem hiding this comment.
Is this supposed to say except?
This entire comment is kinda hard to parse for me
There was a problem hiding this comment.
I reworked this part, thanks
| node.nodes.push({ | ||
| kind: 'word', | ||
| value: `, ${fallback}`, | ||
| }) |
There was a problem hiding this comment.
This seems fine but don't we technically treat the , as a separate node when parsing? Should we do that here too?
There was a problem hiding this comment.
yeah we do, the fallback could also consist of other, nested, functions etc. It's technically more correct to parse the fallback as well but it felt super wasteful to parse it just so it's stringified in the next line. If we ever change stuff so that the stringification won't work anymore if the word node contains stuff it shouldn't, we also have unit tests that would let us know this breaks so I decided to just put it in as a word node.
| `) | ||
| }) | ||
|
|
||
| test('--theme(…) does not inject the fallback if the fallback is `initial`', async () => { |
There was a problem hiding this comment.
This test name feels wrong. The different thing about this test is that the theme key exists and therefore the fallback isn't used.
thecrypticace
left a comment
There was a problem hiding this comment.
left some comments about test names, comments, and a thing about the AST
b775e6a to
14c2ee9
Compare
Closes tailwindlabs#16945 This PR changes the `--theme(…)` function now return CSS `var(…)` definitions unless used in places where `var(…)` is not valid CSS (e.g. in `@media (width >= theme(--breakpoint-md))`): ```css /* input */ @theme { --color-red: red; } .red { color: --theme(--color-red); } /* output */ :root, :host { --color-red: red; } .red { color: var(--color-red); } ``` Furthermore, this adds an `--theme(… inline)` option to the `--theme(…)` function to force the resolution to be inline, e.g.: ```css /* input */ @theme { --color-red: red; } .red { color: --theme(--color-red inline); } /* output */ .red { color: red; } ``` This PR also changes preflight and the default theme to use this new `--theme(…)` function to ensure variables are prefixed correctly. ## Test plan - Added unit tests and a test that pulls in the whole preflight under a prefix theme.
Closes #16945
This PR changes the
--theme(…)function now return CSSvar(…)definitions unless used in places wherevar(…)is not valid CSS (e.g. in@media (width >= theme(--breakpoint-md))):Furthermore, this adds an
--theme(… inline)option to the--theme(…)function to force the resolution to be inline, e.g.:This PR also changes preflight and the default theme to use this new
--theme(…)function to ensure variables are prefixed correctly.Test plan