Giter Club home page Giter Club logo

figma-plugin-react-vite's Introduction

Logo

Figma Plugin Boilerplate: React + Vite

Create scalable Figma plugins with ease, using the power of React + Vite!


Key Features

  1. 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). This boilerplate keeps the sides separated by allowing them to share code (under ./src/common/).

  2. Intercommunitive: Logical sides should be able to communicate with each other without creating huge and unscalable nested if statements. This boilerplate solves it by declaring isolated messages and handlers (under ./src/common/network/messages/)! (Using the Monorepo Networker library)

  3. Easy to Build: Configure the figma.manifest.ts config with your plugin credentials once, then just build with your everyday npm run build command! The /dist folder will be ready to publish already!

  4. Bundled into One File: Figma plugins only accept a single file for main (js) and ui (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.

  5. 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)

  6. Sassy: A classic, this boilerplate supports Sass/Scss/Less and modules! Check out /src/ui/styles/ for 7-1 Sass Template and /src/ui/components/Button.module.scss for module examples.

How to start coding?

  1. First thing after you clone should be to install the dependencies by executing:
npm install
  1. 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)
  2. Follow the steps on opened window. I recommend using Default or Run 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
  3. Note down the id field from the manifest.json it generated.
  4. Go to figma.manifest.ts, and replace the id with the id you noted down. Then configure the manifest there as you like. (See Official Figma Plugin Manifest doc)

Developing

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.

Developing without Figma Context

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

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

Publishing

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.

File Structure

  • src
    • src/common/ : Sources that are intended to be used both by plugin and ui logical sides.
      • src/common/network/ : Networking logic & message declarations used by Plugin - UI logical sides' intercommunication. Whenever a new message type is needed, declare and register here.
    • 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 project
    • scripts/windows/ : Potential custom Windows OS scripts
    • scripts/macos/ : Potential custom Mac OS scripts
  • figma.manifest.ts - A module that exports Figma Plugin Manifest for the build scripts

Caveats

1. Make sure to import SVGS as either component, url or raw!

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 }} />

Preview

figma-plugin-react-vite's People

Contributors

bigtimebuddy avatar igoodie avatar skymins04 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

figma-plugin-react-vite's Issues

Architectural Curiosity of the Project

Base Idea

  • plugin.ts depend on @plugin and @common.
  • index.html depend on @ui and @common.
  • @common don't depend on @plugin and @ui.
  • interact with figma code depend on plugin.ts not index.html,

Current Situation

  • ./src/common/network/messages/CreateRectMessage.ts
    • It create rect to interact with figma.
    • It should only related on plugin.ts. However plugin.ts, index.html are depend on it.(@common include CreateRectMessage.ts)
    • I think it is something wrong.
  • If you created a CreateRectAction.ts(function that create rect to interact figma) under the @plugin.
    • Message object must be created in @common to send in UI
    • Message object, handle function include logic.
    • handle function depend on CreateRectAction.ts, @plugin
    • Then, @common depend on @plugin.
  • If CreateRectAction.ts is under @common
    • It is wrong architectural because CreateRectAction.ts is interact with figma.

Curiosity

How can I add interact figma code without depend on @ui? Could you please let me know if I'm not familiar with how to use the monorepo-networker library?

How to send selection nodes name to app.tsx

Hello, I am now in a situation of how to return the node name string selected by figma to the App.tsx page. So far, it seems that I can only handle this string information in the class under messages.
What if I want to return these strings to App.tsx and use setState to refresh the display of the page.
How should it be done according to the current structure?

Where is the better place to place the figma.on type method?

Hello, I've been using your template to build my own figma plug-in, and the structure looks clear.
But I have a problem with methods like "figma.on". I don't think it's clear where they should be placed in the template.

async function bootstrap() {
  initializeNetwork(NetworkSide.PLUGIN);

  if (figma.editorType === "figma") {
    figma.showUI(__html__, {
      width: 400,
      height: 200,
      title: "Animator Figma",
    });
    // figma.on('selectionchange', getSelection)
  } else if (figma.editorType === "figjam") {
    figma.showUI(__html__, {
      width: 800,
      height: 650,
      title: "Animator FigJam",
    });
  }

  console.log("Bootstrapped @", Networker.Side.current.getName());

  NetworkMessages.HELLO_UI.send({ text: "Hey there, UI!" });
  
  (() => {
    figma.on('selectionchange', ()=>{
      const selection = figma.currentPage.selection
      console.log('selection', selection)
    })
  })()
}

bootstrap();

I created an anonymous function at plugin.ts to execute methods like "figma.on". It works, but it looks a little weird. Is there a more elegant way to execute functions like figma.on?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.