npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@figmazing/event

v0.0.1

Published

Type-safe event system for communication between main and UI contexts in Figma plugins

Readme

@figmazing/event

only ESM support.

This library provides a type-safe event system for communication between the main and UI contexts in Figma plugins.

Features

  • Complete TypeScript-based type inference
  • Automatic inference of event names and payload types
  • Convenient API for event registration and emission
  • Backwards compatibility with the create-figma-plugin event system

Installation

npm install @figmazing/event

Basic Usage

1. Using Without Type Definitions

The simplest way to use the library is without type definitions:

import { event } from '@figmazing/event';

// Listen for events
event('selection-change').on((payload) => {
  console.log('Selection changed:', payload);
});

// Emit events
event('selection-change').emit({ ids: ['1', '2', '3'] });

2. Creating a Typed Event System

For type safety, you can define an event map:

import { createEventSystem } from '@figmazing/event';

// Define event map
interface MyEvents {
  'selection-change': { ids: string[] };
  'document-update': { timestamp: number };
  'notification': { message: string; type: 'info' | 'warning' | 'error' };
}

// Create a typed event system
const myEvents = createEventSystem<MyEvents>();

// Listen for events in the main context
myEvents('selection-change').on((payload) => {
  // payload is automatically inferred as { ids: string[] }
  console.log(`Selected items: ${payload.ids.length}`);
});

// Emit events from the UI context
myEvents('selection-change').emit({ ids: ['1', '2', '3'] });

Advanced Usage

One-time Event Handlers

Use the once method to register an event handler that will run only once:

myEvents('notification').once((payload) => {
  console.log(`Notification (one-time): ${payload.message}`);
});

Unregistering Event Handlers

Call the function returned when registering an event handler to unregister it:

const unsubscribe = myEvents('document-update').on((payload) => {
  console.log(`Document updated: ${payload.timestamp}`);
});

// Later, unregister the handler
unsubscribe();

Using the create-figma-plugin Style

For backwards compatibility, the library also supports the original on, once, and emit functions:

import { on, once, emit } from '@figmazing/event';

// Listen for events
on('selection-change', (payload) => {
  console.log('Selection changed:', payload);
});

// One-time event listening
once('notification', (payload) => {
  console.log('Notification (one-time):', payload);
});

// Emit events
emit('selection-change', { ids: ['1', '2', '3'] });

Example: Using in a Figma Plugin

1. main.ts (Main Context)

import { createEventSystem } from '@figmazing/event';

interface PluginEvents {
  'ui:ready': void;
  'ui:close': void;
  'selection:change': { ids: string[] };
  'create:rectangle': { width: number; height: number; color: string };
}

const pluginEvents = createEventSystem<PluginEvents>();

// Send current selection information when UI is ready
pluginEvents('ui:ready').on(() => {
  const nodes = figma.currentPage.selection;
  const ids = nodes.map(node => node.id);
  pluginEvents('selection:change').emit({ ids });
});

// Handle rectangle creation request
pluginEvents('create:rectangle').on(({ width, height, color }) => {
  const rect = figma.createRectangle();
  rect.resize(width, height);
  
  const [r, g, b] = hexToRgb(color);
  rect.fills = [{ type: 'SOLID', color: { r, g, b } }];
  
  figma.currentPage.appendChild(rect);
});

// Handle UI close request
pluginEvents('ui:close').on(() => {
  figma.closePlugin();
});

// Show UI
figma.showUI(__html__, { width: 300, height: 400 });

2. ui.tsx (UI Context)

import * as React from 'react';
import { useEffect, useState } from 'react';
import { createRoot } from 'react-dom/client';
import { createEventSystem } from '@figmazing/event';

interface PluginEvents {
  'ui:ready': void;
  'ui:close': void;
  'selection:change': { ids: string[] };
  'create:rectangle': { width: number; height: number; color: string };
}

const pluginEvents = createEventSystem<PluginEvents>();

function App() {
  const [selectedIds, setSelectedIds] = useState<string[]>([]);
  const [rectWidth, setRectWidth] = useState(100);
  const [rectHeight, setRectHeight] = useState(100);
  const [rectColor, setRectColor] = useState('#FF0000');
  
  useEffect(() => {
    // Notify main context that UI is ready
    pluginEvents('ui:ready').emit();
    
    // Listen for selection change events
    return pluginEvents('selection:change').on(({ ids }) => {
      setSelectedIds(ids);
    });
  }, []);
  
  const handleCreateRect = () => {
    pluginEvents('create:rectangle').emit({
      width: rectWidth,
      height: rectHeight,
      color: rectColor
    });
  };
  
  const handleClose = () => {
    pluginEvents('ui:close').emit();
  };
  
  return (
    <div className="container">
      <h2>Create Rectangle</h2>
      <div className="form-group">
        <label>Width</label>
        <input 
          type="number" 
          value={rectWidth} 
          onChange={(e) => setRectWidth(Number(e.target.value))} 
        />
      </div>
      <div className="form-group">
        <label>Height</label>
        <input 
          type="number" 
          value={rectHeight} 
          onChange={(e) => setRectHeight(Number(e.target.value))} 
        />
      </div>
      <div className="form-group">
        <label>Color</label>
        <input 
          type="color" 
          value={rectColor} 
          onChange={(e) => setRectColor(e.target.value)} 
        />
      </div>
      <button onClick={handleCreateRect}>Create Rectangle</button>
      <button onClick={handleClose}>Close</button>
      
      <div className="selection-info">
        <h3>Selected Items</h3>
        {selectedIds.length > 0 ? (
          <ul>
            {selectedIds.map(id => <li key={id}>{id}</li>)}
          </ul>
        ) : (
          <p>No items selected.</p>
        )}
      </div>
    </div>
  );
}

createRoot(document.getElementById('app')).render(<App />);

Notes

  • A warning is displayed when an event is emitted without any registered handlers.
  • This library only supports communication between the main and UI contexts in Figma plugins.
  • It does not work in Node.js environments.

Differences from create-figma-plugin

  • Event names and payload types can be managed together.
  • Safer code writing through type inference.
  • More convenient API through method chaining.
  • Improved error handling.