Skip to content

Commit f5499aa

Browse files
Fix issues with the CSS theme() function (#14262)
While working on #14257, we noticed two issues with the CSS `theme()` function: 1. In V3 it's possible to set arrays inside the theme object. An example for this is the default font families as defined here: https://github.com/tailwindlabs/tailwindcss/blob/main/stubs/config.full.js#L303-L311. We now properly join these arrays that are not tuples. 2. We noticed that in the case where there are no modifiers, the fallback values for the CSS `theme()` function had the first word removed. A regression test for this was added.
1 parent 35398e0 commit f5499aa

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Bring back type exports for the cjs build of `@tailwindcss/postcss`. ([#14256](https://github.com/tailwindlabs/tailwindcss/pull/14256))
1313
- Correctly merge tuple values when using the plugin API ([#14260](https://github.com/tailwindlabs/tailwindcss/pull/14260))
14+
- Handle arrays in the CSS `theme()` function when using plugins ([#14262](https://github.com/tailwindlabs/tailwindcss/pull/14262))
15+
- Fix fallback values when using the CSS `theme()` function ([#14262](https://github.com/tailwindlabs/tailwindcss/pull/14262))
1416

1517
## [4.0.0-alpha.20] - 2024-08-23
1618

packages/tailwindcss/src/functions.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,16 +335,16 @@ describe('theme function', () => {
335335
`)
336336
})
337337

338-
test('theme(fontFamily.sans, Helvetica Neue, Helvetica, sans-serif)', async () => {
338+
test('theme(fontFamily.unknown, Helvetica Neue, Helvetica, sans-serif)', async () => {
339339
expect(
340340
await compileCss(css`
341341
.fam {
342-
font-family: theme(fontFamily.sans, Helvetica Neue, Helvetica, sans-serif);
342+
font-family: theme(fontFamily.unknown, Helvetica Neue, Helvetica, sans-serif);
343343
}
344344
`),
345345
).toMatchInlineSnapshot(`
346346
".fam {
347-
font-family: Neue, Helvetica, sans-serif;
347+
font-family: Helvetica Neue, Helvetica, sans-serif;
348348
}"
349349
`)
350350
})

packages/tailwindcss/src/functions.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ export function substituteFunctionsInValue(value: string, pluginApi: PluginAPI):
5959
break
6060
}
6161
path += node.nodes[i].value
62-
skipUntilIndex = i
62+
skipUntilIndex = i + 1
6363
}
6464

6565
path = eventuallyUnquote(path)
66-
let fallbackValues = node.nodes.slice(skipUntilIndex + 2)
66+
let fallbackValues = node.nodes.slice(skipUntilIndex + 1)
6767

6868
replaceWith(cssThemeFn(pluginApi, path, fallbackValues))
6969
}
@@ -90,11 +90,13 @@ function cssThemeFn(
9090
let resolvedValue: string | null = null
9191
let themeValue = pluginApi.theme(path)
9292

93-
if (Array.isArray(themeValue)) {
93+
if (Array.isArray(themeValue) && themeValue.length === 2) {
9494
// When a tuple is returned, return the first element
9595
resolvedValue = themeValue[0]
9696
// We otherwise only ignore string values here, objects (and namespace maps)
9797
// are treated as non-resolved values for the CSS `theme()` function.
98+
} else if (Array.isArray(themeValue)) {
99+
resolvedValue = themeValue.join(', ')
98100
} else if (typeof themeValue === 'string') {
99101
resolvedValue = themeValue
100102
}

0 commit comments

Comments
 (0)