Skip to content

Commit 83b162e

Browse files
committed
add shadow dom support
1 parent 575662a commit 83b162e

File tree

5 files changed

+547
-425
lines changed

5 files changed

+547
-425
lines changed

.size-snapshot.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"react-popper-tooltip.js": {
3-
"bundled": 10837,
4-
"minified": 5193,
5-
"gzipped": 1733,
3+
"bundled": 10999,
4+
"minified": 5259,
5+
"gzipped": 1763,
66
"treeshaked": {
77
"rollup": {
88
"code": 142,

package.json

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-popper-tooltip",
3-
"version": "4.2.0",
3+
"version": "4.3.0",
44
"description": "React tooltip library built around react-popper",
55
"author": "Mohsin Ul Haq <[email protected]>",
66
"license": "MIT",
@@ -33,7 +33,7 @@
3333
"typecheck": "tsc --noEmit",
3434
"lint": "eslint \"{src,tests,examples}**/*.{ts,tsx}\"",
3535
"start": "rollup -c -w",
36-
"test": "jest tests/",
36+
"test": "jest --env=jsdom tests",
3737
"storybook": "start-storybook -p 6006",
3838
"build-storybook": "build-storybook",
3939
"postinstall": "husky install",
@@ -64,25 +64,25 @@
6464
"react-popper": "^2.2.5"
6565
},
6666
"devDependencies": {
67-
"@babel/core": "^7.14.0",
68-
"@babel/plugin-transform-runtime": "^7.13.15",
69-
"@babel/preset-env": "^7.14.1",
67+
"@babel/core": "^7.14.3",
68+
"@babel/plugin-transform-runtime": "^7.14.3",
69+
"@babel/preset-env": "^7.14.2",
7070
"@babel/preset-react": "^7.13.13",
7171
"@babel/preset-typescript": "^7.13.0",
7272
"@storybook/addon-actions": "^6.2.9",
7373
"@storybook/addon-essentials": "^6.2.9",
7474
"@storybook/addon-links": "^6.2.9",
7575
"@storybook/react": "^6.2.9",
7676
"@testing-library/jest-dom": "^5.12.0",
77-
"@testing-library/react": "^11.2.6",
78-
"@testing-library/user-event": "^13.1.8",
77+
"@testing-library/react": "^11.2.7",
78+
"@testing-library/user-event": "^13.1.9",
7979
"@types/jest": "^26.0.23",
80-
"@types/react": "^17.0.5",
81-
"@types/react-dom": "^17.0.3",
82-
"@typescript-eslint/eslint-plugin": "^4.22.1",
83-
"@typescript-eslint/parser": "^4.22.1",
80+
"@types/react": "^17.0.7",
81+
"@types/react-dom": "^17.0.5",
82+
"@typescript-eslint/eslint-plugin": "^4.25.0",
83+
"@typescript-eslint/parser": "^4.25.0",
8484
"babel-loader": "^8.2.2",
85-
"eslint": "^7.26.0",
85+
"eslint": "^7.27.0",
8686
"eslint-config-prettier": "^8.3.0",
8787
"eslint-plugin-prettier": "^3.4.0",
8888
"eslint-plugin-react": "^7.23.2",
@@ -91,10 +91,10 @@
9191
"jest": "^26.6.3",
9292
"lint-staged": "^11.0.0",
9393
"pinst": "^2.1.6",
94-
"prettier": "^2.2.1",
94+
"prettier": "^2.3.0",
9595
"react": "^17.0.1",
9696
"react-dom": "^17.0.1",
97-
"rollup": "^2.47.0",
97+
"rollup": "^2.50.0",
9898
"rollup-plugin-babel": "^4.4.0",
9999
"rollup-plugin-node-resolve": "^5.2.0",
100100
"rollup-plugin-size-snapshot": "^0.12.0",

src/usePopperTooltip.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export function usePopperTooltip(
3535
// Merging options with default options.
3636
// Keys with undefined values are replaced with the default ones if any.
3737
// Keys with other values pass through.
38-
const finalConfig = (Object.keys(defaultConfig) as Array<
39-
keyof typeof defaultConfig
40-
>).reduce(
38+
const finalConfig = (
39+
Object.keys(defaultConfig) as Array<keyof typeof defaultConfig>
40+
).reduce(
4141
(config, key) => ({
4242
...config,
4343
[key]: config[key] !== undefined ? config[key] : defaultConfig[key],
@@ -125,7 +125,7 @@ export function usePopperTooltip(
125125

126126
const handleClickOutside: EventListener = (event) => {
127127
const { tooltipRef, triggerRef } = getLatest();
128-
const target = event.target;
128+
const target = event.composedPath?.()?.[0] || event.target;
129129
if (target instanceof Node) {
130130
if (
131131
tooltipRef != null &&

tests/usePopperTooltip.spec.tsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe('trigger option', () => {
155155
});
156156

157157
test('closeOnOutsideClick removes tooltip on document.body click', async () => {
158-
render(<Tooltip options={{ closeOnOutsideClick: true, trigger: 'click' }} />);
158+
render(<Tooltip options={{ trigger: 'click' }} />);
159159

160160
// Show on click
161161
userEvent.click(screen.getByText(TriggerText));
@@ -168,6 +168,19 @@ test('closeOnOutsideClick removes tooltip on document.body click', async () => {
168168
});
169169
});
170170

171+
test("closeOnOutsideClick doesn't remove tooltip on tooltip click", async () => {
172+
render(<Tooltip options={{ trigger: 'click' }} />);
173+
174+
// Show on click
175+
userEvent.click(screen.getByText(TriggerText));
176+
expect(await screen.findByText(TooltipText)).toBeInTheDocument();
177+
178+
userEvent.click(screen.getByText(TooltipText));
179+
await waitFor(() => {
180+
expect(screen.queryByText(TooltipText)).toBeInTheDocument();
181+
});
182+
});
183+
171184
test('delayShow option renders tooltip after specified delay', async () => {
172185
jest.useFakeTimers();
173186
render(<Tooltip options={{ delayShow: 5000 }} />);

0 commit comments

Comments
 (0)