DEV Community

Gaurav Guha
Gaurav Guha

Posted on Originally published at thefrontkit.com

Building production-ready AI interfaces with Next.js and React

Building AI interfaces that feel polished and accessible takes more than a textarea and an API call. You need keyboard navigation, streaming responses that don't jank the UI, feedback capture for model improvement, and a theming system that survives dark mode. Most teams rebuild these patterns from scratch. Here are the components and patterns that actually ship.

Prompt input that handles files, suggestions, and voice

The prompt input is the entry point. A production version needs auto-resizing, keyboard-navigable suggestions, file attachments with previews, voice input states, and focus management for screen readers.

import { PromptInput } from '@/components/ui/composites/prompt-input'

function ChatInterface() {
  const [prompt, setPrompt] = React.useState('')
  const [files, setFiles] = React.useState([])

  const handleSubmit = async (value: string, attachedFiles: AttachedFile[]) => {
    // Send to your AI API
    await sendToAI(value, attachedFiles)
  }

  return (
    <PromptInput
      value={prompt}
      onChange={setPrompt}
      onSubmit={handleSubmit}
      placeholder="Ask anything..."
      suggestions={[
        { id: '1', text: 'Summarize this document', category: 'actions' },
        { id: '2', text: 'Explain in simple terms', category: 'actions' },
      ]}
      attachedFiles={files}
      onFileAttach={(newFiles) => setFiles([...files, ...newFiles])}
      actionButton="send"
      showSuggestions={true}
    />
  )
}
Enter fullscreen mode Exit fullscreen mode

Key behaviors baked in:

  • Textarea grows with content
  • Arrow keys, Enter, and Escape navigate suggestions
  • File previews with removal buttons
  • Voice recording states with accessible labels
  • Focus moves correctly when suggestions open or close

Streaming responses without UI jank

Streaming tokens require live regions for screen readers and smooth rendering. The response viewer handles markdown, code highlighting, and citations while announcing updates politely.

import { ResponseViewer } from '@/components/ui/composites/response-viewer'

function StreamingResponse({ stream }: { stream: ReadableStream }) {
  const [content, setContent] = React.useState('')

  React.useEffect(() => {
    const reader = stream.getReader()
    const decoder = new TextDecoder()

    async function readStream() {
      while (true) {
        const { done, value } = await reader.read()
        if (done) break

        const chunk = decoder.decode(value)
        setContent(prev => prev + chunk)
      }
    }

    readStream()
  }, [stream])

  return (
    <ResponseViewer
      content={content}
      format="markdown"
      showCitations={true}
      citations={extractCitations(content)}
      aria-live="polite"
      aria-atomic="false"
    />
  )
}
Enter fullscreen mode Exit fullscreen mode

The aria-live="polite" attribute lets screen readers announce updates without interrupting the user. The component handles markdown rendering, code syntax highlighting, and citation links internally.

Feedback loops for model improvement

Capturing ratings and comments lets you improve the model. The feedback modal includes a 5-star rating with keyboard navigation, optional text field, accessible validation, and loading states.

import { FeedbackModal } from '@/components/ui/templates/modal/feedback-modal'

function AIResponseWithFeedback({ responseId }: { responseId: string }) {
  const [showFeedback, setShowFeedback] = React.useState(false)

  const handleFeedbackSubmit = async (data: { rating: number; feedback: string }) => {
    await fetch('/api/feedback', {
      method: 'POST',
      body: JSON.stringify({
        responseId,
        rating: data.rating,
        feedback: data.feedback,
      }),
    })
    setShowFeedback(false)
  }

  return (
    <>
      <div className="flex items-center gap-2">
        <ResponseViewer content={response} />
        <button
          onClick={() => setShowFeedback(true)}
          className="text-sm text-secondary-600 hover:text-primary-600"
        >
          Rate this response
        </button>
      </div>

      <FeedbackModal
        isOpen={showFeedback}
        onClose={() => setShowFeedback(false)}
        onSubmit={handleFeedbackSubmit}
      />
    </>
  )
}
Enter fullscreen mode Exit fullscreen mode

Wire your own /api/feedback endpoint to persist the rating and comment alongside the response ID.

Token-driven theming with Tailwind

Design tokens keep light and dark mode consistent. Map CSS custom properties to Tailwind color and spacing scales so theme changes propagate automatically.

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      colors: {
        primary: {
          50: 'var(--color-primary-50)',
          100: 'var(--color-primary-100)',
          // ... mapped from design token system
          600: 'var(--color-primary-600)',
        },
      },
      spacing: {
        // Tokenized spacing scale
        'token-1': 'var(--spacing-token-1)',
        'token-2': 'var(--spacing-token-2)',
      },
    },
  },
}
Enter fullscreen mode Exit fullscreen mode

Define your tokens in :root and [data-theme="dark"] selectors. Components reference primary-600 or token-2 and inherit the correct values.

Complete prompt screen composition

A full prompt screen combines centered input, suggested actions, topbar navigation, footer links, and mobile-responsive layout in one component.

import { PromptScreen } from '@/components/ui/recipes/prompt-submission/prompt-screen'

export default function HomePage() {
  return (
    <PromptScreen
      heading="What can I help you with?"
      placeholder="Ask anything..."
      suggestedActions={[
        { id: '1', text: 'Adjust lighting', icon: 'SunIcon' },
        { id: '2', text: 'Remove Background', icon: 'PhotoIcon' },
        { id: '3', text: 'Improve image quality', icon: 'SparklesIcon' },
      ]}
      onPromptSubmit={(prompt) => {
        router.push(`/chat?prompt=${encodeURIComponent(prompt)}`)
      }}
    />
  )
}
Enter fullscreen mode Exit fullscreen mode

This handles keyboard flows, focus management, and responsive breakpoints without extra work.

Production checklist

Before shipping, verify:

  • Keyboard navigation works for every interactive element
  • Screen readers announce streaming responses via live regions
  • Focus management handles modals and dynamic content insertion
  • Error states are clear and actionable
  • Loading states provide feedback during API calls
  • Mobile experience is polished and touch-friendly
  • Design tokens stay synced between design files and code

Gotcha: If you skip aria-atomic="false" on the streaming viewer, some screen readers will re-read the entire response on every token chunk instead of announcing only the new text.


This is a shorter version of a guide I wrote at Frontkit Studio. The full post has the complete walkthrough.

Top comments (0)