Relic UI

AI

Confidence Text

AI text that shows how sure it is about every word, with highlights, underlines or fading.

The Eiffel Tower was finished in 1889 and is about 330 (unsure, 41%) metres tall, repainted every seven (unsure, 38%) years.

Installation

npx shadcn@latest add https://relic-ui.vercel.app/r/confidence-text.json

Or straight from GitHub: npx shadcn@latest add relicui/relic-ui/confidence-text

Manual installation

1. Install the dependencies:

npm install motion

2. Copy this code into components/ui/confidence-text.tsx:

"use client"

/**
 * Relic UI · Confidence Text
 * https://relic-ui.vercel.app/docs/confidence-text
 *
 * Copyright (c) 2026 Shubham Tambe
 * Open source under the MIT License: https://relic-ui.vercel.app/license
 */

import { motion, useReducedMotion } from "motion/react"

import { cn } from "@/lib/utils"

export type ConfidenceToken = {
  text: string
  /** How sure the AI is about this word, from 0 to 1. */
  confidence: number
}

export interface ConfidenceTextProps
  extends Omit<React.ComponentProps<"div">, "children"> {
  /** The words, each with a confidence between 0 and 1. */
  tokens: ConfidenceToken[]
  /** How doubt is shown: a warm highlight, an underline, or fading. */
  variant?: "heat" | "underline" | "fade"
  /** Words below this confidence count as unsure (0–1). */
  threshold?: number
  /** Only mark the unsure words; leave confident ones plain. */
  onlyLow?: boolean
  /** Show the percentage when hovering or focusing an unsure word. */
  showTooltip?: boolean
  /** Show an "Unsure → Sure" scale under the text. */
  showLegend?: boolean
  /** Fade the words in one by one when they come into view. */
  animate?: boolean
  /** Speed multiplier for the fade-in. */
  speed?: number
}

const clamp01 = (v: number) => Math.min(Math.max(v, 0), 1)

/** The CSS that shows how unsure a word is. */
function doubtStyle(variant: ConfidenceTextProps["variant"], confidence: number, low: boolean) {
  const doubt = 1 - confidence
  if (variant === "underline") {
    return {
      textDecorationLine: "underline",
      textDecorationStyle: low ? ("wavy" as const) : ("solid" as const),
      textDecorationColor: `color-mix(in oklch, var(--destructive) ${Math.round(doubt * 100)}%, transparent)`,
      textDecorationThickness: `${(1 + doubt * 1.5).toFixed(1)}px`,
      textUnderlineOffset: "4px",
    }
  }
  if (variant === "fade") return { opacity: 0.35 + confidence * 0.65 }
  return {
    backgroundColor: `color-mix(in oklch, var(--destructive) ${Math.round(doubt * 42)}%, transparent)`,
  }
}

/**
 * AI-written text that shows how sure it is about every word. Unsure words
 * glow, get a wavy underline or fade, and reveal their certainty on hover.
 */
