Figma Plugin Samples
repository·main·Indexed 23 days ago
https://github.com/figma/plugin-samplesA collection of sample plugins for Figma and FigJam demonstrating Figma Plugin API capabilities. Examples cover basic shape creation, advanced UI interactions, Dev Mode features, the Annotations API, and W3C design token import/export using Variables APIs. The repository includes guidance on using TypeScript, React, Webpack, and FigJam-specific node types.
What's inside figma-plugin-samples
- This sample demonstrates how to use Figma's Variables APIs to import or export design tokens. It specifically uses the W3C Design Tokens spec format for the JSON data. This allows you to synchronize design tokens between Figma and other design systems or tools that adhere to the W3C standard.
Develop FigJam-specific plugins
mainSome plugins are designed specifically for FigJam and utilize FigJam-specific node types. To work correctly, these plugins should have an
editorTypeof'figjam'in theirmanifest.jsonfile.Supported FigJam node types include:
StickyNode(stickies)ShapeWithTextNode(shapes with text)ConnectorNode(connectors)StampNode(stamps)
Structure of a plugin project
mainA typical plugin sample in this repository is organized into a subdirectory containing:
code.ts: The main logic that interacts with the Figma/FigJam API.ui.html: The HTML file used if the plugin displays a user interface.tsconfig.json: Configuration for the TypeScript compiler, often referencing shared typings.manifest.json: The plugin manifest (required for Figma to recognize the plugin).
Format design tokens using the W3C Design Tokens spec
mainTo use this sample, your tokens must be defined in JSON following the W3C Design Tokens specification. The structure requires a
$valueand a$typefor primitive tokens, and uses curly brace syntax for aliases.Example structure:
- Groups/Tokens: Nested objects representing the token hierarchy.
- Values: The
$valuekey holds the token value. - Types: The
$typekey defines the token type (e.g.,number). - Aliases: Use the format
"{group name.token name}"to reference other tokens.
{ "group name": { "token name": { "$value": 1234, "$type": "number" } }, "alias name": { "$value": "{group name.token name}" } }Emulate Figma UI in your plugin
mainTo make your plugin look and feel like the native Figma interface, you can use the following resources:
- UI2 Design System: Figma's official design system (available via Figma design files).
- Tom's Figma Plugin DS: A community-provided set of CSS and JavaScript files specifically for plugin UI.
Run the Annotations Sample in Dev Mode
mainThe Annotations Sample is a Dev Mode plugin that does not include a plugin UI. You can execute it using one of the following methods:
- Plugin Tab: Open the Plugins tab within Dev Mode.
- Quick Actions Menu: Use the keyboard shortcut
CMD+/(on macOS) orCTRL+/(on Windows) to trigger the Quick Actions menu and run the plugin.
Develop the drag-and-drop-hosted plugin
mainTo develop this sample plugin, you must serve the plugin's UI (
index.html) from a local web server and compile the TypeScript source files.- Serve the UI: Run a local server on port
4004to host theindex.htmlfile. This is required because the plugin uses a hosted UI. - Compile the code: Use the TypeScript compiler to build the project files.
- Serve the UI: Run a local server on port
Styling your plugin UI
mainFor plugins with a UI, it is recommended to match the style and behavior of Figma to ensure a consistent user experience. Two recommended approaches are:
- Figma Plugin DS: A lightweight UI library for styling Figma plugins.
- Create Figma Plugin UI: A library of production-grade Preact components that replicate the Figma editor's UI design.
Explore Figma plugin starters and toolkits
mainBeyond the samples in this repository, several community-maintained toolkits and boilerplates are available for developing Figma plugins with different technology stacks:
Comprehensive Toolkits & CLI
- Create Figma Plugin: A comprehensive toolkit for developing Figma plugins.
- Figplug: A small program for building plugins that includes TypeScript, React/JSX, asset bundling, and plugin manifest generation.
- Plugma: A CLI built with Vite that uses a local dev server for faster development and debugging. Supports most frameworks.
Boilerplates by Framework
- Figma Plugin Boilerplate: For vanilla JavaScript, HTML, and CSS/SCSS without frameworks.
- Figsvelte: A boilerplate specifically for using Svelte.
- Figma Plugin Starter: A boilerplate using React, Vite, and Reshaped.
Component Libraries
- Figma Kit: A set of React components designed for building Figma plugins.
Use the Annotations API to bulk create annotations
mainThe Annotations Sample demonstrates how to use the Figma Annotations API to programmatically create annotations on specific node types in bulk. This sample is designed as an educational starting point for developers looking to automate the annotation process within Figma.Install and compile plugin samples
mainThese samples use TypeScript to leverage the Figma typed plugin API. To use them, you must install TypeScript and compile the code before importing them into the Figma desktop app.
Install TypeScript globally:
npm install -g typescriptInstall dependencies: Run this in the root directory to install the latest Figma typings.
npm installCompile a specific plugin: Navigate to the plugin's directory and run the TypeScript compiler.
cd <plugin-directory>tscImport into Figma: In the Figma desktop app, go to
Plugins > Development > Import plugin from manifest....
$ npm install -g typescript $ npm install $ cd barchart $ tscCommunicate from UI to Plugin via parent.postMessage
mainIn Figma plugins using a UI (iframe), you communicate with the plugin logic (the
code.tssandbox) by sending messages throughparent.postMessage. The message must follow the structure{ pluginMessage: { type: string, ...payload } }.In this
esbuild-reactsample, theAppcomponent demonstrates two message types:create-rectangles: Sends acountnumber to the plugin.cancel: Signals the plugin to stop or cancel the current operation.
// Example: Sending a command to the plugin const onCreate = () => { const count = Number(inputRef.current?.value || 0); parent.postMessage( { pluginMessage: { type: "create-rectangles", count } }, "*" ); }; // Example: Sending a cancel command const onCancel = () => { parent.postMessage({ pluginMessage: { type: "cancel" } }, "*"); };