Last active
April 30, 2019 12:16
-
-
Save mic159/25dde2a21e7f8092a5a9ef0063a63e2b to your computer and use it in GitHub Desktop.
Semantic UI React component to transition between two elements
This file contains hidden or 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
import React, { ReactElement, useEffect, useState } from 'react' | |
import { SemanticTRANSITIONS, Transition, TransitionPropDuration } from 'semantic-ui-react' | |
interface AnimatedTransitionProps { | |
animation?: SemanticTRANSITIONS | |
duration?: number | string | TransitionPropDuration | |
children: ReactElement | |
} | |
const AnimatedTransition = ({children, animation, duration} : AnimatedTransitionProps) => { | |
const [renderdChild, setChild] = useState<ReactElement>(children) | |
const [visible, setVisibility] = useState<boolean>(true) | |
useEffect(() => { | |
setVisibility(false) | |
}, [children.key]) | |
const onHide = () => { | |
setChild(children) | |
setVisibility(true) | |
} | |
return ( | |
<Transition | |
animation={animation} | |
duration={duration} | |
onHide={onHide} | |
visible={visible} | |
> | |
{renderdChild} | |
</Transition> | |
) | |
} | |
export default AnimatedTransition |
This file contains hidden or 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
import React, { useState } from 'react' | |
import { Header, Button, icon } from 'semantic-ui-react' | |
import AnimatedTransition from './AnimatedTransition' | |
/* | |
Example usage of the AnimatedTransition component. | |
Note the "key" prop on the Header, that's the magic that triggers the transition. | |
*/ | |
const Example = () => { | |
const [state, setState] = useState<boolean>(false) | |
return ( | |
<Fragment> | |
<AnimatedTransition animation="scale"> | |
<Header icon key={state.toString()}> | |
<Icon name={state ? 'dashboard' : 'mobile'} /> | |
This thing is {state.toString()} | |
</Header> | |
</AnimatedTransition> | |
<Button onClick={() => setState(!state)}> | |
Toggle | |
</Button> | |
</Fragment> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment