-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include center coordinates control (#620)
- Loading branch information
Showing
7 changed files
with
199 additions
and
23 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,54 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import Clipboard from 'clipboard'; | ||
|
||
interface CopyFieldProps { | ||
value: string; | ||
children: (props: { | ||
value: string; | ||
ref: React.MutableRefObject<any>; | ||
originalValue: string; | ||
showCopiedMsg: boolean; | ||
}) => JSX.Element; | ||
} | ||
|
||
export function CopyField(props: CopyFieldProps) { | ||
const { value, children } = props; | ||
|
||
const [showCopiedMsg, setShowCopiedMsg] = useState(false); | ||
const triggerElement = useRef<any>(); | ||
|
||
const copyValue = useRef<string>(value); | ||
copyValue.current = value; | ||
|
||
useEffect(() => { | ||
if (!triggerElement.current) throw new Error("ref for trigger element is not set"); | ||
|
||
let copiedMsgTimeout: NodeJS.Timeout | undefined; | ||
const clipboard = new Clipboard(triggerElement.current, { | ||
text: () => copyValue.current | ||
}); | ||
|
||
clipboard.on('success', () => { | ||
setShowCopiedMsg(true); | ||
copiedMsgTimeout = setTimeout(() => { | ||
setShowCopiedMsg(false); | ||
}, 2000); | ||
}); | ||
|
||
return () => { | ||
clipboard.destroy(); | ||
if (copiedMsgTimeout) { | ||
clearTimeout(copiedMsgTimeout); | ||
} | ||
}; | ||
}, []); | ||
|
||
const val = showCopiedMsg ? 'Copied!' : value; | ||
|
||
return children({ | ||
value: val, | ||
ref: triggerElement, | ||
originalValue: value, | ||
showCopiedMsg | ||
}); | ||
} |
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,75 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import styled from 'styled-components'; | ||
import { Map } from 'mapbox-gl'; | ||
|
||
import { themeVal } from '@devseed-ui/theme-provider'; | ||
import { Button } from '@devseed-ui/button'; | ||
|
||
import { CopyField } from '../copy-field'; | ||
import { round } from '$utils/format'; | ||
|
||
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')}; | ||
} | ||
`; | ||
|
||
interface MapCoordsProps { | ||
mapInstance: Map; | ||
} | ||
|
||
const getCoords = (mapInstance: Map) => { | ||
const mapCenter = mapInstance.getCenter(); | ||
return { | ||
lng: round(mapCenter.lng, 4), | ||
lat: round(mapCenter.lat, 4) | ||
}; | ||
}; | ||
|
||
export default function MapCoords(props: MapCoordsProps) { | ||
const { mapInstance } = props; | ||
|
||
const [position, setPosition] = useState(getCoords(mapInstance)); | ||
|
||
useEffect(() => { | ||
const posListener = (e) => { | ||
setPosition(getCoords(e.target)); | ||
}; | ||
|
||
mapInstance.on('moveend', posListener); | ||
|
||
return () => { | ||
mapInstance.off('moveend', posListener); | ||
}; | ||
}, [mapInstance]); | ||
|
||
const { lng, lat } = position; | ||
const value = `W ${lng}, N ${lat}`; | ||
|
||
return ( | ||
<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> | ||
); | ||
} |
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
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