-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added coords and a generic hook to render a custom control
- Loading branch information
Showing
6 changed files
with
145 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { MapRef } from 'react-map-gl'; | ||
import styled from 'styled-components'; | ||
import { Button } from '@devseed-ui/button'; | ||
import { themeVal } from '@devseed-ui/theme-provider'; | ||
import useMaps from '../hooks/use-maps'; | ||
import useThemedControl from './hooks/use-themed-control'; | ||
import { round } from '$utils/format'; | ||
import { CopyField } from '$components/common/copy-field'; | ||
|
||
const MapCoordsWrapper = styled.div` | ||
/* Large width so parent will wrap */ | ||
width: 100vw; | ||
${Button} { | ||
background: ${themeVal('color.base-400a')}; | ||
font-weight: ${themeVal('type.base.regular')}; | ||
font-size: 0.75rem; | ||
} | ||
&& ${Button /* sc-selector */}:hover { | ||
background: ${themeVal('color.base-500')}; | ||
} | ||
`; | ||
|
||
const getCoords = (mapInstance?: MapRef) => { | ||
if (!mapInstance) return { lng: 0, lat: 0 }; | ||
const mapCenter = mapInstance.getCenter(); | ||
return { | ||
lng: round(mapCenter.lng, 4), | ||
lat: round(mapCenter.lat, 4) | ||
}; | ||
}; | ||
|
||
export default function MapCoords() { | ||
const { main } = useMaps(); | ||
|
||
const [position, setPosition] = useState(getCoords(main)); | ||
|
||
useEffect(() => { | ||
const posListener = (e) => { | ||
setPosition(getCoords(e.target)); | ||
}; | ||
|
||
if (main) main.on('moveend', posListener); | ||
|
||
return () => { | ||
if (main) main.off('moveend', posListener); | ||
}; | ||
}, [main]); | ||
|
||
const { lng, lat } = position; | ||
const value = `W ${lng}, N ${lat}`; | ||
|
||
useThemedControl( | ||
() => ( | ||
<MapCoordsWrapper> | ||
<CopyField value={value}> | ||
{({ ref, showCopiedMsg }) => ( | ||
<Button | ||
ref={ref} | ||
// @ts-expect-error achromic-text exists but ui-library types are | ||
// not up to date | ||
variation='achromic-text' | ||
size='small' | ||
> | ||
{showCopiedMsg ? 'Copied!' : value} | ||
</Button> | ||
)} | ||
</CopyField> | ||
</MapCoordsWrapper> | ||
), | ||
{ position: 'bottom-left' } | ||
); | ||
|
||
return null; | ||
} |
50 changes: 50 additions & 0 deletions
50
app/scripts/components/common/map/controls/hooks/use-themed-control.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { IControl } from 'mapbox-gl'; | ||
import React, { ReactNode, useEffect, useRef } from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
import { useControl } from 'react-map-gl'; | ||
import { useTheme, ThemeProvider } from 'styled-components'; | ||
|
||
export default function useThemedControl( | ||
renderFn: () => ReactNode, | ||
opts?: any | ||
) { | ||
const theme = useTheme(); | ||
const elementRef = useRef<HTMLDivElement | null>(null); | ||
const rootRef = useRef<ReturnType<typeof createRoot> | null>(null); | ||
|
||
// Define the control methods and its lifecycle | ||
class ThemedControl implements IControl { | ||
onAdd() { | ||
const el = document.createElement('div'); | ||
el.className = 'mapboxgl-ctrl'; | ||
elementRef.current = el; | ||
|
||
// Create a root and render the component | ||
rootRef.current = createRoot(el); | ||
|
||
rootRef.current.render( | ||
<ThemeProvider theme={theme}>{renderFn() as any}</ThemeProvider> | ||
); | ||
|
||
return el; | ||
} | ||
|
||
onRemove() { | ||
// Cleanup if necessary | ||
if (elementRef.current) { | ||
rootRef.current?.unmount(); | ||
} | ||
} | ||
} | ||
|
||
// Listen for changes in dependencies and re-render if necessary | ||
useEffect(() => { | ||
if (rootRef.current) { | ||
rootRef.current.render( | ||
<ThemeProvider theme={theme}>{renderFn() as any}</ThemeProvider> | ||
); | ||
} | ||
}, [renderFn, theme]); | ||
useControl(() => new ThemedControl(), opts); | ||
return null; | ||
} |
30 changes: 13 additions & 17 deletions
30
app/scripts/components/common/map/hooks/use-map-compare.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,25 @@ | ||
import { useContext, useEffect } from "react"; | ||
import { useContext, useEffect } from 'react'; | ||
import MapboxCompare from 'mapbox-gl-compare'; | ||
import { MapsContext } from "../maps"; | ||
import { useMaps } from "./use-maps"; | ||
import { MapsContext } from '../maps'; | ||
import useMaps from './use-maps'; | ||
|
||
export default function useMapCompare() { | ||
const maps = useMaps(); | ||
const { main, compared } = useMaps(); | ||
const { containerId } = useContext(MapsContext); | ||
const hasMapCompare = !!maps.compared; | ||
const hasMapCompare = !!compared; | ||
useEffect(() => { | ||
if (!maps.main) return; | ||
if (!main) return; | ||
|
||
if (maps.compared) { | ||
const compare = new MapboxCompare( | ||
maps.main, | ||
maps.compared, | ||
`#${containerId}`, | ||
{ | ||
mousemove: false, | ||
orientation: 'vertical' | ||
} | ||
); | ||
if (compared) { | ||
const compare = new MapboxCompare(main, compared, `#${containerId}`, { | ||
mousemove: false, | ||
orientation: 'vertical' | ||
}); | ||
|
||
return () => { | ||
compare.remove(); | ||
}; | ||
} | ||
// main should be stable, while we are only interested here in the absence or presence of compared | ||
}, [containerId, hasMapCompare]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters