Skip to content
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 @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add support for `tailwindcss/colors.js`, `tailwindcss/defaultTheme.js`, and `tailwindcss/plugin.js` exports ([#14595](https://github.com/tailwindlabs/tailwindcss/pull/14595))
- Support `keyframes` in JS config file themes ([#14594](https://github.com/tailwindlabs/tailwindcss/pull/14594))
- Support the `color` parameter in JS theme configuration callbacks ([#14651](https://github.com/tailwindlabs/tailwindcss/pull/14651))
- Support using the object parameter in the JS theme configuration callback as `theme()` function ([#14659](https://github.com/tailwindlabs/tailwindcss/pull/14659))
- _Upgrade (experimental)_: Migrate v3 PostCSS setups to v4 in some cases ([#14612](https://github.com/tailwindlabs/tailwindcss/pull/14612))
- _Upgrade (experimental)_: Automatically discover JavaScript config files ([#14597](https://github.com/tailwindlabs/tailwindcss/pull/14597))
- _Upgrade (experimental)_: Migrate legacy classes to the v4 alternative ([#14643](https://github.com/tailwindlabs/tailwindcss/pull/14643))
Expand Down
8 changes: 8 additions & 0 deletions packages/tailwindcss/src/compat/config/resolve-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ test('theme keys can read from the CSS theme', () => {
// Gives access to the colors object directly
primary: colors.green,
}),
transitionColor: (theme) => ({
// The parameter object is also the theme function
...theme('colors'),
}),
},
},
base: '/root',
Expand Down Expand Up @@ -237,6 +241,10 @@ test('theme keys can read from the CSS theme', () => {
'950': '#052e16',
},
},
transitionColor: {
red: 'red',
green: 'green',
},
},
})
})
14 changes: 8 additions & 6 deletions packages/tailwindcss/src/compat/config/resolve-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ export function mergeThemeExtension(
return undefined
}

export interface PluginUtils {
theme(keypath: string, defaultValue?: any): any
type ThemeFunction = (keypath: string, defaultValue?: any) => any
export type PluginUtils = ThemeFunction & {
theme: ThemeFunction
colors: typeof colors
}

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

function mergeTheme(ctx: ResolutionContext) {
let api: PluginUtils = {
theme: createThemeFn(ctx.design, () => ctx.theme, resolveValue),
let themeFn = createThemeFn(ctx.design, () => ctx.theme, resolveValue)
let theme = Object.assign(themeFn, {
theme: themeFn,
colors,
}
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm like 95% sure this is not documented anywhere. Pretty sure the types for v3 consider this usage invalid too. We'll still need to support it for B/C reasons but I think we should introduce a wrapper function that warns the first time its used.

cc @philipp-spiess @RobinMalfait thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #14661


function resolveValue(value: ThemeValue | null | undefined): ResolvedThemeValue {
if (typeof value === 'function') {
return value(api) ?? null
return value(theme) ?? null
}

return value ?? null
Expand Down