Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const selection = new SelectionArea({
// All elements in this container can be selected
selectables: ['.box-wrap > div'],
// The container is also the boundary in this case
boundaries: ['.box-wrap']
}).on('start', ({store, event}) => {
// Remove class if the user isn't pressing the control key or ⌘ key
if (!event.ctrlKey && !event.metaKey) {
// Unselect all elements
for (const el of store.stored) {
el.classList.remove('selected');
}
// Clear previous selection
selection.clearSelection();
}
}).on('move', ({store: {changed: {added, removed}}}) => {
// Add a custom class to the elements that where selected.
for (const el of added) {
el.classList.add('selected');
}
// Remove the class from elements that where removed
// since the last selection
for (const el of removed) {
el.classList.remove('selected');
}
}).on('stop', () => {
selection.keepSelection();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment