Lynx ui sortable

From lynx-family/lynx-ui · View interactive version

Lynx ui sortable is an AI agent skill from the GitHub repository lynx-family/lynx-ui. `Sortable` is a headless drag-to-reorder list primitive for ReactLynx. It performs reordering on the main thread for smooth, gesture-driven animations and exposes a composable `SortableRoot` / `Sortab

How to use this skill

Skill file

lynx-ui-sortable SKILL

Sortable is a headless drag-to-reorder list primitive for ReactLynx. It performs reordering on the main thread for smooth, gesture-driven animations and exposes a composable SortableRoot / SortableItem / SortableItemArea API. For massive datasets, prefer List or FeedList; Sortable targets small to medium reorder-able lists.

1. Core Capabilities

2. AI Coding Guide

Minimal Usable Example

SortableRoot accepts a render-prop child, not static children. Each row is a SortableItem whose sortingKey MUST match SortableData.getSortingKey().

import { useState } from '@lynx-js/react'
import {
  SortableItem,
  SortableItemArea,
  SortableRoot,
  type SortableData,
} from '@lynx-js/lynx-ui'

interface Row { id: string; title: string }

function BasicSortable() {
  const [data, setData] = useState<SortableData<Row>[]>(() =>
    Array.from({ length: 6 }, (_, i) => {
      const id = String(i)
      return {
        dataItem: { id, title: `Row ${i + 1}` },
        getSortingKey: () => id,
      }
    })
  )

  return (
    <SortableRoot
      data={data}
      onSortEnd={setData}
      as='ScrollView'
      scrollableClassName='sortable-scroll'
      scrollableContentClassName='sortable-content'
    >
      {(item) => (
        <SortableItem
          key={item.getSortingKey()}
          sortingKey={item.getSortingKey()}
          as='DraggableRoot'
          className='sortable-row'
        >
          <view className='sortable-row-surface'>
            <text>{item.dataItem.title}</text>
            <SortableItemArea className='sortable-handle'>
              <text>⋮</text>
            </SortableItemArea>
          </view>
        </SortableItem>
      )}
    </SortableRoot>
  )
}

Recommended Prompt Formula

Scenario Description + Drag Surface (whole row vs handle) + Boundary / ScrollView Setup + Locked Rows + Reorder Callback

Example Prompts:

3. Use Cases & Best Practices

Stable rendering during reorder

Keep the render-prop callback stable with useCallback and preserve unchanged

SortableData object identities when reordering. This lets Sortable reuse item

subtrees and their gesture bindings. Replace the affected item object when its

content changes; include external rendering dependencies in the callback's

dependency list so content and disabled state do not become stale.

Whole-row drag surface (default)

<SortableItem
  key={item.getSortingKey()}
  sortingKey={item.getSortingKey()}
>
  <MyRowSurface data={item.dataItem} />
</SortableItem>

Touching anywhere on the row starts a drag. Best for tile / card layouts.

Example Path: apps/examples/Sortable/Basic/index.tsx

Handle-only drag surface

<SortableItem
  key={item.getSortingKey()}
  sortingKey={item.getSortingKey()}
  as='DraggableRoot'
>
  <view className='row'>
    <view className='row-content'>{/* tap content */}</view>
    <SortableItemArea className='row-handle'>
      <text>⋮</text>
    </SortableItemArea>
  </view>
</SortableItem>

Required when row content has its own gestures (taps, switches). Only SortableItemArea starts a drag.

Example Path: apps/examples/Sortable/Basic/index.tsx

Owned `ScrollView` with auto-scroll near edges

<SortableRoot
  as='ScrollView'
  scrollableBoundaryId='mySortableScroll'
  scrollableClassName='short-scroll-view'
  scrollableContentClassName='sortable-root short-sortable-root'
  scrollableStickyUpperOffset={12}
  scrollableStickyLowerOffset={12}
  data={data}
  onSortEnd={setData}
>
  {renderRow}
</SortableRoot>

When the dragged row reaches 12px from the upper / lower edge, the internal scroll-view calls scrollBy so the list keeps moving with the drag. Call lynx.querySelector('#mySortableScroll')?.invoke('scrollBy', { offset }) to scroll programmatically.

Example Path: apps/examples/Sortable/ShortScrollView/index.tsx

External boundary (no owned ScrollView)

<view id='myBoundary' className='boundary'>
  <SortableRoot data={data} onSortEnd={setData} boundaryId='myBoundary'>
    {renderRow}
  </SortableRoot>
</view>

Drags exiting #myBoundary are cancelled. Use this when the sortable list is embedded inside an existing scroll container or modal that you control outside Sortable.

Example Path: apps/examples/Sortable/NoBoundary/index.tsx

Locked rows that never move (`disabled`)

const LOCKED = new Set(['0', '5', '7', '11'])

<SortableRoot data={data} onSortEnd={setData} as='ScrollView'>
  {(item) => {
    const isLocked = LOCKED.has(item.getSortingKey())
    return (
      <SortableItem
        key={item.getSortingKey()}
        sortingKey={item.getSortingKey()}
        as='DraggableRoot'
        disabled={isLocked}
      >
        <view className={isLocked ? 'row row--locked' : 'row'}>
          <text>{item.dataItem.title}</text>
          {isLocked
            ? <view className='row-area row-area--locked'><text>Locked</text></view>
            : <SortableItemArea className='row-area'><text>⋮</text></SortableItemArea>}
        </view>
      </SortableItem>
    )
  }}
</SortableRoot>

Behavior of a disabled row:

Use this for fixed headers/footers, sticky default actions, or rows whose absolute position is part of the design.

Example Path: apps/examples/Sortable/DisableItems/index.tsx

Disable sorting globally

<SortableRoot data={data} onSortEnd={setData} enableSorting={readOnly === false}>
  {renderRow}
</SortableRoot>

enableSorting={false} instantly removes drag bindings from every non-disabled item without unmounting. Useful for read-only modes.

Example Path: apps/examples/Sortable/DisableSorting/index.tsx

4. FAQ

Q: Why is children a function in SortableRoot?

A: Sortable needs to associate each rendered node with an internal sortingKey and ref map. The render-prop signature (item: SortableData<T>) => ReactNode makes that wiring explicit and lets SortableRoot re-render only the rows whose data identity actually changed.

Q: Where is the source of truth for the order?

A: Your application state is. SortableRoot is controlled by data and reports the committed order through onSortEnd(sortedData). You typically setData(sortedData) in the handler. While dragging, only main-thread transforms move; the React data does not change until release.

Q: My handle (SortableItemArea) does not start a drag.

A: Confirm as='DraggableRoot' on the surrounding SortableItem. With the default as='Draggable', the whole row is the drag surface and SortableItemArea is treated as plain content.

Q: Disabled rows still appear to move during drag.

A: They should not. If you see this, check that the disabled prop is actually true for those rows on every render (a stable Set via useMemo is recommended), and that the row's sortingKey is unique and stable across renders. Disabled items are never displaced, never picked as a swap target, and always keep their absolute index in onSortEnd.

Q: How do I make the dragged row visually float above its neighbors?

A: Sortable already raises the dragged row's z-index to 10000 while dragging and resets it after release; no extra styling is required.

Q: Auto-scroll does not happen when I drag near the edges.

A: Auto-scroll requires either as='ScrollView' on SortableRoot (so it owns the scroll-view) or a scrollableBoundaryId pointing at an existing scrollable element. Plain as (default) renders a view with no scrolling, so there is nothing to auto-scroll.

Q: I see a wrong reorder commit when I drag back over a disabled gap and release.

A: Make sure you are on a version that includes the disabled-aware confirmed-swap reset. If you can still reproduce the case where releasing inside a disabled gap commits a stale swap, see apps/examples/Sortable/DisableItemsStaleSwap/index.tsx for the canonical repro and file an issue.

5. Sub components

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.