Skip to content

armgit / mapcn

Public Imported from GitHub
0

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

TypeScript

feat(blocks): static grid thumbnails and whole-card links

Anmol9f5fe0a7278a
diff --git a/package-lock.json b/package-lock.json
index 11b51e8..dc956cb 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -31,6 +31,7 @@
"@types/react-dom": "^19",
"eslint": "^9",
"eslint-config-next": "^16.2.7",
+ "playwright": "^1.64.1",
"prettier-plugin-tailwindcss": "^0.7.2",
"shadcn": "^3.6.2",
"tailwindcss": "^4",
@@ -10319,6 +10320,35 @@
"node": ">=16.20.0"
}
},
+ "node_modules/playwright": {
+ "version": "1.64.1",
+ "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.64.1.tgz",
+ "integrity": "sha512-HCrVhHr5QQuwYoswEUGTb7gGp522yflbv/ST8nF4sFWGuLjQCt17Q/EubpGGSNKYn1tsTCPZxqHhHCfVURu7UA==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "dependencies": {
+ "playwright-core": "1.64.1"
+ },
+ "bin": {
+ "playwright": "cli.js"
+ },
+ "engines": {
+ "node": ">=20"
+ }
+ },
+ "node_modules/playwright-core": {
+ "version": "1.64.1",
+ "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.64.1.tgz",
+ "integrity": "sha512-BC4s8y6SEVA6Q1rSxZTto8hI1KUI3xXlHGZgvGqP4IqPdI/a6FX9H29IDNz7vKo9l21b6LfYosScPL6M3xxyDQ==",
+ "dev": true,
+ "license": "Apache-2.0",
+ "bin": {
+ "playwright-core": "cli.js"
+ },
+ "engines": {
+ "node": ">=20"
+ }
+ },
"node_modules/possible-typed-array-names": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/possible-typed-array-names/-/possible-typed-array-names-1.1.0.tgz",
diff --git a/package.json b/package.json
index 8cde8b0..21f9307 100644
--- a/package.json
+++ b/package.json
@@ -7,7 +7,8 @@
"build": "next build",
"start": "next start",
"lint": "eslint",
- "registry:build": "npx shadcn build -o ./public/r && npx tsx scripts/fix-registry-imports.ts"
+ "registry:build": "npx shadcn build -o ./public/r && npx tsx scripts/fix-registry-imports.ts",
+ "blocks:thumbnails": "npx tsx scripts/capture-block-thumbnails.ts"
},
"dependencies": {
"@vercel/analytics": "^1.6.1",
@@ -33,6 +34,7 @@
"@types/react-dom": "^19",
"eslint": "^9",
"eslint-config-next": "^16.2.7",
+ "playwright": "^1.64.1",
"prettier-plugin-tailwindcss": "^0.7.2",
"shadcn": "^3.6.2",
"tailwindcss": "^4",
diff --git a/public/blocks/analytics-card-dark.jpg b/public/blocks/analytics-card-dark.jpg
new file mode 100644
index 0000000..c17d9a9
Binary files /dev/null and b/public/blocks/analytics-card-dark.jpg differ
diff --git a/public/blocks/analytics-card-light.jpg b/public/blocks/analytics-card-light.jpg
new file mode 100644
index 0000000..43bbee7
Binary files /dev/null and b/public/blocks/analytics-card-light.jpg differ
diff --git a/public/blocks/analytics-map-dark.jpg b/public/blocks/analytics-map-dark.jpg
new file mode 100644
index 0000000..567a3dc
Binary files /dev/null and b/public/blocks/analytics-map-dark.jpg differ
diff --git a/public/blocks/analytics-map-light.jpg b/public/blocks/analytics-map-light.jpg
new file mode 100644
index 0000000..3fd0875
Binary files /dev/null and b/public/blocks/analytics-map-light.jpg differ
diff --git a/public/blocks/choropleth-dark.jpg b/public/blocks/choropleth-dark.jpg
new file mode 100644
index 0000000..c350ebf
Binary files /dev/null and b/public/blocks/choropleth-dark.jpg differ
diff --git a/public/blocks/choropleth-light.jpg b/public/blocks/choropleth-light.jpg
new file mode 100644
index 0000000..d1e5891
Binary files /dev/null and b/public/blocks/choropleth-light.jpg differ
diff --git a/public/blocks/delivery-tracker-dark.jpg b/public/blocks/delivery-tracker-dark.jpg
new file mode 100644
index 0000000..0f883e6
Binary files /dev/null and b/public/blocks/delivery-tracker-dark.jpg differ
diff --git a/public/blocks/delivery-tracker-light.jpg b/public/blocks/delivery-tracker-light.jpg
new file mode 100644
index 0000000..b40269d
Binary files /dev/null and b/public/blocks/delivery-tracker-light.jpg differ
diff --git a/public/blocks/heatmap-dark.jpg b/public/blocks/heatmap-dark.jpg
new file mode 100644
index 0000000..d38f3ae
Binary files /dev/null and b/public/blocks/heatmap-dark.jpg differ
diff --git a/public/blocks/heatmap-light.jpg b/public/blocks/heatmap-light.jpg
new file mode 100644
index 0000000..b735f2c
Binary files /dev/null and b/public/blocks/heatmap-light.jpg differ
diff --git a/public/blocks/logistics-network-dark.jpg b/public/blocks/logistics-network-dark.jpg
new file mode 100644
index 0000000..21ae1ca
Binary files /dev/null and b/public/blocks/logistics-network-dark.jpg differ
diff --git a/public/blocks/logistics-network-light.jpg b/public/blocks/logistics-network-light.jpg
new file mode 100644
index 0000000..49b8785
Binary files /dev/null and b/public/blocks/logistics-network-light.jpg differ
diff --git a/public/blocks/store-locator-dark.jpg b/public/blocks/store-locator-dark.jpg
new file mode 100644
index 0000000..ea89074
Binary files /dev/null and b/public/blocks/store-locator-dark.jpg differ
diff --git a/public/blocks/store-locator-light.jpg b/public/blocks/store-locator-light.jpg
new file mode 100644
index 0000000..d08fd60
Binary files /dev/null and b/public/blocks/store-locator-light.jpg differ
diff --git a/public/blocks/uptime-monitor-dark.jpg b/public/blocks/uptime-monitor-dark.jpg
new file mode 100644
index 0000000..05e207d
Binary files /dev/null and b/public/blocks/uptime-monitor-dark.jpg differ
diff --git a/public/blocks/uptime-monitor-light.jpg b/public/blocks/uptime-monitor-light.jpg
new file mode 100644
index 0000000..4ae69ea
Binary files /dev/null and b/public/blocks/uptime-monitor-light.jpg differ
diff --git a/scripts/capture-block-thumbnails.ts b/scripts/capture-block-thumbnails.ts
new file mode 100644
index 0000000..0c732e6
--- /dev/null
+++ b/scripts/capture-block-thumbnails.ts
@@ -0,0 +1,53 @@
+import { mkdir } from "fs/promises";
+import path from "path";
+import { chromium } from "playwright";
+import registry from "../registry.json";
+
+const baseUrl = process.env.BASE_URL ?? "http://localhost:3000";
+const outDir = path.join(process.cwd(), "public", "blocks");
+const viewport = { width: 1280, height: 800 };
+const schemes = ["light", "dark"] as const;
+
+const only = process.argv.slice(2);
+const blocks = registry.items
+ .filter((item) => item.type === "registry:block")
+ .map((item) => item.name)
+ .filter((name) => only.length === 0 || only.includes(name));
+
+async function main() {
+ await mkdir(outDir, { recursive: true });
+ const browser = await chromium.launch();
+
+ for (const scheme of schemes) {
+ const context = await browser.newContext({
+ viewport,
+ colorScheme: scheme,
+ });
+ const page = await context.newPage();
+
+ for (const name of blocks) {
+ await page.goto(`${baseUrl}/view/${name}`, { waitUntil: "load" });
+ await page.addStyleTag({
+ content: "nextjs-portal { display: none !important; }",
+ });
+ await page.waitForSelector(".maplibregl-canvas", { timeout: 15_000 });
+ await page
+ .waitForLoadState("networkidle", { timeout: 10_000 })
+ .catch(() => {});
+ await page.waitForTimeout(1_500);
+
+ const file = path.join(outDir, `${name}-${scheme}.jpg`);
+ await page.screenshot({ path: file, type: "jpeg", quality: 85 });
+ console.log(`✓ ${path.relative(process.cwd(), file)}`);
+ }
+
+ await context.close();
+ }
+
+ await browser.close();
+}
+
+main().catch((error) => {
+ console.error(error);
+ process.exit(1);
+});
diff --git a/src/app/(main)/blocks/_components/block-card.tsx b/src/app/(main)/blocks/_components/block-card.tsx
index 005c164..467b183 100644
--- a/src/app/(main)/blocks/_components/block-card.tsx
+++ b/src/app/(main)/blocks/_components/block-card.tsx
@@ -1,4 +1,6 @@
+import Image from "next/image";
import type { RegistryBlockItem } from "@/lib/blocks";
+import { getBlockThumbnails } from "@/lib/block-thumbnails";
import { BlockThumbnail } from "./block-thumbnail";
import { OpenBlockLink } from "./blocks-view";
@@ -8,21 +10,39 @@ interface BlockCardProps {
export function BlockCard({ block }: BlockCardProps) {
const title = block.title ?? block.name;
+ const thumbnails = getBlockThumbnails(block.name);
return (
- <article className="bg-muted/30 dark:hover:bg-muted/40 hover:bg-muted/80 has-[a:focus-visible]:border-ring has-[a:focus-visible]:ring-ring/50 relative rounded-xl border p-1.5 transition-[color,background-color,box-shadow] has-[a:focus-visible]:ring-[3px]">
+ <OpenBlockLink
+ name={block.name}
+ className="bg-muted/30 dark:hover:bg-muted/40 hover:bg-muted/80 focus-visible:border-ring focus-visible:ring-ring/50 block rounded-xl border p-1.5 transition-[color,background-color,box-shadow] outline-none focus-visible:ring-[3px]"
+ >
<div className="bg-background overflow-hidden rounded-md border">
- <BlockThumbnail src={`/view/${block.name}`} title={title} />
+ {thumbnails ? (
+ <div className="bg-muted/40 relative aspect-[16/10]">
+ <Image
+ src={thumbnails.light}
+ alt=""
+ fill
+ sizes={THUMBNAIL_SIZES}
+ className="object-cover dark:hidden"
+ />
+ <Image
+ src={thumbnails.dark}
+ alt=""
+ fill
+ sizes={THUMBNAIL_SIZES}
+ className="hidden object-cover dark:block"
+ />
+ </div>
+ ) : (
+ <BlockThumbnail src={`/view/${block.name}`} title={title} />
+ )}
</div>
<div className="min-w-0 px-3 pt-3.5 pb-2.5">
- <h3 className="text-base font-medium tracking-tight">
- <OpenBlockLink
- name={block.name}
- className="inline-block max-w-full truncate text-left align-top outline-none after:absolute after:inset-0 after:rounded-xl after:content-['']"
- >
- {title}
- </OpenBlockLink>
+ <h3 className="truncate text-base font-medium tracking-tight">
+ {title}
</h3>
{block.description && (
<p className="text-muted-foreground mt-1 truncate text-sm">
@@ -30,6 +50,8 @@ export function BlockCard({ block }: BlockCardProps) {
</p>
)}
</div>
- </article>
+ </OpenBlockLink>
);
}
+
+const THUMBNAIL_SIZES = "(min-width: 768px) 50vw, 100vw";
diff --git a/src/app/(main)/blocks/_components/block-thumbnail.tsx b/src/app/(main)/blocks/_components/block-thumbnail.tsx
index 9b3f1df..e194478 100644
--- a/src/app/(main)/blocks/_components/block-thumbnail.tsx
+++ b/src/app/(main)/blocks/_components/block-thumbnail.tsx
@@ -1,11 +1,7 @@
"use client";
import { useEffect, useRef, useState } from "react";
-import { cn } from "@/lib/utils";
-import { useInView } from "./use-in-view";
-// Blocks are designed as full pages, so the thumbnail renders them at a
-// desktop width and scales the whole frame down to fit the card.
const FRAME_WIDTH = 1280;
const FRAME_HEIGHT = 800;
@@ -16,10 +12,6 @@ interface BlockThumbnailProps {
export function BlockThumbnail({ src, title }: BlockThumbnailProps) {
const ref = useRef<HTMLDivElement>(null);
- // Frames stay mounted once loaded so scrolling back never reloads them. Each
- // one holds a WebGL context and Chrome caps those at about 16, so past ~14
- // blocks switch to `{ once: false }` or static screenshots.
- const inView = useInView(ref);
const [scale, setScale] = useState(0);
useEffect(() => {
@@ -39,38 +31,20 @@ export function BlockThumbnail({ src, title }: BlockThumbnailProps) {
ref={ref}
className="bg-muted/40 relative aspect-[16/10] w-full overflow-hidden"
>
- {inView && scale > 0 && (
- <ThumbnailFrame src={src} title={title} scale={scale} />
+ {scale > 0 && (
+ <iframe
+ src={src}
+ title={`${title} preview`}
+ loading="lazy"
+ inert
+ className="pointer-events-none absolute top-0 left-0 origin-top-left border-0"
+ style={{
+ width: FRAME_WIDTH,
+ height: FRAME_HEIGHT,
+ transform: `scale(${scale})`,
+ }}
+ />
)}
</div>
);
}
-
-// Owns the load state so a frame that remounts fades in again.
-function ThumbnailFrame({
- src,
- title,
- scale,
-}: BlockThumbnailProps & { scale: number }) {
- const [loaded, setLoaded] = useState(false);
-
- return (
- <iframe
- src={src}
- title={`${title} preview`}
- // inert keeps the map's controls out of the tab order and away from
- // assistive tech; the card's own button is the only target.
- inert
- onLoad={() => setLoaded(true)}
- className={cn(
- "pointer-events-none absolute top-0 left-0 origin-top-left border-0 transition-opacity duration-500",
- loaded ? "opacity-100" : "opacity-0",
- )}
- style={{
- width: FRAME_WIDTH,
- height: FRAME_HEIGHT,
- transform: `scale(${scale})`,
- }}
- />
- );
-}
diff --git a/src/app/(main)/blocks/_components/blocks-view.tsx b/src/app/(main)/blocks/_components/blocks-view.tsx
index fc12f9d..9899edd 100644
--- a/src/app/(main)/blocks/_components/blocks-view.tsx
+++ b/src/app/(main)/blocks/_components/blocks-view.tsx
@@ -35,7 +35,6 @@ export function BlocksViewProvider({
children: React.ReactNode;
}) {
const [view, setView] = useState<BlocksViewMode>("grid");
- // Bumped on every open so the scroll runs even when the view is unchanged.
const [openRequest, setOpenRequest] = useState(0);
const targetRef = useRef<string | null>(null);
@@ -48,7 +47,6 @@ export function BlocksViewProvider({
}
}, []);
- // A shared link like /blocks#store-locator opens that block directly.
useEffect(() => {
function openFromHash() {
const name = window.location.hash.slice(1);
@@ -121,8 +119,6 @@ export function BlocksViewSwitch() {
);
}
-// A real link to /blocks#name, so the card can be opened in a new tab or have
-// its link copied. Plain clicks switch to list view in place instead.
export function OpenBlockLink({
name,
children,
diff --git a/src/app/(main)/blocks/_components/iframe-preview.tsx b/src/app/(main)/blocks/_components/iframe-preview.tsx
index 922de2a..efd3441 100644
--- a/src/app/(main)/blocks/_components/iframe-preview.tsx
+++ b/src/app/(main)/blocks/_components/iframe-preview.tsx
@@ -1,35 +1,12 @@
-"use client";
-
-import { useRef, useState } from "react";
-import { cn } from "@/lib/utils";
-import { useInView } from "./use-in-view";
-
interface IframePreviewProps {
src: string;
title: string;
}
export function IframePreview({ src, title }: IframePreviewProps) {
- const ref = useRef<HTMLDivElement>(null);
- const inView = useInView(ref);
- const [loaded, setLoaded] = useState(false);
-
return (
- <div
- ref={ref}
- className="bg-background relative h-(--block-preview-height) w-full overflow-hidden rounded-xl border"
- >
- {inView && (
- <iframe
- src={src}
- title={title}
- onLoad={() => setLoaded(true)}
- className={cn(
- "size-full border-0 transition-opacity duration-500",
- loaded ? "opacity-100" : "opacity-0",
- )}
- />
- )}
+ <div className="bg-background relative h-(--block-preview-height) w-full overflow-hidden rounded-xl border">
+ <iframe src={src} title={title} className="size-full border-0" />
</div>
);
}
diff --git a/src/app/(main)/blocks/_components/use-in-view.ts b/src/app/(main)/blocks/_components/use-in-view.ts
deleted file mode 100644
index 5f8d37e..0000000
--- a/src/app/(main)/blocks/_components/use-in-view.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-"use client";
-
-import { useEffect, useState, type RefObject } from "react";
-
-interface UseInViewOptions {
- rootMargin?: string;
- /**
- * Stay true after the first time the element comes near the viewport, so a
- * map preview mounts once and keeps its state while scrolling. Turn off to
- * follow visibility both ways and release offscreen previews.
- */
- once?: boolean;
-}
-
-export function useInView(
- ref: RefObject<Element | null>,
- { rootMargin = "200px", once = true }: UseInViewOptions = {},
-) {
- const [inView, setInView] = useState(false);
-
- useEffect(() => {
- const element = ref.current;
- if (!element) return;
-
- const observer = new IntersectionObserver(
- ([entry]) => {
- if (!once) {
- setInView(entry.isIntersecting);
- } else if (entry.isIntersecting) {
- setInView(true);
- observer.disconnect();
- }
- },
- { rootMargin },
- );
-
- observer.observe(element);
- return () => observer.disconnect();
- }, [ref, rootMargin, once]);
-
- return inView;
-}
diff --git a/src/components/command-search.tsx b/src/components/command-search.tsx
index a75dec3..09afe58 100644
--- a/src/components/command-search.tsx
+++ b/src/components/command-search.tsx
@@ -57,8 +57,6 @@ export function CommandSearch({ className }: { className?: string }) {
};
}, []);
- // Every open and close goes through here, so a pending auto-close from a
- // copy can never shut a palette that was reopened in the meantime.
function handleOpenChange(open: boolean) {
window.clearTimeout(closeTimer.current);
setCopyStatus(null);
@@ -75,7 +73,6 @@ export function CommandSearch({ className }: { className?: string }) {
action();
}
- // Shows the result on the item; closes the palette only after a success.
async function copy(id: CopyId, text: string, event: Event["name"]) {
window.clearTimeout(closeTimer.current);
try {
diff --git a/src/components/site-analytics.tsx b/src/components/site-analytics.tsx
index 9baaa7b..0e25236 100644
--- a/src/components/site-analytics.tsx
+++ b/src/components/site-analytics.tsx
@@ -2,8 +2,6 @@
import { Analytics } from "@vercel/analytics/next";
-// Block previews embed /view pages in iframes. Count only top-level pages so a
-// visit to /blocks doesn't also log a pageview for every preview it loads.
export function SiteAnalytics() {
return (
<Analytics
diff --git a/src/components/theme-provider.tsx b/src/components/theme-provider.tsx
index fafc651..8932b47 100644
--- a/src/components/theme-provider.tsx
+++ b/src/components/theme-provider.tsx
@@ -3,7 +3,6 @@
import * as React from "react";
import { ThemeProvider as NextThemesProvider, useTheme } from "next-themes";
-/** Pressing this key anywhere outside a text field toggles the theme. */
const THEME_HOTKEY = "t";
function ThemeProvider({
diff --git a/src/lib/block-thumbnails.ts b/src/lib/block-thumbnails.ts
new file mode 100644
index 0000000..92455be
--- /dev/null
+++ b/src/lib/block-thumbnails.ts
@@ -0,0 +1,25 @@
+import fs from "fs";
+import path from "path";
+
+const THUMBNAIL_DIR = path.join(process.cwd(), "public", "blocks");
+
+export interface BlockThumbnails {
+ light: string;
+ dark: string;
+}
+
+// Screenshots come from `npm run blocks:thumbnails`. A block without both
+// variants returns null so the card can fall back to a live preview.
+export function getBlockThumbnails(name: string): BlockThumbnails | null {
+ const light = `${name}-light.jpg`;
+ const dark = `${name}-dark.jpg`;
+
+ if (
+ !fs.existsSync(path.join(THUMBNAIL_DIR, light)) ||
+ !fs.existsSync(path.join(THUMBNAIL_DIR, dark))
+ ) {
+ return null;
+ }
+
+ return { light: `/blocks/${light}`, dark: `/blocks/${dark}` };
+}