FigmaExport
repository·master·Indexed 21 days ago
https://github.com/redmadrobot/figma-exportA CLI tool that automates the synchronization of design tokens (colors, typography) and assets (icons, images) from Figma into iOS (Xcode/SwiftUI) and Android (Android Studio/Jetpack Compose) projects. It supports Dark Mode, RTL, and custom code generation via Stencil templates. Integration is available via Homebrew, CocoaPods, and Fastlane.
What's inside figma-export
- FigmaExport is a command-line utility designed to automate the export of design tokens and assets from Figma directly into Xcode (iOS) or Android Studio (Android) projects. It bridges the gap between design and code by syncing component libraries, supporting Dark Mode, SwiftUI, Jetpack Compose, and RTL.
Use Figma Color Variables instead of Styles
masterFigma's Color Variables can be used as an alternative to Color Styles. To use them, you must switch from the
colorskey to thevariablesColorskey in yourfigma-export.yamlconfiguration.Key Features:
- Nesting: Supports any depth of variable nesting (e.g., a variable pointing to another variable).
- Modes: You can specify the
primitivesModeNameparameter to indicate which variable column represents your primitives.
Warning: The Figma API for color variables is in
Betaand subject to change.Configure color exports for Dark Mode and High Contrast
masterWhen running
figma-export colors, you can manage theme variations (Dark Mode, High Contrast) using either multiple files or a single file with suffixes.Multiple Files (Default)
Provide separate Figma files for each theme (e.g., one for light, one for dark).
Single File Mode
Enable the
useSingleFileconfiguration option infigma-export.yaml. You must then use configurable suffixes in your style names:- Dark Mode: Append
_dark(or your custom suffix). - High Contrast (Light): Append
_lightHC(or your custom suffix). - High Contrast (Dark): Append
_darkHC(or your custom suffix).
Note: Names of dark/high-contrast colors must match the names of the light colors they correspond to. If a color exists in the light palette but not in the dark palette, it is treated as a universal color.
- Dark Mode: Append
Exportable Assets and Features
masterFigmaExport supports the following asset types:
- Colors: Exports Figma color styles to Xcode Assets or Android XML, including support for Light/Dark modes and High Contrast colors.
- Typography: Exports text styles to iOS (Swift extensions for
UIFont,LabelStyle, andLabelclasses) and Android (XML and Jetpack ComposeTextStyle). - Icons: Exports Figma components as vector images (PDF/SVG for iOS, XML for Android) with appropriate render modes.
- Images: Exports Figma components as raster images (PNG/WebP) for both platforms.
Note: Exporting icons and images requires a Figma Professional or Organization plan because the utility utilizes Shareable team libraries.
Customize code generation with Stencil templates
masterFigmaExport uses Stencil to generate code. You can override default templates by specifying a directory in your configuration file.
iOS Customization
Set
ios.templatesPathin your config. Required template names:- UIKit Colors:
UIColor+extension.swift.stencil - SwiftUI Colors:
Color+extension.swift.stencil - UIKit Images:
UIImage+extension.swift.stencil - SwiftUI Images:
Image+extension.swift.stencil - Typography:
Label.swift.stencil,LabelStyle.swift.stencil,LabelStyle+extension.swift.stencil,UIFont+extension.swift.stencil,Font.swift.stencil
Android Customization
Set
android.templatesPathin your config. Required template names:colors.xml.stencilColors.kt.stencilIcons.kt.stenciltypography.xml.stencilTypography.kt.stencil
- UIKit Colors:
Export image variants for different iOS idioms
masterTo specify image variants for specific iOS devices (e.g., iPhone, iPad, Mac), add a
~followed by the idiom name as a postfix to the component/asset name in Figma.Example:
my_image~ipadConfigure FigmaExport via YAML
masterFigmaExport uses a
figma-export.yamlfile to store all configuration properties forfigma,ios, andandroidexports.To use a specific configuration file, use the
-ior--inputargument when running the executable. If thefigma-export.yamlfile is located in the same directory as thefigma-exportexecutable, you can omit this option.# Using a default config file in the same directory ./figma-export colors # Specifying a custom config file ./figma-export -i /path/to/custom-config.yaml colorsInstall FigmaExport via CocoaPods and Fastlane
masterTo integrate FigmaExport into an iOS workflow using CocoaPods and Fastlane:
- Add the pod to your
Podfile:pod 'FigmaExport' - Run
pod installto download the binaries toPods/FigmaExport/Release/. - In your
Fastfile, you can invoke the binary using a shell command. Ensure you place afigma-export.yamlconfiguration file at the root of your project.
Example Fastlane lane:
lane :sync_colors do Dir.chdir("../") do sh "Pods/FigmaExport/Release/figma-export colors ." end endpod 'FigmaExport'- Add the pod to your
Set up Figma Personal Access Token
masterBefore installing or running FigmaExport, you must provide a Figma personal access token via the
FIGMA_PERSONAL_TOKENenvironment variable. This token is required to access the Figma API.To set it in your terminal:
export FIGMA_PERSONAL_TOKEN=valueIf you are using Fastlane, add the token to your
fastlane/.envfile:FIGMA_PERSONAL_TOKEN=valueInstall fastlane
masterBefore installing fastlane, ensure you have the latest version of the Xcode command line tools installed. You can install fastlane using either RubyGems or Homebrew.
xcode-select --install # Using RubyGems [sudo] gem install fastlane -NV # OR using Homebrew brew install fastlaneConfigure Android export and Jetpack Compose support
masterIn your
figma-export.yamlfile, you must define several properties for Android exports. To enable Jetpack Compose code generation, additional properties are required.Required for Android:
android.mainRes: The main resource directory.android.icons.output: The output directory for icons (cleared before export).android.images.output: The output directory for images (cleared before export).
Required for Jetpack Compose:
android.mainSrc: The source directory for Compose code.android.resourcePackage: The package name for generated code.android.[typography|colors|icons].composePackageName: The specific package name for the asset type.
Integration with Gradle: You must add the generated directories to your app's
build.gradlefile so they are recognized as resource sets:android { sourceSets { main { res.srcDirs += "src/main/res/figma-export-icons" res.srcDirs += "src/main/res/figma-export-images" } } }android { sourceSets { main { res.srcDirs += "src/main/res/figma-export-icons" res.srcDirs += "src/main/res/figma-export-images" } } }Export Typography for Android
masterTo export typography for Android:
- Place font files under the
resdirectory of your module. - Run
figma-export typography. - Create a top-level style in XML to act as a parent for generated styles:
<style name="FigmaExport.TextAppearance" parent="Widget.AppCompat"> </style>- Place font files under the