# Insight Cards

> Show a set of findings one at a time, with buttons to move between them.

Source: https://www.honestui.com/docs/ai/insight-cards

```tsx
"use client"

import { InsightCard, InsightCards } from "@/components/ai/insight-cards"
import { BarChart, type ChartConfig } from "honestui/charts"
import { LineChart } from "honestui/charts"
import { PieChart } from "honestui/charts"

// Sample figures.
const failedRuns = [
  { week: "Sep 1", failed: 9 },
  { week: "Sep 8", failed: 7 },
  { week: "Sep 15", failed: 8 },
  { week: "Sep 22", failed: 4 },
  { week: "Sep 29", failed: 3 },
  { week: "Oct 6", failed: 2 },
]

const failedRunsConfig = {
  // Ink on paper: one quiet series needs no hue. The gray scale flips with the
  // theme, so the same steps read in light and dark.
  failed: { label: "Failed runs", colors: { light: ["var(--hui-gray-10)"], dark: ["var(--hui-gray-10)"] } },
} satisfies ChartConfig

const lateInvoices = [
  { supplier: "harbor", invoices: 10 },
  { supplier: "northwind", invoices: 7 },
  { supplier: "others", invoices: 7 },
]

const lateInvoicesConfig = {
  harbor: { label: "Harbor Paper Co.", colors: { light: ["var(--hui-gray-12)"], dark: ["var(--hui-gray-12)"] } },
  northwind: { label: "Northwind Dairy", colors: { light: ["var(--hui-gray-10)"], dark: ["var(--hui-gray-10)"] } },
  others: { label: "All others", colors: { light: ["var(--hui-gray-7)"], dark: ["var(--hui-gray-7)"] } },
} satisfies ChartConfig

const replyTimes = [
  { day: "Mon", agent: 42, team: 95 },
  { day: "Tue", agent: 38, team: 110 },
  { day: "Wed", agent: 35, team: 88 },
  { day: "Thu", agent: 31, team: 102 },
  { day: "Fri", agent: 29, team: 97 },
]

const replyTimesConfig = {
  agent: { label: "Agent", colors: { light: ["var(--hui-gray-12)"], dark: ["var(--hui-gray-12)"] } },
  team: { label: "Team", colors: { light: ["var(--hui-gray-8)"], dark: ["var(--hui-gray-8)"] } },
} satisfies ChartConfig

const chartFrame = "h-44 w-full min-w-0"
const summary = "m-0 text-[var(--hui-color-foreground-base-secondary)]"

/**
 * The charts start drawn, so the evidence is there when a card appears.
 * Each card states its finding, plots it with an Honest UI chart, and then
 * gives the plotted values as text, because a canvas does not expose them.
 */
export function InsightCardsDemo() {
  return (
    <div className="w-full max-w-md min-w-0">
      <InsightCards title="Insights">
        <InsightCard title="Failed runs fell from 9 to 2 a week after the rounding fix.">
          <div className={chartFrame}>
            <BarChart data={failedRuns} config={failedRunsConfig} animation={false} className="h-full w-full">
              <BarChart.Grid />
              <BarChart.XAxis dataKey="week" />
              <BarChart.YAxis />
              <BarChart.Tooltip />
              <BarChart.Bar dataKey="failed" variant="default" />
            </BarChart>
          </div>
          <p className={summary}>
            Failed runs per week, oldest first: {failedRuns.map((row) => row.failed).join(", ")}.
          </p>
        </InsightCard>

        <InsightCard title="Two suppliers account for 71% of late invoices.">
          <div className={chartFrame}>
            <PieChart
              data={lateInvoices}
              dataKey="invoices"
              nameKey="supplier"
              config={lateInvoicesConfig}
              animation={false}
              className="h-full w-full"
            >
              <PieChart.Legend />
              <PieChart.Tooltip />
              <PieChart.Pie innerRadius="36%" outerRadius="64%" />
            </PieChart>
          </div>
          <p className={summary}>
            Of 24 late invoices this quarter, Harbor Paper Co. sent 10, Northwind Dairy 7, and all
            other suppliers 7.
          </p>
        </InsightCard>

        <InsightCard title="The agent's first reply is about three times faster than the team's.">
          <div className={chartFrame}>
            <LineChart
              data={replyTimes}
              config={replyTimesConfig}
              xDataKey="day"
              animation={false}
              className="h-full w-full"
            >
              <LineChart.Grid />
              <LineChart.XAxis dataKey="day" />
              <LineChart.Legend />
              <LineChart.Tooltip />
              <LineChart.Line dataKey="team" strokeVariant="dashed" strokeWidth={2} />
              <LineChart.Line dataKey="agent" strokeVariant="solid" strokeWidth={2} />
            </LineChart>
          </div>
          <p className={summary}>
            Median minutes to first reply this week: agent 29 to 42, team 88 to 110.
          </p>
        </InsightCard>
      </InsightCards>
    </div>
  )
}

```

