Skip to content

Commit

Permalink
feat: Support default values for v-bind in CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
leemotive committed Jun 19, 2024
1 parent dadb363 commit 6dcbb8b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 9 additions & 1 deletion packages/compiler-sfc/__tests__/cssVars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,24 @@ describe('CSS vars injection', () => {
`<style>div{
color: v-bind(color);
font-size: v-bind('font.size');
width: v-bind(width, 100%);
font-weight: v-bind('font.weight', bold);
}</style>`,
{ isProd: true },
)
expect(content).toMatch(`_useCssVars(_ctx => ({
"4003f1a6": (_ctx.color),
"41b6490a": (_ctx.font.size)
"41b6490a": (_ctx.font.size),
"3dd5f4e0": (_ctx.width),
"9848e57e": (_ctx.font.weight)
}))}`)

const { code } = compileStyle({
source: `.foo {
color: v-bind(color);
font-size: v-bind('font.size');
width: v-bind(width, 100%);
font-weight: v-bind('font.weight', bold);
}`,
filename: 'test.css',
id: mockId,
Expand All @@ -126,6 +132,8 @@ describe('CSS vars injection', () => {
".foo {
color: var(--4003f1a6);
font-size: var(--41b6490a);
width: var(--3dd5f4e0, 100%);
font-weight: var(--9848e57e, bold);
}"
`)
})
Expand Down
8 changes: 4 additions & 4 deletions packages/compiler-sfc/src/style/cssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ function lexBinding(content: string, start: number): number | null {
state = LexerState.inDoubleQuoteString
} else if (char === `(`) {
parenDepth++
} else if (char === `)`) {
} else if (char === `)` || char === ',') {
if (parenDepth > 0) {
parenDepth--
char === `)` && parenDepth--
} else {
return i
}
Expand Down Expand Up @@ -146,8 +146,8 @@ export const cssVarsPlugin: PluginCreator<CssVarsPluginOptions> = opts => {
const variable = normalizeExpression(value.slice(start, end))
transformed +=
value.slice(lastIndex, match.index) +
`var(--${genVarName(id, variable, isProd)})`
lastIndex = end + 1
`var(--${genVarName(id, variable, isProd)}`
lastIndex = end
}
}
decl.value = transformed + value.slice(lastIndex)
Expand Down

0 comments on commit 6dcbb8b

Please sign in to comment.