element-plus/docs/.vitepress/vitepress/composables/lock-screen.ts
zouhang d8954f945e
chore: make eslint works in docs folder (#3582)
* chore: make eslints work in docs folder

* refactor(dev): improve eslint

* fix eslint issue

Co-authored-by: Kevin <sxzz@sxzz.moe>
Co-authored-by: jeremywuuuuu <15975785+JeremyWuuuuu@users.noreply.github.com>
2021-09-23 14:16:37 +08:00

60 lines
1.5 KiB
TypeScript

import { onUnmounted } from 'vue'
import {
addClass,
hasClass,
getStyle,
removeClass,
} from 'element-plus/lib/utils/dom'
import getScrollBarWidth from 'element-plus/lib/utils/scrollbar-width'
import { isServer } from '../utils'
export const useLockScreen = () => {
let scrollBarWidth = 0
let withoutHiddenClass = false
let bodyPaddingRight = '0'
let computedBodyPaddingRight = 0
onUnmounted(() => {
cleanup()
})
const cleanup = () => {
if (isServer) return
removeClass(document.body, 'el-popup-parent--hidden')
if (withoutHiddenClass) {
document.body.style.paddingRight = bodyPaddingRight
}
}
const lock = () => {
if (isServer) return
withoutHiddenClass = !hasClass(document.body, 'el-popup-parent--hidden')
if (withoutHiddenClass) {
bodyPaddingRight = document.body.style.paddingRight
computedBodyPaddingRight = parseInt(
getStyle(document.body, 'paddingRight'),
10
)
}
scrollBarWidth = getScrollBarWidth()
const bodyHasOverflow =
document.documentElement.clientHeight < document.body.scrollHeight
const bodyOverflowY = getStyle(document.body, 'overflowY')
if (
scrollBarWidth > 0 &&
(bodyHasOverflow || bodyOverflowY === 'scroll') &&
withoutHiddenClass
) {
document.body.style.paddingRight = `${
computedBodyPaddingRight + scrollBarWidth
}px`
}
addClass(document.body, 'el-popup-parent--hidden')
}
return {
lock,
cleanup,
}
}