All Projects → okotoki → figma-messenger

okotoki / figma-messenger

Licence: MIT License
Type-safe communication for good 🧐.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to figma-messenger

Figma-Plugin-Webpack-React-Template
Base for building Figma plugins with React
Stars: ✭ 16 (-33.33%)
Mutual labels:  figma, figma-plugins
figma-dev
Build figma plugins with a modern toolchain
Stars: ✭ 15 (-37.5%)
Mutual labels:  figma, figma-plugins
figma-api-stub
🤖 A stub implementation of Figma Plugins API.
Stars: ✭ 33 (+37.5%)
Mutual labels:  figma, figma-plugins
Figma-Vue3-Template
Plugin template for Figma using Vue3
Stars: ✭ 19 (-20.83%)
Mutual labels:  figma, figma-plugins
Figmatocode
Generate responsive pages and apps on HTML, Tailwind, Flutter and SwiftUI.
Stars: ✭ 2,299 (+9479.17%)
Mutual labels:  figma, figma-plugins
React Figma
⚛️ A React renderer for Figma
Stars: ✭ 1,830 (+7525%)
Mutual labels:  figma, figma-plugins
figma-walker
Walkthrough your project without lifting your keyboard / Figma Plugin
Stars: ✭ 45 (+87.5%)
Mutual labels:  figma, figma-plugins
figma-plus-advanced-rename-plugin
A better and more powerful batch rename plugin for Figma with a dozen of options
Stars: ✭ 28 (+16.67%)
Mutual labels:  figma, figma-plugins
figma-plugins-stats
📈 A CLI to get live and historical stats for your Figma plugins
Stars: ✭ 53 (+120.83%)
Mutual labels:  figma, figma-plugins
FigmaPy
An unofficial Python3+ wrapper for Figma API
Stars: ✭ 19 (-20.83%)
Mutual labels:  figma
awesome-flutter-ui
10+ flutter(android, ios) UI design examples ⚡ - login, books, profile, food order, movie streaming, walkthrough, widgets
Stars: ✭ 848 (+3433.33%)
Mutual labels:  figma
ZestX-Frontend
Month-Long Fest Website ZestX with some Awsome UI and Intact Backend. Implemented admin page for user and event management
Stars: ✭ 28 (+16.67%)
Mutual labels:  figma
Friday
🎓 Friday - Your Personal Class Manager Assistant, It'll never let you miss another assignment deadline or upcoming test.
Stars: ✭ 67 (+179.17%)
Mutual labels:  figma
react-native-figma-squircle
Figma-flavored squircles for React Native
Stars: ✭ 207 (+762.5%)
Mutual labels:  figma
boring-avatars
Boring avatars is a tiny JavaScript React library that generates custom, SVG-based avatars from any username and color palette.
Stars: ✭ 3,582 (+14825%)
Mutual labels:  figma
figma-plugin-typescript-boilerplate
Figma plugin TypeScript boilerplate to start developing right away
Stars: ✭ 43 (+79.17%)
Mutual labels:  figma
plugin-typings
Typings for the Figma Plugin API
Stars: ✭ 91 (+279.17%)
Mutual labels:  figma
figma-tailwindcss
A plugin that tries to bridge the gap between designs and code. Figma tailwindcss lets you export aspects of a design made in Figma to a javascript theme file that you can import into your tailwindcss config
Stars: ✭ 63 (+162.5%)
Mutual labels:  figma
phinger-cursors
Most likely the most over engineered cursor theme.
Stars: ✭ 332 (+1283.33%)
Mutual labels:  figma
100daysofcode
⚠️ Programar no mínimo uma hora todos os dias, durante os próximos 100 dias!!
Stars: ✭ 28 (+16.67%)
Mutual labels:  figma

Type-safe Iframe ↔ Main Thread communication for good 🧐

Usage

Installation

npm i figma-messenger
# or using Yarn
yarn add figma-messenger

Quick usage example

// Shared code between main thread and iframe
// shared.ts
interface IframeToMain {
  setVersion(name: string, value: number): void
}

