Skip to main content
Question

layoutAlign not working


Aamir1

If a parent is set to auto-layout, and it’s children are set to layoutGrow = 1 and layoutAlign = “STRETCH”, the children should fill the parent container both horizontally and vertically, right?

I’m doing the same but for some reason layoutAlign is not working 😦

Here’s the code (Please check out the ‘//Creating a frame for column’ code block… that is where the problem is):

figma.showUI(__html__, { themeColors: true })
figma.ui.resize(300, 250)

let bounds = figma.viewport.bounds
let key = "93fecd27ee35164ce296179f418dd122071b4ecc"

figma.ui.onmessage = async (pluginMessage) => {
  //Importing a component set and saving components as variables
  let componentSet = await figma.importComponentSetByKeyAsync(key);
  let regularCell = componentSet.findChild(e => e.name === "Property 1=Regular Cell") as ComponentNode
  let colHeader = componentSet.findChild(e => e.name === "Property 1=Column Headers") as ComponentNode
  let rowHeader = componentSet.findChild(e => e.name === "Property 1=Row Headers") as ComponentNode

  //Number of rows and columns got from user's input
  let rows = pluginMessage.rows
  let cols = pluginMessage.cols

  //Creating a frame for table
  let table = figma.createFrame()
  table.name = "Table"
  table.layoutMode = "HORIZONTAL"
  table.primaryAxisSizingMode = "FIXED"
  table.counterAxisSizingMode = "FIXED"

  //Resizing the table and placing in the center of the viewport
  table.resizeWithoutConstraints(regularCell.width * cols, regularCell.height * rows)
  table.x = bounds.x + bounds.width / 2 - table.width / 2
  table.y = bounds.y + bounds.height / 2 - table.height / 2

  //Looping through columns
  for (let i = 0; i < cols; i++) {

    //Creating a frame for column
    let columnFrame = figma.createFrame()
    columnFrame.name = `Column ${i + 1}`
    table.appendChild(columnFrame)
    columnFrame.layoutMode = "VERTICAL"
    columnFrame.layoutAlign = "STRETCH"
    columnFrame.layoutGrow = 1
    columnFrame.x = i * regularCell.width

    //Looping through rows
    for (let j = 0; j < rows; j++) {

      //Creating a cell instance
      let temp = j === 0 ? colHeader.createInstance() : i === 0 ? rowHeader.createInstance() : regularCell.createInstance()
      temp.name = `Cell ${j + 1}`
      columnFrame.appendChild(temp)
      temp.layoutAlign = "STRETCH"
      temp.layoutGrow = 1
      temp.x = i * 0
      temp.y = j * temp.height
    }
  }

  figma.currentPage.selection = [table]

  figma.closePlugin();
}

This topic has been closed for comments

Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings