Usage
Installation
npm i figma-messenger
yarn add figma-messenger
Quick usage example
interface IframeToMain {
setVersion(name: string, value: number): void
}
interface MainToIframe {
heyIframe(data: any): void
}
import { createMainThreadMessenger } from 'figma-messenger'
const mainMessenger = createMainThreadMessenger<MainToIframe, IframeToMain>()
mainMessenger.send('heyIframe', { any: 'data'})
mainMessenger.send('unknownMessage')
mainMessenger.send('heyIframe')
mainMessenger.on('setVersion', (name, value) => {
console.log('setVersion', name, value)
})
mainMessenger.off('setVersion')
import { createIframeMessenger, createMainThreadMessenger } from 'figma-messenger'
const iframeMessenger = createIframeMessenger<IframeToMain, MainToIframe>()
iframeMessenger.send('setVersion', 'initial', 1)
iframeMessenger.send('setVersion', 'initial')
iframeMessenger.on('heyIframe', sel => console.log(sel))
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:
interface IframeToMain {
setVersion(name: string, value: number): void
}
interface MainToIframe {
heyIframe(data: any): void
}
const iframeMessenger = createIframeMessenger<IframeToMain, MainToIframe>()
const namedIframeMessenger = createIframeMessenger<IframeToMain, MainToIframe>('SPECIAL')
const mainThreadMessenger = createMainThreadMessenger<MainToIframe, IframeToMain>()
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')
m1.on('msg', callback1)
m2.on('msg', callback2)
m3.on('msg', callback3)
.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.
messenger.off('aMessage', handleMessage)
messenger.on('someMessage')
.send(message: string, ...data?: any[]): void
Send a message to an opposite side.
messenger.on('someMessage', data)
messenger.on('otherMessage', arg1, arg2)
messenger.send('noArgsMessage')
License
MIT