--- name: figma-plugin description: "Figma Plugin API + TypeScript conventions. Loaded automatically when building a Figma plugin." --- # Figma Plugin Platform Build Figma plugins that extend the design tool. TypeScript + Figma Plugin API. ## Project Structure Explain to the designer as: > "Your plugin has two parts: the brain (logic) and the face (UI panel)." ``` src/ code.ts ← Plugin logic (talks to Figma canvas) ui.html ← Plugin panel (what the user sees) ui.ts ← Panel interactivity (optional, for complex UIs) manifest.json ← Plugin name, permissions, entry points tsconfig.json ← TypeScript settings package.json ← Dependencies figma.d.ts ← Type definitions for Figma API (auto-generated) ``` ## Scaffolding No CLI tool — scaffold manually: ```bash mkdir my-plugin && cd my-plugin npm init -y npm install -D typescript @figma/plugin-typings ``` Create `manifest.json`: ```json { "name": "My Plugin", "id": "000000000000000000", "api": "1.0.0", "main": "dist/code.js", "ui": "src/ui.html", "editorType": ["figma"] } ``` Create `tsconfig.json`: ```json { "compilerOptions": { "target": "ES2017", "module": "commonjs", "outDir": "./dist", "strict": true, "typeRoots": ["./node_modules/@figma/plugin-typings"] }, "include": ["src/**/*.ts"] } ``` Add build script to `package.json`: ```json "scripts": { "build": "tsc", "watch": "tsc --watch" } ``` ## Key Conventions ### Two Worlds: Code vs UI The plugin has two separate environments that talk to each other: | | Code (`code.ts`) | UI (`ui.html`) | |---|---|---| | Runs in | Figma's sandbox | An iframe | | Can access | Figma canvas, nodes, styles | HTML, CSS, DOM | | Can't access | DOM, window, fetch | Figma nodes directly | | Communicates via | `figma.ui.postMessage()` | `parent.postMessage()` | > "Think of it like two rooms with a mail slot. The brain room can see and change your design. The face room shows buttons and inputs. They pass notes back and forth." ### Talking to the Canvas ```typescript // code.ts // Read the current selection const selection = figma.currentPage.selection // Create a rectangle const rect = figma.createRectangle() rect.resize(200, 100) rect.fills = [{ type: 'SOLID', color: { r: 0.2, g: 0.4, b: 1 } }] // Create text const text = figma.createText() await figma.loadFontAsync({ family: "Inter", style: "Regular" }) text.characters = "Hello from the plugin!" // Read node properties const node = figma.currentPage.selection[0] if (node.type === 'TEXT') { console.log(node.characters) // the text content } ``` ### Sending Messages Between Code and UI ```typescript // code.ts → ui.html figma.ui.postMessage({ type: 'selection-changed', count: selection.length }) // ui.html → code.ts (in a ``` ### Storing Data Plugins can save data two ways: **Per-document** (stored in the Figma file): ```typescript // Save figma.root.setPluginData('settings', JSON.stringify({ theme: 'dark' })) // Load const settings = JSON.parse(figma.root.getPluginData('settings') || '{}') ``` **Per-user** (stored locally, survives across files): ```typescript // Save await figma.clientStorage.setAsync('preferences', { lastUsed: Date.now() }) // Load const prefs = await figma.clientStorage.getAsync('preferences') ``` > "Per-document is like writing a note on the file itself — anyone who opens the file sees it. Per-user is like your personal notebook — only you see it, but it works in every file." ### No Server, No Database Figma plugins run entirely in the browser. No Supabase, no server actions, no env vars. If the plugin needs external data (e.g., fetch from an API), it must go through the UI iframe: ```html ``` The code.ts sandbox cannot use `fetch` directly. ## Common Commands | Command | What it does | |---|---| | `npm run build` | Compile TypeScript | | `npm run watch` | Auto-compile on save | ## Testing the Plugin 1. Open Figma desktop app 2. Go to Plugins → Development → Import plugin from manifest 3. Select your `manifest.json` 4. Run from Plugins → Development → Your Plugin Name > "To test, open Figma, go to Plugins menu, and pick your plugin from the Development section." ## Publishing 1. Go to figma.com/plugin-docs/publishing 2. Fill in name, description, cover image 3. Submit for review (usually approved within a few days) > "When you're ready to share your plugin with everyone, we'll submit it to Figma's plugin store." ## Limitations - No database — use `pluginData` or `clientStorage` for saving settings - No login — Figma handles user identity - No email — plugins can't send emails - No file uploads — files live in Figma - Code.ts can't use `fetch` — must go through UI iframe - UI runs in an iframe with limited capabilities