-
Notifications
You must be signed in to change notification settings - Fork 0
/
emit-custom-event.ts
34 lines (32 loc) · 1.05 KB
/
emit-custom-event.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Please refer to the terms of the license agreement in the root of the project
*
* (c) 2024 Feedzai
*/
import { getElement } from "./get-element";
/**
* Attaches data to a custom event.
*
* @description The react way of handling events is to pass callbacks to child components.
* This can become cumbersome when a child is several levels deep and one way to avoid passing callbacks deep is using Context.
* However, context or redux are not meant for all use cases.
*
* @example
*
* // Attach data to an event
* emitCustomEvent('a-custom-event-name', data);
*
* // Listen to the event using the custom hook
* useCustomEventListener('a-custom-event-name', data => {
* doSomethingWithData( data );
* });
* @export
* @template GenericType
* @param {string} eventName
* @param {GenericType} [data]
*/
export function emitCustomEvent<GenericType>(eventName: string, data?: GenericType): void {
const element = getElement();
const event: CustomEvent<GenericType> = new CustomEvent(eventName, { detail: data });
element?.dispatchEvent(event);
}