mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-14 09:20:51 +08:00
21 lines
438 B
TypeScript
21 lines
438 B
TypeScript
|
import { isRef, ref } from 'vue'
|
||
|
|
||
|
import type { Ref } from 'vue'
|
||
|
|
||
|
const isBool = (val: unknown): val is boolean => {
|
||
|
return typeof val === 'boolean'
|
||
|
}
|
||
|
|
||
|
export const useToggle = (getToggled?: Ref<boolean>) => {
|
||
|
const val = isRef(getToggled)
|
||
|
? getToggled
|
||
|
: ref(isBool(getToggled) ? getToggled : false)
|
||
|
|
||
|
return [
|
||
|
val,
|
||
|
(toggle?: boolean) => {
|
||
|
val.value = isBool(toggle) ? toggle : !val.value
|
||
|
},
|
||
|
] as const
|
||
|
}
|