Skip to content
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
4 changes: 2 additions & 2 deletions docs/api/mounting-portal.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ That might lead to some unpredictable behaviour, use at your own risk

| Type | Required | Default |
| -------- | -------- | ------- |
| `String` | yes | `false` |
| `String`/`HTMLElement` | yes | `false` |

A querySelector String defining the DOM element to mount the `<PortalTarget>` to.
A querySelector String or HTMLElement defining the DOM element to mount the `<PortalTarget>` to.

## Portal Props

Expand Down
11 changes: 8 additions & 3 deletions src/components/mounting-portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { VNode, VueConstructor, PropOptions } from 'vue'
import Portal from './portal'
import PortalTarget from './portal-target'
import { wormhole } from './wormhole'
import { pick } from '@/utils'
import { pick, isHTMLElement } from '@/utils'

import { PropWithComponent } from '../types'

Expand All @@ -12,6 +12,7 @@ let _id = 0
export type withPortalTarget = VueConstructor<
Vue & {
portalTarget: any
mountTo: any
}
>

Expand All @@ -35,7 +36,9 @@ export default (Vue as withPortalTarget).extend({
bail: {
type: Boolean,
},
mountTo: { type: String, required: true },
mountTo: {
required: true,
},

// Portal
disabled: { type: Boolean },
Expand Down Expand Up @@ -65,7 +68,9 @@ export default (Vue as withPortalTarget).extend({
},
created() {
if (typeof document === 'undefined') return
let el: HTMLElement | null = document.querySelector(this.mountTo)
let el: HTMLElement | null = isHTMLElement(this.mountTo)
? this.mountTo
: document.querySelector(this.mountTo)

if (!el) {
console.error(
Expand Down
7 changes: 7 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ export function pick<T extends object, K extends keyof T>(
{} as Pick<T, K>
)
}

export function isHTMLElement(target: HTMLElement | string) {
if (typeof target === 'string') {
return false
}
return target && target.nodeType && target.nodeType === 1
}