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.

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

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
This Talk discusses modern web debugging and the latest updates in Chrome DevTools. It highlights new features that help pinpoint issues quicker, improved file visibility and source mapping, and ignoring and configuring files. The Breakpoints panel in DevTools has been redesigned for easier access and management. The Talk also covers the challenges of debugging with source maps and the efforts to standardize the source map format. Lastly, it provides tips for improving productivity with DevTools and emphasizes the importance of reporting bugs and using source maps for debugging production code.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
A Framework for Managing Technical Debt
TechLead Conference 2023TechLead Conference 2023
35 min
A Framework for Managing Technical Debt
Top Content
Today's Talk discusses the importance of managing technical debt through refactoring practices, prioritization, and planning. Successful refactoring requires establishing guidelines, maintaining an inventory, and implementing a process. Celebrating success and ensuring resilience are key to building a strong refactoring culture. Visibility, support, and transparent communication are crucial for addressing technical debt effectively. The team's responsibilities, operating style, and availability should be transparent to product managers.
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
Watch video: Debugging JS
Debugging JavaScript is a crucial skill that is often overlooked in the industry. It is important to understand the problem, reproduce the issue, and identify the root cause. Having a variety of debugging tools and techniques, such as console methods and graphical debuggers, is beneficial. Replay is a time-traveling debugger for JavaScript that allows users to record and inspect bugs. It works with Redux, plain React, and even minified code with the help of source maps.
The Whimsical Potential of JavaScript Frameworks
React Summit US 2023React Summit US 2023
28 min
The Whimsical Potential of JavaScript Frameworks
Top Content
Watch video: The Whimsical Potential of JavaScript Frameworks
The speaker discusses the whimsical and detailed work of Stripe, particularly their interactive and dynamic pages. They explain the use of React for building whimsical details and tracking mouse position. The speaker introduces React Spring for smooth animation and React3 Fiber for creating a 3D egg model. They also mention the use of Framer Motion and React server components for animating CSS properties.
Building a Voice-Enabled AI Assistant With Javascript
JSNation 2023JSNation 2023
21 min
Building a Voice-Enabled AI Assistant With Javascript
Top Content
This Talk discusses building a voice-activated AI assistant using web APIs and JavaScript. It covers using the Web Speech API for speech recognition and the speech synthesis API for text to speech. The speaker demonstrates how to communicate with the Open AI API and handle the response. The Talk also explores enabling speech recognition and addressing the user. The speaker concludes by mentioning the possibility of creating a product out of the project and using Tauri for native desktop-like experiences.

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.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.
Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
Integrating LangChain with JavaScript for Web Developers
React Summit 2024React Summit 2024
92 min
Integrating LangChain with JavaScript for Web Developers
Featured Workshop
Vivek Nayyar
Vivek Nayyar
Dive into the world of AI with our interactive workshop designed specifically for web developers. "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" offers a unique opportunity to bridge the gap between AI and web development. Despite the prominence of Python in AI development, the vast potential of JavaScript remains largely untapped. This workshop aims to change that.Throughout this hands-on session, participants will learn how to leverage LangChain—a tool designed to make large language models more accessible and useful—to build dynamic AI agents directly within JavaScript environments. This approach opens up new possibilities for enhancing web applications with intelligent features, from automated customer support to content generation and beyond.We'll start with the basics of LangChain and AI models, ensuring a solid foundation even for those new to AI. From there, we'll dive into practical exercises that demonstrate how to integrate these technologies into real-world JavaScript projects. Participants will work through examples, facing and overcoming the challenges of making AI work seamlessly on the web.This workshop is more than just a learning experience; it's a chance to be at the forefront of an emerging field. By the end, attendees will not only have gained valuable skills but also created AI-enhanced features they can take back to their projects or workplaces.Whether you're a seasoned web developer curious about AI or looking to expand your skillset into new and exciting areas, "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" is your gateway to the future of web development. Join us to unlock the potential of AI in your web projects, making them smarter, more interactive, and more engaging for users.
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.
From Todo App to B2B SaaS with Next.js and Clerk
React Summit US 2023React Summit US 2023
153 min
From Todo App to B2B SaaS with Next.js and Clerk
WorkshopFree
Dev Agrawal
Dev Agrawal
If you’re like me, you probably have a million side-project ideas, some that could even make you money as a micro SaaS, or could turn out to be the next billion dollar startup. But how do you know which ones? How do you go from an idea into a functioning product that can be put into the hands of paying customers without quitting your job and sinking all of your time and investment into it? How can your solo side-projects compete with applications built by enormous teams and large enterprise companies?
Building rich SaaS products comes with technical challenges like infrastructure, scaling, availability, security, and complicated subsystems like auth and payments. This is why it’s often the already established tech giants who can reasonably build and operate products like that. However, a new generation of devtools are enabling us developers to easily build complete solutions that take advantage of the best cloud infrastructure available, and offer an experience that allows you to rapidly iterate on your ideas for a low cost of $0. They take all the technical challenges of building and operating software products away from you so that you only have to spend your time building the features that your users want, giving you a reasonable chance to compete against the market by staying incredibly agile and responsive to the needs of users.
In this 3 hour workshop you will start with a simple task management application built with React and Next.js and turn it into a scalable and fully functioning SaaS product by integrating a scalable database (PlanetScale), multi-tenant authentication (Clerk), and subscription based payments (Stripe). You will also learn how the principles of agile software development and domain driven design can help you build products quickly and cost-efficiently, and compete with existing solutions.