useSelection
Manages single or multi-item selection state with range selection and bulk toggle helpers.
The useSelection hook manages item selection for data tables, lists, and galleries, supporting key extraction (getKey), bulk selection (selectAll, clearSelection, toggleAll), Shift+Click range selection (selectRange), and computed isAllSelected / isPartiallySelected flags for indeterminate header checkboxes.
Installation
Usage
Examples
Data Table with Indeterminate Header Checkbox
Use isAllSelected, isPartiallySelected, and toggleAll to wire up a master checkbox alongside row checkboxes.
Shift+Click Range Selection
Call selectRange(key) when the user holds the Shift key to select every item between the last interacted item and the clicked item.
API Reference
UseSelectionOptions
Configuration options for key extraction, single/multiple mode, and controlled/uncontrolled selection keys.
getKey?(itemTmultiple?value?defaultValue?onChange?(selectedKeysTKey[]UseSelectionReturn
Selection state, flags, and helper methods returned by the hook.
selectedKeysselectedSetSet of currently selected item keys for O(1) lookups.selectedItemsitems whose keys are currently selected.isSelectedtrue if key is currently selected.keyTKeyisAllSelecteditems is selected.isPartiallySelecteditems are selected.isNoneSelecteditems are selected.selectkey.keyTKeydeselectkey.keyTKeytogglekey.keyTKeyselectRangefromKey and toKey inclusive.fromKeyTKeytoKeyTKeyselectAllitems (when multiple is true).cleartoggleAllsetSelectedKeyskeys.keysTKey[]