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

Fix CSS theme() function resolution issue #14614

Merged
merged 6 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Don’t crash when scanning a candidate equal to the configured prefix ([#14588](https://github.com/tailwindlabs/tailwindcss/pull/14588))
- Ensure there's always a space before `!important` when stringifying CSS ([#14611](https://github.com/tailwindlabs/tailwindcss/pull/14611))
- Don't set `display: none` on elements that use `hidden="until-found"` ([#14631](https://github.com/tailwindlabs/tailwindcss/pull/14631))
- Ensure the CSS `theme()` function resolves to the right value in some compatibility situations ([#14614](https://github.com/tailwindlabs/tailwindcss/pull/14614))
- Fix issue that could cause the CLI to crash when files are deleted while watching ([#14616](https://github.com/tailwindlabs/tailwindcss/pull/14616))
- _Upgrade (experimental)_: Ensure CSS before a layer stays unlayered when running codemods ([#14596](https://github.com/tailwindlabs/tailwindcss/pull/14596))
- _Upgrade (experimental)_: Resolve issues where some prefixed candidates were not properly migrated ([#14600](https://github.com/tailwindlabs/tailwindcss/pull/14600))
Expand Down
162 changes: 45 additions & 117 deletions packages/tailwindcss/src/compat/apply-compat-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { rule, toCss, walk, WalkAction, type AstNode } from '../ast'
import type { DesignSystem } from '../design-system'
import type { Theme, ThemeKey } from '../theme'
import { withAlpha } from '../utilities'
import { segment } from '../utils/segment'
import { toKeyPath } from '../utils/to-key-path'
import { applyConfigToTheme } from './apply-config-to-theme'
import { applyKeyframesToAst } from './apply-keyframes-to-ast'
import { createCompatConfig } from './config/create-compat-config'
Expand Down Expand Up @@ -129,23 +126,17 @@ export async function applyCompatibilityHooks({
return resolveThemeVariableValue(path)
}

// Extract an eventual modifier from the path. e.g.:
// - "colors.red.500 / 50%" -> "50%"
let lastSlash = path.lastIndexOf('/')
let modifier: string | null = null
if (lastSlash !== -1) {
modifier = path.slice(lastSlash + 1).trim()
path = path.slice(0, lastSlash).trim() as ThemeKey
}

let themeValue = lookupThemeValue(designSystem.theme, path)

// Apply the opacity modifier if present
if (modifier && themeValue) {
return withAlpha(themeValue, modifier)
}

return themeValue
// If the theme value is not found in the simple resolver, we upgrade to the full backward
// compatibility support implementation of the `resolveThemeValue` function.
upgradeToFullPluginSupport({
thecrypticace marked this conversation as resolved.
Show resolved Hide resolved
designSystem,
base,
ast,
globs,
configs: [],
pluginDetails: [],
})
return designSystem.resolveThemeValue(path)
}

// If there are no plugins or configs registered, we don't need to register
Expand Down Expand Up @@ -176,6 +167,40 @@ export async function applyCompatibilityHooks({
),
])

upgradeToFullPluginSupport({
designSystem,
base,
ast,
globs,
configs,
pluginDetails,
})
}

function upgradeToFullPluginSupport({
designSystem,
base,
ast,
globs,
configs,
pluginDetails,
}: {
designSystem: DesignSystem
base: string
ast: AstNode[]
globs: { origin?: string; pattern: string }[]
configs: {
path: string
base: string
config: UserConfig
}[]
pluginDetails: {
path: string
base: string
plugin: Plugin
options: CssPluginOptions | null
}[]
}) {
let pluginConfigs = pluginDetails.map((detail) => {
if (!detail.options) {
return { config: { plugins: [detail.plugin] }, base: detail.base }
Expand Down Expand Up @@ -291,100 +316,3 @@ export async function applyCompatibilityHooks({
globs.push(file)
}
}

function toThemeKey(keypath: string[]) {
return (
keypath
// [1] should move into the nested object tuple. To create the CSS variable
// name for this, we replace it with an empty string that will result in two
// subsequent dashes when joined.
.map((path) => (path === '1' ? '' : path))

// Resolve the key path to a CSS variable segment
.map((part) =>
part
.replaceAll('.', '_')
.replace(/([a-z])([A-Z])/g, (_, a, b) => `${a}-${b.toLowerCase()}`),
)

// Remove the `DEFAULT` key at the end of a path
// We're reading from CSS anyway so it'll be a string
.filter((part, index) => part !== 'DEFAULT' || index !== keypath.length - 1)
.join('-')
)
}

function lookupThemeValue(theme: Theme, path: string) {
let baseThemeKey = '--' + toThemeKey(toKeyPath(path))

let resolvedValue = theme.get([baseThemeKey as ThemeKey])

if (resolvedValue !== null) {
return resolvedValue
}

for (let [givenKey, upgradeKey] of Object.entries(themeUpgradeKeys)) {
if (!baseThemeKey.startsWith(givenKey)) continue

let upgradedKey = upgradeKey + baseThemeKey.slice(givenKey.length)
let resolvedValue = theme.get([upgradedKey as ThemeKey])

if (resolvedValue !== null) {
return resolvedValue
}
}
}

let themeUpgradeKeys = {
'--colors': '--color',
'--accent-color': '--color',
'--backdrop-blur': '--blur',
'--backdrop-brightness': '--brightness',
'--backdrop-contrast': '--contrast',
'--backdrop-grayscale': '--grayscale',
'--backdrop-hue-rotate': '--hueRotate',
'--backdrop-invert': '--invert',
'--backdrop-opacity': '--opacity',
'--backdrop-saturate': '--saturate',
'--backdrop-sepia': '--sepia',
'--background-color': '--color',
'--background-opacity': '--opacity',
'--border-color': '--color',
'--border-opacity': '--opacity',
'--border-radius': '--radius',
'--border-spacing': '--spacing',
'--box-shadow-color': '--color',
'--caret-color': '--color',
'--divide-color': '--borderColor',
'--divide-opacity': '--borderOpacity',
'--divide-width': '--borderWidth',
'--fill': '--color',
'--flex-basis': '--spacing',
'--gap': '--spacing',
'--gradient-color-stops': '--color',
'--height': '--spacing',
'--inset': '--spacing',
'--margin': '--spacing',
'--max-height': '--spacing',
'--max-width': '--spacing',
'--min-height': '--spacing',
'--min-width': '--spacing',
'--outline-color': '--color',
'--padding': '--spacing',
'--placeholder-color': '--color',
'--placeholder-opacity': '--opacity',
'--ring-color': '--color',
'--ring-offset-color': '--color',
'--ring-opacity': '--opacity',
'--scroll-margin': '--spacing',
'--scroll-padding': '--spacing',
'--space': '--spacing',
'--stroke': '--color',
'--text-color': '--color',
'--text-decoration-color': '--color',
'--text-indent': '--spacing',
'--text-opacity': '--opacity',
'--translate': '--spacing',
'--size': '--spacing',
'--width': '--spacing',
}
42 changes: 42 additions & 0 deletions packages/tailwindcss/src/css-functions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -935,3 +935,45 @@ test('replaces CSS theme() function with values inside imported stylesheets', as
}"
`)
})

test('resolves paths ending with a 1', async () => {
expect(
await compileCss(
css`
@theme {
--spacing-1: 0.25rem;
}

.foo {
margin: theme(spacing.1);
}
`,
[],
),
).toMatchInlineSnapshot(`
":root {
--spacing-1: .25rem;
}

.foo {
margin: .25rem;
}"
`)
})

test('upgrades to a full JS compat theme lookup if a value can not be mapped to a CSS variable', async () => {
expect(
await compileCss(
css`
.semi {
font-weight: theme(fontWeight.semibold);
}
`,
[],
),
).toMatchInlineSnapshot(`
".semi {
font-weight: 600;
}"
`)
})