Package Exports
- cross-web-components
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cross-web-components) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
cross-web-components
Package to establish communication between your web components
EventHandler
import { EventHandler } from 'cross-web-components';
const customEvent = EventHandler.channel('[CUSTOM_CHANNEL]');Example - Dispatch (React)
export const Example = () => {
const customEvent = EventHandler.channel('[MODAL]');
const [id, setId] = useState<string>('');
const onClick = () => customEvent.dispatch<ModalState>('[MODAL_UPDATE_STATE]', { state: true, id });
return (
<>
<input placeholder='id' value={id} onChange={({ target: { value } }) => setId(value)} />
<button onClick={onClick}>Show</button>
</>
);
};Example - Listener (React)
export const Modal = () => {
const customEvent = EventHandler.channel('[MODAL]');
const [state, setState] = useState<ModalState>({ state: false, id: '' });
useEffect(() => {
const listener = customEvent.listener<ModalState>('[MODAL_UPDATE_STATE]', payload => setState(payload));
return () => {
listener.unsubscribe();
};
}, []);
const onClick = () => setState({ id: '', state: false });
return (
<>
{state.state && (
<div>
<h1>{state.id}</h1>
<button onClick={onClick}>Hide</button>
</div>
)}
</>
);
};Get Started
- Add the following in the dependencies of your package.json
npm install rxjs cross-web-components'- Ready to use
import { EventHandler } from 'cross-web-components';