Skip to content
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

fix(hydration): avoid hydration mismatch on style variables with falsy values #12442

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/runtime-dom/__tests__/helpers/useCssVars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,31 @@ describe('useCssVars', () => {
render(h(App), root)
expect(colorInOnMount).toBe(`red`)
})

test('filter non-string、non-boolean、non-number value', () => {
const state = reactive<any>({
color: 'red',
size: {},
width: false,
height: 10,
})
const root = document.createElement('div')
let style!: CSSStyleDeclaration

const App = {
setup() {
useCssVars(() => state)
onMounted(() => {
style = (root.children[0] as HTMLElement).style
})
return () => h('div')
},
}

render(h(App), root)
expect(style.getPropertyValue(`--color`)).toBe(`red`)
expect(style.getPropertyValue(`--size`)).toBe(``)
expect(style.getPropertyValue(`--width`)).toBe(`false`)
expect(style.getPropertyValue(`--height`)).toBe(`10`)
})
})
10 changes: 7 additions & 3 deletions packages/runtime-dom/src/helpers/useCssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
warn,
watch,
} from '@vue/runtime-core'
import { NOOP, ShapeFlags } from '@vue/shared'
import { NOOP, ShapeFlags, isRenderableAttrValue } from '@vue/shared'

export const CSS_VAR_TEXT: unique symbol = Symbol(__DEV__ ? 'CSS_VAR_TEXT' : '')
/**
Expand Down Expand Up @@ -99,8 +99,12 @@ function setVarsOnNode(el: Node, vars: Record<string, string>) {
const style = (el as HTMLElement).style
let cssText = ''
for (const key in vars) {
style.setProperty(`--${key}`, vars[key])
cssText += `--${key}: ${vars[key]};`
const value = vars[key]
if (isRenderableAttrValue(value)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A potential breaking change might have been introduced here. Previously, the client would render undefined value, effectively setting the rule value to unset. However, now undefined value will not be rendered, causing the rule to inherit value from the parent node. However, this is an edge case.
Notably, this change aligns with SSR behavior.

Copy link

@adamdehaven adamdehaven Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I don’t think rendering undefined was proper to render regardless; it was essentially setting the value to a string of ’undefined’ which simply doesn’t map to a value.

This worked because undefined is never a valid value in CSS.

In these scenarios where we want to unset and prevent inheritance, it should actually be set to a value of initial instead, which unsets the value in the current scope.

I was thinking you could use @property syntax to declare the custom property and control inheritance; however, this cannot be defined inline on an element.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately setting the custom property to initial works the same way as an invalid value as it's treated as a string instead of a global CSS value.

const trimVal = String(value).trim()
style.setProperty(`--${key}`, trimVal)
cssText += `--${key}: ${trimVal};`
}
}
;(style as any)[CSS_VAR_TEXT] = cssText
}
Expand Down