This code implements a draggable modal component using React and Framer Motion. It solves the problem of creating an interactive and visually appealing modal that can be closed by dragging a handle. The component utilizes Framer Motion's animation capabilities for smooth transitions and intuitive drag interactions.
Here's a step-by-step breakdown:
- State Management: The
useState hook manages the modal's open/closed state.
- Measurements:
useMeasure from react-use-measure gets the height of the drawer for animation calculations.
- Framer Motion Integration:
useMotionValue, useAnimate, and useDragControls from Framer Motion handle animation, drag gestures, and control the drag behavior respectively. The motion component from Framer Motion is used to create animated elements.
- Drag Interaction: The modal's handle is draggable along the y-axis. When the user drags the handle downwards beyond a 100px threshold, the
handleClose function is triggered.
- Animation:
useAnimate orchestrates the closing animation, which includes fading out the overlay and sliding the drawer upwards.
- Accessibility: While not explicitly shown, ensuring proper ARIA attributes for screen readers is crucial for accessibility (not included in the example).
This component can be applied in various scenarios, such as creating interactive dialog boxes, confirmation modals, or custom draggable panels in web applications. The example demonstrates a draggable modal with content that can be dismissed by dragging its handle downwards. The smooth animations and intuitive drag behavior enhance user experience. The use of Framer Motion simplifies the creation of complex animations. Libraries used include React for UI and Framer Motion for animation and drag interactions.