Building Figma’s Widget Code Generator

Rate this content
Bookmark

Widgets are custom, interactive objects you place in a Figma or Figjam file to extend functionality and make everything a bit more fun. They are written in a declarative style similar to React components, which gets translated to become a node on the canvas. So can you go the other way, from canvas to code? Yes! We’ll discuss how we used the public Figma plugin API to generate widget code from a design file, and make a working widget together using this.

FAQ

FigJam is Figma's online collaborative whiteboard tool that allows users to add elements like post-it notes, drawings, and widgets, facilitating interactive sessions among team members.

Yes, FigJam supports custom widgets through its public widget API, which is component-based and similar to React, allowing users to design and implement their own interactive widgets.

The Figma widget Code Generator is a plugin developed to help users automatically generate code for widgets in FigJam, making the development process faster and more efficient. It is available for download in the Figma community.

To use a custom widget in FigJam, you need to write the widget code, compile it, and then run it in FigJam through the 'Widgets' development menu, where you can add and interact with the widget.

Creating widgets for FigJam requires knowledge of TypeScript and React, as the widget API uses JSX components and Figma-specific layout tags similar to React components.

The widget Code Generator simplifies the development process by automatically generating the necessary React-like components from the design elements within Figma, allowing developers to quickly scaffold and customize widgets.

The public Figma plugin API allows reading from and writing to Figma files, and provides access to various properties like user information, document components, and more, enabling extensive customization and functionality enhancement in plugins.

Users can interact with custom widgets in FigJam by engaging with UI components like buttons, which can be programmed to trigger specific actions such as voting or updating content in real-time.

Jenny Lea
Jenny Lea
19 min
24 Oct, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This Talk introduces Figma's Widget Code Generator and demonstrates how to build a FigJam widget using it. The speaker discusses the implementation of voting functionality, avatar functionality, and remove vote functionality. They also explain how the Widget Code Generator plugin works and how to access properties and modify names using the Figma plugin API.

1. Introduction to Figma's Widget Code Generator

Short description:

Hi, everyone. My name is Jenny, and I'm an engineer at Figma. In this talk, we'll be using Figma's widget Code Generator to build a FigJam widget and discuss its creation. FigJam is an online collaborative whiteboard tool where you can add widgets. To make FigJam files more flexible, there is a public widget API that allows you to write your own widgets. We'll start by designing a simple voting widget and writing the code for it. Then, we'll use the widget code generator plugin to generate the necessary components. By using these components, we can see our design come to life in FigJam. Let's dive in!

Hi, everyone. I hope you're having a lovely conference so far. My name is Jenny, and I'm an engineer at Figma. I'm based in Leeds, in the UK, so for those of you that didn't know Figma had a product team in Europe, now you know, and welcome to this talk on Figma's widget Code Generator. We're going to be using it to build a FigJam widget and then talk about how it was made.

On my screen now, you can see FigJam. This is Figma's online collaborative whiteboard tool where I can do things like add a post-it note, I can do some drawing, most importantly for this talk, I can add a widget. There we go. This can be seen and interacted with by everyone that is in the file, which at the moment is unfortunately just me. And they aren't just restricted to the existing widgets. So to make FigJam files as fun and as flexible as possible, there is a public widget API, a component-based API, quite similar to React, which allows you to write your own widgets. And that's what we're going to do now.

So I've got, if we go into Figma design, I've got my design for my widget here. It's just going to be a simple voting widget with a button here that you press to vote and that will increase the number of total votes so far and then put your little profile picture into a circle and line them up all along the bottom for however many times you voted. So, and then also I've got a code editor on the other side and this is the code for what my widget project looks like. Again, it's super simple. I'm going to write all of my code in code.tsx and I've got a package.json file with a couple of scripts. The one that we want is watch. So, if I run that now, npm run watch, it should start my widget running, put all its compiled code into code.js, and if I go back into FigJam, I can run my widget by going widgets, development, simple vote, and there we go, it says, hello, I am a widget, which is what the widget function is returning. And I can add some exclamation marks to this and they'll update in real time. So, hopefully this widget code is quite familiar to you. It looks just like React using JSX components and some specific Figma layout tags.

