Skip to content

Instantly share code, notes, and snippets.

@simple17
Created May 14, 2020 12:54
Show Gist options
  • Save simple17/250196fb8c81d7aa4f1b8da54062fddb to your computer and use it in GitHub Desktop.
Save simple17/250196fb8c81d7aa4f1b8da54062fddb to your computer and use it in GitHub Desktop.
[expand element] #react
(props) => {
const [elHeight, setElHeight] = useState(0);
const elRef = useRef(null);
const onButtonClick = (e) => {
elHeight ? setElHeight(0) : setElHeight(elRef.current.scrollHeight);
};
const elStyle = {
maxHeight: elHeight,
};
return <>
<div style={elStyle}></div>
<button onClick={onButtonClick}>text</button>
</>;
}
height: auto;
max-height: 0;
overflow: hidden;
transition: max-height .5s ease;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment