Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make sure to unregister all clients when "user-visible" option is out of viewport #1098

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions sandpack-react/src/Playground.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ export const Basic: React.FC = () => {
return (
<div style={{ height: "400vh" }}>
<Sandpack
files={{
"/App.js": `export default function TodoList() {
return (
// This doesn't quite work!
<h1>Hedy Lamarr's Todos</h1>
<img
src="https://i.imgur.com/yXOvdOSs.jpg"
alt="Hedy Lamarr"
class="photo"
>
<ul>
<li>Invent new traffic lights
<li>Rehearse a movie scene
<li>Improve spectrum technology
</ul>
);
}
`,
}}
options={{
initMode: "user-visible",
bundlerURL: "https://786946de.sandpack-bundler-4bw.pages.dev",
Expand Down
12 changes: 11 additions & 1 deletion sandpack-react/src/contexts/utils/useClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ export const useClient: UseClient = (
): void => {
if (entries.some((entry) => entry.isIntersecting)) {
runSandpack();
} else {
unregisterAllClients();
}
};

Expand All @@ -266,7 +268,15 @@ export const useClient: UseClient = (
// If any component registered a lazy anchor ref component, use that for the intersection observer
intersectionObserver.current = new IntersectionObserver((entries) => {
if (entries.some((entry) => entry.isIntersecting)) {
intersectionObserverCallback.current?.(entries);
// Trigger it once
if (
entries.some((entry) => entry.isIntersecting) &&
lazyAnchorRef.current
) {
intersectionObserverCallback.current?.(entries);

intersectionObserver.current?.unobserve(lazyAnchorRef.current);
}
}
}, observerOptions);

Expand Down