0.2.1 โข Published 5 months ago
sicepat-eventbus v0.2.1
sicepat-eventbus
Type-safe event bus for communication between parent and iframe. Provide react hooks for easy integration.
Features
- ๐ Full TypeScript support with type-safe events
- โ๏ธ React hooks for easy integration
- ๐ Bi-directional communication between parent and iframe
- ๐งน Automatic cleanup of event listeners
- ๐ฏ Custom events support for flexibility
Installation
npm install sicepat-eventbus
# or
yarn add sicepat-eventbus
# or
pnpm add sicepat-eventbus
Basic Usage
Container (Parent Window)
import { useRef } from 'react'
import { useEventbusContainer, useEventEffect } from 'sicepat-eventbus/hooks'
function Container() {
const iframeRef = useRef<HTMLIFrameElement>(null)
const { eventBusRef, emit } = useEventbusContainer(iframeRef)
useEventEffect((payload) => {
console.log('Received from client:', payload)
}, [eventBusRef, 'user:login'])
return <iframe ref={iframeRef} src="client-url.html" />
}
Client (Iframe Window)
import { useEventbusClient, useEventEffect } from 'sicepat-eventbus/hooks'
function Client() {
const { eventBusRef, emit } = useEventbusClient()
const handleLogin = () => {
emit('user:login', {
subject: 'auth',
data: { userId: '123' },
metadata: { timestamp: Date.now() }
})
}
return <button onClick={handleLogin}>Login</button>
}
Documentation
API Reference
Hooks
useEventbusContainer
- Create event bus instance for parent windowuseEventbusClient
- Create event bus instance for iframeuseEventEffect
- Listen to type-safe eventsuseCustomEventEffect
- Listen to custom events
Event Types
The package includes predefined event types for common scenarios:
- Form events (
form:*
) - Input events (
input:*
) - Button events (
button:*
) - Table events (
table:*
) - Modal events (
modal:*
) - Navigation events (
nav:*
) - API events (
api:*
) - User events (
user:*
) - System events (
system:*
)
License
MIT