Skip to content

BumpyClock/agent-templates / skills_archive/figma/figma-generate-library

figma-generate-library

Verified

Build/update Figma design systems: variables, tokens, components, theming.

scripts/bindVariablesToComponent.js

View on GitHub
/**
 * bindVariablesToComponent
 *
 * Binds design token variables to the visual properties of a component node.
 * Supports fills, strokes, all padding directions, item spacing, and corner radius.
 * Only binds properties for which a variable ID is provided in `bindings`.
 *
 * This function should be called on each variant individually within a component
 * set, OR on the component set itself for properties shared by all variants.
 *
 * @param {ComponentNode | FrameNode | RectangleNode} component
 *   The Figma node to mutate. Usually a ComponentNode or one of its children.
 * @param {{
 *   fills?: string,
 *   strokes?: string,
 *   paddingTop?: string,
 *   paddingBottom?: string,
 *   paddingLeft?: string,
 *   paddingRight?: string,
 *   itemSpacing?: string,
 *   cornerRadius?: string
 * }} bindings
 *   Each key is a visual property name; each value is a Figma Variable ID
 *   (e.g. "VariableID:123:456"). Omit a key to skip binding that property.
 * @returns {Promise<{ mutatedNodeIds: string[] }>}
 *   List of node IDs that were mutated (for audit/validation purposes).
 */
async function bindVariablesToComponent(component, bindings) {
  const mutatedNodeIds = []

  if (!component) {
    return { mutatedNodeIds }
  }

  // --- Fills ---
  if (bindings.fills) {
    const fillVar = await figma.variables.getVariableByIdAsync(bindings.fills)
    if (fillVar) {
      const existingFills = component.fills
      if (Array.isArray(existingFills) && existingFills.length > 0) {
        // Bind the color of the first fill to the variable
        const boundFill = figma.variables.setBoundVariableForPaint(
          existingFills[0],
          'color',
          fillVar,
        )
        component.fills = [boundFill, ...existingFills.slice(1)]
      } else {
        // No existing fill — create a solid fill bound to the variable
        const boundFill = figma.variables.setBoundVariableForPaint(
          { type: 'SOLID', color: { r: 0.5, g: 0.5, b: 0.5 } },
          'color',
          fillVar,
        )
        component.fills = [boundFill]
      }
      mutatedNodeIds.push(component.id)
    }
  }

  // --- Strokes ---
  if (bindings.strokes) {
    const strokeVar = await figma.variables.getVariableByIdAsync(bindings.strokes)
    if (strokeVar) {
      const existingStrokes = component.strokes
      if (Array.isArray(existingStrokes) && existingStrokes.length > 0) {
        const boundStroke = figma.variables.setBoundVariableForPaint(
          existingStrokes[0],
          'color',
          strokeVar,
        )
        component.strokes = [boundStroke, ...existingStrokes.slice(1)]
      } else {
        const boundStroke = figma.variables.setBoundVariableForPaint(
          { type: 'SOLID', color: { r: 0.5, g: 0.5, b: 0.5 } },
          'color',
          strokeVar,
        )
        component.strokes = [boundStroke]
      }
      if (!mutatedNodeIds.includes(component.id)) {
        mutatedNodeIds.push(component.id)
      }
    }
  }

  // --- Spacing properties (FLOAT variables bound via setBoundVariable) ---
  const floatBindings = [
    ['paddingTop', 'paddingTop'],
    ['paddingBottom', 'paddingBottom'],
    ['paddingLeft', 'paddingLeft'],
    ['paddingRight', 'paddingRight'],
    ['itemSpacing', 'itemSpacing'],
    ['cornerRadius', 'cornerRadius'],
  ]

  for (const [bindingKey, figmaProp] of floatBindings) {
    if (bindings[bindingKey]) {
      const variable = await figma.variables.getVariableByIdAsync(bindings[bindingKey])
      if (variable) {
        component.setBoundVariable(figmaProp, variable)
        if (!mutatedNodeIds.includes(component.id)) {
          mutatedNodeIds.push(component.id)
        }
      }
    }
  }

  return { mutatedNodeIds }
}
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 319kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 196kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 196kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 196kUpdated Claude CodeCodexVerifiedInstall