Drag & Drop
Drag and drop in nano-ui falls into two categories: pointer drags inside the window (reordering rows, dragging tabs, docking panes) and external files or text dragged in from other apps. In every case, the hooks only report drag state and drop locations; updating your application data is up to you.
Reordering a list
Pass the current list of item IDs and each item’s drawn Response (in render order) to useReorder:
chips :: Var [Int] -> NanoUI ()
chips orderVar = rowWith (gap 6) $ do
order <- readVar orderVar -- orderVar <- newVar [0 .. 4] in setup
drawn <- forM order $ \i -> (i,) <$> chip i
r <- useReorder order drawn
writeVar orderVar (reorderOrder r)
reorderOrder returns the final order on the frame the user drops an item. Render from reorderPreview instead if you want surrounding rows to shift live during the drag. reorderDragging identifies the item currently held, and reorderMoved turns True once the cursor passes the drag threshold so you can suppress normal click handlers.
General gestures with useDrag
For custom drag interactions (like dragging tabs or moving items between containers), use useDrag. Allocate a drag handle once in setup with newDrag. In the example below, insertionIndex converts the pointer coordinates into a target slot along a tab bar (returning Nothing when outside):
tabStrip :: DragHandle Text -> Var Text -> [Tab Text ()] -> NanoUI ()
tabStrip dragHandle active documents = do
headers <- tabBar' active documents
gesture <- useDrag dragHandle (tabHeaders headers)
for_ gesture $ \d -> when (dragPhase d == DragReleased) $ do
let rest = filter ((/= dragPayload d) . fst) (tabHeaders headers)
for_ (insertionIndex DragAxisX (tabStripRect headers) (map (respRect . snd) rest) (dragAt d)) $
moveTab (dragPayload d)
dragPhase transitions through DragStarted, Dragging, and either DragReleased or DragCancelled (triggered by Escape, losing capture, or the dragged source disappearing). Both terminal phases last a single frame; commit changes only on DragReleased.
Files and text dropped from other apps
The SDL backend forwards OS drag-and-drop events (the RGFW backend does not). dropZone draws a panel that tracks hover state and reports dropped files or text:
fileDrop :: NanoUI ()
fileDrop = do
(_, _, target) <- dropZone (fillW . padAll 12) (label "Drop files here")
when (dropReceived target) (mapM_ openFile (dropFiles target))
dropHovered is True while an external drag hovers over the zone. If you want drop detection on a custom region without drawing a panel, call useDrop rect. Drop events respect layering, so a drop zone covered by an open modal receives nothing.
Dockable pane grids
paneGrid (imported from NanoUI.Widgets.PaneGrid) manages splittable, resizable, tab-groupable pane layouts. Its response includes pgrDropTarget, which reports where a dragged pane or tab would dock under the cursor and pairs with useDrag:
dockPanes :: PaneGridConfig -> Maybe (Drag Word64) -> NanoUI ()
dockPanes cfg gesture = do
panes <- paneGrid cfg
case (gesture, pgrDropTarget panes) of
(Just d, Just target) | dragPhase d == DragReleased -> void (commitPaneDrop target)
_ -> pure ()
pgdLocation is BesidePane paneId edge (on a pane edge), IntoTabs paneId (in a pane center when pgCenterDrop = CenterTab), or OutsideGrid edge (along the outer grid margin). pgdRect and pgdPane let you validate or highlight the target. Save pgrTree whenever pgrCommitted is True.