Builder generates code from your Figma design that doesn't depend on specific libraries or component methods.
If you want to use existing components in your codebase instead of generating new ones, you can use component mapping.
The following terms are useful to understand in relation to this process:
Figma component: reusable design elements that can be accessed across design spaces. For more details, see Figma's Guide to components in Figma.
Code component: reusable code elements that can be accessed from across your codebase. Depending on your application's framework, the exact code may look different but the concept is the same.
Mapping: Builder connects Figma components to code components, so that when code is generated from a Figma design the existing code components are used wherever possible.
.mapper file: This file lives in your codebase and aids Builder in connecting your Figma components to your code components. It also provides customization for using your component.
The code below shows a code component, Button, being mapped to a Figma component through both a component name and a unique ID.
Figma component properties, such as Type and Size in this example, can be accessed with the strongly-typed figma object from within the mapper() method. The TypeScript interface ensures type safety when working with these Figma properties
The $cmp property is an internal metadata attribute that links the HTML template to the Angular component class for component resolution, preview functionality, and accurate code generation, and does not appear in the final generated code or affect production behavior.
Builder uses $cmp to:
Link the HTML template to the Angular component class by marking the node with a reference.
Enable real-time previews by rendering the component correctly in the Builder Figma plugin.
Improve code generation accuracy to ensure the output matches the mapped component.
Connect the mapping to the registered component in the Builder registry.
The following example shows how to map a Figma component to an Angular component using $cmp:
// src/mappings/MyButton.mapper.ts
import { MyButton } from "@/components/ui/button";
// Standard Angular template
// <button-component [variant]="primary" [size]="large"></button-component>
// HTML template literal in mapper
figmaMapping({
componentKey: "button-component-key",
mapper(figma) {
return html`
<button-component
color=${figma.Color?.toLowerCase()}
size=${figma.Size?.toLowerCase()}
type=${figma.Variant?.toLowerCase()}
$cmp=${MyButton}
>
${figma.$children}
</button-component>
`;
},
});
This example maps the <MyButton> Angular component to a Figma component using a unique componentKey. The mapper() method accesses Figma variables, such as color, size, and type, using the figma object.
Best practices when using $cmp:
Always include $cmp when mapping Angular components.
Use absolute import paths for all component references.
Match imported component names exactly to the Builder registry.
Pass a static reference directly in the mapping without using variables or expressions.
The code below shows a code component, Button, being mapped to a Figma component through a unique ID.
Figma component variables, such as Variant and Size in this example, can be accessed via the figma object from within the mapper() method.
Builder uses HTML template literals in Angular mappings to provide a framework-agnostic way of defining view templates.
The $cmp property is an internal metadata attribute that links the HTML template to the Angular component class for component resolution, preview functionality, and accurate code generation, and does not appear in the final generated code or affect production behavior.
Builder uses $cmp to:
Link the HTML template to the Angular component class by marking the node with a reference.
Enable real-time previews by rendering the component correctly in the Builder Figma plugin.
Improve code generation accuracy to ensure the output matches the mapped component.
Connect the mapping to the registered component in the Builder registry.
The following example shows how to map a Figma component to an Angular component using $cmp:
// src/mappings/mapper.ts
import { MyButton } from "@/components/ui/button";
// Standard Angular template
// <button-component [variant]="primary" [size]="large"></button-component>
// HTML template literal in mapper
figmaMapping({
componentKey: "button-component-key",
mapper(figma) {
return html`
<button-component
color=${figma.Color?.toLowerCase()}
size=${figma.Size?.toLowerCase()}
type=${figma.Variant?.toLowerCase()}
$cmp=${MyButton}
>
${figma.$children}
</button-component>
`;
},
});
This example maps the <MyButton> Angular component to a Figma component using a unique componentKey. The mapper() method accesses Figma variables, such as color, size, and type, using the figma object.
Best practices when using $cmp:
Always include $cmp when mapping Angular components.
Use absolute import paths for all component references.
Match imported component names exactly to the Builder registry.
Pass a static reference directly in the mapping without using variables or expressions.
Important: Your import paths within .mapper files will be reproduced verbatim during code generation. Use published package names or TypeScript path aliases (configured in tsconfig.json) within your .mapper files to ensure your generated code is not broken.
You must publish your mapped components for Builder to know about them. Once your components are published, they are available for use during code generation.
To publish mapped components:
Confirm your local project has .mapper files for the code components you wish to map.
Run the following command in your application's directory:
npx "@builder.io/dev-tools@latest" figma publish
Builder CLI identifies all mapped components (that is, .mapper files) within your application.
When prompted, choose Publish. This publishes the mappings to Builder.
Now, when you use code generation, Figma components mapped to code components are used whenever possible.
Once your components have been mapped and published:
Within Figma, choose a design that makes use of your mapped Figma component.
When exporting your design with the Builder Figma plugin, choose Precise Mode. For more details on Precise Mode, see Figma export modes and Set up for precise mode.
Builder's code generation uses your mapped components within its results, reducing code complexity and duplication.
In the video below, code is generated with the Builder CLI. A published mapped component is present in the design and can be seen in the generated code.