Skip to content

Commit 49444a5

Browse files
authored
fix: clicking outside with user-select: none closes popover
2 parents 4952599 + b9626a0 commit 49444a5

File tree

4 files changed

+20
-70
lines changed

4 files changed

+20
-70
lines changed

lib/CHANGELOG.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
# @omsimos/react-highlight-popover
22

3+
## 1.3.2
4+
5+
### Patch Changes
6+
7+
- ### Bug Fixes 🐞
8+
9+
- **Removed Redundant Click-Outside Logic**:
10+
- The `useEffect` that handled clicks outside the popover has been removed. This was causing issues where clicking outside on elements with `user-select: none` would unintentionally close the popover, even though the text remained highlighted.
11+
- Since clicking outside naturally removes text selection, the previous logic was redundant and caused bugs by closing the popover prematurely.
12+
13+
***
14+
15+
To upgrade to v1.3.2, run:
16+
17+
```bash
18+
npm install @omsimos/react-highlight-popover@latest
19+
```
20+
321
## 1.3.1
422

523
### Patch Changes
624

7-
- ### Fixes 🛠
25+
- ### Bug Fixes 🐞
826

927
- **Removed Unintended `.mjs` File**: An unnecessary file was unintentionally included in the v1.3.0 build. This patch removes the file, ensuring a cleaner build and reducing the package size.
1028

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@omsimos/react-highlight-popover",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"main": "dist/index.js",
55
"module": "dist/index.js",
66
"types": "dist/index.d.ts",

lib/src/HighlightPopover.tsx

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -230,24 +230,6 @@ export const HighlightPopover = memo(function HighlightPopover({
230230
};
231231
}, [handleSelection]);
232232

233-
// Handle clicks outside the popover
234-
useEffect(() => {
235-
const handleClickOutside = (event: MouseEvent) => {
236-
if (
237-
containerRef.current &&
238-
!containerRef.current.contains(event.target as Node)
239-
) {
240-
setShowPopover(false);
241-
onPopoverHide?.();
242-
}
243-
};
244-
245-
document.addEventListener("mousedown", handleClickOutside);
246-
return () => {
247-
document.removeEventListener("mousedown", handleClickOutside);
248-
};
249-
}, [onPopoverHide]);
250-
251233
const contextValue = useMemo<HighlightPopoverContextType>(
252234
() => ({
253235
showPopover,

tests/HighlightPopover.test.tsx

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -83,56 +83,6 @@ describe("HighlightPopover Component", () => {
8383
});
8484
});
8585

86-
test("hides popover when clicking outside", async () => {
87-
const renderPopoverMock = mock(() => (
88-
<div data-testid="popover-hide">Popover Content</div>
89-
));
90-
const onPopoverHideMock = mock();
91-
92-
render(
93-
<HighlightPopover
94-
renderPopover={renderPopoverMock}
95-
onPopoverHide={onPopoverHideMock}
96-
>
97-
<p data-testid="selectable-text-hide">Select this text</p>
98-
</HighlightPopover>,
99-
);
100-
101-
const textElement = screen.getByTestId("selectable-text-hide");
102-
103-
await act(async () => {
104-
const range = document.createRange();
105-
range.selectNodeContents(textElement);
106-
const selection = window.getSelection();
107-
if (selection) {
108-
selection.removeAllRanges();
109-
selection.addRange(range);
110-
}
111-
112-
const event = new Event("selectionchange", {
113-
bubbles: true,
114-
cancelable: true,
115-
});
116-
document.dispatchEvent(event);
117-
});
118-
119-
// Wait for the popover to appear
120-
await waitFor(() => {
121-
expect(screen.getByTestId("popover-hide")).toBeDefined();
122-
});
123-
124-
// Click outside
125-
await act(async () => {
126-
fireEvent.mouseDown(document.body);
127-
});
128-
129-
// Wait for the popover to disappear
130-
await waitFor(() => {
131-
expect(onPopoverHideMock).toHaveBeenCalled();
132-
expect(screen.queryByTestId("popover-hide")).toBeNull();
133-
});
134-
});
135-
13686
test("applies offset to popover position", async () => {
13787
const renderPopoverMock = mock(({ position }) => (
13888
<div

0 commit comments

Comments
 (0)