Skip to content

feat(custom-element): allow specifying additional options for shadowRoot in custom elements #12965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,26 @@ describe('defineCustomElement', () => {
container.appendChild(e)
expect(e.shadowRoot!.innerHTML).toBe('<div></div>')
})

// https://github.com/vuejs/core/issues/12964
// Disabled because of missing support for `delegatesFocus` in jsdom
// https://github.com/jsdom/jsdom/issues/3418
// eslint-disable-next-line vitest/no-disabled-tests
test.skip('shadowRoot should be initialized with delegatesFocus', () => {
const E = defineCustomElement(
{
render() {
return [h('input', { tabindex: 1 })]
},
},
{ shadowRootOptions: { delegatesFocus: true } },
)
customElements.define('my-el-with-delegate-focus', E)

const e = new E()
container.appendChild(e)
expect(e.shadowRoot!.delegatesFocus).toBe(true)
})
})

describe('emits', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type VueElementConstructor<P = {}> = {
export interface CustomElementOptions {
styles?: string[]
shadowRoot?: boolean
shadowRootOptions?: Omit<ShadowRootInit, 'mode'>
nonce?: string
configureApp?: (app: App) => void
}
Expand Down Expand Up @@ -263,7 +264,11 @@ export class VueElement
)
}
if (_def.shadowRoot !== false) {
this.attachShadow({ mode: 'open' })
this.attachShadow(
extend({}, _def.shadowRootOptions, {
mode: 'open',
}) as ShadowRootInit,
)
this._root = this.shadowRoot!
} else {
this._root = this
Expand Down