AI Sources

Where an answer came from: inline numbered citations with a source card, a favicon pile that opens into a list, and a scrolling rail of source cards.

Installation

Installation

One command adds ai-sources and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.

$npx shadcn@latest add https://tweenui.dev/r/ai-sources.json
ai-sources-demo
components/ui/ai-sources-demo.tsx
"use client"

import * as React from "react"

import { AiDemo } from "@/lib/ai-demo"
import { useAiLoop } from "@/lib/ai-kit"
import {
  AiCitations,
  AiSourcePile,
  AiSourceRail,
  type AiSource,
} from "@/registry/tween/ai-sources"

const SOURCES: AiSource[] = [
  {
    id: "effects",
    title: "Synchronizing with Effects",
    domain: "react.dev",
    url: "https://react.dev/learn/synchronizing-with-effects",
    brand: "react",
    snippet:
      "Return a cleanup function so the effect stops whatever it started.",
    relevance: 0.94,
  },
  {
    id: "motion",
    title: "prefers-reduced-motion",
    domain: "developer.mozilla.org",
    url: "https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion",
    brand: "mdnwebdocs",
    snippet:
      "Detects whether the user asked the system to minimise non-essential motion.",
    relevance: 0.88,
  },
  {
    id: "theme",
    title: "Theme variables",
    domain: "tailwindcss.com",
    url: "https://tailwindcss.com/docs/theme",
    brand: "tailwindcss",
    snippet: "Design tokens are defined in CSS with the theme directive.",
    relevance: 0.81,
  },
  {
    id: "registry",
    title: "shadcn-ui/ui registry schema",
    domain: "github.com",
    url: "https://github.com/shadcn-ui/ui",
    brand: "github",
    snippet:
      "Registry items can carry CSS variables and keyframes alongside files.",
    relevance: 0.72,
  },
  {
    id: "parts",
    title: "AI SDK message parts",
    domain: "vercel.com",
    brand: "vercel",
    snippet:
      "A message is a list of typed parts: text, reasoning, tools and sources.",
    relevance: 0.6,
  },
]

function Inline() {
  const root = React.useRef<HTMLDivElement>(null)
  const [active, setActive] = React.useState<string | null>("effects")
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      for (const id of ["effects", "motion", "theme"]) {
        setActive(id)
        await sleep(2100)
      }
    },
    { gap: 0, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 380px)" }}
      onPointerDown={() => setHands(true)}
    >
      <AiCitations
        sources={SOURCES.slice(0, 3)}
        active={active}
        onActiveChange={setActive}
        parts={[
          "Clean up every animation in the effect's return ",
          { cite: "effects" },
          ". Respect reduced motion and show the final state instead ",
          { cite: "motion" },
          ". Tailwind v4 keeps theme tokens in CSS, so the eases can live there too ",
          { cite: "theme" },
          ".",
        ]}
      />
    </div>
  )
}

function Pile() {
  const root = React.useRef<HTMLDivElement>(null)
  const [open, setOpen] = React.useState(true)
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setOpen(false)
      await sleep(1500)
      setOpen(true)
      await sleep(3600)
    },
    { gap: 0, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 360px)" }}
      onPointerDown={() => setHands(true)}
    >
      <AiSourcePile
        sources={SOURCES.slice(0, 4)}
        open={open}
        onOpenChange={setOpen}
      />
    </div>
  )
}

function Rail() {
  const root = React.useRef<HTMLDivElement>(null)
  const [index, setIndex] = React.useState<number | undefined>(undefined)
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      for (const at of [0, 1, 2, 3, 0]) {
        setIndex(at)
        await sleep(1900)
      }
    },
    { gap: 200, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 460px)", minWidth: 0 }}
      onPointerDown={() => {
        setHands(true)
        setIndex(undefined)
      }}
      onWheel={() => {
        setHands(true)
        setIndex(undefined)
      }}
    >
      <AiSourceRail sources={SOURCES} index={index} />
    </div>
  )
}

export function AiSourcesDemo({ only }: { only?: string }) {
  return (
    <AiDemo
      only={only}
      tabs={[
        { name: "Inline citations", node: <Inline /> },
        { name: "Pile", node: <Pile /> },
        { name: "Rail", node: <Rail /> },
      ]}
    />
  )
}

Installation

pnpm dlx shadcn@latest add https://tweenui.dev/r/ai-sources.json

Usage

import {
  AiCitations,
  AiSourcePile,
  AiSourceRail,
} from "@/components/ui/ai-sources"
const sources = [
  { id: "effects", title: "Synchronizing with Effects", domain: "react.dev", url: "https://react.dev/learn/synchronizing-with-effects", brand: "react", relevance: 0.94 },
]
 
<AiCitations
  sources={sources}
  parts={["Clean up every animation in the effect's return ", { cite: "effects" }, "."]}
/>
 
<AiSourcePile sources={sources} />
 
<AiSourceRail sources={sources} />

With the AI SDK, map each source-url part to a source: sourceId to id, title to title, and the URL's host to domain. brand is a Simple Icons slug for the favicon slot; a globe shows without one.

Versions

  • AiCitations puts numbered chips in the sentence. Pressing one turns it blue and swaps its source card in underneath; pressing it again clears it.
  • AiSourcePile is a row of overlapping favicons with a count. It opens into the full list with staggered rows and fades back when closed.
  • AiSourceRail is a row of cards that scrolls and snaps inside its own track. The arrows and dots follow the scroll position, and each match bar fills when its card first comes into view.

Props

AiCitations

PropTypeDefaultDescription
parts(string | { cite: string })[]The answer as text and citations, in order.
sourcesAiSource[]Numbered in this order.
activestring | nullThe source whose card shows. Omit to let the chips manage it.
onActiveChange(id: string | null) => voidCalled when a chip is pressed.

AiSourcePile

PropTypeDefaultDescription
sourcesAiSource[]The first four show in the pile.
openbooleanControls the list.
defaultOpenbooleanfalseStarting state when uncontrolled.
onOpenChange(open: boolean) => voidCalled from the pile button.

AiSourceRail

PropTypeDefaultDescription
sourcesAiSource[]One card each.
indexnumberScrolls this card to the leading edge when it changes.
onIndexChange(index: number) => voidCalled as scrolling changes the leading card.

AiSource

FieldTypeDescription
idstringUnique id, used by citations.
titlestringThe page title.
domainstringThe site, such as react.dev.
urlstringMakes the source a link that opens in a new tab.
brandstringSimple Icons slug for the favicon.
snippetstringA sentence or two, shown on rail cards.
relevancenumberMatch from 0 to 1, shown as a bar on rail cards.