Skip to content

fix(runtime-dom): should not update style if its not change #5754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
10 changes: 10 additions & 0 deletions packages/runtime-dom/__tests__/patchStyle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ describe(`runtime-dom: style patching`, () => {
expect(el.style.cssText).toBe('')
})

it('should not overwritten the same value', () => {
const el = document.createElement('div')
patchProp(el, 'style', {}, { left: '10px' })
expect(el.style.getPropertyValue('left')).toBe('10px')
el.style.left = '20px'
expect(el.style.getPropertyValue('left')).toBe('20px')
patchProp(el, 'style', { left: '10px' }, { left: '10px' })
expect(el.style.getPropertyValue('left')).toBe('20px')
})

// JSDOM doesn't support custom properties on style object so we have to
// mock it here.
function mockElementWithStyle() {
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime-dom/src/modules/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
const isCssString = isString(next)
if (next && !isCssString) {
for (const key in next) {
if (prev && !isString(prev) && prev[key] === next[key]) {
continue
}
setStyle(style, key, next[key])
}
if (prev && !isString(prev)) {
Expand All @@ -21,7 +24,7 @@ export function patchStyle(el: Element, prev: Style, next: Style) {
const currentDisplay = style.display
if (isCssString) {
if (prev !== next) {
style.cssText = next as string
style.cssText = next
}
} else if (prev) {
el.removeAttribute('style')
Expand Down
6 changes: 4 additions & 2 deletions packages/shared/src/normalizeProp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray, isString, isObject, hyphenate } from './'
import { isArray, isString, isObject, hyphenate, extend } from './'
import { isNoUnitNumericStyleProp } from './domAttrConfig'

export type NormalizedStyle = Record<string, string | number>
Expand All @@ -23,7 +23,9 @@ export function normalizeStyle(
} else if (isString(value)) {
return value
} else if (isObject(value)) {
return value
// make sure to clone it if it's reactive, since the user likely wants
// to mutate it.
return extend({}, value)
}
}

Expand Down