Skip to content

Commit cafb0ad

Browse files
committed
feat: Support default values for v-bind in CSS
1 parent 423b462 commit cafb0ad

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

packages/compiler-sfc/__tests__/cssVars.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,25 @@ describe('CSS vars injection', () => {
105105
`<style>div{
106106
color: v-bind(color);
107107
font-size: v-bind('font.size');
108+
width: v-bind(width, 100%);
109+
font-weight: v-bind('font.weight', bold);
108110
}</style>`,
109111
{ isProd: true },
110112
)
111113
expect(content).toMatch(`_useCssVars(_ctx => ({
112114
"4003f1a6": (_ctx.color),
113-
"41b6490a": (_ctx.font.size)
115+
"41b6490a": (_ctx.font.size),
116+
"3dd5f4e0": (_ctx.width),
117+
"9848e57e": (_ctx.font.weight)
114118
}))}`)
115119

116120
const { code } = compileStyle({
117121
source: `.foo {
118122
color: v-bind(color);
119123
font-size: v-bind('font.size');
124+
width: v-bind(width, 100%);
125+
font-weight: v-bind('font.weight', bold);
126+
background-color: v-bind(['red', 'blue'][value]);
120127
}`,
121128
filename: 'test.css',
122129
id: mockId,
@@ -126,6 +133,9 @@ describe('CSS vars injection', () => {
126133
".foo {
127134
color: var(--4003f1a6);
128135
font-size: var(--41b6490a);
136+
width: var(--3dd5f4e0, 100%);
137+
font-weight: var(--9848e57e, bold);
138+
background-color: var(--6a45fa3c);
129139
}"
130140
`)
131141
})

packages/compiler-sfc/src/style/cssVars.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ function lexBinding(content: string, start: number): number | null {
9696
state = LexerState.inSingleQuoteString
9797
} else if (char === `"`) {
9898
state = LexerState.inDoubleQuoteString
99-
} else if (char === `(`) {
99+
} else if ('{[('.includes(char)) {
100100
parenDepth++
101-
} else if (char === `)`) {
101+
} else if ('}]),'.includes(char)) {
102102
if (parenDepth > 0) {
103-
parenDepth--
103+
char !== ',' && parenDepth--
104104
} else {
105105
return i
106106
}
@@ -146,8 +146,8 @@ export const cssVarsPlugin: PluginCreator<CssVarsPluginOptions> = opts => {
146146
const variable = normalizeExpression(value.slice(start, end))
147147
transformed +=
148148
value.slice(lastIndex, match.index) +
149-
`var(--${genVarName(id, variable, isProd)})`
150-
lastIndex = end + 1
149+
`var(--${genVarName(id, variable, isProd)}`
150+
lastIndex = end
151151
}
152152
}
153153
decl.value = transformed + value.slice(lastIndex)

0 commit comments

Comments
 (0)