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(cssVars): css vars use on suspense root #1718

Merged
merged 2 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
45 changes: 44 additions & 1 deletion packages/runtime-dom/__tests__/helpers/useCssVars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
h,
reactive,
nextTick,
ComponentOptions
ComponentOptions,
Suspense
} from '@vue/runtime-dom'

describe('useCssVars', () => {
Expand Down Expand Up @@ -68,6 +69,48 @@ describe('useCssVars', () => {
}))
})

test('on suspense root', async () => {
const state = reactive({ color: 'red' })
const root = document.createElement('div')

const AsyncComp = {
async setup() {
return () => h('p', 'default')
}
}

const App = {
setup() {
useCssVars(() => state)
return () =>
h(Suspense, null, {
default: h(AsyncComp),
fallback: h('div', 'fallback')
})
}
}

render(h(App), root)
// css vars use with fallback tree
for (const c of [].slice.call(root.children as any)) {
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
}
// AsyncComp resolve
await nextTick()
// Suspense effects flush
await nextTick()
// css vars use with default tree
for (const c of [].slice.call(root.children as any)) {
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
}

state.color = 'green'
await nextTick()
for (const c of [].slice.call(root.children as any)) {
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
}
})

test('with <style scoped>', async () => {
const id = 'data-v-12345'

Expand Down
13 changes: 13 additions & 0 deletions packages/runtime-dom/src/helpers/useCssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,23 @@ function setVarsOnVNode(
vars: Record<string, string>,
prefix: string
) {
if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
const { isResolved, isHydrating, fallbackTree, subTree } = vnode.suspense!
if (isResolved || isHydrating) {
vnode = subTree
} else {
vnode.suspense!.effects.push(() => {
setVarsOnVNode(subTree, vars, prefix)
})
vnode = fallbackTree
}
}

// drill down HOCs until it's a non-component vnode
while (vnode.component) {
vnode = vnode.component.subTree
}

if (vnode.shapeFlag & ShapeFlags.ELEMENT && vnode.el) {
const style = vnode.el.style
for (const key in vars) {
Expand Down