## Overview [#overview]

Insight Cards shows several findings in the space of one. Each card states a finding as a sentence and then shows the evidence for it: a chart, a figure, a short explanation. Two buttons move between the cards.

It never advances by itself. The Beautiful UI original rotated on a timer, which moves content a person may be reading.

Insight Cards draws no charts itself. Put an Honest UI [chart](/docs/charts) in each card, as the preview above does with a bar, a pie, and a line chart.

## Anatomy [#anatomy]

<Anatomy title="InsightCards">
  <AnatomyItem name="Heading" description="The title, the position, and the Previous and Next buttons" />

  <AnatomyItem name="InsightCard" description="One finding and its evidence" />
</Anatomy>

Installing `insight-cards` brings [Button](/docs/components/button) along as a registry dependency.

## Behavior [#behavior]

### Moving between cards [#moving-between-cards]

One card is shown at a time, and only that card is mounted, so a chart in another card does not render until a person moves to it. Previous and Next are inactive at the ends. With a single card, the position and the buttons are left out.

### Height [#height]

The position and the buttons sit above the card, so they stay put when cards differ in height. The content below the component moves when they do. Giving every chart the same height, as the preview does, keeps the cards close to one size.

## Accessibility [#accessibility]

The component is a section named by its heading. The position, such as "2 of 3", is a polite live region, so moving to another card announces it. Previous and Next have names, and at either end they are marked inactive without losing focus.

Each card is an article. State the finding in `title`, and give the plotted values as text under the chart. A chart is a canvas: its tooltip needs a pointer, and its values are not exposed to screen readers.

| Key                                  | Action                         |
| ------------------------------------ | ------------------------------ |
| <kbd>Tab</kbd>                       | Move to Previous and Next      |
| <kbd>Enter</kbd> or <kbd>Space</kbd> | Show the previous or next card |

## Installation [#installation]


  

  
    <CliBlock commands="[&#x22;@honestai/insight-cards&#x22;]" />
  

  
    
      
        Add the components it is built from.
      

      <CliBlock commands="[&#x22;button&#x22;]" />

      
        Copy and paste this file into your project.
      

      ### components/ai/insight-cards.tsx

```tsx
"use client"

import * as React from "react"
import { ChevronLeft as ChevronLeftIcon, ChevronRight as ChevronRightIcon } from "honestui/icons"

import { cn } from "@/lib/utils"
import { Button } from "@/components/honest-ui/ui/button"

export type InsightCardsLabels = {
  previous: string
  next: string
  position: (current: number, total: number) => string
}

export const defaultInsightCardsLabels: InsightCardsLabels = {
  previous: "Previous insight",
  next: "Next insight",
  position: (current, total) => `${current} of ${total}`,
}

// The Button primitive's disabled look, kept focusable with aria-disabled.
// Shared with Chat Thread's version pager.
const pagerDisabled =
  "aria-disabled:cursor-not-allowed aria-disabled:bg-transparent aria-disabled:text-[var(--hui-color-foreground-base-tertiary)] aria-disabled:shadow-none aria-disabled:hover:bg-transparent aria-disabled:active:scale-100!"

export type InsightCardsProps = Omit<React.ComponentProps<"section">, "title"> & {
  /** The heading, such as "Insights". */
  title: string
  /** One `InsightCard` for each insight. */
  children: React.ReactNode
  labels?: Partial<InsightCardsLabels>
}

/**
 * A set of findings shown one at a time, with buttons to move between them.
 * It never advances by itself.
 */
function InsightCards({ title, children, labels: labelsProp, className, ...props }: InsightCardsProps) {
  const labels = { ...defaultInsightCardsLabels, ...labelsProp }
  const cards = React.Children.toArray(children)
  const [index, setIndex] = React.useState(0)
  const current = Math.min(index, Math.max(cards.length - 1, 0))
  const headingId = React.useId()

  if (cards.length === 0) return null

  return (
    <section
      aria-labelledby={headingId}
      data-slot="insight-cards"
      className={cn(
        "flex w-full min-w-0 flex-col gap-[var(--hui-space-3)] text-[var(--hui-color-foreground-base-primary)] [font-size:var(--hui-font-size-regular)] [letter-spacing:var(--hui-letter-spacing-regular)] [line-height:var(--hui-line-height-regular)]",
        className
      )}
      {...props}
    >
      <div className="flex min-h-[var(--hui-control-height-sm,var(--hui-space-8))] items-center gap-[var(--hui-space-3)]">
        <p id={headingId} title={title} className="m-0 min-w-0 flex-1 truncate [font-weight:var(--hui-font-weight-medium)]">
          {title}
        </p>
        {cards.length > 1 && (
          <div className="flex shrink-0 items-center gap-[var(--hui-space-1)]">
            <Button
              variant="ghost"
              size="icon-sm"
              aria-label={labels.previous}
              // aria-disabled keeps focus on the button at either end.
              aria-disabled={current === 0}
              className={pagerDisabled}
              onClick={() => setIndex(Math.max(current - 1, 0))}
            >
              <ChevronLeftIcon aria-hidden="true" />
            </Button>
            <span
              aria-live="polite"
              className="px-[var(--hui-space-2)] text-center whitespace-nowrap text-[var(--hui-color-foreground-base-secondary)] [font-size:var(--hui-font-size-small)] [letter-spacing:var(--hui-letter-spacing-small)] [line-height:var(--hui-line-height-small)]"
            >
              {/* Tabular figures on the digits only, since the tabular feature can also widen letters. */}
              {labels.position(current + 1, cards.length)
                .split(/(\d+)/)
                .map((part, partIndex) =>
                  partIndex % 2 === 1 ? (
                    <span key={partIndex} className="tabular-nums">
                      {part}
                    </span>
                  ) : (
                    part
                  )
                )}
            </span>
            <Button
              variant="ghost"
              size="icon-sm"
              aria-label={labels.next}
              aria-disabled={current === cards.length - 1}
              className={pagerDisabled}
              onClick={() => setIndex(Math.min(current + 1, cards.length - 1))}
            >
              <ChevronRightIcon aria-hidden="true" />
            </Button>
          </div>
        )}
      </div>
      {/* Only the current card is mounted, so charts in the others do not run
          until a person reaches them. The pager sits above the card, so a
          shorter or taller card never moves it. A new card fades in. */}
      <div
        key={current}
        className="flex min-w-0 flex-col motion-safe:transition-opacity motion-safe:duration-150 motion-safe:ease-out motion-safe:starting:opacity-0"
      >
        {cards[current]}
      </div>
    </section>
  )
}

export type InsightCardProps = Omit<React.ComponentProps<"article">, "title"> & {
  /** The finding, stated as a sentence. */
  title: React.ReactNode
}

/** One insight: its finding, then the evidence, such as a chart or a figure. */
function InsightCard({ title, className, children, ...props }: InsightCardProps) {
  return (
    <article
      data-slot="insight-card"
      className={cn(
        "flex min-w-0 flex-1 flex-col gap-[var(--hui-space-4)] rounded-[var(--hui-radius-4)] border-[0.5px] border-[var(--hui-color-border-base-primary)] bg-[var(--hui-color-background-base-primary)] p-[var(--hui-space-5)] shadow-[var(--hui-shadow-feather)]",
        className
      )}
      {...props}
    >
      <p className="m-0 break-words [font-weight:var(--hui-font-weight-medium)]">
        {title}
      </p>
      {children}
    </article>
  )
}

export { InsightCards, InsightCard }

```

      
        Update the import paths to match your project setup.
      
    
  


