Skip to content

armgit / mapcn

Public Imported from GitHub
0

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

TypeScript

refactor(docs): unify code block rendering with shared components

anmolb2390dbed3f4
diff --git a/src/app/(main)/blocks/_components/block-viewer-code.tsx b/src/app/(main)/blocks/_components/block-viewer-code.tsx
index ea1a89a..d0bfba0 100644
--- a/src/app/(main)/blocks/_components/block-viewer-code.tsx
+++ b/src/app/(main)/blocks/_components/block-viewer-code.tsx
@@ -1,13 +1,12 @@
"use client";
import * as React from "react";
-import { Check, ChevronRight, Copy, File, Folder } from "lucide-react";
+import { ChevronRight, File, Folder } from "lucide-react";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/collapsible";
-import { Button } from "@/components/ui/button";
import {
Sidebar,
SidebarGroup,
@@ -21,6 +20,8 @@ import {
} from "@/components/ui/sidebar";
import { type FileTree } from "@/lib/blocks";
import { trackEvent } from "@/lib/events";
+import { CodeSurface } from "@/components/code-surface";
+import { CodeCopyButton } from "@/components/code-copy-button";
export interface HighlightedFile {
path: string;
@@ -77,16 +78,24 @@ export function BlockViewerCode({
<FileTreeSidebar />
</div>
<div className="flex min-w-0 flex-1 flex-col">
- <div className="bg-code flex h-12 shrink-0 items-center gap-2 border-b px-4 text-sm">
+ <div className="bg-surface flex h-12 shrink-0 items-center gap-2 border-b px-4 text-sm">
<span className="text-muted-foreground">{file.target}</span>
<div className="ml-auto">
- <CopyCodeButton />
+ <CodeCopyButton
+ text={file.content}
+ onCopy={() =>
+ trackEvent({
+ name: "copy_block_code",
+ properties: { file: file.target },
+ })
+ }
+ />
</div>
</div>
- <div
+ <CodeSurface
key={file.path}
- dangerouslySetInnerHTML={{ __html: file.highlightedContent }}
- className="bg-code no-scrollbar flex-1 overflow-x-auto p-4 text-sm"
+ html={file.highlightedContent}
+ className="flex-1 overflow-x-auto"
/>
</div>
</div>
@@ -173,33 +182,3 @@ function TreeNode({ item, index }: { item: FileTree; index: number }) {
</SidebarMenuItem>
);
}
-
-function CopyCodeButton() {
- const { activeFile, highlightedFiles } = useBlockViewerCode();
- const [copied, setCopied] = React.useState(false);
-
- const file = React.useMemo(
- () => highlightedFiles.find((f) => f.target === activeFile),
- [highlightedFiles, activeFile],
- );
-
- if (!file) return null;
-
- return (
- <Button
- variant="ghost"
- size="icon-sm"
- onClick={async () => {
- await navigator.clipboard.writeText(file.content);
- setCopied(true);
- setTimeout(() => setCopied(false), 2000);
- trackEvent({
- name: "copy_block_code",
- properties: { file: file.target },
- });
- }}
- >
- {copied ? <Check /> : <Copy />}
- </Button>
- );
-}
diff --git a/src/app/(main)/docs/_components/code-block.tsx b/src/app/(main)/docs/_components/code-block.tsx
index f19a790..1214df3 100644
--- a/src/app/(main)/docs/_components/code-block.tsx
+++ b/src/app/(main)/docs/_components/code-block.tsx
@@ -1,5 +1,6 @@
import { highlightCode } from "@/lib/highlight";
-import { CopyButton } from "./copy-button";
+import { CodeSurface } from "@/components/code-surface";
+import { CodeCopyButton } from "@/components/code-copy-button";
interface CodeBlockProps {
code: string;
@@ -18,13 +19,10 @@ export async function CodeBlock({
<div className="relative w-full overflow-hidden rounded-lg border">
{showCopyButton && (
<div className="absolute top-2 right-2 z-10">
- <CopyButton text={code} />
+ <CodeCopyButton text={code} />
</div>
)}
- <div
- className="bg-code no-scrollbar overflow-auto p-4 text-sm"
- dangerouslySetInnerHTML={{ __html: highlighted }}
- />
+ <CodeSurface className="overflow-auto" html={highlighted} />
</div>
);
}
diff --git a/src/app/(main)/docs/_components/component-preview-client.tsx b/src/app/(main)/docs/_components/component-preview-client.tsx
index 925787f..8b9756c 100644
--- a/src/app/(main)/docs/_components/component-preview-client.tsx
+++ b/src/app/(main)/docs/_components/component-preview-client.tsx
@@ -3,7 +3,8 @@
import { useId, useState } from "react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
-import { CopyButton } from "./copy-button";
+import { CodeSurface } from "@/components/code-surface";
+import { CodeCopyButton } from "@/components/code-copy-button";
interface ComponentPreviewClientProps {
children: React.ReactNode;
@@ -34,17 +35,16 @@ export function ComponentPreviewClient({
<div className="relative w-full overflow-hidden rounded-lg border">
<div className="absolute top-2 right-2 z-10">
- <CopyButton text={code} />
+ <CodeCopyButton text={code} />
</div>
- <div
+ <CodeSurface
id={codeId}
className={cn(
- "bg-code no-scrollbar p-4 text-sm",
expanded
? "max-h-[420px] overflow-x-auto"
: "max-h-42 overflow-hidden",
)}
- dangerouslySetInnerHTML={{ __html: highlightedCode }}
+ html={highlightedCode}
/>
{!expanded && (
<div className="from-background to-background/0 absolute inset-x-0 bottom-0 flex w-full items-center justify-center bg-linear-to-t pt-16 pb-6">
diff --git a/src/app/(main)/docs/_components/docs.tsx b/src/app/(main)/docs/_components/docs.tsx
index d7c2855..561291d 100644
--- a/src/app/(main)/docs/_components/docs.tsx
+++ b/src/app/(main)/docs/_components/docs.tsx
@@ -136,7 +136,7 @@ interface DocsNoteProps {
export function DocsNote({ children }: DocsNoteProps) {
return (
- <div className="bg-code text-foreground/80 [&_strong]:text-foreground rounded-lg border px-5 py-4 text-[15px] leading-relaxed [&_strong]:font-medium">
+ <div className="bg-surface text-foreground/80 [&_strong]:text-foreground rounded-lg border px-5 py-4 text-[15px] leading-relaxed [&_strong]:font-medium">
{children}
</div>
);
diff --git a/src/app/(main)/docs/_components/copy-button.tsx b/src/components/code-copy-button.tsx
similarity index 66%
rename from src/app/(main)/docs/_components/copy-button.tsx
rename to src/components/code-copy-button.tsx
index 57e97a7..7051b5e 100644
--- a/src/app/(main)/docs/_components/copy-button.tsx
+++ b/src/components/code-copy-button.tsx
@@ -2,19 +2,27 @@
import { useState } from "react";
import { Check, Copy } from "lucide-react";
+import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
-interface CopyButtonProps {
+interface CodeCopyButtonProps {
text: string;
+ onCopy?: () => void;
+ className?: string;
}
-export function CopyButton({ text }: CopyButtonProps) {
+export function CodeCopyButton({
+ text,
+ onCopy,
+ className,
+}: CodeCopyButtonProps) {
const [copied, setCopied] = useState(false);
const copy = async () => {
await navigator.clipboard.writeText(text);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
+ onCopy?.();
};
return (
@@ -23,7 +31,7 @@ export function CopyButton({ text }: CopyButtonProps) {
size="icon-sm"
onClick={copy}
aria-label={copied ? "Copied" : "Copy code"}
- className="text-muted-foreground bg-code"
+ className={cn("text-muted-foreground bg-code", className)}
>
{copied ? <Check /> : <Copy />}
</Button>
diff --git a/src/components/code-surface.tsx b/src/components/code-surface.tsx
new file mode 100644
index 0000000..114dbbe
--- /dev/null
+++ b/src/components/code-surface.tsx
@@ -0,0 +1,18 @@
+import { cn } from "@/lib/utils";
+
+interface CodeSurfaceProps extends Omit<
+ React.ComponentProps<"div">,
+ "children"
+> {
+ html: string;
+}
+
+export function CodeSurface({ html, className, ...props }: CodeSurfaceProps) {
+ return (
+ <div
+ className={cn("bg-code no-scrollbar py-4 pr-4 text-sm", className)}
+ dangerouslySetInnerHTML={{ __html: html }}
+ {...props}
+ />
+ );
+}
diff --git a/src/styles/globals.css b/src/styles/globals.css
index 117e11a..a6ad0ce 100644
--- a/src/styles/globals.css
+++ b/src/styles/globals.css
@@ -85,7 +85,7 @@
--foreground: oklch(0.145 0 0);
--surface: oklch(0.99 0 0);
--code-number: oklch(0.69 0 0);
- --code: var(--surface)
+ --code: var(--surface);
}
.dark {
@@ -122,7 +122,7 @@
--sidebar-ring: oklch(0.556 0 0);
--surface: oklch(0.195 0 0);
--code-number: oklch(0.54 0 0);
- --code: var(--surface)
+ --code: var(--surface);
}
@layer base {
@@ -140,7 +140,7 @@
cursor: pointer;
}
.maplibregl-popup-content {
- @apply bg-transparent! shadow-none! p-0! rounded-none!;
+ @apply rounded-none! bg-transparent! p-0! shadow-none!;
}
.maplibregl-popup-tip {
@apply hidden!;
@@ -148,11 +148,11 @@
}
.container {
- @apply max-w-[1440px] mx-auto px-6;
+ @apply mx-auto max-w-[1440px] px-6;
}
.container-wide {
- @apply max-w-[1650px] mx-auto px-6;
+ @apply mx-auto max-w-[1650px] px-6;
}
@keyframes fade-up {
@@ -228,17 +228,26 @@ html.dark .shiki span {
background-color: var(--color-code);
}
+html .shiki:focus,
+html .shiki:focus-visible {
+ outline: none;
+}
+
code {
counter-reset: step;
counter-increment: step 0;
}
code .line::before {
- content: counter(step);
- counter-increment: step;
- width: 1rem;
- margin-right: 1.5rem;
- display: inline-block;
- text-align: right;
- color: var(--code-number);
-}
\ No newline at end of file
+ font-size: var(--text-sm);
+ counter-increment: step;
+ content: counter(step);
+ display: inline-block;
+ width: calc(var(--spacing) * 16);
+ padding-right: calc(var(--spacing) * 6);
+ text-align: right;
+ color: var(--color-code-number);
+ background-color: var(--color-code);
+ position: sticky;
+ left: 0;
+}