Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Drag and Drop Animations | Interactive Animations
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Animations in React with Framer Motion

bookDrag and Drop Animations

Making components interactive can greatly enhance user experience in React apps. With Framer Motion, you can add drag-and-drop functionality using the drag prop on motion components. The drag prop enables you to make any element draggable along the x-axis, y-axis, or both, simply by setting it to "x", "y", or true. You can further control the dragging behavior using options like dragConstraints to limit the area within which the component can be dragged, and dragElastic to control the resistance when dragging near the constraint edges. The dragTransition prop lets you customize the animation that occurs when the user releases the dragged element, such as snapping it back to its original position or applying momentum.

Suppose you want to create a draggable card that users can move within a fixed area, and you want the card to snap back to its starting point when released. You can achieve this by combining the drag, dragConstraints, and dragTransition props. Here is a compact example using Framer Motion in a React component:

import { useRef } from "react";
import { motion } from "framer-motion";

export default function DraggableCard() {
  const constraintsRef = useRef(null);

  return (
    <div
      ref={constraintsRef}
      style={{
        width: 300,
        height: 300,
        border: "2px dashed #888",
        position: "relative",
        margin: "40px auto",
      }}
    >
      <motion.div
        drag
        dragConstraints={constraintsRef}
        dragElastic={0.5}
        dragTransition={{ bounceStiffness: 600, bounceDamping: 20 }}
        style={{
          width: 100,
          height: 100,
          background: "#0af",
          borderRadius: 16,
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
          color: "#fff",
          fontWeight: "bold",
          position: "absolute",
          top: 100,
          left: 100,
          cursor: "grab",
        }}
        whileTap={{ scale: 0.95 }}
      >
        Drag me!
      </motion.div>
    </div>
  );
}

In this example, the card can be dragged anywhere within the dashed boundary. When released, the card animates with a snap-back effect if you try to drag it beyond the allowed area, thanks to the elastic and transition settings.

Note
Definition

Draggable UI elements are components that users can click and move within a defined area of the interface. They are commonly used for rearranging items, organizing layouts, or creating interactive experiences such as kanban boards, sliders, and games.

question mark

Which statement best describes the purpose of the drag prop in Framer Motion?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

bookDrag and Drop Animations

Desliza para mostrar el menú

Making components interactive can greatly enhance user experience in React apps. With Framer Motion, you can add drag-and-drop functionality using the drag prop on motion components. The drag prop enables you to make any element draggable along the x-axis, y-axis, or both, simply by setting it to "x", "y", or true. You can further control the dragging behavior using options like dragConstraints to limit the area within which the component can be dragged, and dragElastic to control the resistance when dragging near the constraint edges. The dragTransition prop lets you customize the animation that occurs when the user releases the dragged element, such as snapping it back to its original position or applying momentum.

Suppose you want to create a draggable card that users can move within a fixed area, and you want the card to snap back to its starting point when released. You can achieve this by combining the drag, dragConstraints, and dragTransition props. Here is a compact example using Framer Motion in a React component:

import { useRef } from "react";
import { motion } from "framer-motion";

export default function DraggableCard() {
  const constraintsRef = useRef(null);

  return (
    <div
      ref={constraintsRef}
      style={{
        width: 300,
        height: 300,
        border: "2px dashed #888",
        position: "relative",
        margin: "40px auto",
      }}
    >
      <motion.div
        drag
        dragConstraints={constraintsRef}
        dragElastic={0.5}
        dragTransition={{ bounceStiffness: 600, bounceDamping: 20 }}
        style={{
          width: 100,
          height: 100,
          background: "#0af",
          borderRadius: 16,
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
          color: "#fff",
          fontWeight: "bold",
          position: "absolute",
          top: 100,
          left: 100,
          cursor: "grab",
        }}
        whileTap={{ scale: 0.95 }}
      >
        Drag me!
      </motion.div>
    </div>
  );
}

In this example, the card can be dragged anywhere within the dashed boundary. When released, the card animates with a snap-back effect if you try to drag it beyond the allowed area, thanks to the elastic and transition settings.

Note
Definition

Draggable UI elements are components that users can click and move within a defined area of the interface. They are commonly used for rearranging items, organizing layouts, or creating interactive experiences such as kanban boards, sliders, and games.

question mark

Which statement best describes the purpose of the drag prop in Framer Motion?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2
some-alt