-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(project): add animation, grow and fade components
- Loading branch information
Showing
7 changed files
with
225 additions
and
153 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { useState, useEffect, ReactNode, useRef, CSSProperties } from 'react'; | ||
|
||
type Props = { | ||
createStyle: (status: Status) => CSSProperties; | ||
open?: boolean; | ||
duration?: number; | ||
delay?: number; | ||
onOpenAnimationDone?: () => void; | ||
onCloseAnimationEnd?: () => void; | ||
children: ReactNode; | ||
}; | ||
|
||
export type Status = 'opening' | 'open' | 'closing' | 'closed'; | ||
|
||
const Animation = ({ | ||
createStyle, | ||
open = true, | ||
duration = 250, | ||
delay = 0, | ||
onOpenAnimationDone, | ||
onCloseAnimationEnd, | ||
children, | ||
}: Props): JSX.Element | null => { | ||
const [status, setStatus] = useState<Status>('closed'); | ||
const seconds = duration / 1000; | ||
const transition = `transform ${seconds}s ease-out`; // todo: -webkit-transform; | ||
|
||
const timeout = useRef<NodeJS.Timeout>(); | ||
const timeout2 = useRef<NodeJS.Timeout>(); | ||
|
||
useEffect(() => { | ||
if (timeout.current) clearTimeout(timeout.current); | ||
if (timeout2.current) clearTimeout(timeout2.current); | ||
if (open) { | ||
timeout2.current = setTimeout(() => setStatus('opening'), delay); | ||
timeout.current = setTimeout(() => { | ||
setStatus('open'); | ||
onOpenAnimationDone && onOpenAnimationDone(); | ||
}, duration + delay); | ||
} else { | ||
timeout2.current = setTimeout(() => setStatus('closing'), delay); | ||
timeout.current = setTimeout(() => { | ||
setStatus('closed'); | ||
onCloseAnimationEnd && onCloseAnimationEnd(); | ||
}, duration + delay); | ||
} | ||
}, [duration, delay, transition, open, onOpenAnimationDone, onCloseAnimationEnd]); | ||
|
||
if (!open && status === 'closed') { | ||
return null; | ||
} | ||
|
||
return <div style={createStyle(status)}>{children}</div>; | ||
}; | ||
|
||
export default Animation; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React, { ReactNode, CSSProperties } from 'react'; | ||
|
||
import Animation, { Status } from '../Animation'; | ||
|
||
type Props = { | ||
open?: boolean; | ||
duration?: number; | ||
delay?: number; | ||
onOpenAnimationDone?: () => void; | ||
onCloseAnimationEnd?: () => void; | ||
children: ReactNode; | ||
}; | ||
|
||
const Grow = ({ | ||
open = true, | ||
duration = 250, | ||
delay = 0, | ||
onOpenAnimationDone, | ||
onCloseAnimationEnd, | ||
children, | ||
}: Props): JSX.Element | null => { | ||
const seconds = duration / 1000; | ||
const transition = `transform ${seconds}s ease-out`; // todo: -webkit-transform; | ||
const createStyle = (status: Status): CSSProperties => ({ | ||
transition, | ||
transform: status === 'opening' || status === 'open' ? 'scale(1)' : 'scale(0.1)', | ||
}); | ||
|
||
return ( | ||
<Animation | ||
createStyle={(status: Status) => createStyle(status)} | ||
open={open} | ||
duration={duration} | ||
delay={delay} | ||
onOpenAnimationDone={onOpenAnimationDone} | ||
onCloseAnimationEnd={onCloseAnimationEnd} | ||
> | ||
{children} | ||
</Animation> | ||
); | ||
}; | ||
|
||
export default Grow; |
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
Oops, something went wrong.