mirror of
https://github.com/theoludwig/p61-project.git
synced 2024-07-17 07:00:12 +02:00
51 lines
815 B
TypeScript
51 lines
815 B
TypeScript
import { useState } from "react"
|
|
|
|
export interface UseBooleanResult {
|
|
value: boolean
|
|
toggle: () => void
|
|
setTrue: () => void
|
|
setFalse: () => void
|
|
}
|
|
|
|
export interface UseBooleanOptions {
|
|
/**
|
|
* The initial value of the boolean.
|
|
* @default false
|
|
*/
|
|
initialValue?: boolean
|
|
}
|
|
|
|
/**
|
|
* Hook to manage a boolean state.
|
|
* @param options
|
|
* @returns
|
|
*/
|
|
export const useBoolean = (
|
|
options: UseBooleanOptions = {},
|
|
): UseBooleanResult => {
|
|
const { initialValue = false } = options
|
|
|
|
const [value, setValue] = useState(initialValue)
|
|
|
|
const toggle = (): void => {
|
|
setValue((old) => {
|
|
return !old
|
|
})
|
|
}
|
|
|
|
const setTrue = (): void => {
|
|
setValue(true)
|
|
}
|
|
|
|
const setFalse = (): void => {
|
|
setValue(false)
|
|
}
|
|
|
|
return {
|
|
value,
|
|
toggle,
|
|
setTrue,
|
|
setFalse,
|
|
}
|
|
}
|