interface MainToIframe {
  heyIframe(data: any): void
}

// main.ts
import { createMainThreadMessenger } from 'figma-messenger'

const mainMessenger = createMainThreadMessenger<MainToIframe, IframeToMain>()

// All good
mainMessenger.send('heyIframe', { any: 'data'})

// Error. Argument of type "unknownMessage" is not assignable to parameter of type "heyIframe".
mainMessenger.send('unknownMessage')
// Error. Expected 2 arguments, but got 1.
mainMessenger.send('heyIframe')

mainMessenger.on('setVersion', (name, value) => {
  console.log('setVersion', name, value)
})

// Remove all listeners
mainMessenger.off('setVersion')

// iframe.ts
import { createIframeMessenger, createMainThreadMessenger } from 'figma-messenger'

const iframeMessenger = createIframeMessenger<IframeToMain, MainToIframe>()

// All good
iframeMessenger.send('setVersion', 'initial', 1)

// Error. Expected 3 arguments, but got 2.
iframeMessenger.send('setVersion', 'initial')

iframeMessenger.on('heyIframe', sel => console.log(sel))

// Remove all listeners
iframeMessenger.off('heyIframe')

See more comprehensive live figma plugin example at examples/figma-plugin.
Files shared/types.ts, app.tsx and main/index.ts

Api

createIframeMessenger<MessagesToSend, MessagesToListen>(name?: string) / createMainThreadMessenger<MessagesToSend, MessagesToListen>(name?: string)

Creates a messenger instance for Iframe and Main Thread sides respectively.
Optional name argument. If not set, messenger will be global. Otherwise only will receive events from the messenger with the same name.
Also, takes 2 type arguments:
MessagesToSend – messages to send signature
MessagesToListen – messages to receive signature

Example:

// Messages sent from Iframe side, received on Main Thread side
interface IframeToMain {
  setVersion(name: string, value: number): void
}

// Messages sent from Main Thread side, received on Iframe side
interface MainToIframe {
  heyIframe(data: any): void
}

/**
 * somewhere in iframe code: 
 */

// global messenger
const iframeMessenger = createIframeMessenger<IframeToMain, MainToIframe>()
// named messenger. Will communicate only to messenger with the same name on main thread.
const namedIframeMessenger = createIframeMessenger<IframeToMain, MainToIframe>('SPECIAL')


/**
 * somewhere in main thread code:
 */

// global messenger
const mainThreadMessenger = createMainThreadMessenger<MainToIframe, IframeToMain>()

// named messenger. Will communicate only to messenger 'SPECIAL' on iframe side.
const namedMainThreadMessenger = createMainThreadMessenger<IframeToMain, MainToIframe>('SPECIAL')

Single global listener under the hood makes it possible to create multiple instances, which won't conflict, but would handle messages with same name.

const m1 = createIframeMessenger()
const m2 = createIframeMessenger()
const m3 = createIframeMessenger('SPECIAL')


// When fired globally, "msg" message would be received by m1 and m2, but not by m3.
m1.on('msg', callback1) // receives global message
m2.on('msg', callback2) // receives global message

m3.on('msg', callback3) // only will receive from messenger named 'SPECIAL' on main thread side

.on(message: string, listener: (...arg: any[]) => void): void

Add listener for the message from opposite side.
Callbacks can take no or multiple arguments.

messenger.on('aMessage', handleMessage)
messenger.on('someMessage', (data) => doSomething(data))
messenger.on('otherMessage', (arg1, arg2) => hello(arg1, arg2))
messenger.on('noArgsMessage', () => world())

.off(message: string, , listener: (...arg: any[]) => void): void

Remove one or all listeners for the message.

// remove particular listener
messenger.off('aMessage', handleMessage)

// remove all listeners
messenger.on('someMessage')

.send(message: string, ...data?: any[]): void

Send a message to an opposite side.

// send message with one data item
messenger.on('someMessage', data)
// with multiple data items
messenger.on('otherMessage', arg1, arg2)
// or no data at all
messenger.send('noArgsMessage')

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].