Help Needed: Replicating Selected Figma Element in Frontend
Hi everyone,
I’m working on replicating Figma elements in a React frontend. The goal is to replicate the exact position, size, and style of the selected element in Figma, using the data sent by the Figma plugin.
What I’ve Done
I’m receiving the data from the Figma then try to send it to frontend.
I can implement this by exporting the element using the Figma API. However, the requirement is that we should be able to edit the design without affecting the Figma file. So, I need the data of the selected element in order to replicate it on the frontend and edit it there.
I tried the code below in the Figma plugin, but I’m still encountering an issue. Is there a way to retrieve the data and replicate it? Also, for images, is it possible to get the image URL or the raw data?
// Handle TEXT node properties
if (node.type === "TEXT") {
element.characters = (node as TextNode).characters;
element.fontSize = (node as TextNode).fontSize;
element.fontName = (node as TextNode).fontName;
element.textAlignHorizontal = (node as TextNode).textAlignHorizontal;
element.textAlignVertical = (node as TextNode).textAlignVertical;
element.lineHeight = (node as TextNode).lineHeight;
element.letterSpacing = (node as TextNode).letterSpacing;
element.fills = (node as TextNode).fills;
}
// Handle RECTANGLE (and other shapes) node properties
if (node.type === "RECTANGLE") {
element.fills = (node as RectangleNode).fills;
element.strokes = (node as RectangleNode).strokes;
element.strokeWeight = (node as RectangleNode).strokeWeight;
element.strokeAlign = (node as RectangleNode).strokeAlign;
element.cornerRadius =
"cornerRadius" in node ? (node as RectangleNode).cornerRadius : 0;
element.dashPattern = (node as RectangleNode).dashPattern;
// Handle IMAGE fill if available
const fills = (node as RectangleNode).fills;
if (Array.isArray(fills) && fills.length > 0) {
const imageFill = fills.find((fill) => fill.type === "IMAGE");
if (imageFill) {
const image = figma.getImageByHash(imageFill.imageHash);
...
If you got all the required properties of the object and wrote a function to convert it to React elements, then everything should be fine.
As for the code snippet you provided, I would get all the properties using a recursive function using loops.
You can get the URLs of the images using the REST API. The raw data can be obtained using the exportAsync function I mentioned above.
To use Figma API, I need fileId, right?
But, in Figma plugin, how can I get fileId?
figma.fileKey is returning undefined.
If you mean REST API, then yes, some endpoints require a file key, including the GET image and GET image fills endpoints. But if you want to get JSON of the object, you can use exportAsync function from Plugin API.