@badasukerubin/react-simple-drag-selection
v1.4.0
Published
A react library that provides an efficient and effective drag selection logic.
Maintainers
Readme
react-simple-drag-selection
A simple and configurable React library that provides an efficient and effective drag selection logic.
Setup
Install the repo
npm install @badasukerubin/react-simple-drag-selectionImport the component, hooks and wrap your component with the
DragSelectionContextproviderimport { DragSelectionContext, SelectionBox, useDragSelection, } from "@badasukerubin/react-simple-drag-selection"; ... const selectContainerRef = useRef<HTMLDivElement | null>(null); const selectBoxRef = useRef<HTMLDivElement | null>(null); const selection = useDragSelection({ containerRef: selectContainerRef, boxRef: selectBoxRef, }); <DragSelectionContext.Provider value={selection}> <div ref={selectContainerRef} className={`overflow-${axis}`}> <SelectionBox ref={selectBoxRef} /> <Boxes boxes={boxes} /> </div> </DragSelectionContext.Provider>;Use the
useDragSelectedhook to get the selection state in the component you want to use it in (Box).import { DragSelectionContext, useDragSelected, } from "@badasukerubin/react-simple-drag-selection"; ... export default memo(function Box() { const [color] = useState(getRandomColor()); const boxRef = useRef<HTMLDivElement | null>(null); const selection = useContext(DragSelectionContext); const isSelected = useDragSelected({ elementRef: boxRef, selection }); return ( <div ref={boxRef} className="box" style={{ backgroundColor: isSelected ? "black" : color }} ></div> ); }); ...
Available props
DragSelectionContextis the context provider that provides the selection state to the components that need it.SelectionBoxis the selection box component that is rendered when the selection starts. You can customise this component by passing values forclassName,styleor any other prop that you'd pass to adivelement.useDragSelectionhas the following options:containerRef: React ref to the container elementboxRef: React ref to the selection box elementmouseMoveThreshold: Optional number that determines the distance the mouse has to move before the selection starts (current default is 5px)onSelectionStart: Optional callback function that is called when the selection startsonSelection: Optional callback function that is called when the selection changesonSelectionEnd: Optional callback function that is called when the selection ends
useDragSelectedhas the following options:elementRef: React ref to the element that you want to check if it is selectedselection: The selection object that is provided by theDragSelectionContextonSelected: Optional callback function that is called when the element is selected
You can also ignore elements from having the selection logic interfere with their default behaviour (or that of their descendants) by adding the
ignore-drag-selectionclass to them.<div className="ignore-drag-selection"> <div className="box" draggable={true} ondragstart={dragFunction}></div> </div>
Example
A full example can be found in the example directory.
Roadmap
- [ ] Unselect/Select using Shift and Meta keys
- [ ] Add tests
- [x] Mobile/touch selection support
