Skip to content

Commit 99f2127

Browse files
Callback in theme properties is also the theme function (#14659)
Something we noticed while testing the codemods on one of our codebases is that the callback passed to the `theme` function properties doesn't only expose some properties like `colors`, but it's also a function itself. ```ts /** @type {import('tailwindcss').Config} */ export default { theme: { extend: { colors: (theme) => { // The `theme` is a theme function _and_ the object... console.log(theme('spacing.2'), theme.colors.red['500']) return {} }, }, }, plugins: [], } ``` E.g.: https://play.tailwindcss.com/eV7Jgv17X1?file=config --- h/t to @RobinMalfait for the issue description
1 parent c009c9c commit 99f2127

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Add support for `tailwindcss/colors.js`, `tailwindcss/defaultTheme.js`, and `tailwindcss/plugin.js` exports ([#14595](https://github.com/tailwindlabs/tailwindcss/pull/14595))
1313
- Support `keyframes` in JS config file themes ([#14594](https://github.com/tailwindlabs/tailwindcss/pull/14594))
1414
- Support the `color` parameter in JS theme configuration callbacks ([#14651](https://github.com/tailwindlabs/tailwindcss/pull/14651))
15+
- Support using the object parameter in the JS theme configuration callback as `theme()` function ([#14659](https://github.com/tailwindlabs/tailwindcss/pull/14659))
1516
- _Upgrade (experimental)_: Migrate v3 PostCSS setups to v4 in some cases ([#14612](https://github.com/tailwindlabs/tailwindcss/pull/14612))
1617
- _Upgrade (experimental)_: Automatically discover JavaScript config files ([#14597](https://github.com/tailwindlabs/tailwindcss/pull/14597))
1718
- _Upgrade (experimental)_: Migrate legacy classes to the v4 alternative ([#14643](https://github.com/tailwindlabs/tailwindcss/pull/14643))

packages/tailwindcss/src/compat/config/resolve-config.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ test('theme keys can read from the CSS theme', () => {
202202
// Gives access to the colors object directly
203203
primary: colors.green,
204204
}),
205+
transitionColor: (theme) => ({
206+
// The parameter object is also the theme function
207+
...theme('colors'),
208+
}),
205209
},
206210
},
207211
base: '/root',
@@ -237,6 +241,10 @@ test('theme keys can read from the CSS theme', () => {
237241
'950': '#052e16',
238242
},
239243
},
244+
transitionColor: {
245+
red: 'red',
246+
green: 'green',
247+
},
240248
},
241249
})
242250
})

packages/tailwindcss/src/compat/config/resolve-config.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ export function mergeThemeExtension(
116116
return undefined
117117
}
118118

119-
export interface PluginUtils {
120-
theme(keypath: string, defaultValue?: any): any
119+
type ThemeFunction = (keypath: string, defaultValue?: any) => any
120+
export type PluginUtils = ThemeFunction & {
121+
theme: ThemeFunction
121122
colors: typeof colors
122123
}
123124

@@ -176,14 +177,15 @@ function extractConfigs(ctx: ResolutionContext, { config, base, path }: ConfigFi
176177
}
177178

178179
function mergeTheme(ctx: ResolutionContext) {
179-
let api: PluginUtils = {
180-
theme: createThemeFn(ctx.design, () => ctx.theme, resolveValue),
180+
let themeFn = createThemeFn(ctx.design, () => ctx.theme, resolveValue)
181+
let theme = Object.assign(themeFn, {
182+
theme: themeFn,
181183
colors,
182-
}
184+
})
183185

184186
function resolveValue(value: ThemeValue | null | undefined): ResolvedThemeValue {
185187
if (typeof value === 'function') {
186-
return value(api) ?? null
188+
return value(theme) ?? null
187189
}
188190

189191
return value ?? null

0 commit comments

Comments
 (0)