wip: simplify useCssVars
This commit is contained in:
@@ -10,7 +10,6 @@ import {
|
||||
} from '@vue/runtime-dom'
|
||||
|
||||
describe('useCssVars', () => {
|
||||
const id = 'xxxxxx'
|
||||
async function assertCssVars(getApp: (state: any) => ComponentOptions) {
|
||||
const state = reactive({ color: 'red' })
|
||||
const App = getApp(state)
|
||||
@@ -19,17 +18,13 @@ describe('useCssVars', () => {
|
||||
render(h(App), root)
|
||||
await nextTick()
|
||||
for (const c of [].slice.call(root.children as any)) {
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--${id}-color`)).toBe(
|
||||
`red`
|
||||
)
|
||||
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(`--${id}-color`)).toBe(
|
||||
'green'
|
||||
)
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,12 +32,9 @@ describe('useCssVars', () => {
|
||||
await assertCssVars(state => ({
|
||||
setup() {
|
||||
// test receiving render context
|
||||
useCssVars(
|
||||
(ctx: any) => ({
|
||||
color: ctx.color
|
||||
}),
|
||||
id
|
||||
)
|
||||
useCssVars((ctx: any) => ({
|
||||
color: ctx.color
|
||||
}))
|
||||
return state
|
||||
},
|
||||
render() {
|
||||
@@ -54,7 +46,7 @@ describe('useCssVars', () => {
|
||||
test('on fragment root', async () => {
|
||||
await assertCssVars(state => ({
|
||||
setup() {
|
||||
useCssVars(() => state, id)
|
||||
useCssVars(() => state)
|
||||
return () => [h('div'), h('div')]
|
||||
}
|
||||
}))
|
||||
@@ -65,7 +57,7 @@ describe('useCssVars', () => {
|
||||
|
||||
await assertCssVars(state => ({
|
||||
setup() {
|
||||
useCssVars(() => state, id)
|
||||
useCssVars(() => state)
|
||||
return () => h(Child)
|
||||
}
|
||||
}))
|
||||
@@ -91,7 +83,7 @@ describe('useCssVars', () => {
|
||||
|
||||
const App = {
|
||||
setup() {
|
||||
useCssVars(() => state, id)
|
||||
useCssVars(() => state)
|
||||
return () =>
|
||||
h(Suspense, null, {
|
||||
default: h(AsyncComp),
|
||||
@@ -104,9 +96,7 @@ describe('useCssVars', () => {
|
||||
await nextTick()
|
||||
// css vars use with fallback tree
|
||||
for (const c of [].slice.call(root.children as any)) {
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--${id}-color`)).toBe(
|
||||
`red`
|
||||
)
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
|
||||
}
|
||||
// AsyncComp resolve
|
||||
resolveAsync()
|
||||
@@ -115,30 +105,16 @@ describe('useCssVars', () => {
|
||||
await nextTick()
|
||||
// css vars use with default tree
|
||||
for (const c of [].slice.call(root.children as any)) {
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--${id}-color`)).toBe(
|
||||
`red`
|
||||
)
|
||||
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(`--${id}-color`)).toBe(
|
||||
'green'
|
||||
)
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
|
||||
}
|
||||
})
|
||||
|
||||
test('with <style scoped>', async () => {
|
||||
await assertCssVars(state => ({
|
||||
__scopeId: id,
|
||||
setup() {
|
||||
useCssVars(() => state, id)
|
||||
return () => h('div')
|
||||
}
|
||||
}))
|
||||
})
|
||||
|
||||
test('with subTree changed', async () => {
|
||||
const state = reactive({ color: 'red' })
|
||||
const value = ref(true)
|
||||
@@ -146,7 +122,7 @@ describe('useCssVars', () => {
|
||||
|
||||
const App = {
|
||||
setup() {
|
||||
useCssVars(() => state, id)
|
||||
useCssVars(() => state)
|
||||
return () => (value.value ? [h('div')] : [h('div'), h('div')])
|
||||
}
|
||||
}
|
||||
@@ -155,17 +131,13 @@ describe('useCssVars', () => {
|
||||
await nextTick()
|
||||
// css vars use with fallback tree
|
||||
for (const c of [].slice.call(root.children as any)) {
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--${id}-color`)).toBe(
|
||||
`red`
|
||||
)
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe(`red`)
|
||||
}
|
||||
|
||||
value.value = false
|
||||
await nextTick()
|
||||
for (const c of [].slice.call(root.children as any)) {
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--${id}-color`)).toBe(
|
||||
'red'
|
||||
)
|
||||
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -15,8 +15,7 @@ import { ShapeFlags } from '@vue/shared'
|
||||
* @private
|
||||
*/
|
||||
export function useCssVars(
|
||||
getter: (ctx: ComponentPublicInstance) => Record<string, string>,
|
||||
scopeId: string
|
||||
getter: (ctx: ComponentPublicInstance) => Record<string, string>
|
||||
) {
|
||||
const instance = getCurrentInstance()
|
||||
/* istanbul ignore next */
|
||||
@@ -27,22 +26,18 @@ export function useCssVars(
|
||||
}
|
||||
|
||||
const setVars = () =>
|
||||
setVarsOnVNode(instance.subTree, getter(instance.proxy!), scopeId)
|
||||
setVarsOnVNode(instance.subTree, getter(instance.proxy!))
|
||||
onMounted(() => watchEffect(setVars, { flush: 'post' }))
|
||||
onUpdated(setVars)
|
||||
}
|
||||
|
||||
function setVarsOnVNode(
|
||||
vnode: VNode,
|
||||
vars: Record<string, string>,
|
||||
scopeId: string
|
||||
) {
|
||||
function setVarsOnVNode(vnode: VNode, vars: Record<string, string>) {
|
||||
if (__FEATURE_SUSPENSE__ && vnode.shapeFlag & ShapeFlags.SUSPENSE) {
|
||||
const suspense = vnode.suspense!
|
||||
vnode = suspense.activeBranch!
|
||||
if (suspense.pendingBranch && !suspense.isHydrating) {
|
||||
suspense.effects.push(() => {
|
||||
setVarsOnVNode(suspense.activeBranch!, vars, scopeId)
|
||||
setVarsOnVNode(suspense.activeBranch!, vars)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -55,9 +50,9 @@ function setVarsOnVNode(
|
||||
if (vnode.shapeFlag & ShapeFlags.ELEMENT && vnode.el) {
|
||||
const style = vnode.el.style
|
||||
for (const key in vars) {
|
||||
style.setProperty(`--${scopeId}-${key}`, vars[key])
|
||||
style.setProperty(`--${key}`, vars[key])
|
||||
}
|
||||
} else if (vnode.type === Fragment) {
|
||||
;(vnode.children as VNode[]).forEach(c => setVarsOnVNode(c, vars, scopeId))
|
||||
;(vnode.children as VNode[]).forEach(c => setVarsOnVNode(c, vars))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user