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(carte): initialize map only when container is visible #9442

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/javascript/components/shared/maplibre/MapLibre.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { Style } from 'maplibre-gl';

import invariant from 'tiny-invariant';

import { useStyle } from './hooks';
import { useStyle, useElementVisible } from './hooks';
import { StyleControl } from './StyleControl';

const Context = createContext<{ map?: Map | null }>({});
Expand All @@ -35,6 +35,7 @@ export function MapLibre({ children, layers }: MapLibreProps) {
[]
);
const containerRef = useRef<HTMLDivElement>(null);
const visible = useElementVisible(containerRef);
const [map, setMap] = useState<Map | null>();

const onStyleChange = useCallback(
Expand All @@ -48,7 +49,7 @@ export function MapLibre({ children, layers }: MapLibreProps) {
const { style, ...mapStyleProps } = useStyle(layers, onStyleChange);

useEffect(() => {
if (isSupported && !map) {
if (isSupported && visible && !map) {
invariant(containerRef.current, 'Map container not found');
const map = new Map({
container: containerRef.current,
Expand All @@ -59,7 +60,7 @@ export function MapLibre({ children, layers }: MapLibreProps) {
setMap(map);
});
}
}, [map, style, isSupported]);
}, [map, style, visible, isSupported]);

if (!isSupported) {
return (
Expand Down
35 changes: 34 additions & 1 deletion app/javascript/components/shared/maplibre/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { useCallback, useEffect, useState, useMemo } from 'react';
import {
useCallback,
useEffect,
useState,
useMemo,
type RefObject
} from 'react';
import type {
LngLatBoundsLike,
LngLat,
Expand Down Expand Up @@ -118,3 +124,30 @@ export function useStyle(

return { style, layers, setStyle, setLayerEnabled, setLayerOpacity };
}

function isElementVisible(
element: HTMLElement,
callback: (visible: boolean) => void
) {
if (element.offsetWidth > 0 && element.offsetHeight > 0) {
callback(true);
} else {
callback(false);
const observer = new IntersectionObserver(
(entries) => callback(entries[0].isIntersecting == true),
{ threshold: [0] }
);
observer.observe(element);
return () => observer.unobserve(element);
}
}

export function useElementVisible(element: RefObject<HTMLElement>) {
const [visible, setVisible] = useState(false);
useEffect(() => {
if (element.current) {
return isElementVisible(element.current, setVisible);
}
}, [element]);
return visible;
}