Topic 3 of 5 · 4 min read
Writing component documentation with AI
Goal: Draft component documentation with AI from the code and design, then correct every statement that does not match the real props and behaviour.
AI is good at turning a component's code into readable docs. It is bad at knowing which sentences are true. Feed it the real code and check every claim against it.
Check yourself · 1 of 3
An AI-written doc says a Button accepts size="xl", but the component's types only allow sm, md and lg. What is the right fix?
A component page answers four questions: what is it for, when should I not use it, what can I change (props, variants, states), and what does it do for keyboards and screen readers. The GOV.UK Design System shows the pattern well: each component has guidance, examples and rules.
Why it matters
Documentation is read by the next person who has to choose between two components, and now also by coding agents. If the page says size accepts "xl" and the code does not, someone will ship a broken screen or an agent will invent an API. A wrong doc is worse than a missing one, because people trust it.
Models fill gaps with what is typical for components of that kind. A button usually has variant, size and disabled, so a model may list them even when yours has tone and loading.
How to do it
- Give it the source. Paste the component code, its types, and a short design note. Say that the code is the only source of truth.
- Set the structure. Purpose, when to use, when not to use, props table, states, accessibility, one do and one don't.
- Forbid invention. Add: "If the code does not show it, write 'unknown' and list it as a question." This turns guesses into open items.
- Check the props table against the types. Every prop, type, default and allowed value. This is mechanical and worth doing line by line.
- Run the examples. A code sample in the docs should render. A story in Storybook or a test makes this automatic.
- Have the owner read the guidance. Only the maker knows why "don't use for destructive actions" is a rule.
Before and after
Draft sentence: "The Button supports sizes sm, md, lg and xl and shows a spinner while loading." The code defines size: 'sm' | 'md' | 'lg' and has no loading state.
Corrected: "The Button supports sizes sm, md and lg. It has no loading state; use Button with a disabled state and a separate spinner." The fix is what the code shows, not what is typical.
What goes wrong
- Plausible extra props that do not exist.
- Generic guidance such as "use buttons for actions", which helps nobody choose between two components.
- Docs written once. The code changes and the page does not; regenerate or review the page in the same pull request.
Examples
A props table caught by checking the types
The draft table lists size (sm, md, lg, xl), variant, disabled and loading.
The component's types define size: 'sm' | 'md' | 'lg', tone: 'neutral' | 'danger' and disabled. Three corrections: remove xl, remove loading, rename variant to tone. The model listed the props that buttons usually have.
Key points
- Give the model the component code and types and say the code is the only source of truth.
- Tell it to write 'unknown' where the code does not show something, so guesses become visible questions.
- Check the props table line by line against the real types, defaults and allowed values.
- Run the code examples; a sample that does not render is a wrong doc.
- The component's owner reviews the when-to-use and when-not-to-use guidance.
Common mistakes
- Prompting for docs without the code, so the model describes a typical component, not yours.
- Accepting a props table without comparing every row to the types.
- Publishing generic when-to-use text that does not help someone choose between two real components.
- Updating the component without updating or re-checking its page in the same change.
Check yourself · 2 of 3
What instruction helps stop a model inventing props when drafting component docs?
Check yourself · 3 of 3
Why should the owner of a component review its 'when not to use' guidance?
Practice items, not counted towards your mastery.
Start here
- Design Tokens Format Module (W3C Design Tokens Community Group) (opens in a new tab)StandardFree
The draft format for tokens: $value, $type, groups and aliases, so tools and agents read the same file.
- Guide to variables in Figma (Figma Help) (opens in a new tab)DocsFree
How colour, number and string variables, collections and modes work in a design file.
- Design Systems 101 (NN/g) (opens in a new tab)ArticleFree
Plain definitions of a design system, its parts and why teams keep one.
More for this topic
- GOV.UK Design System (opens in a new tab)DocsFree
A public system with real component pages, usage guidance and a contribution process to copy from.
- Storybook documentation (opens in a new tab)DocsFree
How component stories and docs pages are written and generated from real code.