element-plus/packages/hooks/use-delayed-toggle/index.ts

48 lines
936 B
TypeScript
Raw Normal View History

import { unref } from 'vue'
import { buildProps } from '@element-plus/utils'
import { useTimeout } from '../use-timeout'
import type { ExtractPropTypes, ToRefs } from 'vue'
export const useDelayedToggleProps = buildProps({
showAfter: {
type: Number,
default: 0,
},
hideAfter: {
type: Number,
default: 200,
},
2022-02-11 11:40:50 +08:00
} as const)
export type UseDelayedToggleProps = {
open: (event?: Event) => void
close: (event?: Event) => void
} & ToRefs<ExtractPropTypes<typeof useDelayedToggleProps>>
export const useDelayedToggle = ({
showAfter,
hideAfter,
open,
close,
}: UseDelayedToggleProps) => {
const { registerTimeout } = useTimeout()
const onOpen = (event?: Event) => {
registerTimeout(() => {
open(event)
}, unref(showAfter))
}
const onClose = (event?: Event) => {
registerTimeout(() => {
close(event)
}, unref(hideAfter))
}
return {
onOpen,
onClose,
}
}