dxbh-uiGitHub

OG Image

Blueprint share cards for docs, home and error pages. Inline-styled primitives (canvas, wordmark, headline, lead, status, rule, footer) and three recipes that any ImageResponse renderer can draw at 1200×630. Rendering stays in your app's opengraph-image route.

Preview
og:image · home1200 × 630 @ 0.40

Install

npx shadcn@latest add @dxbh-ui/og-image

Add the @dxbh-ui registry to components.json once:

{
  "registries": {
    "@dxbh-ui": "https://ui.dxbh.dev/r/{name}.json"
  }
}

Usage

demo.tsx
"use client";

import { useLayoutEffect, useRef, useState } from "react";
import { Segmented } from "@/components/ui/segmented/segmented";
import { OG_SIZE, renderOgTree } from "@/components/ui/og-image/og-image";

type Variant = "home" | "doc" | "404" | "500";
const variants = [
  { value: "home", label: "Home" },
  { value: "doc", label: "Doc" },
  { value: "404", label: "404" },
  { value: "500", label: "500" },
] as const;

function tree(variant: Variant) {
  switch (variant) {
    case "home":
      return renderOgTree("home", {
        title: "Components, finished.",
        description: "A shadcn registry of interface pieces with the polish already done.",
      });
    case "doc":
      return renderOgTree("doc", {
        title: "Page States",
        description: "Empty, error and loading states that share one shell.",
        section: "docs / page-states",
        meta: "tier 1",
      });
    case "404":
      return renderOgTree("error", {
        status: 404,
        title: "This page is off the sheet.",
        description: "The address may have moved. Start again from the index.",
      });
    case "500":
      return renderOgTree("error", {
        status: 500,
        title: "Something broke on our side.",
        description: "Try again in a moment.",
      });
  }
}

export default function OgImageDemo() {
  const [variant, setVariant] = useState<Variant>("home");
  const frameRef = useRef<HTMLDivElement>(null);
  const [scale, setScale] = useState(0.4);

  // Fit the 1200×630 canvas to the frame's width (0.4 on a typical demo column).
  useLayoutEffect(() => {
    const frame = frameRef.current;
    if (!frame || typeof ResizeObserver === "undefined") return;
    const ro = new ResizeObserver(([entry]) => setScale(entry.contentRect.width / OG_SIZE.width));
    ro.observe(frame);
    return () => ro.disconnect();
  }, []);

  return (
    <div className="flex w-full max-w-[482px] flex-col items-center gap-5">
      <Segmented label="Card" options={variants} value={variant} onValueChange={setVariant} />
      <figure className="flex w-full flex-col gap-2">
        <div
          ref={frameRef}
          className="relative w-full overflow-hidden border border-border"
          style={{ aspectRatio: `${OG_SIZE.width} / ${OG_SIZE.height}` }}
        >
          <div
            aria-hidden
            style={{ width: OG_SIZE.width, height: OG_SIZE.height, transform: `scale(${scale})`, transformOrigin: "0 0" }}
          >
            {tree(variant)}
          </div>
        </div>
        <figcaption className="flex justify-between font-mono text-xs text-muted-foreground tabular-nums">
          <span>og:image · {variant}</span>
          <span>
            {OG_SIZE.width} × {OG_SIZE.height} @ {scale.toFixed(2)}
          </span>
        </figcaption>
      </figure>
    </div>
  );
}

Props

PropTypeDefault
OgHomeCard.title*

Site headline. Short titles get the 88px size.

string—
OgDocCard.title*

Page title.

string—
OgDocCard.section

Mono path shown opposite the wordmark.

string"docs"
OgErrorCard.status*

Status code set large in mono, such as 404 or 500.

number | string—
OgErrorCard.title*

One line on what went wrong.

string—
description

Supporting line under the title (all recipes).

string—
brand

Wordmark text (all recipes).

string"dxbh-ui"
url

Footer address; the protocol is stripped (all recipes).

string"github.com/dxbh0517/dxbh-ui"
meta

Right-hand footer note (all recipes).

ReactNode—
renderOgTree

Builds a recipe by name, for one route that serves several cards.

(variant: "home" | "doc" | "error", props) => ReactElement—

