-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2919eae
commit fbb8045
Showing
11 changed files
with
107 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useState, useCallback } from 'react'; | ||
|
||
export type Toggler = [ | ||
isOn: boolean, | ||
setOn: () => void, | ||
setOff: () => void, | ||
toggle: () => void, | ||
]; | ||
|
||
export interface ToggleHandlers { | ||
handleToggleOn?: () => void; | ||
handleToggleOff?: () => void; | ||
handleToggle?: (newStatus: boolean) => void; | ||
} | ||
|
||
export default function useToggle(defaultIsOn = false, handlers: ToggleHandlers = {}): Toggler { | ||
const { handleToggleOn, handleToggleOff, handleToggle } = handlers; | ||
const [isOn, setIsOn] = useState(defaultIsOn); | ||
|
||
const setOn = useCallback(() => { | ||
setIsOn(true); | ||
handleToggleOn?.(); | ||
handleToggle?.(true); | ||
}, [handleToggleOn, handleToggle]); | ||
|
||
const setOff = useCallback(() => { | ||
setIsOn(false); | ||
handleToggleOff?.(); | ||
handleToggle?.(false); | ||
}, [handleToggleOff, handleToggle]); | ||
|
||
const toggle = useCallback(() => { | ||
const doToggle = isOn ? setOff : setOn; | ||
doToggle(); | ||
}, [isOn, setOn, setOff]); | ||
|
||
return [isOn, setOn, setOff, toggle]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters