-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: almost finished useTransition and Transition
- Loading branch information
Showing
4 changed files
with
57 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ | |
}, | ||
"[typescript]": { | ||
"editor.defaultFormatter": "dbaeumer.vscode-eslint" | ||
} | ||
} | ||
}, | ||
"editor.formatOnSave": true | ||
} |
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,13 @@ | ||
import {Stage, useTransition} from './useTransition'; | ||
|
||
type TransitionProps = { | ||
state: boolean; | ||
timeout: number; | ||
children: (stage: Stage, shouldMount: boolean) => React.ReactNode; | ||
}; | ||
|
||
export function Transition({state, timeout, children}: TransitionProps) { | ||
const {stage, shouldMount} = useTransition(state, timeout); | ||
|
||
return children(stage, shouldMount); | ||
} |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
console.log('init project for npm package register'); | ||
export {}; | ||
export {useTransition} from './useTransition'; | ||
export {Transition} from './Transition'; |
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,39 @@ | ||
import {useEffect, useRef, useState} from 'react'; | ||
|
||
export type Stage = 'from' | 'enter' | 'leave'; | ||
|
||
export function useTransition(state: boolean, timeout: number) { | ||
// the stage of transition - 'from' | 'enter' | 'leave' | ||
const [stage, setStage] = useState<Stage>(state ? 'enter' : 'from'); | ||
|
||
// the timer for should mount | ||
const timer = useRef<number>(); | ||
const [shouldMount, setShouldMount] = useState(state); | ||
|
||
useEffect( | ||
function handleStateChange() { | ||
clearTimeout(timer.current); | ||
|
||
// when true - trans from to enter | ||
// when false - trans enter to leave, unmount after timeout | ||
if (state === true) { | ||
setStage('from'); | ||
setShouldMount(true); | ||
setTimeout(() => { | ||
setStage('enter'); | ||
}); | ||
} else { | ||
setStage('leave'); | ||
timer.current = setTimeout(() => { | ||
setShouldMount(false); | ||
}, timeout); | ||
} | ||
}, | ||
[state, timeout] | ||
); | ||
|
||
return { | ||
stage, | ||
shouldMount, | ||
}; | ||
} |