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

Commit d4c915d

Browse files
authored
fix: dynamic singleton headless content (#334)
1 parent d5ee338 commit d4c915d

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

demo/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,34 @@ function Singleton() {
168168
);
169169
}
170170

171+
function SingletonHeadlessDynamicContent() {
172+
const [source, target] = useSingletonHeadless();
173+
const [count, setCount] = useState(0);
174+
175+
useEffect(() => {
176+
setInterval(() => {
177+
setCount(c => c + 1);
178+
}, 1000);
179+
}, []);
180+
181+
return (
182+
<>
183+
<TippyHeadless
184+
render={(attrs, content) => (
185+
<ReactSpringBox {...attrs}>{content}</ReactSpringBox>
186+
)}
187+
singleton={source}
188+
/>
189+
<TippyHeadless content={count} singleton={target}>
190+
<button>Reference</button>
191+
</TippyHeadless>
192+
<TippyHeadless content={count + 1} singleton={target}>
193+
<button>Reference</button>
194+
</TippyHeadless>
195+
</>
196+
);
197+
}
198+
171199
function SingletonHeadless() {
172200
const [source, target] = useSingletonHeadless({overrides: ['placement']});
173201
const [enabled, setEnabled] = useState(false);
@@ -365,6 +393,8 @@ function App() {
365393
<Singleton />
366394
<h2>Singleton Headless</h2>
367395
<SingletonHeadless />
396+
<h2>Singleton Headless Dynamic Content</h2>
397+
<SingletonHeadlessDynamicContent />
368398
<h2>Nested Singleton</h2>
369399
<NestedSingleton />
370400
<h2>Plugins</h2>

src/Tippy.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,27 @@ export default function TippyGenerator(tippy) {
7171
if (render) {
7272
computedProps = {
7373
...props,
74-
plugins: isSingletonMode
75-
? [
76-
...plugins,
77-
{
78-
fn: () => ({
79-
onTrigger(_, event) {
80-
const {content} = singleton.data.children.find(
81-
({instance}) =>
82-
instance.reference === event.currentTarget,
83-
);
84-
setSingletonContent(content);
74+
plugins:
75+
isSingletonMode && singleton.data != null
76+
? [
77+
...plugins,
78+
{
79+
fn() {
80+
return {
81+
onTrigger(instance, event) {
82+
const node = singleton.data.children.find(
83+
({instance}) =>
84+
instance.reference === event.currentTarget,
85+
);
86+
instance.state.$$activeSingletonInstance =
87+
node.instance;
88+
setSingletonContent(node.content);
89+
},
90+
};
8591
},
86-
}),
87-
},
88-
]
89-
: plugins,
92+
},
93+
]
94+
: plugins,
9095
render: () => ({popper: mutableBox.container}),
9196
};
9297
}
@@ -120,6 +125,7 @@ export default function TippyGenerator(tippy) {
120125
instance,
121126
content,
122127
props: computedProps,
128+
setSingletonContent,
123129
});
124130
}
125131

@@ -165,6 +171,7 @@ export default function TippyGenerator(tippy) {
165171
instance,
166172
content,
167173
props: computedProps,
174+
setSingletonContent,
168175
});
169176
}
170177
});

src/useSingleton.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export default function useSingletonGenerator(createSingleton) {
9595
data: mutableBox,
9696
hook(data) {
9797
mutableBox.sourceData = data;
98+
mutableBox.setSingletonContent = data.setSingletonContent;
9899
},
99100
cleanup() {
100101
mutableBox.sourceData = null;
@@ -103,18 +104,23 @@ export default function useSingletonGenerator(createSingleton) {
103104

104105
const target = {
105106
hook(data) {
107+
mutableBox.children = mutableBox.children.filter(
108+
({instance}) => data.instance !== instance,
109+
);
110+
mutableBox.children.push(data);
111+
106112
if (
107-
!mutableBox.children.find(
108-
({instance}) => data.instance === instance,
109-
)
113+
mutableBox.instance?.state.isMounted &&
114+
mutableBox.instance?.state.$$activeSingletonInstance ===
115+
data.instance
110116
) {
111-
mutableBox.children.push(data);
117+
mutableBox.setSingletonContent?.(data.content);
118+
}
112119

113-
if (mutableBox.instance && !mutableBox.instance.state.isDestroyed) {
114-
mutableBox.instance.setInstances(
115-
mutableBox.children.map(child => child.instance),
116-
);
117-
}
120+
if (mutableBox.instance && !mutableBox.instance.state.isDestroyed) {
121+
mutableBox.instance.setInstances(
122+
mutableBox.children.map(child => child.instance),
123+
);
118124
}
119125
},
120126
cleanup(instance) {

0 commit comments

Comments
 (0)