Improve Code Generation with Figma's Code Connect UI
A tutorial by Figma Learn. Featured in the OTF curated resource library.
What Is Code Connect?
Figma's Code Connect is a feature that links Figma components to their actual code implementations. When a developer inspects a Figma component, instead of seeing generic CSS snippets, they see the real React/Vue/SwiftUI code that implements that component.
For AI coding tools, this is transformative. When an AI agent can see that a Figma 'Button' component maps to your <Button variant='primary' size='md'> React component, it generates code that uses your actual component library — not a generic approximation.
Code Connect works by maintaining a mapping file in your codebase that tells Figma which code corresponds to which design component. This mapping flows through to AI tools that read Figma data, creating a closed loop between design and implementation.
Setting Up Code Connect
Getting Code Connect running requires setup in both Figma and your codebase.
Install the Code Connect CLI
Install Figma's Code Connect package in your project. It provides the CLI tools for creating and maintaining component mappings.
npm install --save-dev @figma/code-connect
Authenticate with Figma
Generate a Figma personal access token and configure the CLI. This allows the tool to read your Figma file structure and push code mappings back to Figma's Dev Mode.
Create your first mapping
Use the CLI to connect a Figma component to its code implementation. Specify the Figma component URL, the code file, and how Figma properties map to code props.
import figma from '@figma/code-connect'
import { Button } from './components/ui/button'
figma.connect(Button, 'https://figma.com/file/.../Button', {
props: {
label: figma.string('Label'),
variant: figma.enum('Variant', {
Primary: 'primary',
Secondary: 'secondary',
Ghost: 'ghost',
}),
size: figma.enum('Size', {
Small: 'sm',
Medium: 'md',
Large: 'lg',
}),
},
example: (props) => <Button variant={props.variant} size={props.size}>{props.label}</Button>,
})Publish to Figma
Run the publish command to push your mappings to Figma. Now when anyone inspects the Button component in Dev Mode, they see your actual React code instead of generic CSS.
npx figma connect publish
Linking Components to Code
The real power of Code Connect emerges when you map your entire design system:
Property Mapping: Figma component properties (variant toggles, size selectors, boolean switches) map directly to React props. When Figma shows 'Variant: Primary, Size: Large,' the code shows <Button variant='primary' size='lg'>.
Nested Components: When a Card contains a Button and an Avatar, Code Connect shows the composed implementation — how the components nest in code, not just how they look in design.
State Variants: Map Figma's interaction states (default, hover, active, disabled, loading) to your component's state management. AI tools can then generate components with proper state handling.
Responsive Variants: If your component has mobile/desktop variants in Figma, map them to responsive breakpoints in code. The AI understands that the mobile design maps to md:hidden and the desktop design to hidden md:block.
Benefits for AI Code Generation
Accurate Component Usage
AI tools that read Figma data (via MCP or plugins) see your actual component API. Instead of generating `<button className='bg-blue-500 ...'>`, they generate `<Button variant='primary'>` — using your real component library.
Correct Prop Values
When Figma properties map to code props, AI agents know the exact prop values available. No more guessing that 'small' might be 'sm' or 'size-s' — the mapping is explicit.
Design System Consistency
Every AI-generated page uses the same components with the same props, because the mappings enforce consistency. Design drift becomes impossible when the code is generated from the design system.
Reduced Manual Translation
The biggest time savings: no more manually translating 'this Figma frame uses our Button Primary Large' into code. The mapping does the translation automatically for both humans and AI.
Best Practices
Start with Your Most-Used Components
Don't map everything at once. Start with the 10 components used most frequently: Button, Input, Card, Badge, Avatar, etc. These deliver the most immediate impact on AI code generation quality.
Keep Mappings in Sync
When you update a component's API (add a prop, rename a variant), update the Code Connect mapping. Add this to your PR checklist: 'Did this change require a Code Connect update?'
Include Examples for Complex Components
For components with many props or complex composition patterns, include multiple example usages in your mapping. This gives AI tools more reference material for accurate generation.
Version Your Mappings
Commit Code Connect files to version control alongside your components. Review mapping changes in PRs just like code changes. This ensures the team agrees on how design maps to code.