Skip to content

Commit 47e628d

Browse files
authored
feat(custom-element): allow specifying additional options for shadowRoot in custom elements (#12965)
close #12964
1 parent 6b68f72 commit 47e628d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,26 @@ describe('defineCustomElement', () => {
526526
container.appendChild(e)
527527
expect(e.shadowRoot!.innerHTML).toBe('<div></div>')
528528
})
529+
530+
// https://github.com/vuejs/core/issues/12964
531+
// Disabled because of missing support for `delegatesFocus` in jsdom
532+
// https://github.com/jsdom/jsdom/issues/3418
533+
// eslint-disable-next-line vitest/no-disabled-tests
534+
test.skip('shadowRoot should be initialized with delegatesFocus', () => {
535+
const E = defineCustomElement(
536+
{
537+
render() {
538+
return [h('input', { tabindex: 1 })]
539+
},
540+
},
541+
{ shadowRootOptions: { delegatesFocus: true } },
542+
)
543+
customElements.define('my-el-with-delegate-focus', E)
544+
545+
const e = new E()
546+
container.appendChild(e)
547+
expect(e.shadowRoot!.delegatesFocus).toBe(true)
548+
})
529549
})
530550

531551
describe('emits', () => {

packages/runtime-dom/src/apiCustomElement.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export type VueElementConstructor<P = {}> = {
5353
export interface CustomElementOptions {
5454
styles?: string[]
5555
shadowRoot?: boolean
56+
shadowRootOptions?: Omit<ShadowRootInit, 'mode'>
5657
nonce?: string
5758
configureApp?: (app: App) => void
5859
}
@@ -263,7 +264,11 @@ export class VueElement
263264
)
264265
}
265266
if (_def.shadowRoot !== false) {
266-
this.attachShadow({ mode: 'open' })
267+
this.attachShadow(
268+
extend({}, _def.shadowRootOptions, {
269+
mode: 'open',
270+
}) as ShadowRootInit,
271+
)
267272
this._root = this.shadowRoot!
268273
} else {
269274
this._root = this

0 commit comments

Comments
 (0)