Skip to content

Commit 6dcbb8b

Browse files
committed
feat: Support default values for v-bind in CSS
1 parent dadb363 commit 6dcbb8b

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,24 @@ 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);
120126
}`,
121127
filename: 'test.css',
122128
id: mockId,
@@ -126,6 +132,8 @@ describe('CSS vars injection', () => {
126132
".foo {
127133
color: var(--4003f1a6);
128134
font-size: var(--41b6490a);
135+
width: var(--3dd5f4e0, 100%);
136+
font-weight: var(--9848e57e, bold);
129137
}"
130138
`)
131139
})

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ function lexBinding(content: string, start: number): number | null {
9898
state = LexerState.inDoubleQuoteString
9999
} else if (char === `(`) {
100100
parenDepth++
101-
} else if (char === `)`) {
101+
} else if (char === `)` || 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)