dry-ux 1.46.0 → 1.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/helpers/utilities.js +20 -0
- package/package.json +1 -1
|
@@ -196,3 +196,23 @@ const useIsVisible = (ref) => {
|
|
|
196
196
|
return isIntersecting;
|
|
197
197
|
};
|
|
198
198
|
exports.useIsVisible = useIsVisible;
|
|
199
|
+
const usePubSub = () => {
|
|
200
|
+
const getEventName = (event) => `dry-ux-event-${event}`;
|
|
201
|
+
const usePub = () => (event, data) => {
|
|
202
|
+
document.dispatchEvent(new CustomEvent(getEventName(event), {
|
|
203
|
+
detail: data,
|
|
204
|
+
}));
|
|
205
|
+
};
|
|
206
|
+
const useSub = (event, callback) => {
|
|
207
|
+
const controller = new AbortController();
|
|
208
|
+
const unsubscribe = () => controller.abort();
|
|
209
|
+
React.useEffect(() => {
|
|
210
|
+
document.addEventListener(getEventName(event), (event) => callback(event.detail), {
|
|
211
|
+
signal: controller.signal,
|
|
212
|
+
});
|
|
213
|
+
return unsubscribe;
|
|
214
|
+
}, []);
|
|
215
|
+
return unsubscribe;
|
|
216
|
+
};
|
|
217
|
+
return { usePub, useSub };
|
|
218
|
+
};
|