feat(useLazyShow): add useLazyShow function (#1976)

This commit is contained in:
申君健 2024-08-26 14:26:42 +08:00 committed by GitHub
parent b61aeb7dcb
commit 6736545834
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 1 deletions

View File

@ -11,5 +11,6 @@
*/
import { useFloating } from './src/use-floating'
import { useLazyShow } from './src/use-lazy-show'
export { useFloating }
export { useFloating, useLazyShow }

View File

@ -0,0 +1,22 @@
import { hooks } from '@opentiny/vue-common'
const { ref, watch, isRef } = hooks
/** 慢加载的 v-show 的办法, 灵感来自于: https://github.com/antfu/v-lazy-show
* v-show切换显示 tabs\collapse\dropdown\cascader\carousel等
* @example
* const isShow = ref(false)
* const { lazyShow: lazyShowPopper } = useLazyShow(isShow)
*
* <div v-if="lazyShowPopper" v-show="isShow">
* isShow true时DOM
* </div>
*/
export const useLazyShow = (show) => {
const lazyShow = ref(isRef(show) ? show.value : show)
if (!lazyShow.value) {
const stop = watch(show, (v) => v && (lazyShow.value = true) && stop(), { flush: 'sync' })
}
return { lazyShow }
}