Skip to content

Commit d3b72da

Browse files
committed
fix v-bind class co-existence
1 parent 7acb515 commit d3b72da

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

demos/bind.html

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
.orange {
99
color: orange;
1010
}
11+
.static {
12+
font-weight: bold;
13+
}
1114
[v-cloak] {
1215
opacity: 0;
1316
}
@@ -17,21 +20,25 @@
1720
import { createApp, reactive } from '../src'
1821

1922
// for testing
20-
const data = window.data = reactive({
23+
const data = (window.data = reactive({
2124
id: 'green',
2225
classes: ['foo', { red: true }],
2326
style: { color: 'blue' },
2427
obj: {
2528
class: 'orange'
2629
}
27-
})
30+
}))
2831

2932
createApp(data).mount()
3033
</script>
3134

3235
<div v-scope v-cloak>
3336
<div :id="id">simple binding - this should be green</div>
34-
<div :class="classes">class binding - this should be red</div>
35-
<div :style="style">style binding - this should be blue</div>
37+
<div class="static" :class="classes">
38+
class binding - this should be red and bold
39+
</div>
40+
<div style="font-weight: bold" :style="style">
41+
style binding - this should be blue and bold
42+
</div>
3643
<div v-bind="obj">object binding - this should be orange</div>
3744
</div>

src/directives/bind.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ import {
1010

1111
const forceAttrRE = /^(spellcheck|draggable|form|list|type)$/
1212

13-
export const bind: Directive<Element> = ({
13+
export const bind: Directive<Element & { _class?: string }> = ({
1414
el,
1515
get,
1616
effect,
1717
arg,
1818
modifiers
1919
}) => {
2020
let prevValue: any
21+
22+
// record static class
23+
if (arg === 'class') {
24+
el._class = el.className
25+
}
26+
2127
effect(() => {
2228
let value = get()
2329
if (arg) {
@@ -39,9 +45,17 @@ export const bind: Directive<Element> = ({
3945
})
4046
}
4147

42-
function setProp(el: Element, key: string, value: any, prevValue?: any) {
48+
function setProp(
49+
el: Element & { _class?: string },
50+
key: string,
51+
value: any,
52+
prevValue?: any
53+
) {
4354
if (key === 'class') {
44-
el.setAttribute('class', normalizeClass(value) || '')
55+
el.setAttribute(
56+
'class',
57+
normalizeClass(el._class ? [el._class, value] : value) || ''
58+
)
4559
} else if (key === 'style') {
4660
value = normalizeStyle(value)
4761
const { style } = el as HTMLElement

0 commit comments

Comments
 (0)