Skip to content

Commit

Permalink
feat(custom-element): support passing custom-element-specific options…
Browse files Browse the repository at this point in the history
… via 2nd argument of defineCustomElement
  • Loading branch information
yyx990803 committed Aug 6, 2024
1 parent 56c76a8 commit 60a88a2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/runtime-dom/__tests__/customElement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,16 +898,20 @@ describe('defineCustomElement', () => {
})

const toggle = ref(true)
const ES = defineCustomElement({
shadowRoot: false,
render() {
return [
renderSlot(this.$slots, 'default'),
toggle.value ? renderSlot(this.$slots, 'named') : null,
renderSlot(this.$slots, 'omitted', {}, () => [h('div', 'fallback')]),
]
const ES = defineCustomElement(
{
render() {
return [
renderSlot(this.$slots, 'default'),
toggle.value ? renderSlot(this.$slots, 'named') : null,
renderSlot(this.$slots, 'omitted', {}, () => [
h('div', 'fallback'),
]),
]
},
},
})
{ shadowRoot: false },
)
customElements.define('my-el-shadowroot-false-slots', ES)

test('should render slots', async () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/runtime-dom/src/apiCustomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export function defineCustomElement<
Exposed
>
>,
extraOptions?: CustomElementOptions,
): VueElementConstructor<ResolvedProps>

// overload 3: defining a custom element from the returned value of
Expand All @@ -149,6 +150,7 @@ export function defineCustomElement<
T extends DefineComponent<any, any, any, any>,
>(
options: T,
extraOptions?: CustomElementOptions,
): VueElementConstructor<
T extends DefineComponent<infer P, any, any, any>
? ExtractPropTypes<P>
Expand All @@ -165,6 +167,7 @@ export function defineCustomElement(
hydrate?: RootHydrateFunction,
): VueElementConstructor {
const Comp = defineComponent(options, extraOptions) as any
if (isPlainObject(Comp)) extend(Comp, extraOptions)
class VueCustomElement extends VueElement {
static def = Comp
constructor(initialProps?: Record<string, any>) {
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ export {
useShadowRoot,
VueElement,
type VueElementConstructor,
type CustomElementOptions,
} from './apiCustomElement'

// SFC CSS utilities
Expand Down

0 comments on commit 60a88a2

Please sign in to comment.