React horizontal expand. I am struggling with such a simple thing in React. I am trying to just extend my Div container from 500px to 900px or full width on button click. I can only make it appear and disappear. What am I missing, please? Using React hooks. Thanks in advance
Codepen:
codepen.io/brandonwirz/pen/dygBPrb
`
React horizontal expand. I am close but on collapsed the container disappears. Again, I want
the container to go from around 500px to 900px or full width and vice versa.
const Collapse = ({ collapsed, expanded, children }) => {
const [isCollapsed, setIsCollapsed] = React.useState(collapsed)
// const [isExpanded, setIsExpanded] = React.useState("");
return (
<div className="container">
<button
className="collapse-button"
onClick={() => setIsCollapsed(!isCollapsed)}
>
{isCollapsed ? "Show small" : "Show expanded"} content
</button>
<div
className={`collapse-content ${isCollapsed ? "collapsed" : "expanded"}`}
aria-expanded={isCollapsed}
>
{children}
</div>
</div>
)
}
````
2
Answers
The js is working fine, the problem is with the CSS. You cannot see the changes because you’ve set the container’s width to 500px, and also given it a border. These styles should be removed, and set on .collapse-content instead.
in CSS:
you wrote this
.collapsed-content.expanded { width:900px; }
which is wrong.
write this
.collapse-content.expanded { width:900px; }
It’s a spell mistake.