What is @storybook/channels?
The @storybook/channels package is used within the Storybook ecosystem to facilitate communication between different parts of the Storybook environment, such as between the Storybook UI and the preview iframe where stories are rendered. It provides an event-driven channel that can be used for bidirectional communication.
What are @storybook/channels's main functionalities?
Creating a channel
This code sample demonstrates how to create a new channel using the @storybook/channels package. The 'transport' object would be an implementation of the transport layer that the channel will use to send and receive messages.
import Channel from '@storybook/channels';
const channel = new Channel({ transport });
Sending a message
This code sample shows how to send a message with an event name and associated data through the channel. Other parts of the application can listen for 'eventName' and react accordingly.
channel.emit('eventName', eventData);
Listening for a message
This code sample illustrates how to listen for messages on the channel. The 'eventHandler' function will be called with the event data whenever a message with 'eventName' is emitted.
channel.on('eventName', eventHandler);
Removing a listener
This code sample shows how to remove a previously added event listener from the channel. This is useful for cleanup and preventing memory leaks.
channel.removeListener('eventName', eventHandler);
Other packages similar to @storybook/channels
eventemitter3
EventEmitter3 is a high-performance event emitter. While it provides similar event-driven communication capabilities, it does not include a transport layer for communication between different environments like @storybook/channels does.
mitt
Mitt is a tiny functional event emitter / pubsub. It offers similar event handling functionality but lacks the built-in transport layer for cross-environment communication that @storybook/channels provides.
Storybook Channel
Storybook Channel is similar to an EventEmitter.
Channels are used with Storybook implementations to send/receive events between the Storybook Manager and the Storybook Renderer.
class Channel {
addListener(type, listener) {}
addPeerListener(type, listener) {}
emit(type, ...args) {}
eventNames() {}
listenerCount(type) {}
listeners(type) {}
on(type, listener) {}
once(type, listener) {}
prependListener(type, listener) {}
prependOnceListener(type, listener) {}
removeAllListeners(type) {}
removeListener(type, listener) {}
}
The channel takes a Transport object as a parameter which will be used to send/receive messages. The transport object should implement this interface.
class Transport {
send(event) {}
setHandler(handler) {}
}
For more information visit: storybook.js.org
8.0.0
Storybook 8.0 is here
It brings major improvements to Storybook's feature set for testing and documentation, with strengthened framework support across React, Vue, Angular, web-components, Svelte, and more.
- ๐ฉป Built-in visual testing
- โ๏ธ React Server Component support
- ๐๏ธ Improved controls for React and Vue projects
- โก๏ธ Improved Vite architecture, Vitest testing, and Vite 5 support
- ๐งช 2-4x faster Storybooks for testing
- โจ Refreshed desktop UI
- ๐ฒ Rebuilt mobile UX
- ๐
โโ๏ธ No more React requirement in non-React projects
Please checkout our Migration Guide to upgrade from earlier versions of Storybook. To see a comprehensive list of changes that went into 8.0, you can refer to the 8.0 prerelease changelogs.