Skip to content

Instantly share code, notes, and snippets.

@mic159
Last active April 30, 2019 12:16
Show Gist options
  • Save mic159/25dde2a21e7f8092a5a9ef0063a63e2b to your computer and use it in GitHub Desktop.
Save mic159/25dde2a21e7f8092a5a9ef0063a63e2b to your computer and use it in GitHub Desktop.
Semantic UI React component to transition between two elements
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
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