EN
React - how to make left-side animated menu
10
points
In this short article, we would like to show you how to create animated menu in React.
Below solution shows an animated menu floating from the left side on menu button
click event.
Practical example:
// ONLINE-RUNNER:browser;
//Note: Uncomment import lines during working with JSX Compiler.
// import React from 'react';
// import ReactDOM from 'react-dom';
const commonStyle = {
position: 'fixed',
left: 0,
top: 0,
bottom: 0,
background: 'silver',
overflow: 'hidden',
transition: '0.3s'
};
const visibleStyle = {
...commonStyle,
width: '200px'
};
const hiddenStyle = {
...commonStyle,
width: '0px'
};
const App = () => {
const [visible, setVisible] = React.useState(false);
return (
<div style={{height: '200px'}}>
{!visible && <button onClick={() => setVisible(true)}>Show</button>}
<div style={visible ? visibleStyle : hiddenStyle}>
<button onClick={() => setVisible(false)}>Hide</button>
<div>
1<br />
2<br />
3<br />
</div>
</div>
</div>
);
};
const root = document.querySelector('#root');
ReactDOM.render(<App />, root );
Note: with spread syntax (
...
) we are abe to combine objects that were made with styles objects.