I'm trying to create a simple drag n drop carousel.
In order to set the constraints of the inner carousel, I need to calculate the difference between scrollWidth and offsetWidth. Unexpectedly, I get the same value for both. This shouldn't be happening, since the content overflows its parent div.
const fotos = [picOne, picTwo, picThree, picFour]
export const App = () => {
const [width, setWidth] = useState(0)
const ref = useRef(null)
useEffect(() => {
setWidth(ref.current.scrollWidth - ref.current.offsetWidth)
}, [])
return (
<>
<div className="carousel" ref={ref}>
<motion.div className="carousel-insider"
drag="x"
dragConstraints={{right: 0, left: -width}}>
{fotos.map((foto, index) => <img key={index} src={foto}/>)}
</motion.div>
</div>
</>
)
}
Carousel code: https://jsfiddle.net/oa6tLrfq/
What am I doing wrong here?