Polish

  • Blueprint material: a Prussian-blue sheet (oklch 35% 0.105 256), white line ink, a faint drafting grid and a square double keyline (2px, 6px gap, 1px). No blur, no gradients beyond the grid lines, no cards inside the card.
  • Satori-safe: inline styles only, every multi-child element is display:flex, and colours are literal sRGB because ImageResponse reads neither CSS variables nor oklch. The oklch source sits beside each token.
  • Display tracking tightens as size grows (-2.8px at 76px, -3.4px at 88px), and long titles step down a size so they hold to about three lines instead of overflowing the frame.
  • Spatial consistency: one frame inset, one content inset and one stack gap shared by every recipe, so the wordmark and footer sit in the same place on every card.
  • Measurements are mono: the footer address, the section path, the 1200 × 630 note and the dimension line under a status code. No tracked-caps eyebrows.
  • Supporting copy is 8.3:1 on the sheet and captions 5.6:1, so the card still reads when a feed scales it down to a thumbnail.
  • No next/* import: the package returns a React tree, and your route wraps it in ImageResponse.

Source

og-image.tsx
// Entry point for @dxbh-ui/og-image. Import primitives and recipes from here, and
// pass the tree to your framework's image renderer, for example in a Next.js
// opengraph-image route:
//
//   return new ImageResponse(renderOgTree("doc", { title, description }), OG_SIZE);

import type { ReactElement } from "react";
import { OgDocCard, OgErrorCard, OgHomeCard, type OgDocCardProps, type OgErrorCardProps, type OgHomeCardProps } from "./og-recipes";

export * from "./og-tokens";
export * from "./og-primitives";
export * from "./og-recipes";

export type OgVariantProps = {
  home: OgHomeCardProps;
  doc: OgDocCardProps;
  error: OgErrorCardProps;
};

export type OgVariant = keyof OgVariantProps;

/** Builds the tree for one recipe by name. */
export function renderOgTree<V extends OgVariant>(variant: V, props: OgVariantProps[V]): ReactElement {
  switch (variant) {
    case "home":
      return <OgHomeCard {...(props as OgHomeCardProps)} />;
    case "doc":
      return <OgDocCard {...(props as OgDocCardProps)} />;
    case "error":
      return <OgErrorCard {...(props as OgErrorCardProps)} />;
    default:
      throw new Error(`Unknown OG variant "${String(variant)}"`);
  }
}
og-primitives.tsx
// Open Graph primitives. Inline styles only, so the same tree renders in an
// ImageResponse (Satori) and in the DOM. Satori rules followed throughout:
// every element with more than one child is display:flex, no CSS variables,
// no classes, no filters or blur.

import type { CSSProperties, ReactNode } from "react";
import { fitHeadline, OG_COLOR, OG_FONT, OG_SIZE, OG_SPACE, OG_TYPE } from "./og-tokens";

type Base = { style?: CSSProperties };

export type OgCanvasProps = Base & {
  children?: ReactNode;
  /** Canvas size. Defaults to 1200×630. */
  width?: number;
  height?: number;
  /** Draws the faint drafting grid behind the sheet. */
  grid?: boolean;
};

/** The sheet: Prussian-blue ground, drafting grid, square double keyline, and a padded column for content. */
export function OgCanvas({ children, width = OG_SIZE.width, height = OG_SIZE.height, grid = true, style }: OgCanvasProps) {
  const { frame, keylineGap, inset } = OG_SPACE;
  const inner = frame + 2 + keylineGap;
  return (
    <div
      style={{
        width,
        height,
        display: "flex",
        position: "relative",
        overflow: "hidden",
        backgroundColor: OG_COLOR.sheet,
        ...(grid
          ? {
              backgroundImage: `linear-gradient(to right, ${OG_COLOR.grid} 1px, transparent 1px), linear-gradient(to bottom, ${OG_COLOR.grid} 1px, transparent 1px)`,
              backgroundSize: `${OG_SPACE.grid}px ${OG_SPACE.grid}px`,
            }
          : null),
        color: OG_COLOR.ink,
        fontFamily: OG_FONT.sans,
        ...style,
      }}
    >
      <div style={{ position: "absolute", top: frame, right: frame, bottom: frame, left: frame, border: `2px solid ${OG_COLOR.ink}` }} />
      <div style={{ position: "absolute", top: inner, right: inner, bottom: inner, left: inner, border: `1px solid ${OG_COLOR.rule}` }} />
      <div
        style={{
          position: "absolute",
          top: frame + inset,
          right: frame + inset,
          bottom: frame + inset,
          left: frame + inset,
          display: "flex",
          flexDirection: "column",
          justifyContent: "space-between",
        }}
      >
        {children}
      </div>
    </div>
  );
}

export type OgBrandProps = Base & {
  /** Wordmark text. */
  name?: string;
  /** Hides the square mark beside the wordmark. */
  hideMark?: boolean;
};

/** The wordmark: a square keyline mark and the name, tightly tracked. */
export function OgBrand({ name = "dxbh-ui", hideMark = false, style }: OgBrandProps) {
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 14, ...style }}>
      {hideMark ? null : (
        <div
          style={{
            width: 26,
            height: 26,
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
            border: `2px solid ${OG_COLOR.ink}`,
          }}
        >
          <div style={{ width: 10, height: 10, backgroundColor: OG_COLOR.ink }} />
        </div>
      )}
      <div style={{ display: "flex", ...OG_TYPE.brand }}>{name}</div>
    </div>
  );
}

export type OgHeadlineProps = Base & {
  children: ReactNode;
  /** Steps the size down for long string titles so they stay on about three lines. */
  fit?: boolean;
};

/** Display headline: large, semibold, tracked tight. */
export function OgHeadline({ children, fit = true, style }: OgHeadlineProps) {
  const size = fit && typeof children === "string" ? fitHeadline(children) : null;
  return (
    <div
      style={{
        display: "flex",
        maxWidth: 960,
        color: OG_COLOR.ink,
        ...OG_TYPE.headline,
        ...size,
        ...style,
      }}
    >
      {children}
    </div>
  );
}

export type OgLeadProps = Base & { children: ReactNode };

/** Supporting line under the headline, in the muted ink. */
export function OgLead({ children, style }: OgLeadProps) {
  return (
    <div style={{ display: "flex", maxWidth: 880, color: OG_COLOR.muted, ...OG_TYPE.lead, ...style }}>{children}</div>
  );
}

export type OgRuleProps = Base & {
  /** A 1px hairline, or the square double keyline (2px, gap, 1px). */
  variant?: "hairline" | "double";
  /** Rule length. Defaults to the full width of its container. */
  width?: number | string;
};

/** Horizontal rule in line ink. */
export function OgRule({ variant = "hairline", width = "100%", style }: OgRuleProps) {
  if (variant === "double") {
    return (
      <div style={{ display: "flex", flexDirection: "column", width, gap: OG_SPACE.keylineGap, ...style }}>
        <div style={{ display: "flex", height: 2, backgroundColor: OG_COLOR.ink }} />
        <div style={{ display: "flex", height: 1, backgroundColor: OG_COLOR.rule }} />
      </div>
    );
  }
  return <div style={{ display: "flex", width, height: 1, backgroundColor: OG_COLOR.rule, ...style }} />;
}

export type OgDimensionProps = Base & {
  /** Mono caption centred under the dimension line. */
  label: ReactNode;
  width?: number | string;
};

/** A drafting dimension line: end ticks, a hairline between them, and a mono caption. */
export function OgDimension({ label, width = "100%", style }: OgDimensionProps) {
  const tick: CSSProperties = { display: "flex", width: 1, height: 14, backgroundColor: OG_COLOR.faint };
  return (
    <div style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: 10, width, ...style }}>
      <div style={{ display: "flex", alignItems: "center", width: "100%" }}>
        <div style={tick} />
        <div style={{ display: "flex", flexGrow: 1, height: 1, backgroundColor: OG_COLOR.faint }} />
        <div style={tick} />
      </div>
      <div style={{ display: "flex", color: OG_COLOR.faint, fontFamily: OG_FONT.mono, ...OG_TYPE.meta, fontSize: 18 }}>
        {label}
      </div>
    </div>
  );
}

export type OgStatusProps = Base & {
  /** The status code, as a number or a string such as "404". */
  code: number | string;
  /** Mono caption on the dimension line under the code. Pass null to hide the line. */
  label?: ReactNode;
};

/** A large mono status code with a dimension line under it. */
export function OgStatus({ code, label = "status", style }: OgStatusProps) {
  return (
    <div style={{ display: "flex", flexDirection: "column", alignItems: "flex-start", gap: 18, ...style }}>
      <div style={{ display: "flex", fontFamily: OG_FONT.mono, color: OG_COLOR.ink, ...OG_TYPE.status }}>{String(code)}</div>
      {label === null ? null : <OgDimension label={label} />}
    </div>
  );
}

export type OgFooterProps = Base & {
  /** Site address, shown without the protocol. */
  url: string;
  /** Right-aligned mono note: a section, a date, a measurement. */
  meta?: ReactNode;
};

/** Bottom row: a hairline, then the site address and an optional note, both mono. */
export function OgFooter({ url, meta, style }: OgFooterProps) {
  const text: CSSProperties = { display: "flex", fontFamily: OG_FONT.mono, ...OG_TYPE.meta };
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 20, ...style }}>
      <OgRule />
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
        <div style={{ ...text, color: OG_COLOR.muted }}>{url.replace(/^https?:\/\//, "")}</div>
        {meta == null ? null : <div style={{ ...text, color: OG_COLOR.faint }}>{meta}</div>}
      </div>
    </div>
  );
}
og-recipes.tsx
// Recipe layouts composed from the primitives. Each returns a full 1200×630
// tree; hand it to ImageResponse in your app's opengraph-image route.

import type { ReactNode } from "react";
import { OgBrand, OgCanvas, OgFooter, OgHeadline, OgLead, OgRule, OgStatus } from "./og-primitives";
import { OG_COLOR, OG_FONT, OG_SIZE, OG_SPACE, OG_TYPE } from "./og-tokens";

/** Footer address used when a recipe gets no `url`. */
export const OG_DEFAULT_URL = "github.com/dxbh0517/dxbh-ui";

type Shared = {
  /** Wordmark text. Defaults to "dxbh-ui". */
  brand?: string;
  /** Site address in the footer. */
  url?: string;
  /** Right-hand footer note. */
  meta?: ReactNode;
};

export type OgHomeCardProps = Shared & {
  title: string;
  description?: string;
};

/** The site card: wordmark over a double keyline, the largest headline, one line of lead. */
export function OgHomeCard({ title, description, brand, url = OG_DEFAULT_URL, meta = `${OG_SIZE.width} × ${OG_SIZE.height}` }: OgHomeCardProps) {
  return (
    <OgCanvas>
      <div style={{ display: "flex", flexDirection: "column", gap: OG_SPACE.stack }}>
        <OgBrand name={brand} />
        <OgRule variant="double" width={168} />
      </div>
      <div style={{ display: "flex", flexDirection: "column", gap: OG_SPACE.stack }}>
        <OgHeadline>{title}</OgHeadline>
        {description ? <OgLead>{description}</OgLead> : null}
      </div>
      <OgFooter url={url} meta={meta} />
    </OgCanvas>
  );
}

export type OgDocCardProps = Shared & {
  title: string;
  description?: string;
  /** Mono path beside the wordmark, such as "docs / page-states". */
  section?: string;
};

/** A docs page card: wordmark and section path, the page title, its description. */
export function OgDocCard({ title, description, brand, section = "docs", url = OG_DEFAULT_URL, meta }: OgDocCardProps) {
  return (
    <OgCanvas>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
        <OgBrand name={brand} />
        <div style={{ display: "flex", fontFamily: OG_FONT.mono, color: OG_COLOR.faint, ...OG_TYPE.meta }}>{section}</div>
      </div>
      <div style={{ display: "flex", flexDirection: "column", gap: OG_SPACE.stack }}>
        <OgHeadline>{title}</OgHeadline>
        {description ? <OgLead>{description}</OgLead> : null}
      </div>
      <OgFooter url={url} meta={meta} />
    </OgCanvas>
  );
}

export type OgErrorCardProps = Shared & {
  status: number | string;
  title: string;
  description?: string;
};

/** An error page card: the status code set large in mono, the title and description beside it. */
export function OgErrorCard({ status, title, description, brand, url = OG_DEFAULT_URL, meta = "error" }: OgErrorCardProps) {
  return (
    <OgCanvas>
      <OgBrand name={brand} />
      <div style={{ display: "flex", alignItems: "flex-end", gap: 56 }}>
        <OgStatus code={status} label={`http ${status}`} />
        <div style={{ display: "flex", flexDirection: "column", gap: 20, flexShrink: 1, paddingBottom: 46 }}>
          <OgHeadline fit={false} style={{ fontSize: 52, letterSpacing: -1.6, lineHeight: 1.08, maxWidth: 520 }}>
            {title}
          </OgHeadline>
          {description ? <OgLead style={{ fontSize: 26, maxWidth: 520 }}>{description}</OgLead> : null}
        </div>
      </div>
      <OgFooter url={url} meta={meta} />
    </OgCanvas>
  );
}
og-tokens.ts
/* Hallmark · component: og-image · genre: modern-minimal · theme: Blueprint (1/3, dealt) · states: preview · contrast: pass · v2.0.0 */
// Critique: cyanotype sheet + white keylines for share cards; no blur (ImageResponse + Blueprint).

// Tokens for Open Graph cards rendered by an ImageResponse renderer (Satori)
// or by the DOM. Satori has no CSS variables and no oklch, so every colour is
// a literal sRGB value; the oklch source is kept beside each one.

/** Open Graph canvas, in CSS pixels. */
export const OG_SIZE = { width: 1200, height: 630 } as const;

export const OG_COLOR = {
  /** Prussian-blue sheet. oklch(35% 0.105 256). */
  sheet: "#0d3970",
  /** Line ink: the keylines, the wordmark, the headline. */
  ink: "#ffffff",
  /** Supporting copy. oklch(88% 0.03 240); 8.3:1 on the sheet. */
  muted: "#c7dbea",
  /** Measurements and captions. oklch(75% 0.05 245); 5.6:1 on the sheet. */
  faint: "#94b2cc",
  /** Hairline rules and the inner keyline. */
  rule: "rgba(255, 255, 255, 0.55)",
  /** The drafting grid behind everything. */
  grid: "rgba(255, 255, 255, 0.07)",
} as const;

/** Font stacks as plain strings. Pass matching `fonts` to ImageResponse to get them; otherwise it falls back to its default sans. */
export const OG_FONT = {
  sans: '"Geist", "Inter", ui-sans-serif, system-ui, sans-serif',
  mono: '"Geist Mono", ui-monospace, "SF Mono", Menlo, monospace',
} as const;

/** Type scale. Display sizes track tighter as they grow, as optical sizing would. */
export const OG_TYPE = {
  brand: { fontSize: 34, fontWeight: 600, letterSpacing: -1, lineHeight: 1 },
  headline: { fontSize: 76, fontWeight: 600, letterSpacing: -2.8, lineHeight: 1.04 },
  lead: { fontSize: 30, fontWeight: 400, letterSpacing: -0.3, lineHeight: 1.36 },
  status: { fontSize: 232, fontWeight: 500, letterSpacing: -10, lineHeight: 0.9 },
  meta: { fontSize: 20, fontWeight: 400, letterSpacing: 0, lineHeight: 1 },
} as const;

/** Spatial system: one margin, one keyline inset, one grid cell. */
export const OG_SPACE = {
  /** Sheet edge to the outer keyline. */
  frame: 28,
  /** Gap between the two keylines of the double frame. */
  keylineGap: 6,
  /** Outer keyline to content. */
  inset: 64,
  /** Drafting grid cell. */
  grid: 40,
  /** Vertical rhythm between stacked blocks. */
  stack: 28,
} as const;

/** Headline size that keeps long titles on about three lines. */
export function fitHeadline(text: string): { fontSize: number; letterSpacing: number } {
  const n = text.length;
  if (n <= 28) return { fontSize: 88, letterSpacing: -3.4 };
  if (n <= 48) return { fontSize: OG_TYPE.headline.fontSize, letterSpacing: OG_TYPE.headline.letterSpacing };
  if (n <= 72) return { fontSize: 64, letterSpacing: -2.2 };
  return { fontSize: 54, letterSpacing: -1.6 };
}

Extracted from dxbh-ui/registry/dxbh-ui/ui/og-image at 2c3f704.