DraggableStack
DraggableStack
enables you to easily create a stack of sortable draggable components.
It is built on top of the useDragableStack hook.
This component does support variable width and height items.
Demo
Usage
import React, { type FunctionComponent } from "react";
import { StyleSheet, Text, View } from "react-native";
import {
DndProvider,
type ObjectWithId,
Draggable,
DraggableStack,
type DraggableStackProps,
} from "@mgcrea/react-native-dnd/src";
const items = ["π€", "π€π€", "π»π»π»", "πΎπΎπΎπΎ"];
const data = items.map((letter, index) => ({
value: letter,
id: `${index}-${letter}`,
})) satisfies ObjectWithId[];
export const DraggableStackExample: FunctionComponent = () => {
const onStackOrderChange: DraggableStackProps["onOrderChange"] = (value) => {
console.log("onStackOrderChange", value);
};
const onStackOrderUpdate: DraggableStackProps["onOrderUpdate"] = (value) => {
console.log("onStackOrderUpdate", value);
};
return (
<View style={styles.container}>
<Text style={styles.title}>DraggableStack Example</Text>
<DndProvider>
<DraggableStack
direction="row"
gap={10}
style={styles.stack}
onOrderChange={onStackOrderChange}
onOrderUpdate={onStackOrderUpdate}
>
{data.map((letter) => (
<Draggable key={letter.id} id={letter.id} style={[styles.draggable]}>
<Text style={styles.text}>{letter.value}</Text>
</Draggable>
))}
</DraggableStack>
</DndProvider>
</View>
);
};
Props
The DraggableStack
component accepts the following props:
-
- Name
-
direction
- Type
- :FlexStyle['flexDirection']?
- = row
- Description
-
The direction of the grid. Can be either
row
orcolumn
,row-reverse
orcolumn-reverse
. Default isrow
.
-
- Name
-
gap
- Type
- :number?
- = 0
- Description
-
The gap between items in the grid. Default is
0
.
-
- Name
-
style
- Type
- :ViewStyle?
- Description
-
An optional style object to apply custom styles to the container View.
-
- Name
-
onOrderChange
- Type
- :(order: UniqueIdentifier[]) => void?
- Description
-
An optional function that is called when the order of the items in the grid changes after a drag & sort operation. The function receives an array of the item ids in the new order.
-
- Name
-
onOrderUpdate
- Type
- :(nextOrder: UniqueIdentifier[], prevOrder: UniqueIdentifier[]) => void?
- Description
-
An optional function that is called every time the order of the items in the grid changes during a drag & sort operation. The function receives an array of the item ids in the new order, and an array of the item ids in the previous order.
-
- Name
-
shouldSwapWorklet
- Type
- :ShouldSwapWorklet?
- Description
-
An optional function that can be used to override the default behavior of when to swap items. The function receives the following arguments:
(activeLayout: Rectangle, itemLayout: Rectangle) => boolean
. The function should returntrue
if the items should be swapped, orfalse
if they should not be swapped.