export function ConfidenceText({
  tokens,
  variant = "heat",
  threshold = 0.6,
  onlyLow = false,
  showTooltip = true,
  showLegend = false,
  animate = true,
  speed = 1,
  className,
  ...props
}: ConfidenceTextProps) {
  const reduceMotion = useReducedMotion()
  const fadeIn = animate && !reduceMotion

  return (
    <div data-slot="confidence-text" className={cn("w-full", className)} {...props}>
      <p className="leading-loose">
        {tokens.map((token, i) => {
          const confidence = clamp01(token.confidence)
          const low = confidence < threshold
          const marked = !onlyLow || low
          const percent = Math.round(confidence * 100)
          return (
            <span key={i}>
              {i > 0 && " "}
              <motion.span
                className="group relative inline-block outline-none"
                tabIndex={showTooltip && low ? 0 : undefined}
                initial={fadeIn ? { opacity: 0, y: 4 } : false}
                whileInView={{ opacity: 1, y: 0 }}
                viewport={{ once: true }}
                transition={{ delay: (i * 0.035) / Math.max(speed, 0.1), duration: 0.3 }}
              >
                <span
                  className={cn(
                    "rounded-[3px] px-px transition-colors",
                    low && showTooltip && "cursor-help group-focus-visible:ring-2 group-focus-visible:ring-ring/50"
                  )}
                  style={marked ? doubtStyle(variant, confidence, low) : undefined}
                >
                  {token.text}
                </span>
                {low && <span className="sr-only"> (unsure, {percent}%)</span>}
                {showTooltip && low && (
                  <span
                    aria-hidden
                    className="pointer-events-none absolute bottom-full left-1/2 z-10 mb-1 -translate-x-1/2 translate-y-1 rounded-md bg-foreground px-1.5 py-0.5 font-mono text-[10px] whitespace-nowrap text-background opacity-0 transition-[background-color,color,text-decoration-color,opacity,box-shadow] group-hover:translate-y-0 group-hover:opacity-100 group-focus-visible:translate-y-0 group-focus-visible:opacity-100"
                  >
                    {percent}% sure
                  </span>
                )}
              </motion.span>
            </span>
          )
        })}
      </p>
      {showLegend && (
        <div aria-hidden className="mt-3 flex items-center gap-2 text-xs text-muted-foreground">
          <span>Unsure</span>
          <span
            className="h-1.5 w-24 rounded-full"
            style={{
              background:
                variant === "fade"
                  ? "linear-gradient(to right, color-mix(in oklch, var(--foreground) 35%, transparent), var(--foreground))"
                  : "linear-gradient(to right, color-mix(in oklch, var(--destructive) 55%, transparent), transparent)",
            }}
          />
          <span>Sure</span>
        </div>
      )}
    </div>
  )
}

Usage

import { ConfidenceText } from "@/components/ui/confidence-text"

const tokens = [
  { text: "The", confidence: 0.99 },
  { text: "Eiffel", confidence: 0.98 },
  { text: "Tower", confidence: 0.99 },
  { text: "was", confidence: 0.97 },
  { text: "finished", confidence: 0.93 },
  { text: "in", confidence: 0.95 },
  { text: "1889", confidence: 0.82 },
  { text: "and", confidence: 0.96 },
  { text: "is", confidence: 0.97 },
  { text: "about", confidence: 0.7 },
  { text: "330", confidence: 0.41 },
  { text: "metres", confidence: 0.88 },
  { text: "tall,", confidence: 0.9 },
  { text: "repainted", confidence: 0.74 },
  { text: "every", confidence: 0.66 },
  { text: "seven", confidence: 0.38 },
  { text: "years.", confidence: 0.85 },
]

export default function ConfidenceTextDemo() {
  return (
    <div className="w-full max-w-md text-[15px]">
      <ConfidenceText tokens={tokens} showLegend />
    </div>
  )
}

Customize

Try the options below, then copy the code.

Mount Everest is roughly 8849 (unsure, 44%) metres tall and was first climbed in 1953 (unsure, 55%) by two climbers.

variant
<ConfidenceText
  tokens={tokens}
  showLegend={true}
/>

Props

PropTypeDefault
tokens

The words, each with a confidence from 0 to 1 (e.g. from token probabilities).

{ text: string; confidence: number }[]—
variant

How doubt is shown: a warm highlight, a (wavy) underline, or fading.

"heat" | "underline" | "fade""heat"
threshold

Words below this confidence count as unsure.

number0.6
onlyLow

Only mark the unsure words.

booleanfalse
showTooltip

Show “62% sure” when hovering or focusing an unsure word.

booleantrue
showLegend

Show an “Unsure → Sure” scale under the text.

booleanfalse
animate

Fade the words in one by one when they come into view.

booleantrue
speed

Speed multiplier for the fade-in.

number1

Accessibility

  • Unsure words get a hidden “(unsure, 42%)” note, so screen readers hear the doubt too.
  • Unsure words can be reached with Tab to show their tooltip.
  • Doubt is shown by shape and opacity as well as color.
  • Animation turns off automatically for users who enable reduced motion in their system settings.