## Usage [#usage]

```tsx
import { BarChart, type ChartConfig } from "honestui/charts"
import "honestui/charts.css"

import { InsightCard, InsightCards } from "@/components/ai/insight-cards"

const config = {
  failed: { label: "Failed runs", colors: { light: ["#be123c"], dark: ["#fb7185"] } },
} satisfies ChartConfig

<InsightCards title="Insights">
  <InsightCard title={insight.finding}>
    <div className="h-44 w-full">
      <BarChart data={insight.rows} config={config} className="h-full w-full">
        <BarChart.Grid />
        <BarChart.XAxis dataKey="week" />
        <BarChart.Tooltip />
        <BarChart.Bar dataKey="failed" />
      </BarChart>
    </div>
    <p>{insight.summary}</p>
  </InsightCard>
</InsightCards>
```

Give the chart a container with a height, or it has no room to draw. Charts come from the `honestui` package; see [chart installation](/docs/charts/installation).

## Examples [#examples]

### A single insight [#a-single-insight]

With one card, there is nothing to move between.

```tsx
import { InsightCard, InsightCards } from "@/components/ai/insight-cards"

export function InsightCardsSingleExample() {
  return (
    <div className="w-full max-w-md min-w-0">
      <InsightCards title="Latest insight">
        <InsightCard title="Export totals now match the ledger on all 1,204 invoices.">
          <p className="m-0 text-[var(--hui-color-foreground-base-secondary)]">
            With one card, the position and the buttons are left out.
          </p>
        </InsightCard>
      </InsightCards>
    </div>
  )
}

```

## API reference [#api-reference]

### InsightCards [#insightcards]

Accepts native section props beyond those listed. Its children are the cards.

| Prop     | Type                          | Default  | Description                         |
| -------- | ----------------------------- | -------- | ----------------------------------- |
| `title`  | string                        | Required | The heading.                        |
| `labels` | `Partial<InsightCardsLabels>` | English  | Button names and the position text. |

### InsightCard [#insightcard]

Accepts native article props beyond those listed. Its children are the evidence.

| Prop    | Type      | Default  | Description                        |
| ------- | --------- | -------- | ---------------------------------- |
| `title` | ReactNode | Required | The finding, stated as a sentence. |
