You can do this with a keyframe animation. The key is setting a max-width
greater than how big the element will be. Note that the animation timing takes into account the total max-width.
CSS
.test {
animation-name: test;
animation-duration: 1s;
animation-direction: forwards;
overflow: hidden;
white-space: nowrap;
}
@keyframes test {
0% {
max-width: 0;
}
100% {
max-width: 200px;
}
}
and then you need to add the class to your dynamic object
function App() {
const [isOpen, setIsOpen] = useState(false);
return (
<div className="container">
<button onClick={() => setIsOpen(!isOpen)}>
{isOpen ? "Hide" : "Show"}
</button>
{isOpen && <div class='test'>Dynamic Content</div>}
<input type="text" />
</div>
);
}
You can toggle the animation duration and make it happen on removal too if you want but I'll leave it to you to play with.
https://codesandbox.io/s/6wj6z1ppxr