Skip to content
This repository was archived by the owner on Nov 9, 2024. It is now read-only.

Commit 9baf207

Browse files
committed
fix: transform className prop into a plugin
closes #204
1 parent 05010b6 commit 9baf207

File tree

5 files changed

+56
-66
lines changed

5 files changed

+56
-66
lines changed

src/Tippy.js

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import {
66
ssrSafeCreateDiv,
77
toDataAttributes,
88
deepPreserveProps,
9-
updateClassName,
109
} from './utils';
1110
import {useMutableBox, useIsomorphicLayoutEffect} from './util-hooks';
11+
import {classNamePlugin} from './className-plugin';
1212

1313
export default function TippyGenerator(tippy) {
1414
function Tippy({
1515
children,
1616
content,
17-
className,
1817
visible,
1918
singleton,
2019
render,
@@ -96,10 +95,10 @@ export default function TippyGenerator(tippy) {
9695

9796
// CREATE
9897
useIsomorphicLayoutEffect(() => {
99-
const instance = tippy(
100-
mutableBox.ref || ssrSafeCreateDiv(),
101-
computedProps,
102-
);
98+
const instance = tippy(mutableBox.ref || ssrSafeCreateDiv(), {
99+
...computedProps,
100+
plugins: [classNamePlugin, ...(props.plugins || [])],
101+
});
103102

104103
mutableBox.instance = instance;
105104

@@ -116,7 +115,6 @@ export default function TippyGenerator(tippy) {
116115
instance,
117116
content,
118117
props: computedProps,
119-
className,
120118
});
121119
}
122120

@@ -159,7 +157,6 @@ export default function TippyGenerator(tippy) {
159157
instance,
160158
content,
161159
props,
162-
className,
163160
});
164161
}
165162
});
@@ -206,32 +203,6 @@ export default function TippyGenerator(tippy) {
206203
});
207204
}, [attrs.placement, attrs.referenceHidden, attrs.escaped, ...deps]);
208205

209-
useIsomorphicLayoutEffect(() => {
210-
if (className) {
211-
if (render) {
212-
if (process.env.NODE_ENV !== 'production') {
213-
console.warn(
214-
[
215-
'@tippyjs/react: Cannot use `className` prop in conjunction',
216-
'with the `render` prop. Place the className on the element',
217-
'you are rendering.',
218-
].join(' '),
219-
);
220-
}
221-
222-
return;
223-
}
224-
225-
const box = mutableBox.instance.popper.firstElementChild;
226-
227-
updateClassName(box, 'add', className);
228-
229-
return () => {
230-
updateClassName(box, 'remove', className);
231-
};
232-
}
233-
}, [className, ...deps]);
234-
235206
return (
236207
<>
237208
{children

src/className-plugin.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {updateClassName} from './utils';
2+
3+
export const classNamePlugin = {
4+
name: 'className',
5+
defaultValue: '',
6+
fn(instance) {
7+
const box = instance.popper.firstElementChild;
8+
const isDefaultRenderFn = () => !!instance.props.render?.$$tippy;
9+
10+
function add() {
11+
if (instance.props.className && !isDefaultRenderFn()) {
12+
if (process.env.NODE_ENV !== 'production') {
13+
console.warn(
14+
[
15+
'@tippyjs/react: Cannot use `className` prop in conjunction with',
16+
'`render` prop. Place the className on the element you are',
17+
'rendering.',
18+
].join(' '),
19+
);
20+
}
21+
22+
return;
23+
}
24+
25+
updateClassName(box, 'add', instance.props.className);
26+
}
27+
28+
function remove() {
29+
if (isDefaultRenderFn()) {
30+
updateClassName(box, 'remove', instance.props.className);
31+
}
32+
}
33+
34+
return {
35+
onCreate: add,
36+
onBeforeUpdate: remove,
37+
onAfterUpdate: add,
38+
};
39+
},
40+
};

src/useSingleton.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {useMutableBox, useIsomorphicLayoutEffect} from './util-hooks';
2-
import {deepPreserveProps, updateClassName} from './utils';
2+
import {deepPreserveProps} from './utils';
3+
import {classNamePlugin} from './className-plugin';
34
import {useMemo} from 'react';
45

56
export default function useSingletonGenerator(createSingleton) {
@@ -31,6 +32,7 @@ export default function useSingletonGenerator(createSingleton) {
3132
...sourceData.props,
3233
popperOptions: sourceData.instance.props.popperOptions,
3334
overrides,
35+
plugins: [classNamePlugin, ...(sourceData.props.plugins || [])],
3436
},
3537
);
3638

@@ -77,34 +79,6 @@ export default function useSingletonGenerator(createSingleton) {
7779
}
7880
});
7981

80-
useIsomorphicLayoutEffect(() => {
81-
const className = mutableBox.sourceData?.className;
82-
83-
if (className) {
84-
if (mutableBox.sourceData?.props.render) {
85-
if (process.env.NODE_ENV !== 'production') {
86-
console.warn(
87-
[
88-
'@tippyjs/react: Cannot use `className` prop in conjunction',
89-
'with the `render` prop. Place the className on the element',
90-
'you are rendering.',
91-
].join(' '),
92-
);
93-
}
94-
95-
return;
96-
}
97-
98-
const box = mutableBox.instance.popper.firstElementChild;
99-
100-
updateClassName(box, 'add', className);
101-
102-
return () => {
103-
updateClassName(box, 'remove', className);
104-
};
105-
}
106-
});
107-
10882
return useMemo(() => {
10983
const source = {
11084
data: mutableBox,

test/Tippy.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ describe('<Tippy />', () => {
485485

486486
expect(spy).toHaveBeenCalledWith(
487487
[
488-
'@tippyjs/react: Cannot use `className` prop in conjunction',
489-
'with the `render` prop. Place the className on the element',
490-
'you are rendering.',
488+
'@tippyjs/react: Cannot use `className` prop in conjunction with',
489+
'`render` prop. Place the className on the element you are',
490+
'rendering.',
491491
].join(' '),
492492
);
493493

test/__snapshots__/Tippy.test.js.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
exports[`<Tippy /> props.plugins 1`] = `
44
Array [
5+
Object {
6+
"defaultValue": "",
7+
"fn": [Function],
8+
"name": "className",
9+
},
510
Object {
611
"fn": [Function],
712
},

0 commit comments

Comments
 (0)