Skip to content

Commit bf7ccce

Browse files
committed
Add custom event hook
1 parent 7bfe66e commit bf7ccce

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/use-custom-event.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* useCustomEvent Hook to allow firing custom events
3+
*
4+
* @param {string} eventName - The name of the custom event to fire.
5+
* @returns {function} - The function to call the custom event can accept a payload of data
6+
*/
7+
export const useCustomEvent = eventName => {
8+
let event
9+
return payload => {
10+
if (window.CustomEvent && typeof window.CustomEvent === 'function') {
11+
event = new CustomEvent(eventName, { bubbles: true, detail: payload })
12+
} else {
13+
event = document.createEvent('CustomEvent')
14+
event.initCustomEvent(eventName, true, true, payload)
15+
}
16+
document.dispatchEvent(event)
17+
}
18+
}

0 commit comments

Comments
 (0)