Index of every page on this site: llms.txt
All skills
bergside avatar

/figma-plugin

@746501e
by bergsidebergside/design-md-figma160 stars
20
Run it yourself

Use when building Figma plugins, creating design automation tools, implementing sandbox/UI communication, or working with the Figma Plugin API for node manipulation, styles, and components.

Use this Skill: https://skilld.dev/gh/bergside/design-md-figma/figma-plugin

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesui-architecture-and-messaging.md

≈1.8k tokens on demand. Your agent reads this file only when SKILL.md points to it.

UI Architecture and Messaging

Building plugin user interfaces and inter-thread communication.

UI Architecture

┌─────────────────────────────────────────────────────────────┐
│                    MAIN THREAD (code.ts)                    │
│                                                             │
│   figma.showUI(__html__)                                    │
│         │                                                   │
│         ▼                                                   │
│   ┌─────────────────────────────────────────────────────┐  │
│   │                 UI IFRAME (ui.html)                  │  │
│   │                                                      │  │
│   │   • Full browser environment                         │  │
│   │   • HTML, CSS, JavaScript                            │  │
│   │   • Can use React, Vue, Svelte, etc.                │  │
│   │   • NO access to Figma API                          │  │
│   │   • Communicates via postMessage                     │  │
│   │                                                      │  │
│   └─────────────────────────────────────────────────────┘  │
│                                                             │
└─────────────────────────────────────────────────────────────┘

Showing UI

Basic UI

// code.ts
figma.showUI(__html__);  // __html__ is replaced with ui.html contents at build

// With options
figma.showUI(__html__, {
  width: 400,
  height: 300,
  title: 'My Plugin',
  visible: true,
  position: { x: 100, y: 100 },
  themeColors: true,  // Use Figma's theme colors
});

UI Options

interface ShowUIOptions {
  width?: number;           // Default: 300
  height?: number;          // Default: 200
  visible?: boolean;        // Default: true
  position?: { x: number; y: number };
  title?: string;
  themeColors?: boolean;    // Inject Figma CSS variables
}

Resize UI

// From main thread
figma.ui.resize(500, 400);

// From UI (request main thread to resize)
parent.postMessage({
  pluginMessage: { type: 'resize', width: 500, height: 400 }
}, '*');

// code.ts
figma.ui.onmessage = (msg) => {
  if (msg.type === 'resize') {
    figma.ui.resize(msg.width, msg.height);
  }
};

Message Communication

UI → Main Thread

<!-- ui.html -->
<script>
// Send message to main thread
function sendMessage(type, data) {
  parent.postMessage({ pluginMessage: { type, ...data } }, '*');
}

// Examples
sendMessage('create-shape', { shape: 'rectangle', width: 100, height: 50 });
sendMessage('update-color', { color: '#FF5733' });
sendMessage('close');
</script>

Main Thread → UI

// code.ts
// Send data to UI
figma.ui.postMessage({
  type: 'selection-data',
  nodes: figma.currentPage.selection.map(node => ({
    id: node.id,
    name: node.name,
    type: node.type,
  })),
});

// Send on selection change
figma.on('selectionchange', () => {
  figma.ui.postMessage({
    type: 'selection-changed',
    count: figma.currentPage.selection.length,
  });
});

Receiving in UI

<script>
window.onmessage = (event) => {
  const msg = event.data.pluginMessage;
  if (!msg) return;

  switch (msg.type) {
    case 'selection-data':
      renderNodes(msg.nodes);
      break;
    case 'selection-changed':
      updateCount(msg.count);
      break;
    case 'error':
      showError(msg.message);
      break;
  }
};
</script>

Typed Messages

// shared/types.ts
export type MainToUI =
  | { type: 'selection-changed'; count: number }
  | { type: 'node-data'; node: SerializedNode }
  | { type: 'error'; message: string }
  | { type: 'styles-loaded'; styles: StyleData[] };

export type UIToMain =
  | { type: 'create-shape'; shape: 'rectangle' | 'ellipse'; size: number }
  | { type: 'apply-style'; styleId: string }
  | { type: 'close' };

// code.ts
figma.ui.onmessage = (msg: UIToMain) => {
  switch (msg.type) {
    case 'create-shape':
      // TypeScript knows shape and size exist
      break;
  }
};

// ui.ts
declare function postMessage(msg: UIToMain): void;

Plain HTML/CSS/JS

Basic Structure

<!-- ui.html -->
<!DOCTYPE html>
<html>
<head>
  <style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body {
      font-family: Inter, system-ui, sans-serif;
      font-size: 11px;
      color: var(--figma-color-text);
      background: var(--figma-color-bg);
      padding: 12px;
    }

    .input-group {
      margin-bottom: 12px;
    }

    label {
      display: block;
      margin-bottom: 4px;
      font-weight: 500;
    }

    input, select {
      width: 100%;
      padding: 8px;
      border: 1px solid var(--figma-color-border);
      border-radius: 4px;
      background: var(--figma-color-bg);
      color: var(--figma-color-text);
    }

    input:focus, select:focus {
      outline: none;
      border-color: var(--figma-color-border-brand);
    }

    button {
      padding: 8px 16px;
      border: none;
      border-radius: 6px;
      cursor: pointer;
      font-weight: 500;
    }

    .btn-primary {
      background: var(--figma-color-bg-brand);
      color: white;
    }

    .btn-secondary {
      background: var(--figma-color-bg-secondary);
      color: var(--figma-color-text);
    }

    .btn-row {
      display: flex;
      gap: 8px;
      justify-content: flex-end;
      margin-top: 16px;
    }
  </style>
</head>
<body>
  <div class="input-group">
    <label for="name">Name</label>
    <input type="text" id="name" placeholder="Enter name">
  </div>

  <div class="input-group">
    <label for="size">Size</label>
    <input type="number" id="size" value="100" min="1">
  </div>

  <div class="btn-row">
    <button class="btn-secondary" id="cancel">Cancel</button>
    <button class="btn-primary" id="create">Create</button>
  </div>

  <script>
    const nameInput = document.getElementById('name');
    const sizeInput = document.getElementById('size');

    document.getElementById('create').onclick = () => {
      parent.postMessage({
        pluginMessage: {
          type: 'create',
          name: nameInput.value,
          size: parseInt(sizeInput.value, 10),
        }
      }, '*');
    };

    document.getElementById('cancel').onclick = () => {
      parent.postMessage({ pluginMessage: { type: 'close' } }, '*');
    };

    // Receive messages
    window.onmessage = (event) => {
      const msg = event.data.pluginMessage;
      if (msg?.type === 'update') {
        nameInput.value = msg.name || '';
      }
    };
  </script>
</body>
</html>

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts6mo3 checks · Risk SAFE
  • Gen Agent Trust Hub6mo

    This skill provides a comprehensive set of references and templates for developing Figma plugins. It covers architecture, API usage, UI development, and build processes. No malicious patterns or security risks were identified.

  • Socket6mo

    No alerts

  • Snyk6mo

    Risk: LOW · No issues

Signed by skilld at 746501e. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 9 months ago

README badge

README badge for bergside/design-md-figma