@@ -24,6 +24,7 @@ import {
2424 type VNodeProps ,
2525 createVNode ,
2626 defineComponent ,
27+ getCurrentInstance ,
2728 nextTick ,
2829 warn ,
2930} from '@vue/runtime-core'
@@ -191,7 +192,10 @@ export class VueElement extends BaseClass {
191192 private _numberProps : Record < string , true > | null = null
192193 private _styles ?: HTMLStyleElement [ ]
193194 private _ob ?: MutationObserver | null = null
194- private _root : Element | ShadowRoot
195+ /**
196+ * @internal
197+ */
198+ public _root : Element | ShadowRoot
195199 private _slots ?: Record < string , Node [ ] >
196200
197201 constructor (
@@ -247,6 +251,7 @@ export class VueElement extends BaseClass {
247251 this . _ob = null
248252 }
249253 render ( null , this . _root )
254+ this . _instance ! . isCE = undefined
250255 this . _instance = null
251256 }
252257 } )
@@ -395,7 +400,7 @@ export class VueElement extends BaseClass {
395400 if ( ! this . _instance ) {
396401 vnode . ce = instance => {
397402 this . _instance = instance
398- instance . isCE = true
403+ instance . isCE = this
399404 // HMR
400405 if ( __DEV__ ) {
401406 instance . ceReload = newStyles => {
@@ -508,3 +513,25 @@ export class VueElement extends BaseClass {
508513 }
509514 }
510515}
516+
517+ /**
518+ * Retrieve the shadowRoot of the current custom element. Only usable in setup()
519+ * of a `defineCustomElement` component.
520+ */
521+ export function useShadowRoot ( ) : ShadowRoot | null {
522+ const instance = getCurrentInstance ( )
523+ const el = instance && instance . isCE
524+ if ( el ) {
525+ return el . shadowRoot
526+ } else if ( __DEV__ ) {
527+ if ( ! instance ) {
528+ warn ( `useCustomElementRoot called without an active component instance.` )
529+ } else {
530+ warn (
531+ `useCustomElementRoot can only be used in components defined via ` +
532+ `defineCustomElement.` ,
533+ )
534+ }
535+ }
536+ return null
537+ }
0 commit comments