useDraggable
useDraggable is a custom React Hook that enables a component to become draggable for drag-and-drop operations. This hook manages the registration, layout, and state of a draggable component.
Minimal Example
// src/components/DraggableComponent.tsx
import { useDraggable } from "@mgcrea/react-native-dnd";
const DraggableComponent = ({ id, data, disabled }) => {
const { offset, setNodeRef, activeId, setNodeLayout, draggableState } = useDraggable({
id,
data,
disabled,
});
return (
<Animated.View ref={setNodeRef} onLayout={setNodeLayout}>
{/* Render component content */}
</Animated.View>
);
};
Advanced Usage
// src/components/MyDraggable.tsx
import { useDraggable, type AnimateProps, type UseDraggableOptions } from "@mgcrea/react-native-dnd";
import { type FunctionComponent, type PropsWithChildren } from "react";
import { type ViewProps } from "react-native";
import Animated, { useAnimatedStyle } from "react-native-reanimated";
export type MyDraggableProps = AnimateProps<ViewProps> & UseDraggableOptions;
export const MyDraggable: FunctionComponent<PropsWithChildren<MyDraggableProps>> = ({
children,
id,
data,
disabled,
activationDelay,
activationTolerance,
style,
...otherProps
}) => {
const { setNodeRef, setNodeLayout, activeId, actingId, offset } = useDraggable({
id,
data,
disabled,
activationDelay,
activationTolerance,
});
const animatedStyle = useAnimatedStyle(() => {
const isActive = activeId.value === id;
const isResting = actingId.value !== id;
const style = {
opacity: isActive ? 0.9 : 1,
zIndex: isActive ? 999 : 1,
transform: [
{
translateX: offset.x.value,
},
{
translateY: offset.y.value,
},
],
};
return style;
}, [id]);
return (
<Animated.View ref={setNodeRef} onLayout={setNodeLayout} style={[style, animatedStyle]} {...otherProps}>
{children}
</Animated.View>
);
};
Parameters
useDraggable accepts an object with the following properties:
-
- Name
-
id - Type
- :UniqueIdentifier
- Description
-
A unique identifier for the draggable component.
-
- Name
-
data - Type
- :Data?
- = {}
- Description
-
An optional data object that can be used to store additional information about the draggable component. Default is an empty object (
{}).
-
- Name
-
activationDelay - Type
- :number?
- = 0
- Description
-
A number representing the duration, in milliseconds, that this draggable item needs to be held for before allowing a drag to start. Default is
0.
-
- Name
-
activationTolerance - Type
- :number?
- = false
- Description
-
A number representing the distance, in points, of motion that is tolerated before the drag operation is aborted.
-
- Name
-
disabled - Type
- :boolean?
- = false
- Description
-
A boolean indicating whether the draggable component should be disabled (i.e., not be able to be dragged). Default is
false.
Return values
useDraggable returns an object with the following properties:
-
- Name
-
offset - Type
- :SharedPoint
- Description
-
A
SharedPointcontaining the x and y offsets of the draggable component.
-
- Name
-
setNodeRef - Type
- :RefCallback<Animated.View>
- Description
-
A ref callback function used to store a reference to the draggable component’s
Animated.View.
-
- Name
-
setNodeLayout - Type
- :ViewProps["onLayout"]"
- Description
-
An
onLayoutevent handler that should be attached to the draggable component’sAnimated.Viewto track its layout.
-
- Name
-
activeId - Type
- :SharedValue<UniqueIdentifier | null>
- Description
-
A
SharedValuecontaining the ID of the currently active draggable component (eg. being interacted with) ornullif there’s no active draggable component.
-
- Name
-
actingId - Type
- :SharedValue<UniqueIdentifier | null>
- Description
-
A
SharedValuecontaining the ID of the currently acting draggable component (eg. not at rest) ornullif there’s no acting draggable component.
-
- Name
-
draggableState - Type
- :SharedValue<DraggableState>
- Description
-
A
SharedValuecontaining the state of the currently dragged component.