import { ReactNode, useEffect, useRef } from 'react'; import * as L from 'leaflet'; import { useMap } from 'react-leaflet'; import { createRoot, Root } from 'react-dom/client'; export interface ButtonDef { title: string; active: boolean; onClick: () => void; icon: ReactNode; } interface GroupControlProps { position?: L.ControlPosition; buttons: ButtonDef[]; } export function GroupControl({ position = 'bottomright', buttons }: GroupControlProps) { const map = useMap(); // References const controlRef = useRef(null); const rootRef = useRef(null); useEffect(() => { const ctrl = new L.Control({ position }); controlRef.current = ctrl; ctrl.onAdd = () => { return L.DomUtil.create('div', 'leaflet-bar leaflet-control custom-control'); }; ctrl.addTo(map); // @ts-expect-error ctrl is a L.Control const container = (ctrl as unknown)._container as HTMLElement; rootRef.current = createRoot(container); L.DomEvent.disableClickPropagation(container); return () => { ctrl.remove(); rootRef.current!.unmount(); }; }, [map, position]); useEffect(() => { if (rootRef.current) { rootRef.current.render(
{buttons.map((b, i) => ( { e.preventDefault(); e.stopPropagation(); b.onClick(); }} style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', padding: '4px' }} > {b.icon} ))}
); } }, [buttons]); return null; }