Skip to content

armgit / mapcn

Public Imported from GitHub
0

Beautiful map components. 100% Free, Zero config, one command setup.

TypeScript

Update: Improved rerendering

Anmoldeep Singhbb1524204e6f
diff --git a/public/map-preview.png b/public/map-preview.png
deleted file mode 100644
index e298902..0000000
Binary files a/public/map-preview.png and /dev/null differ
diff --git a/src/app/globals.css b/src/app/globals.css
index a358b36..0b37aaf 100644
--- a/src/app/globals.css
+++ b/src/app/globals.css
@@ -118,7 +118,7 @@
@layer base {
* {
- @apply border-border outline-ring/50;
+ @apply border-border outline-ring/50 overscroll-none;
}
body {
@apply bg-background text-foreground font-sans;
diff --git a/src/app/layout.tsx b/src/app/layout.tsx
index fac210c..372b275 100644
--- a/src/app/layout.tsx
+++ b/src/app/layout.tsx
@@ -68,12 +68,21 @@ export const metadata: Metadata = {
siteName: siteName,
title: "mapcn - Beautiful maps made simple",
description: siteDescription,
+ images: [
+ {
+ url: "/banner.png",
+ width: 1200,
+ height: 630,
+ alt: "mapcn - Beautiful maps, made simple",
+ },
+ ],
},
twitter: {
card: "summary_large_image",
title: "mapcn - Beautiful maps made simple",
description: siteDescription,
creator: creator,
+ images: ["/banner.png"],
},
alternates: {
canonical: siteUrl,
diff --git a/src/app/opengraph-image.tsx b/src/app/opengraph-image.tsx
deleted file mode 100644
index 754f314..0000000
--- a/src/app/opengraph-image.tsx
+++ /dev/null
@@ -1,90 +0,0 @@
-import { ImageResponse } from "next/og";
-export const runtime = "edge";
-
-export const alt = "mapcn - Beautiful maps, made simple";
-export const size = {
- width: 1200,
- height: 630,
-};
-export const contentType = "image/png";
-
-const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || "http://localhost:3000";
-
-export default async function Image() {
- return new ImageResponse(
- (
- <div
- style={{
- display: "flex",
- flexDirection: "column",
- alignItems: "center",
- justifyContent: "flex-start",
- position: "relative",
- width: "100%",
- height: "100%",
- background:
- "linear-gradient(135deg, rgb(3, 7, 30) 0%, rgb(15, 52, 96) 50%, rgb(56, 189, 248) 100%)",
- paddingTop: "60px",
- }}
- >
- {/* Title Section */}
- <div
- style={{
- display: "flex",
- flexDirection: "column",
- alignItems: "center",
- }}
- >
- <div
- style={{
- fontSize: "72px",
- fontWeight: 700,
- color: "#ffffff",
- letterSpacing: "-2px",
- }}
- >
- mapcn
- </div>
- <div
- style={{
- fontSize: "32px",
- fontWeight: 400,
- color: "rgba(186, 230, 253, 0.95)",
- letterSpacing: "-0.5px",
- }}
- >
- Beautiful maps, made simple
- </div>
- </div>
-
- <div
- style={{
- position: "absolute",
- bottom: 0,
- left: "50%",
- transform: "translateX(-50%) translateY(10%)",
- display: "flex",
- justifyContent: "center",
- overflow: "hidden",
- borderRadius: "20px",
- boxShadow: "0 25px 50px -12px rgba(0, 0, 0, 0.4)",
- width: "880px",
- height: "420px",
- }}
- >
- <img
- src={`${siteUrl}/map-preview.png`}
- alt="Map Preview"
- style={{
- objectFit: "contain",
- objectPosition: "bottom",
- }}
- />
- </div>
- </div>
- ),
- {
- ...size,
- }
- );
-}
diff --git a/src/registry/map.tsx b/src/registry/map.tsx
index a9f77f9..b679041 100644
--- a/src/registry/map.tsx
+++ b/src/registry/map.tsx
@@ -5,6 +5,7 @@ import "maplibre-gl/dist/maplibre-gl.css";
import { useTheme } from "next-themes";
import {
createContext,
+ useCallback,
useContext,
useEffect,
useId,
@@ -181,6 +182,7 @@ function MapMarker({
const markerRef = useRef<MapLibreGL.Marker | null>(null);
const markerElementRef = useRef<HTMLDivElement | null>(null);
const [isReady, setIsReady] = useState(false);
+ const markerOptionsRef = useRef(markerOptions);
useEffect(() => {
if (!isLoaded || !map) return;
@@ -198,9 +200,13 @@ function MapMarker({
markerRef.current = marker;
- if (onClick) container.addEventListener("click", onClick);
- if (onMouseEnter) container.addEventListener("mouseenter", onMouseEnter);
- if (onMouseLeave) container.addEventListener("mouseleave", onMouseLeave);
+ const handleClick = (e: MouseEvent) => onClick?.(e);
+ const handleMouseEnter = (e: MouseEvent) => onMouseEnter?.(e);
+ const handleMouseLeave = (e: MouseEvent) => onMouseLeave?.(e);
+
+ container.addEventListener("click", handleClick);
+ container.addEventListener("mouseenter", handleMouseEnter);
+ container.addEventListener("mouseleave", handleMouseLeave);
const handleDragStart = () => {
const lngLat = marker.getLngLat();
@@ -215,31 +221,27 @@ function MapMarker({
onDragEnd?.({ lng: lngLat.lng, lat: lngLat.lat });
};
- if (draggable) {
- marker.on("dragstart", handleDragStart);
- marker.on("drag", handleDrag);
- marker.on("dragend", handleDragEnd);
- }
+ marker.on("dragstart", handleDragStart);
+ marker.on("drag", handleDrag);
+ marker.on("dragend", handleDragEnd);
setIsReady(true);
return () => {
- if (onClick) container.removeEventListener("click", onClick);
- if (onMouseEnter)
- container.removeEventListener("mouseenter", onMouseEnter);
- if (onMouseLeave)
- container.removeEventListener("mouseleave", onMouseLeave);
- if (draggable) {
- marker.off("dragstart", handleDragStart);
- marker.off("drag", handleDrag);
- marker.off("dragend", handleDragEnd);
- }
+ container.removeEventListener("click", handleClick);
+ container.removeEventListener("mouseenter", handleMouseEnter);
+ container.removeEventListener("mouseleave", handleMouseLeave);
+
+ marker.off("dragstart", handleDragStart);
+ marker.off("drag", handleDrag);
+ marker.off("dragend", handleDragEnd);
+
marker.remove();
markerRef.current = null;
markerElementRef.current = null;
setIsReady(false);
};
- }, [isLoaded]);
+ }, [map, isLoaded]);
useEffect(() => {
markerRef.current?.setLngLat([longitude, latitude]);
@@ -249,6 +251,30 @@ function MapMarker({
markerRef.current?.setDraggable(draggable);
}, [draggable]);
+ useEffect(() => {
+ if (!markerRef.current) return;
+ const prev = markerOptionsRef.current;
+
+ if (prev.offset !== markerOptions.offset) {
+ markerRef.current.setOffset(markerOptions.offset ?? [0, 0]);
+ }
+ if (prev.rotation !== markerOptions.rotation) {
+ markerRef.current.setRotation(markerOptions.rotation ?? 0);
+ }
+ if (prev.rotationAlignment !== markerOptions.rotationAlignment) {
+ markerRef.current.setRotationAlignment(
+ markerOptions.rotationAlignment ?? "auto"
+ );
+ }
+ if (prev.pitchAlignment !== markerOptions.pitchAlignment) {
+ markerRef.current.setPitchAlignment(
+ markerOptions.pitchAlignment ?? "auto"
+ );
+ }
+
+ markerOptionsRef.current = markerOptions;
+ }, [markerOptions]);
+
return (
<MarkerContext.Provider
value={{ markerRef, markerElementRef, map, isReady }}
@@ -298,6 +324,7 @@ function MarkerPopup({
const containerRef = useRef<HTMLDivElement | null>(null);
const popupRef = useRef<MapLibreGL.Popup | null>(null);
const [mounted, setMounted] = useState(false);
+ const popupOptionsRef = useRef(popupOptions);
useEffect(() => {
if (!isReady || !markerRef.current) return;
@@ -325,6 +352,20 @@ function MarkerPopup({
};
}, [isReady]);
+ useEffect(() => {
+ if (!popupRef.current) return;
+ const prev = popupOptionsRef.current;
+
+ if (prev.offset !== popupOptions.offset) {
+ popupRef.current.setOffset(popupOptions.offset ?? 16);
+ }
+ if (prev.maxWidth !== popupOptions.maxWidth && popupOptions.maxWidth) {
+ popupRef.current.setMaxWidth(popupOptions.maxWidth ?? "none");
+ }
+
+ popupOptionsRef.current = popupOptions;
+ }, [popupOptions]);
+
const handleClose = () => popupRef.current?.remove();
if (!mounted || !containerRef.current) return null;
@@ -367,6 +408,7 @@ function MarkerTooltip({
const containerRef = useRef<HTMLDivElement | null>(null);
const popupRef = useRef<MapLibreGL.Popup | null>(null);
const [mounted, setMounted] = useState(false);
+ const popupOptionsRef = useRef(popupOptions);
useEffect(() => {
if (!isReady || !markerRef.current || !markerElementRef.current || !map)
@@ -408,6 +450,20 @@ function MarkerTooltip({
};
}, [isReady, map]);
+ useEffect(() => {
+ if (!popupRef.current) return;
+ const prev = popupOptionsRef.current;
+
+ if (prev.offset !== popupOptions.offset) {
+ popupRef.current.setOffset(popupOptions.offset ?? 16);
+ }
+ if (prev.maxWidth !== popupOptions.maxWidth && popupOptions.maxWidth) {
+ popupRef.current.setMaxWidth(popupOptions.maxWidth ?? "none");
+ }
+
+ popupOptionsRef.current = popupOptions;
+ }, [popupOptions]);
+
if (!mounted || !containerRef.current) return null;
return createPortal(
@@ -517,30 +573,33 @@ function MapControls({
const { map, isLoaded } = useMap();
const [waitingForLocation, setWaitingForLocation] = useState(false);
- if (!isLoaded) return null;
+ const handleZoomIn = useCallback(() => {
+ map?.zoomTo(map.getZoom() + 1, { duration: 300 });
+ }, [map]);
- const handleZoomIn = () => map?.zoomTo(map.getZoom() + 1, { duration: 300 });
- const handleZoomOut = () => map?.zoomTo(map.getZoom() - 1, { duration: 300 });
- const handleResetBearing = () => map?.resetNorthPitch({ duration: 300 });
+ const handleZoomOut = useCallback(() => {
+ map?.zoomTo(map.getZoom() - 1, { duration: 300 });
+ }, [map]);
+
+ const handleResetBearing = useCallback(() => {
+ map?.resetNorthPitch({ duration: 300 });
+ }, [map]);
- const handleLocate = () => {
+ const handleLocate = useCallback(() => {
setWaitingForLocation(true);
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(
- (position) => {
+ (pos) => {
const coords = {
- longitude: position.coords.longitude,
- latitude: position.coords.latitude,
+ longitude: pos.coords.longitude,
+ latitude: pos.coords.latitude,
};
map?.flyTo({
center: [coords.longitude, coords.latitude],
zoom: 14,
duration: 1500,
});
- onLocate?.({
- longitude: coords.longitude,
- latitude: coords.latitude,
- });
+ onLocate?.(coords);
setWaitingForLocation(false);
},
(error) => {
@@ -549,9 +608,9 @@ function MapControls({
}
);
}
- };
+ }, [map, onLocate]);
- const handleFullscreen = () => {
+ const handleFullscreen = useCallback(() => {
const container = map?.getContainer();
if (!container) return;
if (document.fullscreenElement) {
@@ -559,7 +618,9 @@ function MapControls({
} else {
container.requestFullscreen();
}
- };
+ }, [map]);
+
+ if (!isLoaded) return null;
return (
<div
@@ -610,7 +671,7 @@ function MapControls({
);
}
-function CompassButton({ onClick }: { onClick?: () => void }) {
+function CompassButton({ onClick }: { onClick: () => void }) {
const { isLoaded, map } = useMap();
const compassRef = useRef<SVGSVGElement>(null);
@@ -636,12 +697,7 @@ function CompassButton({ onClick }: { onClick?: () => void }) {
}, [isLoaded, map]);
return (
- <button
- onClick={onClick}
- aria-label="Reset bearing to north"
- type="button"
- className="flex items-center justify-center size-8 hover:bg-accent transition-colors"
- >
+ <ControlButton onClick={onClick} label="Reset bearing to north">
<svg
ref={compassRef}
viewBox="0 0 24 24"
@@ -653,7 +709,7 @@ function CompassButton({ onClick }: { onClick?: () => void }) {
<path d="M12 22L16 12H12V22Z" className="fill-muted-foreground/60" />
<path d="M12 22L8 12H12V22Z" className="fill-muted-foreground/30" />
</svg>
- </button>
+ </ControlButton>
);
}
@@ -677,6 +733,7 @@ function MapPopup({
}: MapPopupProps) {
const { map } = useMap();
const popupRef = useRef<MapLibreGL.Popup | null>(null);
+ const popupOptionsRef = useRef(popupOptions);
const container = useMemo(() => document.createElement("div"), []);
@@ -684,7 +741,7 @@ function MapPopup({
if (!map) return;
const popup = new MapLibreGL.Popup({
- offset: 12,
+ offset: 16,
...popupOptions,
closeButton: false,
})
@@ -712,6 +769,20 @@ function MapPopup({
popupRef.current?.setLngLat([longitude, latitude]);
}, [longitude, latitude]);
+ useEffect(() => {
+ if (!popupRef.current) return;
+ const prev = popupOptionsRef.current;
+
+ if (prev.offset !== popupOptions.offset) {
+ popupRef.current.setOffset(popupOptions.offset ?? 16);
+ }
+ if (prev.maxWidth !== popupOptions.maxWidth && popupOptions.maxWidth) {
+ popupRef.current.setMaxWidth(popupOptions.maxWidth ?? "none");
+ }
+
+ popupOptionsRef.current = popupOptions;
+ }, [popupOptions]);
+
const handleClose = () => {
popupRef.current?.remove();
onClose?.();
@@ -761,37 +832,31 @@ function MapRoute({
const sourceId = `route-source-${id}`;
const layerId = `route-layer-${id}`;
+ // Add source and layer on mount
useEffect(() => {
- if (!isLoaded || !map || coordinates.length < 2) return;
-
- const addRoute = () => {
- if (map.getLayer(layerId)) map.removeLayer(layerId);
- if (map.getSource(sourceId)) map.removeSource(sourceId);
-
- map.addSource(sourceId, {
- type: "geojson",
- data: {
- type: "Feature",
- properties: {},
- geometry: { type: "LineString", coordinates },
- },
- });
+ if (!isLoaded || !map) return;
- map.addLayer({
- id: layerId,
- type: "line",
- source: sourceId,
- layout: { "line-join": "round", "line-cap": "round" },
- paint: {
- "line-color": color,
- "line-width": width,
- "line-opacity": opacity,
- ...(dashArray && { "line-dasharray": dashArray }),
- },
- });
- };
+ map.addSource(sourceId, {
+ type: "geojson",
+ data: {
+ type: "Feature",
+ properties: {},
+ geometry: { type: "LineString", coordinates: [] },
+ },
+ });
- addRoute();
+ map.addLayer({
+ id: layerId,
+ type: "line",
+ source: sourceId,
+ layout: { "line-join": "round", "line-cap": "round" },
+ paint: {
+ "line-color": color,
+ "line-width": width,
+ "line-opacity": opacity,
+ ...(dashArray && { "line-dasharray": dashArray }),
+ },
+ });
return () => {
try {
@@ -801,17 +866,32 @@ function MapRoute({
// ignore
}
};
- }, [
- isLoaded,
- map,
- coordinates,
- color,
- width,
- opacity,
- dashArray,
- sourceId,
- layerId,
- ]);
+ }, [isLoaded, map, sourceId, layerId]);
+
+ // When coordinates change, update the source data
+ useEffect(() => {
+ if (!isLoaded || !map || coordinates.length < 2) return;
+
+ const source = map.getSource(sourceId) as MapLibreGL.GeoJSONSource;
+ if (source) {
+ source.setData({
+ type: "Feature",
+ properties: {},
+ geometry: { type: "LineString", coordinates },
+ });
+ }
+ }, [isLoaded, map, coordinates, sourceId]);
+
+ useEffect(() => {
+ if (!isLoaded || !map || !map.getLayer(layerId)) return;
+
+ map.setPaintProperty(layerId, "line-color", color);
+ map.setPaintProperty(layerId, "line-width", width);
+ map.setPaintProperty(layerId, "line-opacity", opacity);
+ if (dashArray) {
+ map.setPaintProperty(layerId, "line-dasharray", dashArray);
+ }
+ }, [isLoaded, map, layerId, color, width, opacity, dashArray]);
return null;
}