Basic template for creating simple, scalable UI plugins inside Figma
-
Logical Sides in Mind: Figma plugins that render a UI work on two different processes (split into code.js and index.html in Figma docs).
-
Easy to Build: Configure the
figma.manifest.ts
config with your plugin credentials once, then just build with your everydaynpm run build
command! The/dist
folder will be ready to publish already! -
Bundled into One File: Figma plugins only accept a single file for
main
(js) andui
(html), which makes deployment of multiple files linked to each other impossible. This boilerplate is configured to bundle/inline most of the things you need like rasterize/vector image asset imports, CSS URL statements, and of course, source code imports. -
SVG as Component: Yes, you can import SVGs as inlined sources with
*.svg?url
, but what about actually importing them as React components? Easy! You can import an SVG file as a React component with*.svg?component
(See/src/ui/app.tsx
for examples) (Using the vite-plugin-react-rich-svg plugin)
- First thing after you clone should be to install the dependencies by executing:
npm install
- Create a figma plugin. In Figma, right click while you're in a design file. Follow
Plugins > Development > New Plugin...
. You can also type"New Plugin...
to the global search (Windows: CTRL + P, Mac: ⌘ Command + P) - Follow the steps on opened window. I recommend using
Default
orRun once
layout, because you'll only need to save the manifest (for the plugin id it generates). Click "Save as", and save it to a temporary place. Then click "Open folder" to navigate to the folder it generated - Note down the
id
field from themanifest.json
it generated. - Go to
figma.manifest.ts
, and replace theid
with the id you noted down. Then configure the manifest there as you like. (See Official Figma Plugin Manifest doc)
Development is very straight forward. Just run the dev command, and it will start compiling your files as you code.
npm run dev
Once dev is ran, dist/
folder will be created, which includes your manifest.json
. You can load it in Figma, by Right Click > Plugins > Development > Import plugin from manifest...
Tip: You can turn on the Hot reload plugin
option in Figma, to automatically reload when files in dist/
changes.
If you like developing your UI first, then integrating with Figma context; you can run your UI code in browser just like your every other Vite project by running:
npm run dev:ui-only
Remember: since Figma context is not available in "ui-only" mode, any attempt to Figma API/SDK calls will look like a crash on your inspector/console.
Building with the following command line will yield with a dist
folder, which is ready to be used by Figma:
npm run build
dist/manifest.json
then can be used to load the plugin. In Figma, right click while you're in a design file. Follow Plugins > Development > Import plugin from manifest...
. You can also type "Import plugin from manifest...
to the global search (Windows: CTRL + P, Mac: ⌘ Command + P). Then select dist/manifest.json
After building, built dist
folder is going to contain every artifact you need in order to publish your plugin. Just build, and follow Figma's Official Post on Publishing Plugins.
src
src/plugin/
: Sources of the plugin logical side. Place everything that interracts with figma here.src/ui/
: Sources of the ui logical side, a classical Vite + React source base.
scripts
scripts/vite/
: Potential custom vite plugins written for your projectscripts/windows/
: Potential custom Windows OS scriptsscripts/macos/
: Potential custom Mac OS scripts
figma.manifest.ts
- A module that exports Figma Plugin Manifest for the build scripts
Importing image assets other than .svg
is easy. However, when you are importing .svg
, by default it will load as a base64 data-uri, to import as a React component, you must add the query string ?react
.
import MyImage from "@ui/assets/my_image.svg?component"; // <MyImage />
import myImage from "@ui/assets/my_image.svg?url"; // "data:svg+xml,..."
import myImageRaw from "@ui/assets/my_image.svg?raw"; // "<svg>...</svg>"
...
<MyImage className="something" />
<img src={myImage} />
<div dangerouslySetInnerHTML={{ __html: myImageRaw }} />