import { useState , useMemo } from 'react' ; type IState = string | number | boolean | undefined ; export interface IAction < T = IState > { toggle : ( value ? : T ) => void ; setLeft : ( ) => void ; setRight : ( ) => void ; } function useToggle < T = boolean | undefined > ( ) ; function useToggle < T = IState > ( defaultValue ) ; function useToggle < T = IState , U = IState > ( defaultValue , reverseValue ) ; function useToggle < D extends IState = IState , R extends IState = IState > ( defaultValue = false as D , reverseValue ? ) { const [ value , setValue ] = useState < D | R > ( defaultValue ) ; const action = useMemo ( ( ) => { const reverseValueOrigin = ( reverseValue === void 0 ? ! defaultValue : reverseValue ) as D | R ; const toggle = ( value ? ) => { if ( value !== undefined ) { setValue ( value ) ; return ; } setValue ( v => ( v === defaultValue ? reverseValueOrigin : defaultValue ) ) ; } ; const setLeft = ( ) => { setValue ( defaultValue ) ; } ; const setRight = ( ) => { setValue ( reverseValueOrigin ) ; } ; return { toggle , setLeft , setRight } ; } , [ setValue ] ) ; return [ value , action ] ; } export default useToggle ;