Figma Code Connect

repository·main·Indexed 23 days ago

https://github.com/figma/code-connect

A tool for connecting design system components in code with Figma, allowing Dev Mode to display production code snippets instead of autogenerated examples. It features template files for framework-agnostic mapping, property mapping, and a client for interacting with the Figma REST API. Supports multiple languages via template tags including TSX, HTML, Swift, and Kotlin, and provides specialized helpers for React and Jetpack Compose.

Tokens
19.2K
Snippets
21
Records
106
Agent score
81%

What's inside @figma/code-connect

  1. What is Code Connect

    main

    Code Connect is a tool that connects your design system components in code with your design system in Figma. It allows Figma's Dev Mode to display true-to-production code snippets instead of autogenerated examples.

    Key capabilities include:

    • Connecting component definitions: Linking Figma components to their actual code implementations.
    • Property mapping: Mapping properties from code to Figma to enable dynamic and correct code examples.

    This helps drive consistent and correct adoption of a design system across both design and engineering teams.

  2. Use template files for Code Connect

    main

    Template files are the recommended, framework-agnostic way to use Code Connect. They provide full control over how components appear in Dev Mode and are the only method that will receive active maintenance moving forward.

    Note on deprecation: Framework-specific parsers will no longer receive updates or support starting August 17th, 2026. If you are currently using parser-based Code Connect, you should follow the migration guide to switch to template files.

  3. Run icon import scripts for Code Connect

    main

    To automate the connection of large numbers of icons, use the provided example scripts. You must provide a FIGMA_ACCESS_TOKEN environment variable, which can be set directly in the command or provided via an .env file.

    To run the script using npx tsx:

    FIGMA_ACCESS_TOKEN=<my token> npx tsx import-icons.ts

    If the token is already in an .env file, you can simply run:

    # Assuming FIGMA_ACCESS_TOKEN is in .env
    npx tsx import-icons.ts
    FIGMA_ACCESS_TOKEN=<my token> npx tsx import-icons.ts
  4. Generate property declarations for Code Connect templates

    main

    The template generation logic converts Figma component property definitions into executable Code Connect code.

    | Figma Property Type | Code Connect Method | Notes | | :--- | :--- | :| | BOOLEAN | figma.selectedInstance.getBoolean("name") | | | TEXT | figma.selectedInstance.getString("name") | | | VARIANT | figma.selectedInstance.getEnum("name", { ... }) | Used for multi-option variants. | | VARIANT (Boolean) | figma.selectedInstance.getBoolean("name") | Used if variants are true/false, yes/no, etc. |

    Property Name Normalization:

    • Figma Property Name: Strips variant index suffixes (e.g., Property#123 $\rightarrow$ Property).
    • Code Variable Name: Converted to camelCase and stripped of special characters.
  5. How Code Connect generates prop mappings

    main

    During the setup wizard, Code Connect automatically attempts to map your code component's props to Figma component properties. It uses a combination of name matching (fuzzy or via embeddings) and type compatibility to determine if a prop should be treated as a standard value or an Intrinsic.

    The Matching Process:

    1. Name Matching: The CLI searches for matches between your code prop names and Figma property names (or variant values) using a Searcher. If componentMatchResults (embeddings) are available, they are prioritized over fuzzy matching.
    2. Type Validation: Once a name match is found, the system checks if the code's propSignature is compatible with the Figma property type.
    3. Intrinsic Generation: If compatible, it generates an Intrinsic mapping. For example, a boolean prop in code might be mapped to a Figma variant property that acts as a boolean.

    Supported Intrinsic Kinds:

    • String: Mapped when the code prop is a string and the Figma property is Text.
    • Boolean: Mapped when the code prop is false | true and the Figma property is a Boolean or a two-option Variant.
    • Enum: Mapped when the code prop has multiple string/number options (e.g., 'primary' | 'secondary') and the Figma property is a Variant. The system uses generateValueMapping to align code enum values with Figma variant options.
    • Instance: Mapped when the code prop signature is JSX-like (e.g., ReactElement, ReactNode) and the Figma property is an InstanceSwap.
  6. Understand ResultSection types for code generation

    main

    Code Connect uses ResultSection to represent the different types of content that can be emitted in a template. A ResultSectionList is an array of these sections.

    Section Types

    TypeDescription
    CODEA raw string of code. Contains code: string and optional nestedImports: string[].
    INSTANCEA Figma instance. Contains guid, symbolId, and optional resultSections or nestedImports.
    SLOTA component slot. Contains propertyName and an optional guid.
    ERRORAn error state. Contains a message and an optional errorObject.

    Usage

    These types are primarily used when calling methods like __render__() or when working with the TemplateStringResult returned by template tags.

  7. Use component modifiers in Code Connect

    main

    Code Connect uses modifiers to bridge the gap between Figma components and your actual code. Modifiers allow you to define how a component's properties are retrieved and how it is rendered in code.

    There are two primary types of modifiers:

    1. getProps: Used to extract the props from a component instance.
    2. render: Used to define a render function that returns a JSX element, allowing Code Connect to map Figma properties to specific code implementations.

    When using the render modifier, the first argument must be a function (arrow function, function expression, or function declaration) that returns a single JSX element.

  8. How Code Connect detects parsers and labels

    main

    If no parser or label is explicitly defined in your figma.config.json, Code Connect uses heuristics to determine them by walking up the directory tree from your current location:

    Parser Detection Heuristics

    • React: Found if package.json contains react in dependencies or peerDependencies.
    • HTML: Found if package.json is present but no other supported web frameworks are detected.
    • Swift: Found if *.xcodeproj or Package.swift files exist.
    • Compose: Found if build.gradle.kts or build.gradle files exist.
    • Custom: Manually specified in config.

    Label Detection Heuristics

    Code Connect looks for specific libraries in package.json to set a default label:

    • Angular: Sets label to Angular if angular is a dependency/peer dependency.
    • Vue: Sets label to Vue if vue is a dependency/peer dependency.
  9. Configure AI-powered property matching via embeddings

    main

    When setting up Code Connect, you can enable AI-powered property matching by setting useAi: true. This uses vector embeddings to find the best semantic matches between your code's props and Figma's component properties.

    Requirements

    • accessToken: A valid Figma access token.
    • figmaUrl: The URL of the Figma file.
    • apiUrlOverride (Optional): An override for the embeddings API endpoint.

    Behavior

    • If useAi is true, the CLI attempts to fetch embeddings for all unique matchable names (React props, Figma properties, and variant values).
    • If the embedding service is unavailable or returns an error, the CLI logs the error and falls back to standard fuzzy matching to ensure the setup process continues.
  10. How the preview command handles property combinations

    main

    The preview command allows you to see how your Code Connect templates render with different component properties. There are two primary modes of operation:

    1. Specific Preview (--props): You provide exact property values. This is useful for testing a specific state.
    2. Exhaustive Preview (--all): The CLI attempts to enumerate and render all possible property combinations for the component.

    Constraints and Limits:

    • Mutual Exclusion: You cannot use --props and --all at the same time.
    • Combination Cap: When using --all, the number of combinations is capped by --max-combinations (defaulting to 500) to prevent overwhelming the server. If a component exceeds this limit, the CLI will warn you and only render the first set of combinations.
    • Type Disambiguation: If a property name is ambiguous across multiple types, you must use the TYPE:name=value syntax (e.g., BOOLEAN:name=true).