Design ↔ Code
Use an agent to turn a design into code or bring existing code onto the canvas.
Give it access to both the .pen document and the project files, and tell it
which components and tokens to reuse.
| Your goal | Start here |
|---|---|
| Design with the components already in your app | Code → Design |
| Implement a screen or component you designed | Design → Code |
| Carry colors, spacing, and typography between the two | Variables and design tokens |
| Export HTML without an agent request | HTML export settings |
Set up the project
Save the .pen document in the project where the agent works. Keep its library
and referenced assets alongside it:
my-project/
├── src/
│ ├── components/
│ └── styles/
├── design/
│ ├── ui.lib.pen
│ ├── checkout.pen
│ └── assets/
└── package.jsonOpen the document in pen.dev. Use the integrated agent in the desktop app, or a connected external client such as Claude Code or Codex. The agent needs access to both the design and the codebase. Include the relevant file paths in your request.
If you have a pen.dev design library, import it into the document before building screens. Tell the agent to use those components and variables.
Code → Design Workflow
Importing Existing Code
Start with a representative component, such as a Button or Card, before asking for a full screen. Give the agent the source file, any shared styles or tokens, and the design document it should update:
Read src/components/Button.tsx and src/styles/tokens.css.
Recreate the Button in design/ui.pen as a reusable component using
pen.dev variables for the shared colors, spacing, and typography.
Include a text label I can override in an instance.Review the result against the running component: layout, padding, type, colors, and the states you need to design with. Ask for corrections with specific file paths and component names. Then package the components as a library and import it into your screen documents.
The agent recreates design elements from the code. Check the component structure and variable bindings before relying on it as a design system.
Import a rendered page
To start from a running app instead of individual source files, open its URL in the desktop browser and import the page or an element as editable layers. A local development server works too.
The imported layers capture the page’s appearance; they do not retain its JavaScript interactions. Turn the elements you plan to reuse into components, and bind their properties to your variables.
Design → Code Workflow
Basic Export to Code
- Build the screen with your library’s components and variables, then save the
.penfile - Select the frame or component you want to implement
- Ask the agent to implement it in the project, naming the destination and existing code it should reuse
- Run the app and compare the result with the design
For example:
Implement the selected Checkout frame from design/checkout.pen in
src/pages/Checkout.tsx. Reuse src/components/Button.tsx and Card.tsx,
and use the existing tokens in src/styles/tokens.css.
Follow the project's current framework and styling conventions.Give the agent requirements that are not visible in the design, such as form validation, loading states, keyboard behavior, and navigation. Inspect the changed files and test the implemented interactions in the app.
For a static HTML export, use the properties panel’s Code section and choose HTML + Tailwind or HTML + CSS. See HTML export settings for Copy HTML, Export HTML, and asset options.
Variables & Design Tokens
Keep token names aligned between the codebase and the design. Tell the agent which file is the source for the change and which should be updated.
To bring CSS changes into the design:
Read src/styles/tokens.css and update the matching variables in
design/ui.lib.pen. Preserve the Light and Dark theme values.
Report any tokens that have no matching design variable.To apply design changes to CSS:
Update the matching tokens in src/styles/tokens.css from the variables in
design/ui.lib.pen. Preserve the existing token names and theme selectors.
Show me any values that cannot be mapped directly.Review the values and theme mappings on both sides. See Variables and themes for bindings, aliases, and inheritance inside pen.dev.
Two-Way Sync
Design and code change when you ask the agent to update them. A request to recreate a component does not establish a continuously synchronized link.
For each iteration, identify what changed and where it should go. For example, after changing a button’s spacing in the library:
Compare the Button in design/ui.lib.pen with src/components/Button.tsx.
Apply the design's spacing change to the existing code component.
Keep its behavior and public props intact.Review the code diff, run the relevant checks, and inspect the app. When the code is the starting point, reverse the request and inspect the resulting canvas. Save the library and reopen documents that import it to load its saved changes.
Keep design and code together
Commit the .pen files, library, and required assets alongside the code change
so reviewers can inspect both. Save and close affected .pen files before
switching branches. Follow Working with Git
for shared assets and conflict recovery.
For scripts that generate layers inside the canvas, see Code on Canvas.