Reputation: 9673
I have a horizontal row of boxes, right aligned. I want to add a new box to the right side by having it gradually slide in, moving the other boxes to the left as it does so.
https://codesandbox.io/s/vibrant-curran-xqcbh?file=/src/App.js is where I've gotten. Here's my code:
import { motion, AnimatePresence } from "framer-motion";
import React, { useState } from "react";
import "./styles.css";
export default function App() {
const [texts, setTexts] = useState(["foo", "foo"]);
return (
<div className="m-2">
<button
className="btn btn-primary mb-3"
onClick={() => {
setTexts([...texts, "bar"]);
}}
>
Add box
</button>
<div className="d-flex justify-content-end">
<AnimatePresence initial={false}>
{texts.map((text, i) => (
<motion.div
key={i}
positionTransition
initial={{ x: 100 }}
animate={{ x: 0 }}
transition={{ duration: 2, type: "tween" }}
>
<div className="alert alert-primary mr-2" style={{ width: 100 }}>
{text}
</div>
</motion.div>
))}
</AnimatePresence>
</div>
</div>
);
}
There's two problems:
Any ideas for where to go from here?
I used framer-motion but I'm not wedded to it, if another library makes this easier.
Upvotes: 1
Views: 374
Reputation: 48
Just need a matching spring for the positionTransition.
const spring = {
duration: 2,
type: "tween"
};
...
positionTransition={spring}
See example https://codesandbox.io/s/kind-mendel-zl1fl?file=/src/App.js
Upvotes: 1