Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

feat: improve dotted map api

Dillion Verma54d27b0296c4
diff --git a/apps/www/public/llms-full.txt b/apps/www/public/llms-full.txt
index f4989dfb..87b33138 100644
--- a/apps/www/public/llms-full.txt
+++ b/apps/www/public/llms-full.txt
@@ -5830,6 +5830,7 @@ Title: Dotted Map
Description: A component with a dotted map.
--- file: magicui/dotted-map.tsx ---
+import * as React from "react"
import { createMap } from "svg-dotted-map"
import { cn } from "@/lib/utils"
@@ -5848,15 +5849,17 @@ export interface DottedMapProps extends React.SVGProps<SVGSVGElement> {
dotColor?: string
markerColor?: string
dotRadius?: number
+ stagger?: boolean
}
export function DottedMap({
width = 150,
height = 75,
- mapSamples = 5500,
+ mapSamples = 5000,
markers = [],
markerColor = "#FF6900",
dotRadius = 0.2,
+ stagger = true,
className,
style,
}: DottedMapProps) {
@@ -5868,16 +5871,43 @@ export function DottedMap({
const processedMarkers = addMarkers(markers)
+ // Compute stagger helpers in a single, simple pass
+ const { xStep, yToRowIndex } = React.useMemo(() => {
+ const sorted = [...points].sort((a, b) => a.y - b.y || a.x - b.x)
+ const rowMap = new Map<number, number>()
+ let step = 0
+ let prevY = Number.NaN
+ let prevXInRow = Number.NaN
+
+ for (const p of sorted) {
+ if (p.y !== prevY) {
+ // new row
+ prevY = p.y
+ prevXInRow = Number.NaN
+ if (!rowMap.has(p.y)) rowMap.set(p.y, rowMap.size)
+ }
+ if (!Number.isNaN(prevXInRow)) {
+ const delta = p.x - prevXInRow
+ if (delta > 0) step = step === 0 ? delta : Math.min(step, delta)
+ }
+ prevXInRow = p.x
+ }
+
+ return { xStep: step || 1, yToRowIndex: rowMap }
+ }, [points])
+
return (
<svg
viewBox={`0 0 ${width} ${height}`}
- className={cn("fill-black dark:fill-white", className)}
+ className={cn("text-gray-500 dark:text-gray-500", className)}
style={{ width: "100%", height: "100%", ...style }}
>
{points.map((point, index) => {
+ const rowIndex = yToRowIndex.get(point.y) ?? 0
+ const offsetX = stagger && rowIndex % 2 === 1 ? xStep / 2 : 0
return (
<circle
- cx={point.x}
+ cx={point.x + offsetX}
cy={point.y}
r={dotRadius}
fill="currentColor"
@@ -5886,9 +5916,11 @@ export function DottedMap({
)
})}
{processedMarkers.map((marker, index) => {
+ const rowIndex = yToRowIndex.get(marker.y) ?? 0
+ const offsetX = stagger && rowIndex % 2 === 1 ? xStep / 2 : 0
return (
<circle
- cx={marker.x}
+ cx={marker.x + offsetX}
cy={marker.y}
r={marker.size ?? dotRadius}
fill={markerColor}
@@ -5907,34 +5939,89 @@ Title: Dotted Map Demo
--- file: example/dotted-map-demo.tsx ---
import { DottedMap } from "@/registry/magicui/dotted-map"
+const markers = [
+ {
+ lat: 40.7128,
+ lng: -74.006,
+ size: 0.3,
+ }, // New York
+ {
+ lat: 34.0522,
+ lng: -118.2437,
+ size: 0.3,
+ }, // Los Angeles
+ {
+ lat: 51.5074,
+ lng: -0.1278,
+ size: 0.3,
+ }, // London
+ {
+ lat: -33.8688,
+ lng: 151.2093,
+ size: 0.3,
+ }, // Sydney
+ {
+ lat: 48.8566,
+ lng: 2.3522,
+ size: 0.3,
+ }, // Paris
+ {
+ lat: 35.6762,
+ lng: 139.6503,
+ size: 0.3,
+ }, // Tokyo
+ {
+ lat: 55.7558,
+ lng: 37.6176,
+ size: 0.3,
+ }, // Moscow
+ {
+ lat: 39.9042,
+ lng: 116.4074,
+ size: 0.3,
+ }, // Beijing
+ {
+ lat: 28.6139,
+ lng: 77.209,
+ size: 0.3,
+ }, // New Delhi
+ {
+ lat: -23.5505,
+ lng: -46.6333,
+ size: 0.3,
+ }, // São Paulo
+ {
+ lat: 1.3521,
+ lng: 103.8198,
+ size: 0.3,
+ }, // Singapore
+ {
+ lat: 25.2048,
+ lng: 55.2708,
+ size: 0.3,
+ }, // Dubai
+ {
+ lat: 52.52,
+ lng: 13.405,
+ size: 0.3,
+ }, // Berlin
+ {
+ lat: 19.4326,
+ lng: -99.1332,
+ size: 0.3,
+ }, // Mexico City
+ {
+ lat: -26.2041,
+ lng: 28.0473,
+ size: 0.3,
+ }, // Johannesburg
+]
+
export default function Component() {
return (
<div className="relative h-[500px] w-full overflow-hidden rounded-lg border">
<div className="to-background absolute inset-0 bg-radial from-transparent to-70%" />
- <DottedMap
- markers={[
- {
- lat: 40.7128,
- lng: -74.006,
- size: 0.5,
- }, // New York
- {
- lat: 34.0522,
- lng: -118.2437,
- size: 0.5,
- }, // Los Angeles
- {
- lat: 51.5074,
- lng: -0.1278,
- size: 0.5,
- }, // London
- {
- lat: -33.8688,
- lng: 151.2093,
- size: 0.5,
- }, // Sydney
- ]}
- />
+ <DottedMap markers={markers} />
</div>
)
}
diff --git a/apps/www/public/r/dotted-map-demo.json b/apps/www/public/r/dotted-map-demo.json
index 683b2c74..6f0c8af4 100644
--- a/apps/www/public/r/dotted-map-demo.json
+++ b/apps/www/public/r/dotted-map-demo.json
@@ -10,7 +10,7 @@
"files": [
{
"path": "registry/example/dotted-map-demo.tsx",
- "content": "import { DottedMap } from \"@/registry/magicui/dotted-map\"\n\nexport default function Component() {\n return (\n <div className=\"relative h-[500px] w-full overflow-hidden rounded-lg border\">\n <div className=\"to-background absolute inset-0 bg-radial from-transparent to-70%\" />\n <DottedMap\n markers={[\n {\n lat: 40.7128,\n lng: -74.006,\n size: 0.5,\n }, // New York\n {\n lat: 34.0522,\n lng: -118.2437,\n size: 0.5,\n }, // Los Angeles\n {\n lat: 51.5074,\n lng: -0.1278,\n size: 0.5,\n }, // London\n {\n lat: -33.8688,\n lng: 151.2093,\n size: 0.5,\n }, // Sydney\n ]}\n />\n </div>\n )\n}\n",
+ "content": "import { DottedMap } from \"@/registry/magicui/dotted-map\"\n\nconst markers = [\n {\n lat: 40.7128,\n lng: -74.006,\n size: 0.3,\n }, // New York\n {\n lat: 34.0522,\n lng: -118.2437,\n size: 0.3,\n }, // Los Angeles\n {\n lat: 51.5074,\n lng: -0.1278,\n size: 0.3,\n }, // London\n {\n lat: -33.8688,\n lng: 151.2093,\n size: 0.3,\n }, // Sydney\n {\n lat: 48.8566,\n lng: 2.3522,\n size: 0.3,\n }, // Paris\n {\n lat: 35.6762,\n lng: 139.6503,\n size: 0.3,\n }, // Tokyo\n {\n lat: 55.7558,\n lng: 37.6176,\n size: 0.3,\n }, // Moscow\n {\n lat: 39.9042,\n lng: 116.4074,\n size: 0.3,\n }, // Beijing\n {\n lat: 28.6139,\n lng: 77.209,\n size: 0.3,\n }, // New Delhi\n {\n lat: -23.5505,\n lng: -46.6333,\n size: 0.3,\n }, // São Paulo\n {\n lat: 1.3521,\n lng: 103.8198,\n size: 0.3,\n }, // Singapore\n {\n lat: 25.2048,\n lng: 55.2708,\n size: 0.3,\n }, // Dubai\n {\n lat: 52.52,\n lng: 13.405,\n size: 0.3,\n }, // Berlin\n {\n lat: 19.4326,\n lng: -99.1332,\n size: 0.3,\n }, // Mexico City\n {\n lat: -26.2041,\n lng: 28.0473,\n size: 0.3,\n }, // Johannesburg\n]\n\nexport default function Component() {\n return (\n <div className=\"relative h-[500px] w-full overflow-hidden rounded-lg border\">\n <div className=\"to-background absolute inset-0 bg-radial from-transparent to-70%\" />\n <DottedMap markers={markers} />\n </div>\n )\n}\n",
"type": "registry:example"
}
]
diff --git a/apps/www/public/r/dotted-map.json b/apps/www/public/r/dotted-map.json
index 9ed2a251..81d83dae 100644
--- a/apps/www/public/r/dotted-map.json
+++ b/apps/www/public/r/dotted-map.json
@@ -10,7 +10,7 @@
"files": [
{
"path": "registry/magicui/dotted-map.tsx",
- "content": "import { createMap } from \"svg-dotted-map\"\n\nimport { cn } from \"@/lib/utils\"\n\ninterface Marker {\n lat: number\n lng: number\n size?: number\n}\n\nexport interface DottedMapProps extends React.SVGProps<SVGSVGElement> {\n width?: number\n height?: number\n mapSamples?: number\n markers?: Marker[]\n dotColor?: string\n markerColor?: string\n dotRadius?: number\n}\n\nexport function DottedMap({\n width = 150,\n height = 75,\n mapSamples = 5500,\n markers = [],\n markerColor = \"#FF6900\",\n dotRadius = 0.2,\n className,\n style,\n}: DottedMapProps) {\n const { points, addMarkers } = createMap({\n width,\n height,\n mapSamples,\n })\n\n const processedMarkers = addMarkers(markers)\n\n return (\n <svg\n viewBox={`0 0 ${width} ${height}`}\n className={cn(\"fill-black dark:fill-white\", className)}\n style={{ width: \"100%\", height: \"100%\", ...style }}\n >\n {points.map((point, index) => {\n return (\n <circle\n cx={point.x}\n cy={point.y}\n r={dotRadius}\n fill=\"currentColor\"\n key={`${point.x}-${point.y}-${index}`}\n />\n )\n })}\n {processedMarkers.map((marker, index) => {\n return (\n <circle\n cx={marker.x}\n cy={marker.y}\n r={marker.size ?? dotRadius}\n fill={markerColor}\n key={`${marker.x}-${marker.y}-${index}`}\n />\n )\n })}\n </svg>\n )\n}\n",
+ "content": "import * as React from \"react\"\nimport { createMap } from \"svg-dotted-map\"\n\nimport { cn } from \"@/lib/utils\"\n\ninterface Marker {\n lat: number\n lng: number\n size?: number\n}\n\nexport interface DottedMapProps extends React.SVGProps<SVGSVGElement> {\n width?: number\n height?: number\n mapSamples?: number\n markers?: Marker[]\n dotColor?: string\n markerColor?: string\n dotRadius?: number\n stagger?: boolean\n}\n\nexport function DottedMap({\n width = 150,\n height = 75,\n mapSamples = 5000,\n markers = [],\n markerColor = \"#FF6900\",\n dotRadius = 0.2,\n stagger = true,\n className,\n style,\n}: DottedMapProps) {\n const { points, addMarkers } = createMap({\n width,\n height,\n mapSamples,\n })\n\n const processedMarkers = addMarkers(markers)\n\n // Compute stagger helpers in a single, simple pass\n const { xStep, yToRowIndex } = React.useMemo(() => {\n const sorted = [...points].sort((a, b) => a.y - b.y || a.x - b.x)\n const rowMap = new Map<number, number>()\n let step = 0\n let prevY = Number.NaN\n let prevXInRow = Number.NaN\n\n for (const p of sorted) {\n if (p.y !== prevY) {\n // new row\n prevY = p.y\n prevXInRow = Number.NaN\n if (!rowMap.has(p.y)) rowMap.set(p.y, rowMap.size)\n }\n if (!Number.isNaN(prevXInRow)) {\n const delta = p.x - prevXInRow\n if (delta > 0) step = step === 0 ? delta : Math.min(step, delta)\n }\n prevXInRow = p.x\n }\n\n return { xStep: step || 1, yToRowIndex: rowMap }\n }, [points])\n\n return (\n <svg\n viewBox={`0 0 ${width} ${height}`}\n className={cn(\"text-gray-500 dark:text-gray-500\", className)}\n style={{ width: \"100%\", height: \"100%\", ...style }}\n >\n {points.map((point, index) => {\n const rowIndex = yToRowIndex.get(point.y) ?? 0\n const offsetX = stagger && rowIndex % 2 === 1 ? xStep / 2 : 0\n return (\n <circle\n cx={point.x + offsetX}\n cy={point.y}\n r={dotRadius}\n fill=\"currentColor\"\n key={`${point.x}-${point.y}-${index}`}\n />\n )\n })}\n {processedMarkers.map((marker, index) => {\n const rowIndex = yToRowIndex.get(marker.y) ?? 0\n const offsetX = stagger && rowIndex % 2 === 1 ? xStep / 2 : 0\n return (\n <circle\n cx={marker.x + offsetX}\n cy={marker.y}\n r={marker.size ?? dotRadius}\n fill={markerColor}\n key={`${marker.x}-${marker.y}-${index}`}\n />\n )\n })}\n </svg>\n )\n}\n",
"type": "registry:ui"
}
]
diff --git a/apps/www/registry/example/dotted-map-demo.tsx b/apps/www/registry/example/dotted-map-demo.tsx
index d532fe83..f7836f03 100644
--- a/apps/www/registry/example/dotted-map-demo.tsx
+++ b/apps/www/registry/example/dotted-map-demo.tsx
@@ -1,33 +1,88 @@
import { DottedMap } from "@/registry/magicui/dotted-map"
+const markers = [
+ {
+ lat: 40.7128,
+ lng: -74.006,
+ size: 0.3,
+ }, // New York
+ {
+ lat: 34.0522,
+ lng: -118.2437,
+ size: 0.3,
+ }, // Los Angeles
+ {
+ lat: 51.5074,
+ lng: -0.1278,
+ size: 0.3,
+ }, // London
+ {
+ lat: -33.8688,
+ lng: 151.2093,
+ size: 0.3,
+ }, // Sydney
+ {
+ lat: 48.8566,
+ lng: 2.3522,
+ size: 0.3,
+ }, // Paris
+ {
+ lat: 35.6762,
+ lng: 139.6503,
+ size: 0.3,
+ }, // Tokyo
+ {
+ lat: 55.7558,
+ lng: 37.6176,
+ size: 0.3,
+ }, // Moscow
+ {
+ lat: 39.9042,
+ lng: 116.4074,
+ size: 0.3,
+ }, // Beijing
+ {
+ lat: 28.6139,
+ lng: 77.209,
+ size: 0.3,
+ }, // New Delhi
+ {
+ lat: -23.5505,
+ lng: -46.6333,
+ size: 0.3,
+ }, // São Paulo
+ {
+ lat: 1.3521,
+ lng: 103.8198,
+ size: 0.3,
+ }, // Singapore
+ {
+ lat: 25.2048,
+ lng: 55.2708,
+ size: 0.3,
+ }, // Dubai
+ {
+ lat: 52.52,
+ lng: 13.405,
+ size: 0.3,
+ }, // Berlin
+ {
+ lat: 19.4326,
+ lng: -99.1332,
+ size: 0.3,
+ }, // Mexico City
+ {
+ lat: -26.2041,
+ lng: 28.0473,
+ size: 0.3,
+ }, // Johannesburg
+]
+
export default function Component() {
return (
<div className="relative h-[500px] w-full overflow-hidden rounded-lg border">
<div className="to-background absolute inset-0 bg-radial from-transparent to-70%" />
- <DottedMap
- markers={[
- {
- lat: 40.7128,
- lng: -74.006,
- size: 0.5,
- }, // New York
- {
- lat: 34.0522,
- lng: -118.2437,
- size: 0.5,
- }, // Los Angeles
- {
- lat: 51.5074,
- lng: -0.1278,
- size: 0.5,
- }, // London
- {
- lat: -33.8688,
- lng: 151.2093,
- size: 0.5,
- }, // Sydney
- ]}
- />
+ <DottedMap markers={markers} />
</div>
)
}
diff --git a/apps/www/registry/magicui/dotted-map.tsx b/apps/www/registry/magicui/dotted-map.tsx
index 43482720..c8e1339e 100644
--- a/apps/www/registry/magicui/dotted-map.tsx
+++ b/apps/www/registry/magicui/dotted-map.tsx
@@ -1,3 +1,4 @@
+import * as React from "react"
import { createMap } from "svg-dotted-map"
import { cn } from "@/lib/utils"
@@ -16,15 +17,17 @@ export interface DottedMapProps extends React.SVGProps<SVGSVGElement> {
dotColor?: string
markerColor?: string
dotRadius?: number
+ stagger?: boolean
}
export function DottedMap({
width = 150,
height = 75,
- mapSamples = 5500,
+ mapSamples = 5000,
markers = [],
markerColor = "#FF6900",
dotRadius = 0.2,
+ stagger = true,
className,
style,
}: DottedMapProps) {
@@ -36,16 +39,43 @@ export function DottedMap({
const processedMarkers = addMarkers(markers)
+ // Compute stagger helpers in a single, simple pass
+ const { xStep, yToRowIndex } = React.useMemo(() => {
+ const sorted = [...points].sort((a, b) => a.y - b.y || a.x - b.x)
+ const rowMap = new Map<number, number>()
+ let step = 0
+ let prevY = Number.NaN
+ let prevXInRow = Number.NaN
+
+ for (const p of sorted) {
+ if (p.y !== prevY) {
+ // new row
+ prevY = p.y
+ prevXInRow = Number.NaN
+ if (!rowMap.has(p.y)) rowMap.set(p.y, rowMap.size)
+ }
+ if (!Number.isNaN(prevXInRow)) {
+ const delta = p.x - prevXInRow
+ if (delta > 0) step = step === 0 ? delta : Math.min(step, delta)
+ }
+ prevXInRow = p.x
+ }
+
+ return { xStep: step || 1, yToRowIndex: rowMap }
+ }, [points])
+
return (
<svg
viewBox={`0 0 ${width} ${height}`}
- className={cn("fill-black dark:fill-white", className)}
+ className={cn("text-gray-500 dark:text-gray-500", className)}
style={{ width: "100%", height: "100%", ...style }}
>
{points.map((point, index) => {
+ const rowIndex = yToRowIndex.get(point.y) ?? 0
+ const offsetX = stagger && rowIndex % 2 === 1 ? xStep / 2 : 0
return (
<circle
- cx={point.x}
+ cx={point.x + offsetX}
cy={point.y}
r={dotRadius}
fill="currentColor"
@@ -54,9 +84,11 @@ export function DottedMap({
)
})}
{processedMarkers.map((marker, index) => {
+ const rowIndex = yToRowIndex.get(marker.y) ?? 0
+ const offsetX = stagger && rowIndex % 2 === 1 ? xStep / 2 : 0
return (
<circle
- cx={marker.x}
+ cx={marker.x + offsetX}
cy={marker.y}
r={marker.size ?? dotRadius}
fill={markerColor}