---
title: Building a UI
summary: Components, layout, styling and accessibility, the parts of a frontend you need to specify.
section: frontend
phase: build
order: 1
last_reviewed: 2026-11-20
---

# Building a UI

A UI is a tree of components that turn data into pixels and user actions into events. Agents are good at writing components. They're bad at guessing your design intent, so that's the part you supply.

## You'll be able to
- Describe a UI precisely enough that an agent builds what you pictured
- Choose a framework, styling approach and component library
- Catch accessibility and responsiveness problems in review

## The mental model
**UI = f(state).** You describe what the screen should look like for a given state, and the framework updates the DOM when that state changes. Components are functions with **props** (inputs) and sometimes their own **state**.

## Key concepts
- **Component:** A reusable piece of UI (`<Button>`, `<UserCard>`). Composition means building big components from small ones.
- **Props vs state:** Props are passed in from the parent. State is owned locally and changes over time.
- **Layout:** Flexbox (one dimension) and CSS Grid (two dimensions). **Responsive** design means layouts adapt to the screen size, using breakpoints and mobile-first CSS.
- **Styling approaches:** Utility classes (Tailwind), CSS Modules, CSS-in-JS. **Design tokens** are named values for colours, spacing and type.
- **Component libraries:** shadcn/ui (copy-in, built on Radix), MUI, Mantine, Chakra. **Headless** libraries (Radix, React Aria) give you behaviour and accessibility with no styling.
- **Forms:** Controlled inputs, client-side validation for user experience, server-side validation for correctness (always both).
- **Accessibility (a11y):** Semantic HTML (`<button>`, not a clickable `<div>`), labels, keyboard navigation, focus states, colour contrast (WCAG AA), alt text.
- **UI states:** Every data-driven view has **loading, empty, error and success** states. Agents tend to build only the success state.

## Decisions you make (not the agent)
| Decision | Sensible default | Choose otherwise when… |
|---|---|---|
| Framework | React via Next.js | Simpler sites: Astro. Preference: Svelte/Vue |
| Styling | Tailwind CSS | Existing design system or team preference |
| Components | shadcn/ui | You need a full prebuilt suite (MUI) |
| Design reference | Give a screenshot, Figma link, or named product | Never leave this to "modern and clean" |
| Breakpoints | Mobile-first; test at 375px, 768px, 1280px | — |

## What good output looks like
- Small components with clear props, matching the existing folder conventions
- Semantic HTML, keyboard support and visible focus states
- Loading, empty and error states all handled
- No hardcoded colours or spacing outside the design tokens and theme

## Red flags
- `<div onClick>` instead of `<button>`
- Images without `alt`; inputs without labels
- One 600-line component
- A new UI library installed when the project already has one
- **Common agent mistakes:** only the success state is built; a placeholder is used in place of a label (it disappears once the user types); colours and spacing are hardcoded instead of coming from the tokens; a styled `<div>` replaces a real button, which loses the keyboard behaviour a `<button>` gets for free

## Worked example

**Task:** Add a signup form (name, email, password) to a Next.js app.

You prompt: *"Add a signup form."* It looks right on your laptop. The agent used placeholders instead of labels, a styled `<div onClick>` for the submit button, hardcoded hex colours, and built only the success path.

**What a good result contains:** real `<label>` elements tied to their inputs, a `<button type="submit">`, validation messages next to the fields, loading and error states for the request, and colours from the project's tokens.

**The wrong turn:** the "button" can't be reached with the Tab key and doesn't respond to Enter or Space, because a `<div>` doesn't get the keyboard behaviour a button has by default. The placeholder text vanishes as soon as someone types, so they can't see what a field was for. Nothing happens visibly while the request is in flight, and a failed request shows nothing at all.

**How review catches it:** put the mouse away and use only the keyboard: Tab through the form and press Enter. Resize to a phone width. Turn on network throttling or go offline and submit. Then re-prompt with the rules stated: semantic form elements, labels, all four states, and tokens only.

## Sample prompts

### Weak vs strong
```compare
## Weak
Make a nice dashboard page.
## Why it fails
No layout, data source, states or accessibility requirements, and "nice" has no criteria, so the agent invents a generic dashboard and only the success state.
## Strong
Build a /dashboard page in our Next.js + Tailwind + shadcn/ui app.
Layout: left sidebar nav (collapsible on mobile into a sheet), top bar with
search and user menu, main area with 4 stat cards in a responsive grid
(1 col mobile, 2 tablet, 4 desktop) and a recent-orders table below.
Reference: similar density to Linear's UI. See attached screenshot for tone.
Data: use the existing getDashboardStats() and getRecentOrders() in lib/data.ts.
States: skeleton loaders, empty state for no orders, error state with retry.
A11y: keyboard-navigable nav, proper table semantics, AA contrast.
Use existing components in components/ui before creating new ones.
## Why it works
It gives the layout, a visual reference, a data source, all four UI states, accessibility requirements and a reuse constraint.
## What changed
- **Layout** is described, including how it changes by screen size
- **Reference** gives the tone and density to match
- **Data** points at the existing functions instead of inventing new ones
- **States** lists loading, empty and error, not just success
- **A11y** names keyboard and contrast requirements
- **Reuse** says to use existing components first
```

```text
Audit components/ for accessibility issues: semantic elements, labels, focus
management, keyboard traps, contrast, alt text. Output a table of issues by file,
severity, and fix, then fix the high-severity ones.
```
*Why it works:* A table by file and severity turns a vague "make it accessible" into work you can review and finish.

### Review a UI before you accept it
```text
Review the signup form against this checklist and report each failure with file and line: native elements for interactive controls (button, a, input), a label for every input, keyboard order and visible focus, loading, empty and error states, colours and spacing from tokens, and layout at 375, 768 and 1280 px. Don't fix anything yet.
```
*Why it works:* A fixed checklist stops "looks fine to me" and gives you a list you can verify one item at a time.

### Reuse before building
```text
Before you write any new component, list the components in components/ui and the design tokens that already exist, say which you will reuse for this screen, and justify any new component you think is necessary. Wait for my OK.
```
*Why it works:* It prevents the most common UI drift: a second button, a second modal, a one-off colour.

### Explain what the UI does not handle
```text
For each component you built, list the states it handles (loading, empty, error, success, disabled) and the ones it doesn't. For every interactive element, say which native element it uses, and explain any that is not a native button, link or input.
```
*Why it works:* It makes the agent account for the missing states and the non-native controls, which are exactly where agent-built UIs fall short. Check the answer against the code.

## Check your understanding

```quiz
Q: Why use a real <button> instead of a styled <div onClick>?
- [ ] Buttons are faster to render
- [x] A button can be reached with Tab and activated with Enter or Space by default. A div has none of that unless you rebuild it
- [ ] Divs can't have click handlers
> Why: Native elements bring keyboard support and semantics with them. Rebuilding a button from a div takes extra attributes and handlers, and is easy to get wrong.

---

Q: Which states should every data-driven view handle?
- [ ] Success only
- [x] Loading, empty, error and success
- [ ] Loading and success
> Why: Agents tend to build only the success path. Real users see the other three constantly.

---

Q: A form needs validation. Where should it happen?
- [ ] Only in the browser, for a fast response
- [ ] Only on the server
- [x] In both: the browser for a better user experience, the server because it is the one that can enforce correctness
> Why: Client code can be bypassed. Browser validation is a courtesy, server validation is the rule.
```

## Checklist
- [ ] Works at mobile, tablet and desktop widths
- [ ] Every interactive element is reachable by keyboard
- [ ] Loading, empty and error states exist
- [ ] Lighthouse accessibility score ≥ 90
- [ ] Every input has a real `<label>`, and buttons are real `<button>`s
- [ ] Colours and spacing come from the design tokens

## Go deeper
- [react.dev](https://react.dev/learn)
- [MDN: Learn web development](https://developer.mozilla.org/en-US/docs/Learn)
- [WCAG quick reference](https://www.w3.org/WAI/WCAG22/quickref/)
- [MDN: HTML, a good basis for accessibility](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Accessibility/HTML): native elements, labels and keyboard behaviour
- [State and data fetching](/docs/frontend/state-and-data-fetching.md): where the data your components show should live
- [Reviewing and verifying AI output](/docs/ai-agents/reviewing-ai-output.md): checking what the agent built
