element-plus/packages/hooks/use-popper-container/index.ts
三咲智子 6503e55277
refactor(utils): migrate utils (#5949)
* refactor(utils-v2): migrate utils

* refactor(utils-v2): migrate utils

* refactor(utils-v2): migrate utils

* refactor(utils): remove

* refactor(utils): rename

* refactor(utils): move EVENT_CODE to constants

* refactor: remove generic
2022-02-11 11:03:15 +08:00

26 lines
852 B
TypeScript

import { onBeforeMount } from 'vue'
import { isClient } from '@vueuse/core'
import { generateId } from '@element-plus/utils'
let cachedContainer: HTMLElement
export const POPPER_CONTAINER_ID = `el-popper-container-${generateId()}`
export const POPPER_CONTAINER_SELECTOR = `#${POPPER_CONTAINER_ID}`
export const usePopperContainer = () => {
onBeforeMount(() => {
if (!isClient) return
// 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
if (process.env.NODE_ENV === 'test' || !cachedContainer) {
const container = document.createElement('div')
container.id = POPPER_CONTAINER_ID
document.body.appendChild(container)
cachedContainer = container
}
})
}