2022-03-25 15:35:56 +08:00
|
|
|
import { isRef, onScopeDispose, watch } from 'vue'
|
2020-09-09 21:18:08 +08:00
|
|
|
|
2022-07-26 18:12:14 +08:00
|
|
|
import { computed } from '@vue/reactivity'
|
2021-12-12 23:28:03 +08:00
|
|
|
import { isClient } from '@vueuse/core'
|
2020-09-09 21:18:08 +08:00
|
|
|
import {
|
|
|
|
addClass,
|
2022-02-11 11:03:15 +08:00
|
|
|
getScrollBarWidth,
|
2022-03-25 15:35:56 +08:00
|
|
|
getStyle,
|
|
|
|
hasClass,
|
|
|
|
removeClass,
|
|
|
|
throwError,
|
2022-02-11 11:03:15 +08:00
|
|
|
} from '@element-plus/utils'
|
2022-07-26 18:12:14 +08:00
|
|
|
import { useNamespace } from '../use-namespace'
|
2020-09-09 21:18:08 +08:00
|
|
|
|
|
|
|
import type { Ref } from 'vue'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook that monitoring the ref value to lock or unlock the screen.
|
|
|
|
* When the trigger became true, it assumes modal is now opened and vice versa.
|
|
|
|
* @param trigger {Ref<boolean>}
|
|
|
|
*/
|
2021-11-29 15:58:44 +08:00
|
|
|
export const useLockscreen = (trigger: Ref<boolean>) => {
|
2020-09-09 21:18:08 +08:00
|
|
|
if (!isRef(trigger)) {
|
|
|
|
throwError(
|
2021-11-29 15:58:44 +08:00
|
|
|
'[useLockscreen]',
|
2021-09-04 19:29:28 +08:00
|
|
|
'You need to pass a ref param to this function'
|
2020-09-09 21:18:08 +08:00
|
|
|
)
|
|
|
|
}
|
2022-07-26 18:12:14 +08:00
|
|
|
|
|
|
|
const ns = useNamespace('popup')
|
|
|
|
|
|
|
|
const hiddenCls = computed(() => ns.bm('parent', 'hidden'))
|
|
|
|
|
|
|
|
if (!isClient || hasClass(document.body, hiddenCls.value)) {
|
2021-12-07 17:25:50 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-09 21:18:08 +08:00
|
|
|
let scrollBarWidth = 0
|
|
|
|
let withoutHiddenClass = false
|
2022-08-12 13:14:58 +08:00
|
|
|
let bodyWidth = '0'
|
2021-03-16 21:40:45 +08:00
|
|
|
|
|
|
|
const cleanup = () => {
|
2022-07-26 18:12:14 +08:00
|
|
|
removeClass(document.body, hiddenCls.value)
|
2021-03-16 21:40:45 +08:00
|
|
|
if (withoutHiddenClass) {
|
2022-08-12 13:14:58 +08:00
|
|
|
document.body.style.width = bodyWidth
|
2021-03-16 21:40:45 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-04 19:29:28 +08:00
|
|
|
watch(trigger, (val) => {
|
2021-11-29 15:58:44 +08:00
|
|
|
if (!val) {
|
2021-03-16 21:40:45 +08:00
|
|
|
cleanup()
|
2021-11-29 15:58:44 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-26 18:12:14 +08:00
|
|
|
withoutHiddenClass = !hasClass(document.body, hiddenCls.value)
|
2021-11-29 15:58:44 +08:00
|
|
|
if (withoutHiddenClass) {
|
2022-08-12 13:14:58 +08:00
|
|
|
bodyWidth = document.body.style.width
|
2021-11-29 15:58:44 +08:00
|
|
|
}
|
2022-07-26 18:12:14 +08:00
|
|
|
scrollBarWidth = getScrollBarWidth(ns.namespace.value)
|
2021-11-29 15:58:44 +08:00
|
|
|
const bodyHasOverflow =
|
|
|
|
document.documentElement.clientHeight < document.body.scrollHeight
|
|
|
|
const bodyOverflowY = getStyle(document.body, 'overflowY')
|
|
|
|
if (
|
|
|
|
scrollBarWidth > 0 &&
|
|
|
|
(bodyHasOverflow || bodyOverflowY === 'scroll') &&
|
|
|
|
withoutHiddenClass
|
|
|
|
) {
|
2022-08-12 13:14:58 +08:00
|
|
|
document.body.style.width = `calc(100% - ${scrollBarWidth}px)`
|
2020-09-09 21:18:08 +08:00
|
|
|
}
|
2022-07-26 18:12:14 +08:00
|
|
|
addClass(document.body, hiddenCls.value)
|
2020-09-09 21:18:08 +08:00
|
|
|
})
|
2021-11-29 15:58:44 +08:00
|
|
|
onScopeDispose(() => cleanup())
|
2020-09-09 21:18:08 +08:00
|
|
|
}
|