An FAQ as a conversation: a person who types the answer

Build a chat-style FAQ in React: tap a question and watch a real person read it, type the answer in bursts, slip on a word and fix it, then send.

6 min read

The real component. Click the picture to open it live and try it before you read how it was built.

An accordion answers questions; a person answering them builds trust. This FAQ is a chat with someone real from the team.

Typing that feels human

A reply is typed as a list of frames: what the bubble shows at each keystroke and how long it stays. It grows in bursts, slips once on a long word and fixes it, then sends. Screen readers skip the draft and hear the finished message.

src/registry/faq/chat-reply/draft-frames.ts

// How a reply is typed out: what the bubble shows at each keystroke, and how long it stays.
export interface DraftFrame {
  text: string;
  delayMs: number;
}

const FRAME_MS = 32;
const SLIP_KEY_MS = 70;
const NOTICE_MS = 280;
const ERASE_MS = 55;
const BEFORE_SEND_MS = 320;
// The slip happens on the first long word after this share of the reply.
const SLIP_FROM = 0.3;
const MIN_TYPING_MS = 900;
const MAX_TYPING_MS = 2400;
const MS_PER_CHARACTER = 11;

// Long enough to read as typing, short enough that nobody waits for an answer.
export const getTypingMs = (text: string) =>
  Math.min(MAX_TYPING_MS, Math.max(MIN_TYPING_MS, text.length * MS_PER_CHARACTER));

// The first word of four letters or more past SLIP_FROM: where the two letters get swapped.
function findSlip(text: string): { at: number; typo: string } | null {
  for (const match of text.matchAll(/[a-z]{4,}/g)) {
    if (match.index < text.length * SLIP_FROM) continue;
    const [word] = match;
    return { at: match.index, typo: word[1] + word[0] + word[2] };
  }
  return null;
}

// Typing `text` from `from` to `to` in even bursts.
function typeRange(text: string, from: number, to: number, burst: number): DraftFrame[] {
  const frames: DraftFrame[] = [];
  for (let end = from + burst; end < to; end += burst) frames.push({ text: text.slice(0, end), delayMs: FRAME_MS });
  frames.push({ text: text.slice(0, to), delayMs: FRAME_MS });
  return frames;
}

// The reply typed by a person: words in short bursts, one slip on the way (two letters swapped, noticed, erased
// and typed again), then a breath before it is sent.
export function buildDraftFrames(text: string): DraftFrame[] {
  const burst = Math.max(1, Math.round(text.length / (getTypingMs(text) / FRAME_MS)));
  const slip = findSlip(text);
  if (!slip) return [...typeRange(text, 0, text.length, burst), { text, delayMs: BEFORE_SEND_MS }];

  const before = text.slice(0, slip.at);
  const wrong = [1, 2, 3].map((count) => ({ text: before + slip.typo.slice(0, count), delayMs: SLIP_KEY_MS }));
  wrong[wrong.length - 1].delayMs = NOTICE_MS;
  const erase = [2, 1, 0].map((count) => ({ text: before + slip.typo.slice(0, count), delayMs: ERASE_MS }));
  return [
    ...typeRange(text, 0, slip.at, burst),
    ...wrong,
    ...erase,
    ...typeRange(text, slip.at, text.length, burst),
    { text, delayMs: BEFORE_SEND_MS },
  ];
}
How a reply is typed

One conversation, two ways in

Questions sit both beside the person's card and as suggested replies in the chat. Both read the same conversation, so asking from either ticks the question off everywhere. Follow-ups of the last answer are offered first.

src/registry/faq/chat-reply/use-chat.ts

"use client";

import { useEffect, useRef, useState } from "react";
import type { ChatMessage, ChatQuestion } from "./types";

// After a question is sent: how long before it shows as read, then before the person starts typing.
const READ_MS = 450;
const START_TYPING_MS = 900;
const SUGGESTIONS_SHOWN = 3;

export interface Draft {
  id: string;
  text: string;
}

// The questions to offer next: the follow-ups of the last one answered first, then the rest in order.
function pickSuggestions(questions: ChatQuestion[], asked: ReadonlySet<string>, lastId: string | null): ChatQuestion[] {
  const open = questions.filter((question) => !asked.has(question.id));
  const followUps = questions.find((question) => question.id === lastId)?.followUps ?? [];
  const rank = (question: ChatQuestion) =>
    followUps.includes(question.id) ? followUps.indexOf(question.id) : followUps.length + open.indexOf(question);
  return [...open].sort((a, b) => rank(a) - rank(b)).slice(0, SUGGESTIONS_SHOWN);
}

// The thread as it starts: the greeting, then the first question already asked and answered.
function openingThread(greeting: string, first: ChatQuestion | undefined): ChatMessage[] {
  const opening: ChatMessage[] = [{ id: "greeting", from: "person", text: greeting }];
  if (first)
    opening.push(
      { id: `${first.id}-q`, from: "visitor", text: first.question },
      { id: `${first.id}-a`, from: "person", text: first.answer },
    );
  return opening;
}

// The conversation's state: the messages so far, which question was read, the reply being typed, and what to
// offer next. One question at a time: while a reply is on its way, nothing else can be asked.
export function useChat(questions: ChatQuestion[], greeting: string) {
  const [first] = questions;
  const [messages, setMessages] = useState(() => openingThread(greeting, first));
  const [asked, setAsked] = useState<ReadonlySet<string>>(() => new Set(first ? [first.id] : []));
  const [lastId, setLastId] = useState<string | null>(first?.id ?? null);
  const [readId, setReadId] = useState<string | null>(first ? `${first.id}-q` : null);
  const [draft, setDraft] = useState<Draft | null>(null);
  const [isBusy, setIsBusy] = useState(false);
  const timers = useRef<ReturnType<typeof setTimeout>[]>([]);

  useEffect(() => () => timers.current.forEach(clearTimeout), []);

  const send = (id: string, question: string, answer: string) => {
    if (isBusy) return;
    setIsBusy(true);
    setMessages((current) => [...current, { id: `${id}-q`, from: "visitor", text: question }]);
    timers.current.push(
      setTimeout(() => setReadId(`${id}-q`), READ_MS),
      setTimeout(() => setDraft({ id: `${id}-a`, text: answer }), START_TYPING_MS),
    );
  };

  const ask = (question: ChatQuestion) => {
    if (isBusy) return;
    setAsked((current) => new Set(current).add(question.id));
    setLastId(question.id);
    send(question.id, question.question, question.answer);
  };

  const finishDraft = () => {
    if (!draft) return;
    setMessages((current) => [...current, { id: draft.id, from: "person", text: draft.text }]);
    setDraft(null);
    setIsBusy(false);
  };

  const suggestions = pickSuggestions(questions, asked, lastId);
  return { messages, asked, readId, draft, isBusy, suggestions, ask, send, finishDraft };
}

export type Chat = ReturnType<typeof useChat>;
The conversation's state

Anything else goes to email

A question that isn't on the list opens the visitor's email with the question as its subject, and the question joins the thread.

More articles