-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
176 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { ReactElement } from 'react'; | ||
|
||
import { PRIMARY_COLOR } from '@graasp/ui'; | ||
|
||
import { | ||
Box, | ||
boxesIntersect, | ||
useSelectionContainer, | ||
} from '@air/react-drag-to-select'; | ||
|
||
import { ITEM_CARD_CLASS } from '@/config/selectors'; | ||
|
||
import { useSelectionContext } from './SelectionContext'; | ||
|
||
export const useDragSelection = ({ | ||
elementClass = ITEM_CARD_CLASS, | ||
adjustments = {}, | ||
} = {}): (() => ReactElement) => { | ||
const { addToSelection, clearSelection } = useSelectionContext(); | ||
|
||
const { DragSelection } = useSelectionContainer({ | ||
eventsElement: document.getElementById('root'), | ||
onSelectionChange: (box) => { | ||
/** | ||
* Here we make sure to adjust the box's left and top with the scroll position of the window | ||
* @see https://github.com/AirLabsTeam/react-drag-to-select/#scrolling | ||
*/ | ||
const scrollAwareBox: Box = { | ||
...box, | ||
top: box.top + window.scrollY, | ||
left: box.left + window.scrollX, | ||
}; | ||
|
||
Array.from(document.getElementsByClassName(elementClass)).forEach( | ||
(item) => { | ||
const bb = item.getBoundingClientRect(); | ||
if ( | ||
boxesIntersect(scrollAwareBox, bb) && | ||
item.parentNode instanceof HTMLElement | ||
) { | ||
const itemId = item.parentNode.dataset.id; | ||
if (itemId) { | ||
addToSelection(itemId); | ||
} | ||
} | ||
}, | ||
); | ||
}, | ||
shouldStartSelecting: (e) => { | ||
// does not trigger drag selection if mousedown on card | ||
if (e instanceof HTMLElement) { | ||
return !e?.closest(`.${elementClass}`); | ||
} | ||
return true; | ||
}, | ||
onSelectionStart: () => { | ||
// clear selection on new dragging action | ||
clearSelection(); | ||
}, | ||
onSelectionEnd: () => {}, | ||
selectionProps: { | ||
style: { | ||
// adjustement | ||
// https://github.com/AirLabsTeam/react-drag-to-select/issues/30 | ||
...adjustments, | ||
border: `2px dashed ${PRIMARY_COLOR}`, | ||
borderRadius: 4, | ||
backgroundColor: 'lightblue', | ||
opacity: 0.5, | ||
}, | ||
}, | ||
isEnabled: true, | ||
}); | ||
|
||
return DragSelection; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.