A tiny DIV based selector supporting mouse and touch written in TypeScript.
- Modes for selecting elements
- Elements needs to be only partially covered
- Elements must be fully covered
- Callback at end of selection with list of selected elements
- Can switch between selecting and deselecting (already selected) elements
import{SelectionMode,Selector}from"@vitoni/selector";// identifies all elements which can be selectedconstselectableElementsSelector="div.selectable";functionhandleSelected(selectedElements: HTMLElement[],selectionMode: SelectionMode){// Other uses cases might change the data model which the elements visualize instead of the elements themselves.// The changed data model would trigger an update of the respective elements indirectly.selectedElements.forEach((selectedElement)=>{if(selectionMode==SelectionMode.ADD){selectedElement.classList.add(selectectedClass);}else{selectedElement.classList.remove(selectectedClass);}});}constselector=newSelector(selectableElementsSelector,handleSelected);
...
selector.mount();
...
// change mode of selection to deselect elementsselector.setSelectionMode(SelectionMode.REMOVE);
...
// change mode of selection to select elementsselector.setSelectionMode(SelectionMode.ADD);
...
// cleanup only needed for SPAselector.unmount();mount()registers event listeners and adds a<div>element to DOM to visualize the selection area.unmount()deregisters event listeners and removes the selection area<div>element from DOM.- The callback
handleSelectedis called at the end of the selection with all elements which have been marked by the selection area. - The
SelectionModeindicates whether the elements matching the selection should be regarded as selected or deselected.
...
constareaSelector="#"+areaId;// Identifies an area, the selection is restricted to.// Useful:// - when multiple selection areas are used// - to show visual constrains of the selection areaconstselectablesRootSelector=areaSelector+" div.selectables-root";// identifies all elements which can be selectedconstselectableElementsSelector="div.selectable";
...
constselector=newSelector(selectableElementsSelector,handleSelected,{
selectablesRootSelector
});
...- The
selectablesRootSelectorallows to restrict the selection to the area identified by the selector.
...
// allows changes on single elements without selection / changing the selection modefunctiononClick(element: HTMLElement){if(element.classList.contains(selectectedClass)){element.classList.remove(selectectedClass);}else{element.classList.add(selectectedClass);}}constselector=newSelector(selectableElementsSelector,handleSelected,{
onClick
});
...The example code in setup.ts shows how all these work together and also shows usage of multiple selection areas.
npm installnpm run devThe workspace is configured to apply ESLint rules on save. To allow linting without IDE support there are also CLI commands available:
Show linting errors:
npm run lintFix linting errors automatically:
npm run lint-fixMake selector package locally available:
cd selector
npm linkUse local version directly in consuming project
cd consuming-project
npm link "@vitoni/selector"