Skip to content

feat(customElement): support attachInternals method #12129

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
30 changes: 30 additions & 0 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
render,
renderSlot,
useHost,
useHostInternals,
useShadowRoot,
} from '../src'

Expand Down Expand Up @@ -1164,6 +1165,21 @@ describe('defineCustomElement', () => {
const style = el.shadowRoot?.querySelector('style')!
expect(style.textContent).toBe(`div { color: red; }`)
})

// wait for jsdom to fix https://github.com/jsdom/jsdom/issues/3732
test.todo('useHostInternals', async () => {
const Foo = defineCustomElement({
setup() {
const internals = useHostInternals()!
internals.ariaLive = 'polite'
return () => h('div', 'hello')
},
})
customElements.define('my-el-use-host-internals', Foo)
container.innerHTML = `<my-el-use-host-internals>`
const el = container.childNodes[0] as VueElement
expect(el._internals?.ariaLive).toBe('polite')
})
})

describe('expose', () => {
Expand Down Expand Up @@ -1419,6 +1435,20 @@ describe('defineCustomElement', () => {
expect(e.shadowRoot!.innerHTML).toBe(`false,boolean`)
})

test('support attachInternals method', () => {
const E = defineCustomElement({
formAssociated: true,
render() {
return h('div', 'hello')
},
})
customElements.define('my-el-attach-internals', E)
container.innerHTML = `<my-el-attach-internals></my-el-attach-internals>`
const e = container.childNodes[0] as VueElement
expect(e.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
expect(e._internals).toBeTruthy()
})

test('hyphenated attr removal', async () => {
const E = defineCustomElement({
props: {
Expand Down
15 changes: 15 additions & 0 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ export function defineCustomElement(
if (isPlainObject(Comp)) extend(Comp, extraOptions)
class VueCustomElement extends VueElement {
static def = Comp
static formAssociated = !!options.formAssociated

constructor(initialProps?: Record<string, any>) {
super(Comp, initialProps, _createApp)
}
Expand Down Expand Up @@ -204,6 +206,7 @@ export class VueElement
implements ComponentCustomElementInterface
{
_isVueCE = true
_internals: ElementInternals | null = null
/**
* @internal
*/
Expand Down Expand Up @@ -253,6 +256,9 @@ export class VueElement
private _createApp: CreateAppFunction<Element> = createApp,
) {
super()

if (this.attachInternals) this._internals = this.attachInternals()

if (this.shadowRoot && _createApp !== createApp) {
this._root = this.shadowRoot
} else {
Expand Down Expand Up @@ -710,3 +716,12 @@ export function useShadowRoot(): ShadowRoot | null {
const el = __DEV__ ? useHost('useShadowRoot') : useHost()
return el && el.shadowRoot
}

/**
* Retrieve the ElementInternals of the current custom element. Only usable in setup()
* of a `defineCustomElement` component.
*/
export function useHostInternals(): ElementInternals | null {
const el = __DEV__ ? useHost('useHostInternals') : useHost()
return el && el._internals
}
1 change: 1 addition & 0 deletions packages/runtime-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export {
defineSSRCustomElement,
useShadowRoot,
useHost,
useHostInternals,
VueElement,
type VueElementConstructor,
type CustomElementOptions,
Expand Down
Loading