2022-01-04 09:15:15 +08:00
|
|
|
import { onBeforeMount } from 'vue'
|
2022-01-06 12:00:57 +08:00
|
|
|
import { isClient } from '@vueuse/core'
|
2022-02-11 11:03:15 +08:00
|
|
|
import { generateId } from '@element-plus/utils'
|
2022-01-04 09:15:15 +08:00
|
|
|
|
|
|
|
let cachedContainer: HTMLElement
|
|
|
|
|
|
|
|
export const POPPER_CONTAINER_ID = `el-popper-container-${generateId()}`
|
|
|
|
|
|
|
|
export const POPPER_CONTAINER_SELECTOR = `#${POPPER_CONTAINER_ID}`
|
|
|
|
|
2022-04-10 14:08:12 +08:00
|
|
|
const createContainer = () => {
|
|
|
|
const container = document.createElement('div')
|
|
|
|
container.id = POPPER_CONTAINER_ID
|
|
|
|
document.body.appendChild(container)
|
|
|
|
return container
|
|
|
|
}
|
|
|
|
|
2022-01-04 09:15:15 +08:00
|
|
|
export const usePopperContainer = () => {
|
|
|
|
onBeforeMount(() => {
|
2022-01-06 12:00:57 +08:00
|
|
|
if (!isClient) return
|
2022-01-04 09:15:15 +08:00
|
|
|
|
|
|
|
// This is for bypassing the error that when under testing env, we often encounter
|
|
|
|
// document.body.innerHTML = '' situation
|
|
|
|
// for this we need to disable the caching since it's not really needed
|
2022-04-10 14:08:12 +08:00
|
|
|
if (
|
|
|
|
process.env.NODE_ENV === 'test' ||
|
|
|
|
!cachedContainer ||
|
|
|
|
!document.body.querySelector(POPPER_CONTAINER_SELECTOR)
|
|
|
|
) {
|
|
|
|
cachedContainer = createContainer()
|
2022-01-04 09:15:15 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|