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

Commit 7198256

Browse files
committed
fix: deepPreserveProps
1 parent c70faf3 commit 7198256

File tree

4 files changed

+76
-16
lines changed

4 files changed

+76
-16
lines changed

demo/index.js

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useState, useEffect, useRef} from 'react';
1+
import React, {useState, useEffect, useRef, forwardRef} from 'react';
22
import ReactDOM from 'react-dom';
33
import styled from 'styled-components';
44
import {useSpring, animated} from 'react-spring';
@@ -42,6 +42,53 @@ const ReactFramerBox = styled(motion.div)`
4242
}
4343
`;
4444

45+
const LazyTippy = forwardRef((props, ref) => {
46+
const [mounted, setMounted] = useState(false);
47+
48+
const lazyPlugin = {
49+
fn: () => ({
50+
onMount: () => setMounted(true),
51+
onHidden: () => setMounted(false),
52+
}),
53+
};
54+
55+
const computedProps = {...props};
56+
57+
computedProps.plugins = [lazyPlugin, ...(props.plugins || [])];
58+
59+
if (props.render) {
60+
computedProps.render = (...args) => (mounted ? props.render(...args) : '');
61+
} else {
62+
computedProps.content = mounted ? props.content : '';
63+
}
64+
65+
return <Tippy {...computedProps} ref={ref} />;
66+
});
67+
68+
function CountContent() {
69+
const [count, setCount] = useState(0);
70+
71+
useEffect(() => {
72+
const interval = setInterval(() => {
73+
setCount(c => c + 1);
74+
}, 100);
75+
76+
return () => clearInterval(interval);
77+
}, []);
78+
79+
return count;
80+
}
81+
82+
function LazyTippyExample() {
83+
return (
84+
<LazyTippy content={<CountContent />} hideOnClick={false}>
85+
<LazyTippy content={<CountContent />} trigger="click" placement="bottom">
86+
<button>Lazy tippy</button>
87+
</LazyTippy>
88+
</LazyTippy>
89+
);
90+
}
91+
4592
function ContentString() {
4693
const [count, setCount] = useState(0);
4794

@@ -52,7 +99,7 @@ function ContentString() {
5299
}, []);
53100

54101
return (
55-
<Tippy content={count}>
102+
<Tippy content={count} popperOptions={{modifiers: [{name: 'hi'}]}}>
56103
<button>ContentString</button>
57104
</Tippy>
58105
);
@@ -311,7 +358,7 @@ function App() {
311358
<h2>Content</h2>
312359
<ContentString />
313360
<ContentElement />
314-
<h2>Special</h2>
361+
{/* <h2>Special</h2>
315362
<DisabledProp />
316363
<VisibleProp />
317364
<h2>Singleton</h2>
@@ -332,6 +379,8 @@ function App() {
332379
<ReferenceProp />
333380
<h2>Nested update</h2>
334381
<NestedUpdate />
382+
<h2>Lazy Tippy</h2>
383+
<LazyTippyExample /> */}
335384
</>
336385
);
337386
}

src/useSingleton.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default function useSingletonGenerator(createSingleton) {
7575
const {content, ...props} = sourceData.props;
7676

7777
instance.setProps(
78-
deepPreserveProps(instance, {
78+
deepPreserveProps(instance.props, {
7979
...props,
8080
overrides,
8181
}),

src/utils.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,30 @@ export function toDataAttributes(attrs) {
3232
return dataAttrs;
3333
}
3434

35+
export function uniqueByName(arr) {
36+
const output = [];
37+
const lookup = Object.create(null);
38+
39+
for (const item in arr) {
40+
if (lookup[arr[item].name] === undefined) {
41+
output.push(arr[item]);
42+
}
43+
lookup[arr[item].name] = arr[item];
44+
}
45+
46+
return output;
47+
}
48+
3549
export function deepPreserveProps(instanceProps, componentProps) {
3650
return {
3751
...componentProps,
3852
popperOptions: {
3953
...instanceProps.popperOptions,
4054
...componentProps.popperOptions,
41-
modifiers: [
42-
// Preserve tippy's internal + plugin modifiers
43-
...(instanceProps.popperOptions?.modifiers || []).filter(
44-
modifier => modifier.name.indexOf('tippy') >= 0,
45-
),
55+
modifiers: uniqueByName([
56+
...(instanceProps.popperOptions?.modifiers || []),
4657
...(componentProps.popperOptions?.modifiers || []),
47-
],
58+
]),
4859
},
4960
};
5061
}

test/__snapshots__/Tippy.test.js.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ exports[`<Tippy /> render prop 1`] = `
2626
exports[`<Tippy /> render prop preserve popperOptions 1`] = `
2727
Object {
2828
"modifiers": Array [
29+
Object {
30+
"enabled": true,
31+
"fn": [Function],
32+
"name": "x",
33+
"phase": "main",
34+
},
2935
Object {
3036
"enabled": true,
3137
"fn": [Function],
@@ -35,12 +41,6 @@ Object {
3541
"computeStyles",
3642
],
3743
},
38-
Object {
39-
"enabled": true,
40-
"fn": [Function],
41-
"name": "x",
42-
"phase": "main",
43-
},
4444
],
4545
"strategy": "fixed",
4646
}

0 commit comments

Comments
 (0)