Skip to content

Commit 775103a

Browse files
committed
feat(custom-element): useHost() helper
1 parent e044b6e commit 775103a

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

packages/runtime-dom/__tests__/customElement.spec.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ref,
1515
render,
1616
renderSlot,
17+
useHost,
1718
useShadowRoot,
1819
} from '../src'
1920

@@ -975,8 +976,22 @@ describe('defineCustomElement', () => {
975976
})
976977
})
977978

978-
describe('useShadowRoot', () => {
979-
test('should work for style injection', () => {
979+
describe('helpers', () => {
980+
test('useHost', () => {
981+
const Foo = defineCustomElement({
982+
setup() {
983+
const host = useHost()!
984+
host.setAttribute('id', 'host')
985+
return () => h('div', 'hello')
986+
},
987+
})
988+
customElements.define('my-el-use-host', Foo)
989+
container.innerHTML = `<my-el-use-host>`
990+
const el = container.childNodes[0] as VueElement
991+
expect(el.id).toBe('host')
992+
})
993+
994+
test('useShadowRoot for style injection', () => {
980995
const Foo = defineCustomElement({
981996
setup() {
982997
const root = useShadowRoot()!
@@ -986,8 +1001,8 @@ describe('defineCustomElement', () => {
9861001
return () => h('div', 'hello')
9871002
},
9881003
})
989-
customElements.define('my-el', Foo)
990-
container.innerHTML = `<my-el></my-el>`
1004+
customElements.define('my-el-use-shadow-root', Foo)
1005+
container.innerHTML = `<my-el-use-shadow-root>`
9911006
const el = container.childNodes[0] as VueElement
9921007
const style = el.shadowRoot?.querySelector('style')!
9931008
expect(style.textContent).toBe(`div { color: red; }`)

packages/runtime-dom/src/apiCustomElement.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -653,24 +653,31 @@ export class VueElement
653653
}
654654
}
655655

656-
/**
657-
* Retrieve the shadowRoot of the current custom element. Only usable in setup()
658-
* of a `defineCustomElement` component.
659-
*/
660-
export function useShadowRoot(): ShadowRoot | null {
656+
export function useHost(caller?: string): VueElement | null {
661657
const instance = getCurrentInstance()
662-
const el = instance && instance.ce
658+
const el = instance && (instance.ce as VueElement)
663659
if (el) {
664-
return (el as VueElement).shadowRoot
660+
return el
665661
} else if (__DEV__) {
666662
if (!instance) {
667-
warn(`useShadowRoot called without an active component instance.`)
663+
warn(
664+
`${caller || 'useHost'} called without an active component instance.`,
665+
)
668666
} else {
669667
warn(
670-
`useShadowRoot can only be used in components defined via ` +
668+
`${caller || 'useHost'} can only be used in components defined via ` +
671669
`defineCustomElement.`,
672670
)
673671
}
674672
}
675673
return null
676674
}
675+
676+
/**
677+
* Retrieve the shadowRoot of the current custom element. Only usable in setup()
678+
* of a `defineCustomElement` component.
679+
*/
680+
export function useShadowRoot(): ShadowRoot | null {
681+
const el = __DEV__ ? useHost('useShadowRoot') : useHost()
682+
return el && el.shadowRoot
683+
}

packages/runtime-dom/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ export {
247247
defineCustomElement,
248248
defineSSRCustomElement,
249249
useShadowRoot,
250+
useHost,
250251
VueElement,
251252
type VueElementConstructor,
252253
type CustomElementOptions,

0 commit comments

Comments
 (0)