@rousen/react-naver-maps 0.0.14 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -160,7 +160,7 @@ const useScriptLoader = ({ ncpKeyId, submodules, disableGL, }) => {
160
160
  return { isScriptLoaded };
161
161
  };
162
162
 
163
- const EVENT_TO_PROP$2 = {
163
+ const EVENT_TO_PROP$7 = {
164
164
  init: "onInit",
165
165
  zoomstart: "onZoomStart",
166
166
  zoomend: "onZoomEnd",
@@ -206,6 +206,9 @@ const Map = forwardRef(({ id, mapTypeId = "normal", ncpKeyId, children, mapOptio
206
206
  useEffect(() => {
207
207
  if (!isScriptLoaded || !containerRef.current)
208
208
  return;
209
+ if (id === "current") {
210
+ throw new Error("id cannot be 'current' use other id");
211
+ }
209
212
  const newInstance = new naver.maps.Map(containerRef.current, {
210
213
  ...mapOptions,
211
214
  gl: !disableGL,
@@ -224,8 +227,8 @@ const Map = forwardRef(({ id, mapTypeId = "normal", ncpKeyId, children, mapOptio
224
227
  const handlersRef = useRef({});
225
228
  useEffect(() => {
226
229
  const next = {};
227
- Object.keys(EVENT_TO_PROP$2).forEach((eventKey) => {
228
- const propKey = EVENT_TO_PROP$2[eventKey];
230
+ Object.keys(EVENT_TO_PROP$7).forEach((eventKey) => {
231
+ const propKey = EVENT_TO_PROP$7[eventKey];
229
232
  const fn = eventProps[propKey];
230
233
  if (typeof fn === "function")
231
234
  next[eventKey] = fn;
@@ -241,7 +244,7 @@ const Map = forwardRef(({ id, mapTypeId = "normal", ncpKeyId, children, mapOptio
241
244
  if (!mapInstance)
242
245
  return;
243
246
  const disposers = [];
244
- Object.keys(EVENT_TO_PROP$2).forEach((eventKey) => {
247
+ Object.keys(EVENT_TO_PROP$7).forEach((eventKey) => {
245
248
  const listener = naver.maps.Event.addListener(mapInstance, eventKey, (...args) => {
246
249
  handlersRef.current[eventKey]?.(...args);
247
250
  });
@@ -263,7 +266,7 @@ const Map = forwardRef(({ id, mapTypeId = "normal", ncpKeyId, children, mapOptio
263
266
  Map.displayName = "Map";
264
267
 
265
268
  /* eslint-disable react-hooks/exhaustive-deps */
266
- const EVENT_TO_PROP$1 = {
269
+ const EVENT_TO_PROP$6 = {
267
270
  click: "onClick",
268
271
  position_changed: "onPositionChanged",
269
272
  rightclick: "onRightclick",
@@ -289,7 +292,7 @@ const Marker = forwardRef((props, ref) => {
289
292
  const options = {};
290
293
  Object.keys(props).forEach((key) => {
291
294
  const propKey = key;
292
- if (Object.values(EVENT_TO_PROP$1).includes(propKey)) {
295
+ if (Object.values(EVENT_TO_PROP$6).includes(propKey)) {
293
296
  const value = props[propKey];
294
297
  if (value !== undefined) {
295
298
  eventProps[propKey] = value;
@@ -387,8 +390,8 @@ const Marker = forwardRef((props, ref) => {
387
390
  const handlersRef = useRef({});
388
391
  useEffect(() => {
389
392
  const next = {};
390
- Object.keys(EVENT_TO_PROP$1).forEach((eventKey) => {
391
- const propKey = EVENT_TO_PROP$1[eventKey];
393
+ Object.keys(EVENT_TO_PROP$6).forEach((eventKey) => {
394
+ const propKey = EVENT_TO_PROP$6[eventKey];
392
395
  const fn = eventProps[propKey];
393
396
  if (typeof fn === "function")
394
397
  next[eventKey] = fn;
@@ -399,7 +402,7 @@ const Marker = forwardRef((props, ref) => {
399
402
  if (!instance)
400
403
  return;
401
404
  const disposers = [];
402
- Object.keys(EVENT_TO_PROP$1).forEach((eventKey) => {
405
+ Object.keys(EVENT_TO_PROP$6).forEach((eventKey) => {
403
406
  const listener = naver.maps.Event.addListener(instance, eventKey, (...args) => {
404
407
  handlersRef.current[eventKey]?.(...args);
405
408
  });
@@ -509,47 +512,310 @@ const Overlay = ({ children, ...options }) => {
509
512
  };
510
513
 
511
514
  /* eslint-disable react-hooks/exhaustive-deps */
512
- const Polygon = ({ onClick = () => { }, onMouseEnter = () => { }, ...options }) => {
515
+ const EVENT_TO_PROP$5 = {
516
+ click: "onClick",
517
+ dblclick: "onDblclick",
518
+ rightclick: "onRightclick",
519
+ mousedown: "onMouseDown",
520
+ mouseup: "onMouseUp",
521
+ mouseover: "onMouseOver",
522
+ mouseout: "onMouseOut",
523
+ mouseenter: "onMouseEnter",
524
+ mouseleave: "onMouseLeave",
525
+ };
526
+ const Polygon = forwardRef((props, ref) => {
513
527
  const { current: map } = useMap();
514
- const [instance, setInstance] = useState(null);
515
- const callbacksRef = useRef({});
528
+ const [instance, setInstance] = useState();
529
+ const eventProps = {};
530
+ const options = {};
531
+ Object.keys(props).forEach((key) => {
532
+ const propKey = key;
533
+ if (Object.values(EVENT_TO_PROP$5).includes(propKey)) {
534
+ const value = props[propKey];
535
+ if (value !== undefined) {
536
+ eventProps[propKey] = value;
537
+ }
538
+ }
539
+ else {
540
+ const value = props[key];
541
+ if (value !== undefined) {
542
+ options[key] = value;
543
+ }
544
+ }
545
+ });
516
546
  useEffect(() => {
517
- callbacksRef.current = {
518
- click: onClick,
519
- mouseenter: onMouseEnter,
547
+ if (!map)
548
+ return undefined;
549
+ const newInstance = new naver.maps.Polygon(options);
550
+ setInstance(newInstance);
551
+ newInstance.setMap(map);
552
+ return () => {
553
+ if (!map)
554
+ return;
555
+ newInstance.setMap(null);
520
556
  };
521
- }, [onClick]);
557
+ }, [map]);
522
558
  useEffect(() => {
523
559
  if (!instance)
524
560
  return;
525
- const handlers = [];
526
- Object.entries(callbacksRef.current).forEach(([key, value]) => {
527
- if (!value)
528
- return;
529
- handlers.push(instance.addListener(key, value));
561
+ instance.setOptions(options);
562
+ }, [instance, options]);
563
+ const handlersRef = useRef({});
564
+ useEffect(() => {
565
+ const next = {};
566
+ Object.keys(EVENT_TO_PROP$5).forEach((eventKey) => {
567
+ const propKey = EVENT_TO_PROP$5[eventKey];
568
+ const fn = eventProps[propKey];
569
+ if (typeof fn === "function")
570
+ next[eventKey] = fn;
530
571
  });
572
+ handlersRef.current = next;
573
+ }, [eventProps]);
574
+ useEffect(() => {
575
+ if (!instance)
576
+ return;
577
+ const disposers = [];
578
+ Object.keys(EVENT_TO_PROP$5).forEach((eventKey) => {
579
+ const listener = naver.maps.Event.addListener(instance, eventKey, (...args) => {
580
+ handlersRef.current[eventKey]?.(...args);
581
+ });
582
+ disposers.push(() => naver.maps.Event.removeListener(listener));
583
+ });
584
+ return () => disposers.forEach((d) => d());
585
+ }, [instance]);
586
+ useImperativeHandle(ref, () => instance, [instance]);
587
+ return null;
588
+ });
589
+ Polygon.displayName = "Polygon";
590
+
591
+ /* eslint-disable react-hooks/exhaustive-deps */
592
+ const EVENT_TO_PROP$4 = {
593
+ click: "onClick",
594
+ dblclick: "onDblclick",
595
+ rightclick: "onRightclick",
596
+ mousedown: "onMouseDown",
597
+ mouseup: "onMouseUp",
598
+ mouseover: "onMouseOver",
599
+ mouseout: "onMouseOut",
600
+ mouseenter: "onMouseEnter",
601
+ mouseleave: "onMouseLeave",
602
+ };
603
+ const Polyline = forwardRef((props, ref) => {
604
+ const { current: map } = useMap();
605
+ const [instance, setInstance] = useState();
606
+ const eventProps = {};
607
+ const options = {};
608
+ Object.keys(props).forEach((key) => {
609
+ const propKey = key;
610
+ if (Object.values(EVENT_TO_PROP$4).includes(propKey)) {
611
+ const value = props[propKey];
612
+ if (value !== undefined) {
613
+ eventProps[propKey] = value;
614
+ }
615
+ }
616
+ else {
617
+ const value = props[key];
618
+ if (value !== undefined) {
619
+ options[key] = value;
620
+ }
621
+ }
622
+ });
623
+ useEffect(() => {
624
+ if (!map)
625
+ return undefined;
626
+ const newInstance = new naver.maps.Polyline(options);
627
+ setInstance(newInstance);
628
+ newInstance.setMap(map);
531
629
  return () => {
532
- handlers.forEach((handler) => instance.removeListener(handler));
630
+ if (!map)
631
+ return;
632
+ newInstance.setMap(null);
533
633
  };
634
+ }, [map]);
635
+ const handlersRef = useRef({});
636
+ useEffect(() => {
637
+ const next = {};
638
+ Object.keys(EVENT_TO_PROP$4).forEach((eventKey) => {
639
+ const propKey = EVENT_TO_PROP$4[eventKey];
640
+ const fn = eventProps[propKey];
641
+ if (typeof fn === "function")
642
+ next[eventKey] = fn;
643
+ });
644
+ handlersRef.current = next;
645
+ }, [eventProps]);
646
+ useEffect(() => {
647
+ if (!instance)
648
+ return;
649
+ const disposers = [];
650
+ Object.keys(EVENT_TO_PROP$4).forEach((eventKey) => {
651
+ const listener = naver.maps.Event.addListener(instance, eventKey, (...args) => {
652
+ handlersRef.current[eventKey]?.(...args);
653
+ });
654
+ disposers.push(() => naver.maps.Event.removeListener(listener));
655
+ });
656
+ return () => disposers.forEach((d) => d());
534
657
  }, [instance]);
658
+ useImperativeHandle(ref, () => instance, [instance]);
659
+ return null;
660
+ });
661
+ Polyline.displayName = "Polyline";
662
+
663
+ /* eslint-disable react-hooks/exhaustive-deps */
664
+ const EVENT_TO_PROP$3 = {
665
+ click: "onClick",
666
+ dblclick: "onDblclick",
667
+ rightclick: "onRightclick",
668
+ mousedown: "onMouseDown",
669
+ mouseup: "onMouseUp",
670
+ mouseover: "onMouseOver",
671
+ mouseout: "onMouseOut",
672
+ mouseenter: "onMouseEnter",
673
+ mouseleave: "onMouseLeave",
674
+ };
675
+ const Rectangle = forwardRef((props, ref) => {
676
+ const { current: map } = useMap();
677
+ const [instance, setInstance] = useState();
678
+ const eventProps = {};
679
+ const options = {};
680
+ Object.keys(props).forEach((key) => {
681
+ const propKey = key;
682
+ if (Object.values(EVENT_TO_PROP$3).includes(propKey)) {
683
+ const value = props[propKey];
684
+ if (value !== undefined) {
685
+ eventProps[propKey] = value;
686
+ }
687
+ }
688
+ else {
689
+ const value = props[key];
690
+ if (value !== undefined) {
691
+ options[key] = value;
692
+ }
693
+ }
694
+ });
535
695
  useEffect(() => {
536
696
  if (!map)
537
- return;
538
- const newInstance = new naver.maps.Polygon(options);
539
- newInstance.setMap(map);
697
+ return undefined;
698
+ const newInstance = new naver.maps.Rectangle(options);
540
699
  setInstance(newInstance);
700
+ newInstance.setMap(map);
541
701
  return () => {
702
+ if (!map)
703
+ return;
542
704
  newInstance.setMap(null);
543
705
  };
544
706
  }, [map]);
545
707
  useEffect(() => {
546
- instance?.addListener("click", onClick);
547
- }, []);
708
+ if (!instance)
709
+ return;
710
+ instance.setOptions(options);
711
+ }, [instance, options]);
712
+ const handlersRef = useRef({});
713
+ useEffect(() => {
714
+ const next = {};
715
+ Object.keys(EVENT_TO_PROP$3).forEach((eventKey) => {
716
+ const propKey = EVENT_TO_PROP$3[eventKey];
717
+ const fn = eventProps[propKey];
718
+ if (typeof fn === "function")
719
+ next[eventKey] = fn;
720
+ });
721
+ handlersRef.current = next;
722
+ }, [eventProps]);
723
+ useEffect(() => {
724
+ if (!instance)
725
+ return;
726
+ const disposers = [];
727
+ Object.keys(EVENT_TO_PROP$3).forEach((eventKey) => {
728
+ const listener = naver.maps.Event.addListener(instance, eventKey, (...args) => {
729
+ handlersRef.current[eventKey]?.(...args);
730
+ });
731
+ disposers.push(() => naver.maps.Event.removeListener(listener));
732
+ });
733
+ return () => disposers.forEach((d) => d());
734
+ }, [instance]);
735
+ useImperativeHandle(ref, () => instance, [instance]);
548
736
  return null;
737
+ });
738
+ Rectangle.displayName = "Rectangle";
739
+
740
+ /* eslint-disable react-hooks/exhaustive-deps */
741
+ const EVENT_TO_PROP$2 = {
742
+ click: "onClick",
743
+ dblclick: "onDblclick",
744
+ rightclick: "onRightclick",
745
+ mousedown: "onMouseDown",
746
+ mouseup: "onMouseUp",
747
+ mouseover: "onMouseOver",
748
+ mouseout: "onMouseOut",
749
+ mouseenter: "onMouseEnter",
750
+ mouseleave: "onMouseLeave",
549
751
  };
752
+ const Circle = forwardRef((props, ref) => {
753
+ const { current: map } = useMap();
754
+ const [instance, setInstance] = useState();
755
+ const eventProps = {};
756
+ const options = {};
757
+ Object.keys(props).forEach((key) => {
758
+ const propKey = key;
759
+ if (Object.values(EVENT_TO_PROP$2).includes(propKey)) {
760
+ const value = props[propKey];
761
+ if (value !== undefined) {
762
+ eventProps[propKey] = value;
763
+ }
764
+ }
765
+ else {
766
+ const value = props[key];
767
+ if (value !== undefined) {
768
+ options[key] = value;
769
+ }
770
+ }
771
+ });
772
+ useEffect(() => {
773
+ if (!map)
774
+ return undefined;
775
+ const newInstance = new naver.maps.Circle(options);
776
+ setInstance(newInstance);
777
+ newInstance.setMap(map);
778
+ return () => {
779
+ if (!map)
780
+ return;
781
+ newInstance.setMap(null);
782
+ };
783
+ }, [map]);
784
+ useEffect(() => {
785
+ if (!instance)
786
+ return;
787
+ instance.setOptions(options);
788
+ }, [instance, options]);
789
+ const handlersRef = useRef({});
790
+ useEffect(() => {
791
+ const next = {};
792
+ Object.keys(EVENT_TO_PROP$2).forEach((eventKey) => {
793
+ const propKey = EVENT_TO_PROP$2[eventKey];
794
+ const fn = eventProps[propKey];
795
+ if (typeof fn === "function")
796
+ next[eventKey] = fn;
797
+ });
798
+ handlersRef.current = next;
799
+ }, [eventProps]);
800
+ useEffect(() => {
801
+ if (!instance)
802
+ return;
803
+ const disposers = [];
804
+ Object.keys(EVENT_TO_PROP$2).forEach((eventKey) => {
805
+ const listener = naver.maps.Event.addListener(instance, eventKey, (...args) => {
806
+ handlersRef.current[eventKey]?.(...args);
807
+ });
808
+ disposers.push(() => naver.maps.Event.removeListener(listener));
809
+ });
810
+ return () => disposers.forEach((d) => d());
811
+ }, [instance]);
812
+ useImperativeHandle(ref, () => instance, [instance]);
813
+ return null;
814
+ });
815
+ Circle.displayName = "Circle";
550
816
 
551
817
  /* eslint-disable react-hooks/exhaustive-deps */
552
- const EVENT_TO_PROP = {
818
+ const EVENT_TO_PROP$1 = {
553
819
  click: "onClick",
554
820
  dblclick: "onDblclick",
555
821
  rightclick: "onRightclick",
@@ -560,14 +826,14 @@ const EVENT_TO_PROP = {
560
826
  mouseenter: "onMouseEnter",
561
827
  mouseleave: "onMouseLeave",
562
828
  };
563
- const Polyline = forwardRef((props, ref) => {
829
+ const Ellipse = forwardRef((props, ref) => {
564
830
  const { current: map } = useMap();
565
831
  const [instance, setInstance] = useState();
566
832
  const eventProps = {};
567
833
  const options = {};
568
834
  Object.keys(props).forEach((key) => {
569
835
  const propKey = key;
570
- if (Object.values(EVENT_TO_PROP).includes(propKey)) {
836
+ if (Object.values(EVENT_TO_PROP$1).includes(propKey)) {
571
837
  const value = props[propKey];
572
838
  if (value !== undefined) {
573
839
  eventProps[propKey] = value;
@@ -583,7 +849,7 @@ const Polyline = forwardRef((props, ref) => {
583
849
  useEffect(() => {
584
850
  if (!map)
585
851
  return undefined;
586
- const newInstance = new naver.maps.Polyline(options);
852
+ const newInstance = new naver.maps.Ellipse(options);
587
853
  setInstance(newInstance);
588
854
  newInstance.setMap(map);
589
855
  return () => {
@@ -592,11 +858,16 @@ const Polyline = forwardRef((props, ref) => {
592
858
  newInstance.setMap(null);
593
859
  };
594
860
  }, [map]);
861
+ useEffect(() => {
862
+ if (!instance)
863
+ return;
864
+ instance.setOptions(options);
865
+ }, [instance, options]);
595
866
  const handlersRef = useRef({});
596
867
  useEffect(() => {
597
868
  const next = {};
598
- Object.keys(EVENT_TO_PROP).forEach((eventKey) => {
599
- const propKey = EVENT_TO_PROP[eventKey];
869
+ Object.keys(EVENT_TO_PROP$1).forEach((eventKey) => {
870
+ const propKey = EVENT_TO_PROP$1[eventKey];
600
871
  const fn = eventProps[propKey];
601
872
  if (typeof fn === "function")
602
873
  next[eventKey] = fn;
@@ -607,7 +878,7 @@ const Polyline = forwardRef((props, ref) => {
607
878
  if (!instance)
608
879
  return;
609
880
  const disposers = [];
610
- Object.keys(EVENT_TO_PROP).forEach((eventKey) => {
881
+ Object.keys(EVENT_TO_PROP$1).forEach((eventKey) => {
611
882
  const listener = naver.maps.Event.addListener(instance, eventKey, (...args) => {
612
883
  handlersRef.current[eventKey]?.(...args);
613
884
  });
@@ -618,7 +889,114 @@ const Polyline = forwardRef((props, ref) => {
618
889
  useImperativeHandle(ref, () => instance, [instance]);
619
890
  return null;
620
891
  });
621
- Polyline.displayName = "Polyline";
892
+ Ellipse.displayName = "Ellipse";
893
+
894
+ const EVENT_TO_PROP = {
895
+ addfeature: "onAddFeature",
896
+ removefeature: "onRemoveFeature",
897
+ setfeature: "onSetFeature",
898
+ setgeometry: "onSetGeometry",
899
+ setproperty: "onSetProperty",
900
+ removeproperty: "onRemoveProperty",
901
+ click: "onClick",
902
+ dblclick: "onDblclick",
903
+ rightclick: "onRightclick",
904
+ mousedown: "onMouseDown",
905
+ mouseup: "onMouseUp",
906
+ mouseover: "onMouseOver",
907
+ mouseout: "onMouseOut",
908
+ mousemove: "onMouseMove",
909
+ };
910
+ const DataLayer = ({ type, url, autoStyle = true, style, onLoad, onError, ...eventProps }) => {
911
+ const { current: map } = useMap();
912
+ const [layer, setLayer] = useState();
913
+ const cleanUpFeatures = useMemo(() => {
914
+ return (target) => {
915
+ target.getAllFeature().forEach((feature) => {
916
+ target.removeFeature(feature);
917
+ });
918
+ };
919
+ }, []);
920
+ useEffect(() => {
921
+ if (!map)
922
+ return;
923
+ const newLayer = new naver.maps.Data();
924
+ newLayer.setMap(map);
925
+ setLayer(newLayer);
926
+ return () => {
927
+ newLayer.setMap(null);
928
+ setLayer(undefined);
929
+ };
930
+ }, [map]);
931
+ useEffect(() => {
932
+ if (!layer)
933
+ return;
934
+ const controller = new AbortController();
935
+ const load = async () => {
936
+ try {
937
+ cleanUpFeatures(layer);
938
+ const response = await fetch(url, { signal: controller.signal });
939
+ if (!response.ok) {
940
+ throw new Error(`Failed to fetch data layer: ${response.status}`);
941
+ }
942
+ if (type === "geojson") {
943
+ const geojson = (await response.json());
944
+ if (!controller.signal.aborted) {
945
+ layer.addGeoJson(geojson, autoStyle);
946
+ onLoad?.(layer);
947
+ }
948
+ return;
949
+ }
950
+ const text = await response.text();
951
+ if (controller.signal.aborted)
952
+ return;
953
+ const xml = new DOMParser().parseFromString(text, "text/xml");
954
+ if (type === "gpx")
955
+ layer.addGpx(xml, autoStyle);
956
+ if (type === "kml")
957
+ layer.addKml(xml, autoStyle);
958
+ onLoad?.(layer);
959
+ }
960
+ catch (error) {
961
+ if (controller.signal.aborted)
962
+ return;
963
+ onError?.(error);
964
+ }
965
+ };
966
+ load();
967
+ return () => controller.abort();
968
+ }, [layer, type, url, autoStyle, cleanUpFeatures, onLoad, onError]);
969
+ useEffect(() => {
970
+ if (!layer || style === undefined)
971
+ return;
972
+ layer.setStyle(style);
973
+ }, [layer, style]);
974
+ const handlersRef = useRef({});
975
+ useEffect(() => {
976
+ const next = {};
977
+ Object.keys(EVENT_TO_PROP).forEach((eventKey) => {
978
+ const propKey = EVENT_TO_PROP[eventKey];
979
+ const fn = eventProps[propKey];
980
+ if (typeof fn === "function") {
981
+ next[eventKey] = fn;
982
+ }
983
+ });
984
+ handlersRef.current = next;
985
+ }, [eventProps]);
986
+ useEffect(() => {
987
+ if (!layer)
988
+ return;
989
+ const disposers = [];
990
+ Object.keys(EVENT_TO_PROP).forEach((eventKey) => {
991
+ const listener = naver.maps.Event.addListener(layer, eventKey, (...args) => {
992
+ handlersRef.current[eventKey]?.(...args);
993
+ });
994
+ disposers.push(() => naver.maps.Event.removeListener(listener));
995
+ });
996
+ return () => disposers.forEach((d) => d());
997
+ }, [layer]);
998
+ return null;
999
+ };
622
1000
 
623
- export { Map, MapProvider, Marker, Overlay, Polygon, Polyline, useMap };
1001
+ export { Circle, DataLayer, Ellipse, Map, MapProvider, Marker, Overlay, Polygon, Polyline, Rectangle, useMap };
624
1002
  //# sourceMappingURL=index.esm.js.map