Skip to content

Commit fa8253e

Browse files
Fix support for declaration fallbacks in plugins (#14265)
This PR fixes support for "fallback" values for declarations in plugins. A plugin using `addUtilities`, `matchUtilities`, `addComponents`, etc… should be able to specify "fallback" values for declarations by passing an array as the value of a declaration however this does not currently work in v4 (but it does in v3): ```js export default { plugins: [ function ({ addUtilities }) { addUtilities({ '.outlined': { outline: ['1px solid ButtonText', '1px auto -webkit-focus-ring-color'], }, }) }, ], }; ``` After this PR the candidate `outlined` will now produce the following CSS — like it does in v3: ```css .outlined { outline: 1px solid ButtonText; outline: 1px auto -webkit-focus-ring-color; } ```
1 parent f5499aa commit fa8253e

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Correctly merge tuple values when using the plugin API ([#14260](https://github.com/tailwindlabs/tailwindcss/pull/14260))
1414
- Handle arrays in the CSS `theme()` function when using plugins ([#14262](https://github.com/tailwindlabs/tailwindcss/pull/14262))
1515
- Fix fallback values when using the CSS `theme()` function ([#14262](https://github.com/tailwindlabs/tailwindcss/pull/14262))
16+
- Fix support for declaration fallbacks in plugins ([#14265](https://github.com/tailwindlabs/tailwindcss/pull/14265))
1617

1718
## [4.0.0-alpha.20] - 2024-08-23
1819

packages/tailwindcss/src/plugin-api.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,35 @@ describe('addUtilities()', () => {
976976
`)
977977
})
978978

979+
test('utilities can use arrays for fallback declaration values', async () => {
980+
let compiled = await compile(
981+
css`
982+
@plugin "my-plugin";
983+
@tailwind utilities;
984+
`,
985+
{
986+
async loadPlugin() {
987+
return ({ addUtilities }: PluginAPI) => {
988+
addUtilities([
989+
{
990+
'.outlined': {
991+
outline: ['1px solid ButtonText', '1px auto -webkit-focus-ring-color'],
992+
},
993+
},
994+
])
995+
}
996+
},
997+
},
998+
)
999+
1000+
expect(optimizeCss(compiled.build(['outlined'])).trim()).toMatchInlineSnapshot(`
1001+
".outlined {
1002+
outline: 1px solid buttontext;
1003+
outline: 1px auto -webkit-focus-ring-color;
1004+
}"
1005+
`)
1006+
})
1007+
9791008
test('camel case properties are converted to kebab-case', async () => {
9801009
let compiled = await compile(
9811010
css`

packages/tailwindcss/src/plugin-api.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ function buildPluginApi(
290290
return api
291291
}
292292

293-
export type CssInJs = { [key: string]: string | CssInJs | CssInJs[] }
293+
export type CssInJs = { [key: string]: string | string[] | CssInJs | CssInJs[] }
294294

295295
function objectToAst(rules: CssInJs | CssInJs[]): AstNode[] {
296296
let ast: AstNode[] = []
@@ -310,6 +310,14 @@ function objectToAst(rules: CssInJs | CssInJs[]): AstNode[] {
310310

311311
ast.push(decl(name, String(value)))
312312
}
313+
} else if (Array.isArray(value)) {
314+
for (let item of value) {
315+
if (typeof item === 'string') {
316+
ast.push(decl(name, item))
317+
} else {
318+
ast.push(rule(name, objectToAst(item)))
319+
}
320+
}
313321
} else if (value !== null) {
314322
ast.push(rule(name, objectToAst(value)))
315323
}

0 commit comments

Comments
 (0)