So, if we go back to our design, if we wanted to start coding this from scratch, we'd have to go through and look at our simple vote layout and see that it's a frame with auto layout set. So, we'd have to start adding an auto layout and then we'd have to look at the properties in that auto layout like the gap of four and put this in as spacing and you can see it would take quite a long time. So, earlier this year, I helped as part of our biannual maker week to create a widget code generator plugin and if we run that, this is available to everyone on Figma community, so if you just google Figma's widget code generator, this is something you can install yourself onto your Figma app. But if we run it here, and I'll just minimize this window, run it, and click on the thing that we want to get the code for, so our simple vote widget, you can see it generates all of these components for us, with a layout that looks pretty similar to the layout of the layers in Figma, and we can copy all of that and paste it into our text editor. It's complaining about avatar, so we'll just comment that out and come back to it in a minute. But what we've got is a function called Simple Vote. So if we take this Simple Vote component and then use it inside our main widget code, we'll go back to Big Jam, it's starting to look quite like our design already, isn't it? We've got the gradient, we've got the text, it's all laid out in the right place, but we're missing the pictures along the bottom, and that has probably got something to do with the thing that I just commented out.

2. Implementing the Voting Functionality

Short description:

If we use the Simple Vote component in our main widget code, it will start to resemble our design. However, we're missing the pictures along the bottom, which is related to the commented-out code. To resolve this, we need to go to the avatar component and include its code in our file. Once we do that, the widget will have the desired appearance. To make the button functional, we need to add an onclick event that updates the voters array and displays the user's picture. We achieve this by using the Figma widget API's you sync state to store the voters' state and the Figma.CurrentUser object to get the user's profile. Finally, we attach the onclick event to the VoteButton and update the displayed number using the voters.length property.

So if we take this Simple Vote component and then use it inside our main widget code, we'll go back to Big Jam, it's starting to look quite like our design already, isn't it? We've got the gradient, we've got the text, it's all laid out in the right place, but we're missing the pictures along the bottom, and that has probably got something to do with the thing that I just commented out.

So if we put it back, it's complaining that it cannot find avatar. And this is because in our design here, frames 6, 7 and 8, these are all instances of a component called avatar, and instead of doing this inline and just writing out the code three times, what we want you to do is go to the avatar component and get the code for that and put it into your file just like we did with simple vote. So now we've got a function for avatar, and these instances are a bit happier. And if we go back to FigJam again, if you've got really beady eyes, you'll be able to see that there are three faint white circles. That's because they're outlined in white. If we change the outline to red just so we can see where the circles are, there we've got it. We've basically got our widget. It doesn't do anything though. So we need to add an on click to this button. So when you click it, it will add you to the list of voters, increase that number and put your picture along the bottom.

Okay, so in simple vote, how we're going to do that is by storing some state of our voters. You probably recognize this from React. Instead of saying use state, we're going to say you sync state, which is part of the Figma widget API, give it a name and start it off as an empty array. Because this is a TypeScript file, I'm also going to give it a type that I know is going to be user. And then we want to update this voters array when someone clicks on the button. I'm going to make an onclick function. And when they click, we want to get your profile as the person who's voted, because that's going to let us get your profile photo. And to do that, we can say voter is Figma.CurrentUser. And Figma here is a global object that is provided in the widget run time that lets us get things like the current user and loads of other information about what's going on in the file. So now we've got this voter. I know that this voter can sometimes be null. It probably won't be, but just to protect against that, I'm going to say if we don't have a photo, voter, don't do anything. And if we do have a voter, we're going to set voters, get our original list, and add a new voter to the list. And our OnClick is useless if we don't put it anywhere. So we want it to happen when we click on this button, which we can find in the layout because it's called VoteButton. And all of these tags can have an OnClick attached to them. So I'm going to put that OnClick on there. And then we also need to use the voters array to update the number here. So previously where it's had three hard coded, instead, we're going to use voters.length.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Modern Web Debugging
JSNation 2023JSNation 2023
29 min
Modern Web Debugging
Top Content
Few developers enjoy debugging, and debugging can be complex for modern web apps because of the multiple frameworks, languages, and libraries used. But, developer tools have come a long way in making the process easier. In this talk, Jecelyn will dig into the modern state of debugging, improvements in DevTools, and how you can use them to reliably debug your apps.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Our understanding of performance & user-experience has heavily evolved over the years. Web Developer Tooling needs to similarly evolve to make sure it is user-centric, actionable and contextual where modern experiences are concerned. In this talk, Addy will walk you through Chrome and others have been thinking about this problem and what updates they've been making to performance tools to lower the friction for building great experiences on the web.
A Framework for Managing Technical Debt
TechLead Conference 2023TechLead Conference 2023
35 min
A Framework for Managing Technical Debt
Top Content
Let’s face it: technical debt is inevitable and rewriting your code every 6 months is not an option. Refactoring is a complex topic that doesn't have a one-size-fits-all solution. Frontend applications are particularly sensitive because of frequent requirements and user flows changes. New abstractions, updated patterns and cleaning up those old functions - it all sounds great on paper, but it often fails in practice: todos accumulate, tickets end up rotting in the backlog and legacy code crops up in every corner of your codebase. So a process of continuous refactoring is the only weapon you have against tech debt.In the past three years, I’ve been exploring different strategies and processes for refactoring code. In this talk I will describe the key components of a framework for tackling refactoring and I will share some of the learnings accumulated along the way. Hopefully, this will help you in your quest of improving the code quality of your codebases.

Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
As developers, we spend much of our time debugging apps - often code we didn't even write. Sadly, few developers have ever been taught how to approach debugging - it's something most of us learn through painful experience.  The good news is you _can_ learn how to debug effectively, and there's several key techniques and tools you can use for debugging JS and React apps.
The Whimsical Potential of JavaScript Frameworks
React Summit US 2023React Summit US 2023
28 min
The Whimsical Potential of JavaScript Frameworks
Top Content
When it comes to building whimsical interfaces, React is a surprisingly capable ally. In this talk, I'll show you how I use React to orchestrate complex interactions by digging into some examples from my blog.
Building a Voice-Enabled AI Assistant With Javascript
JSNation 2023JSNation 2023
21 min
Building a Voice-Enabled AI Assistant With Javascript
Top Content
In this talk, we'll build our own Jarvis using Web APIs and langchain. There will be live coding.

