Figma to React With AI, Are We There Yet?

Rate this content
Bookmark

What if we could generate production-ready React components (including props, responsiveness, CSS styling, TypeScript typings, and more) from Figma designs, automatically? This seemingly impossible task is becoming a reality thanks to the latest advancements in AI (such as ChatGPT).


In this talk, we’ll discuss some of the techniques we are using at Anima to automate the boring parts of frontend development, generating React components from Figma designs. We’ll cover some algorithmic techniques, processes and deep-learning based approaches that could help you speed up your day-to-day frontend work.

This talk has been presented at React Advanced Conference 2023, check out the latest edition of this React Conference.

Watch video on a separate page

FAQ

Anima's mission is to automate as much as possible the tedious aspects of front-end development, particularly the process of converting designs into code.

AI technologies can automate the conversion of Figma designs into React code, manage naming conventions, and adapt code to fit team-specific guidelines, significantly speeding up the development process.

The main challenges include accurately translating the design elements and properties from Figma into a React code structure and ensuring the resulting code adheres to specific coding standards and conventions.

One basic approach involves using heuristics, simple rules-based algorithms, to determine HTML tags and styles for Figma nodes and recursively generate HTML code.

Large language models can creatively generate and refine code, helping to improve naming conventions and adapt the generated code to meet specific team guidelines and coding practices.

Anima uses advanced techniques like neural style transfer to adapt heuristic-generated code to follow specific team conventions, blending generated code with team-specific styles and practices.

The speaker suggested that future AI technologies could enable real-time updates between Figma designs and codebases, further automating and streamlining the development process.

Federico Terzi
Federico Terzi
21 min
23 Oct, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's Talk explores how AI can empower developers to write better React code and automate the process of converting Figma designs into code. It discusses the use of heuristics to convert Figma designs into working HTML code and the potential of AI in generating better CSS class names. The Talk also highlights the importance of generating code that follows team conventions and automating design and code updates. Finally, it emphasizes the benefits of using large language models to automate tasks and improve developer productivity.

1. AI Empowering React Code and Figma Translations

Short description:

Today we're going to talk about AI and how it will empower us to write better React code. We'll explore automating the tedious task of recreating designs into code and discuss translating Figma representations into React representations. Figma provides a hierarchy of nodes, supports components, properties, subcomponents, actions, and events. Accessing Figma information is easy with its API.

Hey, everyone. Today we're going to talk about AI and in particular, how the recent advancements in AI technologies are going to empower us in the near future to write better React code, faster while also automating away some of the boring parts of our job.

So before we start a short presentation, I'm Federico, I'm a software architect at Anima and our mission is to automate as much as possible the boring parts of front-end development. So if you're interested, visit animaapp.com. Enough said, let's get started.

As a front-end developer, I used to focus a lot on how to write good React code. But that's not what we are going to discuss today. Today we won't be developers trying to write React code, but rather developers trying to create a machine that writes React code for us automatically. And we're going to start this journey with a story.

I worked for about a year as a front-end developer. And my day-to-day job involved many of the problems I'm sure you're all very familiar with, like interfacing with APIs, state management, performance optimizations. But there was one particular task I found extremely tedious. Every time we had to implement a feature, a designer would come to me with a design, usually in Figma, and then asked me to make it happen as an actual feature. But before implementing the actual functionality, the business logic, what I really liked, my first job was to take the design and then painfully recreate it into code, with all the styling, structure, elements, and so on. And because recreating a pixel-perfect design with code is not easy, the process often requires some back and forth between developers and designers. Because I'm sure you know, designers are really good at spotting slight differences between their designs and the production code. Anyway, very tedious. To the point where many developers started to ask themselves a question, can we automate it? And if we go from a high-level perspective, the question is, can we translate the information in a Figma design into a code representation? And at first, it might seem like an impossible task, as the two representations look quite different.

So, let's dive a bit into what's possible in Figma. First of all, in a Figma design, nodes are structured in a hierarchy, which is very similar to the DOM structure in an HTML page. Figma also supports components, and those components support properties, and those properties can define things like text content, visibility, instances, and many other things. Figma also supports subcomponents. So we can have a button component instead of a car component. And finally, Figma even supports actions and events. So for example, I can say, when I click on this button, open this web page. And by now you might have realized that this information is not that different from our React code. It's kind of the same information, but in a totally different representation. And so our mission today is to translate the Figma representation into a React representation. Before we dive into this translation process, we briefly need to discuss how to actually access the information contained in a Figma design. And it turns out that Figma makes it really easy, because it not only provides an API to access the information about a design, but it provides two.

2. Converting Figma Design to Code with Heuristics

Short description:

We can convert a Figma design into code using heuristics. By using a small JavaScript function, we can convert any Figma design into working HTML code. The function takes a Figma node as input and decides the corresponding HTML tag. It then uses the getCSSAsync method to get an approximate CSS representation of the node styles. The function is called recursively for all the children, and the information is put together in an HTML string. Although the resulting code may not be production-ready, it works and can be extended to handle more cases and produce higher quality code.

One that is REST-based, and another that is JavaScript-based. If you're interested, I highly recommend checking out the documentation, but for the sake of this talk, you can basically think of a Figma design as a JSON document. This JSON structure contains all the design nodes, along with their style properties and other useful information. It's like HTML, but in JSON and using Figma-specific properties. The contained information is roughly the same.

So, we have this JSON document representing a design, right? How do we convert it into code? And there are multiple possible approaches, so let's see some of them. We start from the most basic one, heuristics. And you might have heard of the term heuristic before. For the sake of this talk, we'll refer to heuristics as a collection of rules that define the behavior of our system. In simple words, imagine some code that says, if this happens, do this, otherwise do that. Pretty straightforward.

And we can actually convert a Figma design into code by just using heuristics. In fact, here we've prepared a small JavaScript function which is less than 20 lines of code that can convert any Figma design into somewhat working HTML code. So, let's see what it does step by step. First of all, the function takes as input a Figma node. Then we need to decide which HTML tag the current node corresponds to and we use a very simple rule. If it's a text node, make it a span, otherwise make it a div. Then we use a Figma method called getCSSAsync which basically gives us an approximate CSS representation of the node styles. And finally, we basically call the function recursively for all the children. We can then put together all this information in an HTML string and if I open up Figma, I have a video here to show you. So basically if you open Figma and go to Plugin, Development, Open Console, this opens up a console you can paste any JavaScript you want to execute on. If I paste the function we just created, this produces some HTML code. Now if we get that HTML code and we paste it into CodePen, give it a sec, this is the card. That's pretty impressive.

And the thing is, let me go next, the thing is this is the resulting code and for sure it's not production ready. It's ugly, it's verbose, it uses inline styles, but the thing is, it works and it took us less than 20 lines of code. So you can imagine that even though it's not perfect, you can extend these heuristics to handle more and more cases and produce higher quality code. But even though heuristic can get us pretty far in creating high quality code, it's not a bulletproof solution and have its problems. For example, let's say that you want to generate code that uses CSS classes rather than inline styles. One of the problems you need to face is that you need to come up with good CSS class names for each node.