Skip to content
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

feat: mounting-portal component support mount to customized HTMLElement #278

Open
wants to merge 1 commit into
base: develop
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
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
}