@globalfishingwatch/react-map-gl
Version:
A React wrapper for MapboxGL-js and overlay API.
83 lines (76 loc) • 2.01 kB
JavaScript
import * as React from 'react';
import { useMemo } from 'react';
import PropTypes from 'prop-types';
import useDraggableControl, { draggableControlDefaultProps, draggableControlPropTypes } from './draggable-control';
import { crispPixel } from '../utils/crisp-pixel';
const propTypes = Object.assign({}, draggableControlPropTypes, {
className: PropTypes.string,
longitude: PropTypes.number.isRequired,
latitude: PropTypes.number.isRequired
});
const defaultProps = Object.assign({}, draggableControlDefaultProps, {
className: ''
});
function getPosition({
props,
state,
context
}) {
const {
longitude,
latitude,
offsetLeft,
offsetTop
} = props;
const {
dragPos,
dragOffset
} = state;
if (dragPos && dragOffset) {
return [dragPos[0] + dragOffset[0], dragPos[1] + dragOffset[1]];
}
let [x, y] = context.viewport.project([longitude, latitude]);
x += offsetLeft;
y += offsetTop;
return [x, y];
}
function Marker(props) {
const thisRef = useDraggableControl(props);
const {
state,
containerRef
} = thisRef;
const {
draggable
} = props;
const {
dragPos
} = state;
const [x, y] = getPosition(thisRef);
const transform = "translate(".concat(crispPixel(x), "px, ").concat(crispPixel(y), "px)");
const cursor = draggable ? dragPos ? 'grabbing' : 'grab' : 'auto';
const control = useMemo(() => {
const containerStyle = {
position: 'absolute',
left: 0,
top: 0,
transform,
cursor
};
return React.createElement("div", {
className: "mapboxgl-marker ".concat(props.className),
ref: thisRef.containerRef,
style: containerStyle
}, props.children);
}, [props.className]);
const container = containerRef.current;
if (container) {
container.style.transform = transform;
container.style.cursor = cursor;
}
return control;
}
Marker.defaultProps = defaultProps;
Marker.propTypes = propTypes;
export default Marker;
//# sourceMappingURL=marker.js.map