Skip to content
Notra logoNotra UI
Esc
↑↓navigate↵open⌘Jpreview
On this page

Code Block

The code block from the Notra dashboard, with a filename or tabs in the header, highlighted code and a copy button.

Every setup snippet in the Notra dashboard sits in this block. The filename or a tab switcher sits on the grey shell and the code sits on the lifted card, the same frame as the Table and Card. Code is highlighted with sugar-high.

"use client";

import { useState } from "react";

import { CodeBlock, CodeBlockTabs } from "../components/code-block";

const SNIPPETS = {
  next: {
    file: "proxy.ts",
    code: `import { withGeo } from "@usenotra/geo/next";

export const proxy = withGeo({
  token: process.env.NOTRA_GEO_TOKEN,
});

export const config = { matcher: ["/((?!_next|api).*)"] };`,
  },
  astro: {
    file: "src/middleware.ts",
    code: `import { geoMiddleware } from "@usenotra/geo/astro";

export const onRequest = geoMiddleware({
  token: import.meta.env.NOTRA_GEO_TOKEN,
});`,
  },
} as const;

const FRAMEWORKS = [
  { value: "next", label: "Next.js" },
  { value: "astro", label: "Astro" },
] as const;

export default function CodeBlockDemo() {
  const [framework, setFramework] = useState<keyof typeof SNIPPETS>("next");
  const snippet = SNIPPETS[framework];

  return (
    <div className="mx-auto flex w-full max-w-xl flex-col gap-4 p-4 sm:p-6">
      <CodeBlock
        code={snippet.code}
        filename={snippet.file}
        tabs={
          <CodeBlockTabs
            label="Framework"
            onValueChange={(value) => {
              if (value === "next" || value === "astro") {
                setFramework(value);
              }
            }}
            options={FRAMEWORKS}
            value={framework}
          />
        }
      />
      <CodeBlock code="NOTRA_GEO_TOKEN=ntr_geo_••••••••" variant="command" />
    </div>
  );
}

Installation

$ bunx --bun shadcn@latest add @notra/code-block

This installs the block into components/code-block/ and adds the --sh-* highlight colors for light and dark mode.

Usage

import { CodeBlock, CodeBlockTabs } from "@/components/code-block/components/code-block";

<CodeBlock code={source} filename="proxy.ts" />;

<CodeBlock
  code={source}
  tabs={
    <CodeBlockTabs
      label="Framework"
      onValueChange={setFramework}
      options={[
        { value: "next", label: "Next.js" },
        { value: "astro", label: "Astro" },
      ]}
      value={framework}
    />
  }
/>;

<CodeBlock code="NOTRA_GEO_TOKEN=..." variant="command" />;
Prop Default Description
code The source to show and copy.
filename Shown on the left of the header, or on the right when there are tabs.
tabs A CodeBlockTabs switcher for the header.
headerEnd Extra content at the end of the header.
variant "panel" "panel" for a highlighted block, "command" for a one-line field.
label "Snippet" Names the block for the copy button and screen readers. The command variant defaults to "Command".
wrap false Wraps long lines instead of scrolling.
onCopy Called after the code is copied.

Long lines scroll sideways inside the card and the copy button stays in the corner. On narrow screens the header tabs scroll too, so the block fits any width.