figma-jsonrpc
TypeScript icon, indicating that this package has built-in type declarations

0.4.1 • Public • Published

figma-JSONRPC

Leverage JSON-RPC to communicate between your Figma plugin and your Figma UI.

Installation

npm install figma-jsonrpc

Usage

  • Define your API in a separate file (in api.ts for example):

    import { createPluginAPI, createUIAPI } from "figma-jsonrpc";
    
    // those methods will be executed in the Figma plugin,
    // regardless of where they are called from
    export const api = createPluginAPI({
      ping() {
        return "pong";
      },
      setToken(token: string) {
        return figma.clientStorage.setAsync("token", token);
      },
      getToken() {
        return figma.clientStorage.getAsync("token");
      }
    });
    
    // those methods will be executed in the Figma UI,
    // regardless of where they are called from
    export const uiApi = createUIAPI({
      selectionChanged(selection) {
        return "pong";
      }
    });
  • Use the UI API in the plugin:

    import { uiApi } from "./api";
    
    // This shows the HTML page in "ui.html".
    figma.showUI(__html__);
    
    figma.on("selectionchange", () => {
      uiApi.selectionChange(figma.currentPage.selection);
    });
  • Use the API in the UI:

    import { api } from "./api";
    
    const pong = await api.ping();
    const token = await api.getToken();
    await api.setToken("new token");

The typescript definition of the API is automatically inferred from the methods passed to rpc .

⚠️ You always need to import the API in both the plugin and the UI, even if you aren't using it. It is necessary so that both part can handle calls from each other.

One more thing...

Using React? There are a couple of treats for you.

Two React hooks you can use in your UI which will setup the necessary APIs for you.

  • useFigmaSelection: get and set the current Figma selection

    import useFigmaSelection from "figma-jsonrpc/hooks/useFigmaSelection";
    
    const Component = () => {
      const [selection, setSelection] = useFigmaSelection();
    };
  • useFigmaSetting: get and set any setting

    import useFigmaSetting from "figma-jsonrpc/hooks/useFigmaSetting";
    
    const Component = () => {
      const [token, error, loading, setToken] = useFigmaSetting("token");
    };

In both cases, you also need to import figma-jsonrpc/hooks/* in your plugin to create the APIs.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i figma-jsonrpc

Weekly Downloads

4,885

Version

0.4.1

License

MIT

Unpacked Size

13.3 kB

Total Files

11

Last publish

Collaborators

  • mathieudutour