Workshops on related topic

React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Building a Shopify App with React & Node
React Summit Remote Edition 2021React Summit Remote Edition 2021
87 min
Building a Shopify App with React & Node
Top Content
WorkshopFree
Jennifer Gray
Hanna Chen
2 authors
Shopify merchants have a diverse set of needs, and developers have a unique opportunity to meet those needs building apps. Building an app can be tough work but Shopify has created a set of tools and resources to help you build out a seamless app experience as quickly as possible. Get hands on experience building an embedded Shopify app using the Shopify App CLI, Polaris and Shopify App Bridge.We’ll show you how to create an app that accesses information from a development store and can run in your local environment.
Build a chat room with Appwrite and React
JSNation 2022JSNation 2022
41 min
Build a chat room with Appwrite and React
WorkshopFree
Wess Cope
Wess Cope
API's/Backends are difficult and we need websockets. You will be using VS Code as your editor, Parcel.js, Chakra-ui, React, React Icons, and Appwrite. By the end of this workshop, you will have the knowledge to build a real-time app using Appwrite and zero API development. Follow along and you'll have an awesome chat app to show off!
Hard GraphQL Problems at Shopify
GraphQL Galaxy 2021GraphQL Galaxy 2021
164 min
Hard GraphQL Problems at Shopify
WorkshopFree
Rebecca Friedman
Jonathan Baker
Alex Ackerman
Théo Ben Hassen
 Greg MacWilliam
5 authors
At Shopify scale, we solve some pretty hard problems. In this workshop, five different speakers will outline some of the challenges we’ve faced, and how we’ve overcome them.

Table of contents:
1 - The infamous "N+1" problem: Jonathan Baker - Let's talk about what it is, why it is a problem, and how Shopify handles it at scale across several GraphQL APIs.
2 - Contextualizing GraphQL APIs: Alex Ackerman - How and why we decided to use directives. I’ll share what directives are, which directives are available out of the box, and how to create custom directives.
3 - Faster GraphQL queries for mobile clients: Theo Ben Hassen - As your mobile app grows, so will your GraphQL queries. In this talk, I will go over diverse strategies to make your queries faster and more effective.
4 - Building tomorrow’s product today: Greg MacWilliam - How Shopify adopts future features in today’s code.
5 - Managing large APIs effectively: Rebecca Friedman - We have thousands of developers at Shopify. Let’s take a look at how we’re ensuring the quality and consistency of our GraphQL APIs with so many contributors.
0 To Auth In An Hour For Your JavaScript App
JSNation 2023JSNation 2023
57 min
0 To Auth In An Hour For Your JavaScript App
WorkshopFree
Asaf Shen
Asaf Shen
Passwordless authentication may seem complex, but it is simple to add it to any app using the right tool.
We will enhance a full-stack JS application (Node.js backend + Vanilla JS frontend) to authenticate users with One Time Passwords (email) and OAuth, including:
- User authentication – Managing user interactions, returning session / refresh JWTs- Session management and validation – Storing the session securely for subsequent client requests, validating / refreshing sessions
At the end of the workshop, we will also touch on another approach to code authentication using frontend Descope Flows (drag-and-drop workflows), while keeping only session validation in the backend. With this, we will also show how easy it is to enable biometrics and other passwordless authentication methods.