UNPKG

react-naver-maps

Version:

React Navermaps API integration for modern development.

136 lines (134 loc) 3.29 kB
import { getKeys } from "./chunk-4MXCHP7N.mjs"; import { getUncontrolledKey, makeUncontrolledKeyMap } from "./chunk-SI4FST6P.mjs"; import { omitUndefined } from "./chunk-RVDAKL7Q.mjs"; import { useNavermaps } from "./chunk-2R4EMKHB.mjs"; import { HandleEvents } from "./chunk-533MYCRP.mjs"; import { Overlay } from "./chunk-IV7GZJYG.mjs"; // src/overlays/marker.tsx import mapKeys from "lodash.mapkeys"; import pick from "lodash.pick"; import { forwardRef, useLayoutEffect, useImperativeHandle, useRef, useState } from "react"; import { useFirstMountState } from "react-use"; import { jsx } from "react/jsx-runtime"; var primitiveKeys = [ "animation", "icon", "shape", "title", "cursor", "clickable", "draggable", "visible", "zIndex" ]; var locationalKeys = ["position"]; var uncontrolledKeyMap = makeUncontrolledKeyMap(locationalKeys); var kvoKeys = [ ...primitiveKeys, ...locationalKeys ]; var kvoEvents = kvoKeys.map((key) => `${key}_changed`); var uiEvents = [ "mousedown", "mouseup", "click", "dblclick", "rightclick", "mouseover", "mouseout", "dragstart", "drag", "dragend" ]; var events = [...uiEvents, ...kvoEvents]; function makeInitialOption(props) { const uncontrolledProps = pick(props, getKeys(uncontrolledKeyMap)); const prefixCleared = mapKeys(uncontrolledProps, (_, key) => uncontrolledKeyMap[key]); const kvoProps = pick(props, kvoKeys); return omitUndefined({ ...kvoProps, ...prefixCleared }); } function isLocationalKey(key) { return locationalKeys.includes(key); } function isEqualKvo(kvo, target) { if (kvo === void 0) { return false; } if (kvo === target) { return true; } try { return kvo.equals(target); } catch { return kvo === target; } } var Marker = forwardRef(function Marker2(props, ref) { const navermaps = useNavermaps(); const [marker] = useState(() => new navermaps.Marker(makeInitialOption(props))); useImperativeHandle(ref, () => marker); const isFirst = useFirstMountState(); const dirtiesRef = useRef({}); dirtiesRef.current = getDirties(); function getDirties() { if (isFirst) { return {}; } return kvoKeys.reduce((acc, key) => { if (props[key] === void 0) { return acc; } if (isLocationalKey(key) && props[getUncontrolledKey(key)] !== void 0) { return acc; } const kvos = marker.getOptions(key); if (isEqualKvo(kvos[key], props[key])) { return acc; } return { ...acc, [key]: props[key] }; }, {}); } function pickDirties(keys) { return pick(dirtiesRef.current, keys); } useLayoutEffect(() => { const { position } = pickDirties(["position"]); if (position) { marker.setPosition(position); } }, [dirtiesRef.current["position"]]); useLayoutEffect(() => { const dirties = pickDirties(primitiveKeys); if (Object.values(dirties).length < 1) { return; } marker.setOptions(dirties); }, primitiveKeys.map((key) => dirtiesRef.current[key])); return /* @__PURE__ */ jsx(Overlay, { element: marker, children: /* @__PURE__ */ jsx(HandleEvents, { events, listeners: props }) }); }); export { Marker }; //# sourceMappingURL=chunk-CM6ZCNTJ.mjs.map