Created
May 14, 2020 12:54
-
-
Save simple17/250196fb8c81d7aa4f1b8da54062fddb to your computer and use it in GitHub Desktop.
[expand element] #react
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
(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> | |
</>; | |
} |
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
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