mirror of https://github.com/vuejs/core.git
				
				
				
			feat(custom-element): support css `:host` selector by applying css vars on host element (#8830)
close #8826
This commit is contained in:
		
							parent
							
								
									917c0631cc
								
							
						
					
					
						commit
						03a9ea2b88
					
				| 
						 | 
					@ -4,6 +4,7 @@ import {
 | 
				
			||||||
  Suspense,
 | 
					  Suspense,
 | 
				
			||||||
  Teleport,
 | 
					  Teleport,
 | 
				
			||||||
  createStaticVNode,
 | 
					  createStaticVNode,
 | 
				
			||||||
 | 
					  defineCustomElement,
 | 
				
			||||||
  h,
 | 
					  h,
 | 
				
			||||||
  nextTick,
 | 
					  nextTick,
 | 
				
			||||||
  reactive,
 | 
					  reactive,
 | 
				
			||||||
| 
						 | 
					@ -381,4 +382,26 @@ describe('useCssVars', () => {
 | 
				
			||||||
      expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
 | 
					      expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // #8826
 | 
				
			||||||
 | 
					  test('with custom element', async () => {
 | 
				
			||||||
 | 
					    const state = reactive({ color: 'red' })
 | 
				
			||||||
 | 
					    const container = document.createElement('div')
 | 
				
			||||||
 | 
					    const App = defineCustomElement({
 | 
				
			||||||
 | 
					      styles: [`div { color: red; }`],
 | 
				
			||||||
 | 
					      setup() {
 | 
				
			||||||
 | 
					        useCssVars(() => state)
 | 
				
			||||||
 | 
					        return () => {
 | 
				
			||||||
 | 
					          return h('div', 'hello')
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					    })
 | 
				
			||||||
 | 
					    customElements.define('css-vars-ce', App)
 | 
				
			||||||
 | 
					    container.innerHTML = `<css-vars-ce></css-vars-ce>`
 | 
				
			||||||
 | 
					    document.body.appendChild(container)
 | 
				
			||||||
 | 
					    await nextTick()
 | 
				
			||||||
 | 
					    expect(container.innerHTML).toBe(
 | 
				
			||||||
 | 
					      `<css-vars-ce style="--color: red;"></css-vars-ce>`,
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -38,7 +38,11 @@ export function useCssVars(getter: (ctx: any) => Record<string, string>) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const setVars = () => {
 | 
					  const setVars = () => {
 | 
				
			||||||
    const vars = getter(instance.proxy)
 | 
					    const vars = getter(instance.proxy)
 | 
				
			||||||
    setVarsOnVNode(instance.subTree, vars)
 | 
					    if (instance.ce) {
 | 
				
			||||||
 | 
					      setVarsOnNode(instance.ce as any, vars)
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      setVarsOnVNode(instance.subTree, vars)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    updateTeleports(vars)
 | 
					    updateTeleports(vars)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue