# Grok Link Preview

A link unfurled into its cover image, site, title and description.

Build a Link Preview in this project that looks and behaves like Grok's. Measured from the running Grok app (grok.com). Keep every value exact. Map the tokens onto the project's own theme variables instead of hard-coding them. Use the project's own component library for behaviour, or Base UI primitives if it has none.

Fidelity: Measured from Grok's page card, light and dark. The 22px radius card, image, site row with its icon tile, title and two-line description are measured. X-VF isn't shipped, so text renders heavier than Grok.

## Tokens

| Token | Light | Dark |
| --- | --- | --- |
| `--grok-border-focus-primary` | `color-mix(in srgb, var(--grok-accent) var(--grok-alpha-focus-ring), transparent)` | `color-mix(in srgb, var(--grok-accent) var(--grok-alpha-focus-ring), transparent)` |
| `--grok-border-width-hairline` | `0.5px` | `0.5px` |
| `--grok-card-bg` | `rgb(255 255 255)` | `rgb(35 33 31)` |
| `--grok-card-shadow-raised` | `0 0 0 var(--grok-border-width-hairline) var(--grok-card-ring), 0 1px 4px rgb(0 0 0 / 0.07), 0 6px 12px rgb(0 0 0 / 0.035)` | `0 0 0 var(--grok-border-width-hairline) var(--grok-card-ring), 0 1px 4px rgb(0 0 0 / 0.07), 0 6px 12px rgb(0 0 0 / 0.035)` |
| `--grok-card-text` | `rgb(5 5 5)` | `rgb(252 252 252)` |
| `--grok-card-text-muted` | `rgb(99 99 99)` | `rgb(158 158 158)` |
| `--grok-card-divider` | `rgb(5 5 5 / 0.06)` | `rgb(252 252 252 / 0.06)` |
| `--grok-card-chip-border` | `rgb(5 5 5 / 0.08)` | `rgb(252 252 252 / 0.06)` |
| `--grok-font-weight-card-title` | `550` | `550` |
| `--grok-font-weight-card-label` | `500` | `500` |
| `--grok-font-sans` | `"X-VF", system-ui, -apple-system, "Segoe UI", sans-serif` | `"X-VF", system-ui, -apple-system, "Segoe UI", sans-serif` |
| `--grok-font-size-section-headline` | `13px` | `13px` |
| `--grok-font-size-chrome` | `14px` | `14px` |
| `--grok-font-weight-regular` | `425` | `425` |
| `--grok-line-height-compact` | `20px` | `20px` |
| `--grok-letter-spacing-body` | `0.0027em` | `0.0027em` |
| `--grok-radius-xs` | `4px` | `4px` |
| `--grok-space-0-25` | `1px` | `1px` |
| `--grok-space-1` | `4px` | `4px` |
| `--grok-space-1-5` | `6px` | `6px` |
| `--grok-space-4` | `16px` | `16px` |
| `--grok-icon-size-sm` | `16px` | `16px` |

## CSS

Scoped under `[data-style="grok"]`; dark rules sit under `[data-theme="dark"]`. The `ui-*` classes and data attributes match the structure below.

```css
[data-style="grok"] {
  .ui-link-preview {
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
    border-radius: 22px;
    background-color: var(--grok-card-bg);
    box-shadow: var(--grok-card-shadow-raised);
    color: var(--grok-card-text);
    font-family: var(--grok-font-sans);
    font-size: var(--grok-font-size-chrome);
    font-weight: var(--grok-font-weight-regular);
    line-height: var(--grok-line-height-compact);
    letter-spacing: var(--grok-letter-spacing-body);
    text-decoration: none;
    outline: none;

    &:focus-visible {
      box-shadow:
        var(--grok-card-shadow-raised),
        0 0 0 2px var(--grok-border-focus-primary);
    }
  }

  .ui-link-preview-media {
    display: block;
    overflow: hidden;
    border-bottom: 1px solid var(--grok-card-divider);

    & img {
      display: block;
      width: 100%;
      height: auto;
      max-height: 294px;
      object-fit: cover;
    }
  }

  .ui-link-preview-content {
    display: flex;
    flex-direction: column;
    gap: var(--grok-space-0-25);
    min-width: 0;
    padding: var(--grok-space-4);
  }

  .ui-link-preview-site {
    display: flex;
    align-items: center;
    gap: var(--grok-space-1-5);
    min-width: 0;
    margin-bottom: var(--grok-space-1);
    overflow: hidden;
    color: var(--grok-card-text-muted);
    font-size: var(--grok-font-size-section-headline);
    line-height: 18px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .ui-link-preview-icon {
    display: inline-flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: var(--grok-icon-size-sm);
    height: var(--grok-icon-size-sm);
    overflow: hidden;
    border-radius: var(--grok-radius-xs);
    background-color: var(--grok-card-bg);
    box-shadow:
      0 0 0 var(--grok-border-width-hairline) var(--grok-card-chip-border),
      0 2px 6px rgb(0 0 0 / 0.06);
    color: var(--grok-card-text-muted);
    font-size: 10px;
    font-weight: var(--grok-font-weight-card-title);
    line-height: 1;

    & img,
    & svg {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .ui-link-preview-title {
    overflow: hidden;
    color: var(--grok-card-text);
    font-weight: var(--grok-font-weight-card-label);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .ui-link-preview-description {
    display: -webkit-box;
    overflow: hidden;
    color: var(--grok-card-text-muted);
    font-size: var(--grok-font-size-section-headline);
    line-height: 18px;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }
}
```

## Structure

How Replicas assembles it on Base UI.

```tsx
import type { ComponentProps, ReactNode } from "react";
import { join } from "./utils";

function Root({ className, target = "_blank", rel = "noreferrer", ...props }: ComponentProps<"a">) {
  return <a className={join("ui-link-preview", className)} target={target} rel={rel} {...props} />;
}

type MediaProps = Omit<ComponentProps<"span">, "children"> & { src: string; alt?: string };

function Media({ className, src, alt = "", ...props }: MediaProps) {
  return (
    <span className={join("ui-link-preview-media", className)} {...props}>
      <img src={src} alt={alt} />
    </span>
  );
}

function Content({ className, ...props }: ComponentProps<"span">) {
  return <span className={join("ui-link-preview-content", className)} {...props} />;
}

type SiteProps = ComponentProps<"span"> & { icon?: ReactNode };

function Site({ className, icon, children, ...props }: SiteProps) {
  return (
    <span className={join("ui-link-preview-site", className)} {...props}>
      {icon ? (
        <span className="ui-link-preview-icon" aria-hidden>
          {icon}
        </span>
      ) : null}
      {children}
    </span>
  );
}

function Title({ className, ...props }: ComponentProps<"span">) {
  return <span className={join("ui-link-preview-title", className)} {...props} />;
}

function Description({ className, ...props }: ComponentProps<"span">) {
  return <span className={join("ui-link-preview-description", className)} {...props} />;
}

export const LinkPreview = {
  Root,
  Media,
  Content,
  Site,
  Title,
  Description,
};
```

## Example

```tsx
import { LinkPreview } from "@replicas/ui";

const cover = `data:image/svg+xml,${encodeURIComponent(
  '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 126"><rect width="240" height="126" fill="#f4f1ec"/><rect x="24" y="28" width="120" height="12" rx="3" fill="#2b2b2b"/><rect x="24" y="50" width="160" height="8" rx="2" fill="#b9b3aa"/><rect x="24" y="64" width="136" height="8" rx="2" fill="#b9b3aa"/><rect x="24" y="90" width="56" height="16" rx="8" fill="#2b2b2b"/></svg>',
)}`;

export default function LinkPreviewDemo() {
  return (
    <LinkPreview.Root href="https://example.com/guides/streaming" style={{ width: "100%", maxWidth: 520 }}>
      <LinkPreview.Media src={cover} />
      <LinkPreview.Content>
        <LinkPreview.Site icon="E">example.com</LinkPreview.Site>
        <LinkPreview.Title>Streaming responses from the edge</LinkPreview.Title>
        <LinkPreview.Description>
          How to stream model output to the browser token by token, with back-pressure and cancellation.
        </LinkPreview.Description>
      </LinkPreview.Content>
    </LinkPreview.Root>
  );
}
```

## Foundations

The rest of the Grok style, as a skill: https://replicas.raphaelsalaja.com/skills/grok/SKILL.md