This repository has been archived by the owner on Jan 23, 2025. It is now read-only.
-
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.
fix: prevent modal calls on selection (#1387)
* fix: prevent modal calls on selection * refactor: use custom overlay for selection
- Loading branch information
Showing
8 changed files
with
196 additions
and
124 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
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,98 @@ | ||
import { useState } 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, | ||
} = {}): JSX.Element => { | ||
const { addToSelection, clearSelection } = useSelectionContext(); | ||
const [boundingBox, setBoundingBox] = useState<null | { | ||
top: number; | ||
left: number; | ||
}>(null); | ||
|
||
const { DragSelection: HookComponent } = 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, | ||
}; | ||
|
||
setBoundingBox(scrollAwareBox); | ||
|
||
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(); | ||
setBoundingBox(null); | ||
}, | ||
onSelectionEnd: () => { | ||
setBoundingBox(null); | ||
}, | ||
selectionProps: { | ||
style: { | ||
display: 'none', | ||
}, | ||
}, | ||
isEnabled: true, | ||
}); | ||
|
||
// we don't use native overlay because it's bound to the wrapper | ||
// https://github.com/AirLabsTeam/react-drag-to-select/issues/30 | ||
const DragSelection = ( | ||
<> | ||
<div | ||
style={{ | ||
position: 'fixed', | ||
border: `2px dashed ${PRIMARY_COLOR}`, | ||
borderRadius: 4, | ||
backgroundColor: 'lightblue', | ||
opacity: 0.5, | ||
zIndex: 999, | ||
display: boundingBox?.top ? 'block' : 'none', | ||
...boundingBox, | ||
}} | ||
/> | ||
<HookComponent /> | ||
</> | ||
); | ||
|
||
return DragSelection; | ||
}; |
Oops, something went wrong.