Skip to content

Commit

Permalink
fix: ensure selection is within containerRef
Browse files Browse the repository at this point in the history
  • Loading branch information
joshxfi committed Sep 8, 2024
1 parent 0ce36aa commit 7f5156a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/react-highlight-popover/src/HighlightPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ export function HighlightPopover({

const isSelectionWithinContainer = useCallback((selection: Selection) => {
if (!containerRef.current) return false;
const range = selection.getRangeAt(0);
return containerRef.current.contains(range.commonAncestorContainer);
for (let i = 0; i < selection.rangeCount; i++) {
const range = selection.getRangeAt(i);
if (
!containerRef.current.contains(range.startContainer) ||
!containerRef.current.contains(range.endContainer)
) {
return false;
}
}
return true;
}, []);

const handleSelection = useCallback(() => {
Expand Down

0 comments on commit 7f5156a

Please sign in to comment.