Command palette

@yasinsensoy/native

React Native · Components

SortableList

A list sorted by dragging: task lists, priority order, form rows.

8 exports

Live example

The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.

Preview

  • Finish the designDesign
  • Write the API endpointsBackend
  • Finish the testsTest
  • Update the docsDocs
  • Publish the releaseRelease

Code

import { SortableList } from "@yasinsensoy/native";

const [tasks, setTasks] = useState(list);

<SortableList items={tasks} onReorder={setTasks}>
  {(gorev) => (
    <div className="flex items-center gap-2 rounded-lg border bg-card p-3">
      {gorev.label}
    </div>
  )}
</SortableList>

Events

  1. Events appear here when you interact with the preview.

Grid: color order (mouse, touch, keyboard: Space + arrows)

  • #ef4444
  • #f59e0b
  • #10b981
  • #3b82f6
  • #8b5cf6
  • #ec4899
  • #14b8a6
  • #64748b
  • #f97316

Order of options inside a form (SelectField options in this order)

  • 1.Turkish
  • 2.English
  • 3.German

Import

import { SortableList, SortableHandle, moveItem } from "@yasinsensoy/native";

SortableList

component

A list sorted by dragging: task lists, priority order, form rows. Same API as the web SortableList.

  • Touch: long-press an item and drag (immediately from the handle with handle); other items slide to make room, and the item settles into place on release. List scrolling works normally until the long press completes.
  • Accessibility: every item has "Move up" / "Move down" actions (VoiceOver/TalkBack rotor); moves and drops are announced.
  • Layout: stacked, side by side or grid; items of different sizes also land in the right place.
SortableList({ items: itemsProp, defaultItems, onReorder, onMove, getKey, getLabel, children, orientation, handle, longPressDelay, disabled, isItemDisabled, gap, onDragStart, onDragEnd, style, itemStyle, testID, }: SortableListProps<T>): JSX.Element

Examples

<SortableList items={tasks} onReorder={setTasks}>
  {(task, { active }) => <Card style={{ opacity: active ? 0.9 : 1 }}><Text>{task.label}</Text></Card>}
</SortableList>

Drag handle of a SortableList item. When the list has handle, the item drags only from here, immediately without a long press. For screen readers the "Move up" / "Move down" actions are on the handle.

SortableHandle({ children, style }: SortableHandleProps): JSX.Element

Examples

<SortableList items={rows} onReorder={setRows} handle>
  {(row) => (
    <View style={{ flexDirection: "row", gap: 8 }}>
      <SortableHandle />
      <Text>{row.label}</Text>
    </View>
  )}
</SortableList>

moveItem

function

Moves an item in an array from index from to index to; returns a new array (the input is not changed). SortableList onReorder and useFieldArray move use the same rule.

moveItem(items: readonly T[], from: number, to: number): T[]

Examples

moveItem(["a", "b", "c"], 0, 2); // ["b", "c", "a"]

Layout: stacked, side by side or grid.

type SortableOrientation = "vertical" | "horizontal" | "grid"

Current state of a SortableList item (when rendering content).

NameTypeDescription
indexnumberIts current index.
draggingfalse | "pointer"Being dragged? On native only by touch ("pointer").
activebooleanIs this the dragged item (same as dragging; same name as web).
disabledbooleanNot draggable?

Information about a move.

NameTypeDescription
itemTThe moved item.
keystringThe item's key.
fromnumberIts old index.
tonumberIts new index.

Props of the SortableList component.

NameTypeDefaultDescription
items?readonly T[]Items (controlled). When the order changes onReorder is called with the new array.
defaultItems?readonly T[]Initial items in uncontrolled use; the list keeps the order itself.
onReorder?(items: T[], info: SortableMoveInfo<T>) => voidCalled with the new array when the order changes.
onMove?(from: number, to: number) => voidOnly the order change (from → to); useFieldArray's move can be passed.
getKey?(item: T) => string | numberThe item's stable key. Defaults to item.key, then item.id.
getLabel?(item: T) => stringThe item's name in screen reader announcements. Defaults to label, title, name.
children(item: T, state: SortableItemState) => ReactNodeThe item's content. When handle is given, use SortableHandle in the content; the item is dragged only from there.
orientation?SortableOrientation"vertical"Layout. In a grid, columns are set via style (flexDirection: "row", flexWrap: "wrap").
handle?booleanfalsetrue: the item drags only from SortableHandle, immediately (for form rows). false: from anywhere on the item, after a long press, so the list scrolls normally.
longPressDelay?number250Long press duration (ms; without handle).
disabled?booleanDisables all sorting.
isItemDisabled?(item: T) => booleanThis item cannot be dragged.
gap?number2Gap between items (ui.space units).
onDragStart?(info: { item: T; key: string; index: number; }) => voidWhen a drag starts.
onDragEnd?(info: SortableMoveInfo<T> & { cancelled: boolean; }) => voidWhen a drag ends; on cancel to equals from.
style?StyleProp<ViewStyle>Container style.
itemStyle?StyleProp<ViewStyle>Style of each item's container.
testID?stringTest id.

SortableHandle props.

NameTypeDescription
children?ReactNodeContent instead of the icon. If omitted the ⋮⋮ handle icon is drawn.
style?StyleProp<ViewStyle>Container style.