@xyo-network/react-event 2.64.0-rc.7 → 2.64.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. package/dist/browser/hooks/index.cjs +78 -0
  2. package/dist/browser/hooks/index.cjs.map +1 -0
  3. package/dist/browser/hooks/index.d.cts +3 -0
  4. package/dist/browser/hooks/index.d.cts.map +1 -0
  5. package/dist/browser/hooks/index.js +54 -2
  6. package/dist/browser/hooks/index.js.map +1 -1
  7. package/dist/browser/hooks/useCustomEvent.cjs +60 -0
  8. package/dist/{node/hooks/useCustomEvent.mjs.map → browser/hooks/useCustomEvent.cjs.map} +1 -1
  9. package/dist/browser/hooks/useCustomEvent.d.cts +4 -0
  10. package/dist/browser/hooks/useCustomEvent.d.cts.map +1 -0
  11. package/dist/browser/hooks/useCustomEvent.js +2 -1
  12. package/dist/browser/hooks/useCustomEvent.js.map +1 -1
  13. package/dist/browser/hooks/useEvent.cjs +77 -0
  14. package/dist/browser/hooks/useEvent.cjs.map +1 -0
  15. package/dist/browser/hooks/useEvent.d.cts +11 -0
  16. package/dist/browser/hooks/useEvent.d.cts.map +1 -0
  17. package/dist/browser/hooks/useEvent.js +38 -2
  18. package/dist/browser/hooks/useEvent.js.map +1 -1
  19. package/dist/browser/index.cjs +78 -0
  20. package/dist/browser/index.cjs.map +1 -0
  21. package/dist/browser/index.d.cts +2 -0
  22. package/dist/browser/index.d.cts.map +1 -0
  23. package/dist/browser/index.js +54 -1
  24. package/dist/browser/index.js.map +1 -1
  25. package/dist/docs.json +982 -0
  26. package/dist/node/hooks/index.cjs +84 -0
  27. package/dist/node/hooks/index.cjs.map +1 -0
  28. package/dist/node/hooks/index.d.cts +3 -0
  29. package/dist/node/hooks/index.d.cts.map +1 -0
  30. package/dist/node/hooks/index.js +54 -23
  31. package/dist/node/hooks/index.js.map +1 -1
  32. package/dist/node/hooks/useCustomEvent.cjs +65 -0
  33. package/dist/node/hooks/useCustomEvent.cjs.map +1 -0
  34. package/dist/node/hooks/useCustomEvent.d.cts +4 -0
  35. package/dist/node/hooks/useCustomEvent.d.cts.map +1 -0
  36. package/dist/node/hooks/useCustomEvent.js +14 -36
  37. package/dist/node/hooks/useCustomEvent.js.map +1 -1
  38. package/dist/node/hooks/useEvent.cjs +82 -0
  39. package/dist/node/hooks/useEvent.cjs.map +1 -0
  40. package/dist/node/hooks/useEvent.d.cts +11 -0
  41. package/dist/node/hooks/useEvent.d.cts.map +1 -0
  42. package/dist/node/hooks/useEvent.js +41 -28
  43. package/dist/node/hooks/useEvent.js.map +1 -1
  44. package/dist/node/index.cjs +84 -0
  45. package/dist/node/index.cjs.map +1 -0
  46. package/dist/node/index.d.cts +2 -0
  47. package/dist/node/index.d.cts.map +1 -0
  48. package/dist/node/index.js +54 -21
  49. package/dist/node/index.js.map +1 -1
  50. package/package.json +12 -12
  51. package/dist/node/hooks/index.mjs +0 -3
  52. package/dist/node/hooks/index.mjs.map +0 -1
  53. package/dist/node/hooks/useCustomEvent.mjs +0 -38
  54. package/dist/node/hooks/useEvent.mjs +0 -18
  55. package/dist/node/hooks/useEvent.mjs.map +0 -1
  56. package/dist/node/index.mjs +0 -2
  57. package/dist/node/index.mjs.map +0 -1
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/hooks/index.ts
21
+ var hooks_exports = {};
22
+ __export(hooks_exports, {
23
+ useCustomEvent: () => useCustomEvent,
24
+ useEvent: () => useEvent
25
+ });
26
+ module.exports = __toCommonJS(hooks_exports);
27
+
28
+ // src/hooks/useCustomEvent.ts
29
+ var import_assert = require("@xylabs/assert");
30
+ var import_react = require("react");
31
+ var useCustomEvent = (type, listener, customRef) => {
32
+ const ref = customRef ?? (0, import_react.createRef)();
33
+ (0, import_react.useEffect)(() => {
34
+ const element = ref == null ? void 0 : ref.current;
35
+ const currentListener = listener;
36
+ const handler = currentListener ? (event) => {
37
+ const detail = (0, import_assert.assertEx)(event.detail, "Event missing detail");
38
+ const stop = currentListener(detail);
39
+ if (stop) {
40
+ event.stopPropagation();
41
+ }
42
+ } : void 0;
43
+ if (handler && element) {
44
+ element == null ? void 0 : element.addEventListener(type, handler);
45
+ }
46
+ return () => {
47
+ if (handler && element) {
48
+ element == null ? void 0 : element.removeEventListener(type, handler);
49
+ }
50
+ };
51
+ });
52
+ const dispatch = (detail) => {
53
+ var _a;
54
+ const event = new CustomEvent(type, {
55
+ bubbles: true,
56
+ cancelable: true,
57
+ composed: true,
58
+ detail
59
+ });
60
+ (_a = ref.current) == null ? void 0 : _a.dispatchEvent(event);
61
+ };
62
+ return [ref, dispatch];
63
+ };
64
+
65
+ // src/hooks/useEvent.ts
66
+ var useEvent = (listener, sharableRef) => {
67
+ const [ref, customDispatch] = useCustomEvent(
68
+ "xyo",
69
+ listener ? (detail) => {
70
+ return listener(detail.noun, detail.verb, detail.data);
71
+ } : void 0,
72
+ sharableRef
73
+ );
74
+ const dispatch = (noun, verb, data) => {
75
+ return customDispatch({ data, noun, verb });
76
+ };
77
+ return [ref, dispatch];
78
+ };
79
+ // Annotate the CommonJS export names for ESM import in node:
80
+ 0 && (module.exports = {
81
+ useCustomEvent,
82
+ useEvent
83
+ });
84
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/hooks/index.ts","../../../src/hooks/useCustomEvent.ts","../../../src/hooks/useEvent.ts"],"sourcesContent":["export * from './useCustomEvent'\nexport * from './useEvent'\n","import { assertEx } from '@xylabs/assert'\nimport { createRef, RefObject, useEffect } from 'react'\n\nexport type CustomEventDispatch<T = unknown> = (detail: T) => boolean | void\n\nexport const useCustomEvent = <TElement extends HTMLElement, TDetail = unknown>(\n type: string,\n listener?: CustomEventDispatch<TDetail>,\n customRef?: RefObject<TElement>,\n): [RefObject<TElement>, CustomEventDispatch<TDetail>] => {\n const ref = customRef ?? createRef<TElement>()\n useEffect(() => {\n const element = ref?.current\n const currentListener = listener\n const handler = currentListener\n ? (event: CustomEventInit<TDetail> & Event) => {\n const detail = assertEx(event.detail, 'Event missing detail')\n const stop = currentListener(detail)\n if (stop) {\n event.stopPropagation()\n }\n }\n : undefined\n\n if (handler && element) {\n element?.addEventListener(type, handler)\n }\n return () => {\n if (handler && element) {\n element?.removeEventListener(type, handler)\n }\n }\n })\n const dispatch: CustomEventDispatch<TDetail> = (detail?: TDetail) => {\n const event = new CustomEvent<TDetail>(type, {\n bubbles: true,\n cancelable: true,\n composed: true,\n detail,\n })\n ref.current?.dispatchEvent(event)\n }\n return [ref, dispatch]\n}\n","import { RefObject } from 'react'\n\nimport { useCustomEvent } from './useCustomEvent'\n\nexport type EventNoun = 'payload' | 'boundwitness' | 'address' | 'hash' | 'signature' | 'schema'\nexport type EventVerb = 'click' | 'favorite'\n\nexport interface Event<TNoun = EventNoun, TVerb = EventVerb, TData = string> {\n data?: TData\n noun: TNoun\n verb: TVerb\n}\n\nexport type EventDispatch<TNoun = EventNoun, TVerb = EventVerb, TData = string> = (noun: TNoun, verb: TVerb, data?: TData) => boolean | void\n\nexport const useEvent = <T extends HTMLElement, TNoun = EventNoun, TVerb = EventVerb, TData = string>(\n listener?: EventDispatch<TNoun, TVerb, TData>,\n sharableRef?: RefObject<T>,\n): [RefObject<T>, EventDispatch<TNoun, TVerb, TData>] => {\n const [ref, customDispatch] = useCustomEvent<T, Event<TNoun, TVerb, TData>>(\n 'xyo',\n listener\n ? (detail: Event<TNoun, TVerb, TData>) => {\n return listener(detail.noun, detail.verb, detail.data)\n }\n : undefined,\n sharableRef,\n )\n\n const dispatch: EventDispatch<TNoun, TVerb, TData> = (noun: TNoun, verb: TVerb, data?: TData) => {\n return customDispatch({ data, noun, verb })\n }\n return [ref, dispatch]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAyB;AACzB,mBAAgD;AAIzC,IAAM,iBAAiB,CAC5B,MACA,UACA,cACwD;AACxD,QAAM,MAAM,iBAAa,wBAAoB;AAC7C,8BAAU,MAAM;AACd,UAAM,UAAU,2BAAK;AACrB,UAAM,kBAAkB;AACxB,UAAM,UAAU,kBACZ,CAAC,UAA4C;AAC3C,YAAM,aAAS,wBAAS,MAAM,QAAQ,sBAAsB;AAC5D,YAAM,OAAO,gBAAgB,MAAM;AACnC,UAAI,MAAM;AACR,cAAM,gBAAgB;AAAA,MACxB;AAAA,IACF,IACA;AAEJ,QAAI,WAAW,SAAS;AACtB,yCAAS,iBAAiB,MAAM;AAAA,IAClC;AACA,WAAO,MAAM;AACX,UAAI,WAAW,SAAS;AACtB,2CAAS,oBAAoB,MAAM;AAAA,MACrC;AAAA,IACF;AAAA,EACF,CAAC;AACD,QAAM,WAAyC,CAAC,WAAqB;AAjCvE;AAkCI,UAAM,QAAQ,IAAI,YAAqB,MAAM;AAAA,MAC3C,SAAS;AAAA,MACT,YAAY;AAAA,MACZ,UAAU;AAAA,MACV;AAAA,IACF,CAAC;AACD,cAAI,YAAJ,mBAAa,cAAc;AAAA,EAC7B;AACA,SAAO,CAAC,KAAK,QAAQ;AACvB;;;AC5BO,IAAM,WAAW,CACtB,UACA,gBACuD;AACvD,QAAM,CAAC,KAAK,cAAc,IAAI;AAAA,IAC5B;AAAA,IACA,WACI,CAAC,WAAuC;AACtC,aAAO,SAAS,OAAO,MAAM,OAAO,MAAM,OAAO,IAAI;AAAA,IACvD,IACA;AAAA,IACJ;AAAA,EACF;AAEA,QAAM,WAA+C,CAAC,MAAa,MAAa,SAAiB;AAC/F,WAAO,eAAe,EAAE,MAAM,MAAM,KAAK,CAAC;AAAA,EAC5C;AACA,SAAO,CAAC,KAAK,QAAQ;AACvB;","names":[]}
@@ -0,0 +1,3 @@
1
+ export * from './useCustomEvent';
2
+ export * from './useEvent';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAA;AAChC,cAAc,YAAY,CAAA"}
@@ -1,25 +1,56 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
1
+ // src/hooks/useCustomEvent.ts
2
+ import { assertEx } from "@xylabs/assert";
3
+ import { createRef, useEffect } from "react";
4
+ var useCustomEvent = (type, listener, customRef) => {
5
+ const ref = customRef ?? createRef();
6
+ useEffect(() => {
7
+ const element = ref == null ? void 0 : ref.current;
8
+ const currentListener = listener;
9
+ const handler = currentListener ? (event) => {
10
+ const detail = assertEx(event.detail, "Event missing detail");
11
+ const stop = currentListener(detail);
12
+ if (stop) {
13
+ event.stopPropagation();
14
+ }
15
+ } : void 0;
16
+ if (handler && element) {
17
+ element == null ? void 0 : element.addEventListener(type, handler);
18
+ }
19
+ return () => {
20
+ if (handler && element) {
21
+ element == null ? void 0 : element.removeEventListener(type, handler);
22
+ }
23
+ };
24
+ });
25
+ const dispatch = (detail) => {
26
+ var _a;
27
+ const event = new CustomEvent(type, {
28
+ bubbles: true,
29
+ cancelable: true,
30
+ composed: true,
31
+ detail
32
+ });
33
+ (_a = ref.current) == null ? void 0 : _a.dispatchEvent(event);
34
+ };
35
+ return [ref, dispatch];
36
+ };
37
+
38
+ // src/hooks/useEvent.ts
39
+ var useEvent = (listener, sharableRef) => {
40
+ const [ref, customDispatch] = useCustomEvent(
41
+ "xyo",
42
+ listener ? (detail) => {
43
+ return listener(detail.noun, detail.verb, detail.data);
44
+ } : void 0,
45
+ sharableRef
46
+ );
47
+ const dispatch = (noun, verb, data) => {
48
+ return customDispatch({ data, noun, verb });
49
+ };
50
+ return [ref, dispatch];
51
+ };
52
+ export {
53
+ useCustomEvent,
54
+ useEvent
13
55
  };
14
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
- var hooks_exports = {};
17
- module.exports = __toCommonJS(hooks_exports);
18
- __reExport(hooks_exports, require("./useCustomEvent"), module.exports);
19
- __reExport(hooks_exports, require("./useEvent"), module.exports);
20
- // Annotate the CommonJS export names for ESM import in node:
21
- 0 && (module.exports = {
22
- ...require("./useCustomEvent"),
23
- ...require("./useEvent")
24
- });
25
56
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/hooks/index.ts"],"sourcesContent":["export * from './useCustomEvent'\nexport * from './useEvent'\n"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,0BAAc,6BAAd;AACA,0BAAc,uBADd;","names":[]}
1
+ {"version":3,"sources":["../../../src/hooks/useCustomEvent.ts","../../../src/hooks/useEvent.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { createRef, RefObject, useEffect } from 'react'\n\nexport type CustomEventDispatch<T = unknown> = (detail: T) => boolean | void\n\nexport const useCustomEvent = <TElement extends HTMLElement, TDetail = unknown>(\n type: string,\n listener?: CustomEventDispatch<TDetail>,\n customRef?: RefObject<TElement>,\n): [RefObject<TElement>, CustomEventDispatch<TDetail>] => {\n const ref = customRef ?? createRef<TElement>()\n useEffect(() => {\n const element = ref?.current\n const currentListener = listener\n const handler = currentListener\n ? (event: CustomEventInit<TDetail> & Event) => {\n const detail = assertEx(event.detail, 'Event missing detail')\n const stop = currentListener(detail)\n if (stop) {\n event.stopPropagation()\n }\n }\n : undefined\n\n if (handler && element) {\n element?.addEventListener(type, handler)\n }\n return () => {\n if (handler && element) {\n element?.removeEventListener(type, handler)\n }\n }\n })\n const dispatch: CustomEventDispatch<TDetail> = (detail?: TDetail) => {\n const event = new CustomEvent<TDetail>(type, {\n bubbles: true,\n cancelable: true,\n composed: true,\n detail,\n })\n ref.current?.dispatchEvent(event)\n }\n return [ref, dispatch]\n}\n","import { RefObject } from 'react'\n\nimport { useCustomEvent } from './useCustomEvent'\n\nexport type EventNoun = 'payload' | 'boundwitness' | 'address' | 'hash' | 'signature' | 'schema'\nexport type EventVerb = 'click' | 'favorite'\n\nexport interface Event<TNoun = EventNoun, TVerb = EventVerb, TData = string> {\n data?: TData\n noun: TNoun\n verb: TVerb\n}\n\nexport type EventDispatch<TNoun = EventNoun, TVerb = EventVerb, TData = string> = (noun: TNoun, verb: TVerb, data?: TData) => boolean | void\n\nexport const useEvent = <T extends HTMLElement, TNoun = EventNoun, TVerb = EventVerb, TData = string>(\n listener?: EventDispatch<TNoun, TVerb, TData>,\n sharableRef?: RefObject<T>,\n): [RefObject<T>, EventDispatch<TNoun, TVerb, TData>] => {\n const [ref, customDispatch] = useCustomEvent<T, Event<TNoun, TVerb, TData>>(\n 'xyo',\n listener\n ? (detail: Event<TNoun, TVerb, TData>) => {\n return listener(detail.noun, detail.verb, detail.data)\n }\n : undefined,\n sharableRef,\n )\n\n const dispatch: EventDispatch<TNoun, TVerb, TData> = (noun: TNoun, verb: TVerb, data?: TData) => {\n return customDispatch({ data, noun, verb })\n }\n return [ref, dispatch]\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,WAAsB,iBAAiB;AAIzC,IAAM,iBAAiB,CAC5B,MACA,UACA,cACwD;AACxD,QAAM,MAAM,aAAa,UAAoB;AAC7C,YAAU,MAAM;AACd,UAAM,UAAU,2BAAK;AACrB,UAAM,kBAAkB;AACxB,UAAM,UAAU,kBACZ,CAAC,UAA4C;AAC3C,YAAM,SAAS,SAAS,MAAM,QAAQ,sBAAsB;AAC5D,YAAM,OAAO,gBAAgB,MAAM;AACnC,UAAI,MAAM;AACR,cAAM,gBAAgB;AAAA,MACxB;AAAA,IACF,IACA;AAEJ,QAAI,WAAW,SAAS;AACtB,yCAAS,iBAAiB,MAAM;AAAA,IAClC;AACA,WAAO,MAAM;AACX,UAAI,WAAW,SAAS;AACtB,2CAAS,oBAAoB,MAAM;AAAA,MACrC;AAAA,IACF;AAAA,EACF,CAAC;AACD,QAAM,WAAyC,CAAC,WAAqB;AAjCvE;AAkCI,UAAM,QAAQ,IAAI,YAAqB,MAAM;AAAA,MAC3C,SAAS;AAAA,MACT,YAAY;AAAA,MACZ,UAAU;AAAA,MACV;AAAA,IACF,CAAC;AACD,cAAI,YAAJ,mBAAa,cAAc;AAAA,EAC7B;AACA,SAAO,CAAC,KAAK,QAAQ;AACvB;;;AC5BO,IAAM,WAAW,CACtB,UACA,gBACuD;AACvD,QAAM,CAAC,KAAK,cAAc,IAAI;AAAA,IAC5B;AAAA,IACA,WACI,CAAC,WAAuC;AACtC,aAAO,SAAS,OAAO,MAAM,OAAO,MAAM,OAAO,IAAI;AAAA,IACvD,IACA;AAAA,IACJ;AAAA,EACF;AAEA,QAAM,WAA+C,CAAC,MAAa,MAAa,SAAiB;AAC/F,WAAO,eAAe,EAAE,MAAM,MAAM,KAAK,CAAC;AAAA,EAC5C;AACA,SAAO,CAAC,KAAK,QAAQ;AACvB;","names":[]}
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/hooks/useCustomEvent.ts
21
+ var useCustomEvent_exports = {};
22
+ __export(useCustomEvent_exports, {
23
+ useCustomEvent: () => useCustomEvent
24
+ });
25
+ module.exports = __toCommonJS(useCustomEvent_exports);
26
+ var import_assert = require("@xylabs/assert");
27
+ var import_react = require("react");
28
+ var useCustomEvent = (type, listener, customRef) => {
29
+ const ref = customRef ?? (0, import_react.createRef)();
30
+ (0, import_react.useEffect)(() => {
31
+ const element = ref == null ? void 0 : ref.current;
32
+ const currentListener = listener;
33
+ const handler = currentListener ? (event) => {
34
+ const detail = (0, import_assert.assertEx)(event.detail, "Event missing detail");
35
+ const stop = currentListener(detail);
36
+ if (stop) {
37
+ event.stopPropagation();
38
+ }
39
+ } : void 0;
40
+ if (handler && element) {
41
+ element == null ? void 0 : element.addEventListener(type, handler);
42
+ }
43
+ return () => {
44
+ if (handler && element) {
45
+ element == null ? void 0 : element.removeEventListener(type, handler);
46
+ }
47
+ };
48
+ });
49
+ const dispatch = (detail) => {
50
+ var _a;
51
+ const event = new CustomEvent(type, {
52
+ bubbles: true,
53
+ cancelable: true,
54
+ composed: true,
55
+ detail
56
+ });
57
+ (_a = ref.current) == null ? void 0 : _a.dispatchEvent(event);
58
+ };
59
+ return [ref, dispatch];
60
+ };
61
+ // Annotate the CommonJS export names for ESM import in node:
62
+ 0 && (module.exports = {
63
+ useCustomEvent
64
+ });
65
+ //# sourceMappingURL=useCustomEvent.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/hooks/useCustomEvent.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { createRef, RefObject, useEffect } from 'react'\n\nexport type CustomEventDispatch<T = unknown> = (detail: T) => boolean | void\n\nexport const useCustomEvent = <TElement extends HTMLElement, TDetail = unknown>(\n type: string,\n listener?: CustomEventDispatch<TDetail>,\n customRef?: RefObject<TElement>,\n): [RefObject<TElement>, CustomEventDispatch<TDetail>] => {\n const ref = customRef ?? createRef<TElement>()\n useEffect(() => {\n const element = ref?.current\n const currentListener = listener\n const handler = currentListener\n ? (event: CustomEventInit<TDetail> & Event) => {\n const detail = assertEx(event.detail, 'Event missing detail')\n const stop = currentListener(detail)\n if (stop) {\n event.stopPropagation()\n }\n }\n : undefined\n\n if (handler && element) {\n element?.addEventListener(type, handler)\n }\n return () => {\n if (handler && element) {\n element?.removeEventListener(type, handler)\n }\n }\n })\n const dispatch: CustomEventDispatch<TDetail> = (detail?: TDetail) => {\n const event = new CustomEvent<TDetail>(type, {\n bubbles: true,\n cancelable: true,\n composed: true,\n detail,\n })\n ref.current?.dispatchEvent(event)\n }\n return [ref, dispatch]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAyB;AACzB,mBAAgD;AAIzC,IAAM,iBAAiB,CAC5B,MACA,UACA,cACwD;AACxD,QAAM,MAAM,iBAAa,wBAAoB;AAC7C,8BAAU,MAAM;AACd,UAAM,UAAU,2BAAK;AACrB,UAAM,kBAAkB;AACxB,UAAM,UAAU,kBACZ,CAAC,UAA4C;AAC3C,YAAM,aAAS,wBAAS,MAAM,QAAQ,sBAAsB;AAC5D,YAAM,OAAO,gBAAgB,MAAM;AACnC,UAAI,MAAM;AACR,cAAM,gBAAgB;AAAA,MACxB;AAAA,IACF,IACA;AAEJ,QAAI,WAAW,SAAS;AACtB,yCAAS,iBAAiB,MAAM;AAAA,IAClC;AACA,WAAO,MAAM;AACX,UAAI,WAAW,SAAS;AACtB,2CAAS,oBAAoB,MAAM;AAAA,MACrC;AAAA,IACF;AAAA,EACF,CAAC;AACD,QAAM,WAAyC,CAAC,WAAqB;AAjCvE;AAkCI,UAAM,QAAQ,IAAI,YAAqB,MAAM;AAAA,MAC3C,SAAS;AAAA,MACT,YAAY;AAAA,MACZ,UAAU;AAAA,MACV;AAAA,IACF,CAAC;AACD,cAAI,YAAJ,mBAAa,cAAc;AAAA,EAC7B;AACA,SAAO,CAAC,KAAK,QAAQ;AACvB;","names":[]}
@@ -0,0 +1,4 @@
1
+ import { RefObject } from 'react';
2
+ export type CustomEventDispatch<T = unknown> = (detail: T) => boolean | void;
3
+ export declare const useCustomEvent: <TElement extends HTMLElement, TDetail = unknown>(type: string, listener?: CustomEventDispatch<TDetail> | undefined, customRef?: RefObject<TElement> | undefined) => [RefObject<TElement>, CustomEventDispatch<TDetail>];
4
+ //# sourceMappingURL=useCustomEvent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useCustomEvent.d.ts","sourceRoot":"","sources":["../../../src/hooks/useCustomEvent.ts"],"names":[],"mappings":"AACA,OAAO,EAAa,SAAS,EAAa,MAAM,OAAO,CAAA;AAEvD,MAAM,MAAM,mBAAmB,CAAC,CAAC,GAAG,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,OAAO,GAAG,IAAI,CAAA;AAE5E,eAAO,MAAM,cAAc,0DACnB,MAAM,0JAqCb,CAAA"}
@@ -1,62 +1,40 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var useCustomEvent_exports = {};
20
- __export(useCustomEvent_exports, {
21
- useCustomEvent: () => useCustomEvent
22
- });
23
- module.exports = __toCommonJS(useCustomEvent_exports);
24
- var import_assert = require("@xylabs/assert");
25
- var import_react = require("react");
26
- const useCustomEvent = (type, listener, customRef) => {
27
- const ref = customRef ?? (0, import_react.createRef)();
28
- (0, import_react.useEffect)(() => {
29
- const element = ref?.current;
1
+ // src/hooks/useCustomEvent.ts
2
+ import { assertEx } from "@xylabs/assert";
3
+ import { createRef, useEffect } from "react";
4
+ var useCustomEvent = (type, listener, customRef) => {
5
+ const ref = customRef ?? createRef();
6
+ useEffect(() => {
7
+ const element = ref == null ? void 0 : ref.current;
30
8
  const currentListener = listener;
31
9
  const handler = currentListener ? (event) => {
32
- const detail = (0, import_assert.assertEx)(event.detail, "Event missing detail");
10
+ const detail = assertEx(event.detail, "Event missing detail");
33
11
  const stop = currentListener(detail);
34
12
  if (stop) {
35
13
  event.stopPropagation();
36
14
  }
37
15
  } : void 0;
38
16
  if (handler && element) {
39
- element?.addEventListener(type, handler);
17
+ element == null ? void 0 : element.addEventListener(type, handler);
40
18
  }
41
19
  return () => {
42
20
  if (handler && element) {
43
- element?.removeEventListener(type, handler);
21
+ element == null ? void 0 : element.removeEventListener(type, handler);
44
22
  }
45
23
  };
46
24
  });
47
25
  const dispatch = (detail) => {
26
+ var _a;
48
27
  const event = new CustomEvent(type, {
49
28
  bubbles: true,
50
29
  cancelable: true,
51
30
  composed: true,
52
31
  detail
53
32
  });
54
- ref.current?.dispatchEvent(event);
33
+ (_a = ref.current) == null ? void 0 : _a.dispatchEvent(event);
55
34
  };
56
35
  return [ref, dispatch];
57
36
  };
58
- // Annotate the CommonJS export names for ESM import in node:
59
- 0 && (module.exports = {
37
+ export {
60
38
  useCustomEvent
61
- });
39
+ };
62
40
  //# sourceMappingURL=useCustomEvent.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/hooks/useCustomEvent.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { createRef, RefObject, useEffect } from 'react'\n\nexport type CustomEventDispatch<T = unknown> = (detail: T) => boolean | void\n\nexport const useCustomEvent = <TElement extends HTMLElement, TDetail = unknown>(\n type: string,\n listener?: CustomEventDispatch<TDetail>,\n customRef?: RefObject<TElement>,\n): [RefObject<TElement>, CustomEventDispatch<TDetail>] => {\n const ref = customRef ?? createRef<TElement>()\n useEffect(() => {\n const element = ref?.current\n const currentListener = listener\n const handler = currentListener\n ? (event: CustomEventInit<TDetail> & Event) => {\n const detail = assertEx(event.detail, 'Event missing detail')\n const stop = currentListener(detail)\n if (stop) {\n event.stopPropagation()\n }\n }\n : undefined\n\n if (handler && element) {\n element?.addEventListener(type, handler)\n }\n return () => {\n if (handler && element) {\n element?.removeEventListener(type, handler)\n }\n }\n })\n const dispatch: CustomEventDispatch<TDetail> = (detail?: TDetail) => {\n const event = new CustomEvent<TDetail>(type, {\n bubbles: true,\n cancelable: true,\n composed: true,\n detail,\n })\n ref.current?.dispatchEvent(event)\n }\n return [ref, dispatch]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAyB;AACzB,mBAAgD;AAIzC,MAAM,iBAAiB,CAC5B,MACA,UACA,cACwD;AACxD,QAAM,MAAM,iBAAa,wBAAoB;AAC7C,8BAAU,MAAM;AACd,UAAM,UAAU,KAAK;AACrB,UAAM,kBAAkB;AACxB,UAAM,UAAU,kBACZ,CAAC,UAA4C;AAC3C,YAAM,aAAS,wBAAS,MAAM,QAAQ,sBAAsB;AAC5D,YAAM,OAAO,gBAAgB,MAAM;AACnC,UAAI,MAAM;AACR,cAAM,gBAAgB;AAAA,MACxB;AAAA,IACF,IACA;AAEJ,QAAI,WAAW,SAAS;AACtB,eAAS,iBAAiB,MAAM,OAAO;AAAA,IACzC;AACA,WAAO,MAAM;AACX,UAAI,WAAW,SAAS;AACtB,iBAAS,oBAAoB,MAAM,OAAO;AAAA,MAC5C;AAAA,IACF;AAAA,EACF,CAAC;AACD,QAAM,WAAyC,CAAC,WAAqB;AACnE,UAAM,QAAQ,IAAI,YAAqB,MAAM;AAAA,MAC3C,SAAS;AAAA,MACT,YAAY;AAAA,MACZ,UAAU;AAAA,MACV;AAAA,IACF,CAAC;AACD,QAAI,SAAS,cAAc,KAAK;AAAA,EAClC;AACA,SAAO,CAAC,KAAK,QAAQ;AACvB;","names":[]}
1
+ {"version":3,"sources":["../../../src/hooks/useCustomEvent.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { createRef, RefObject, useEffect } from 'react'\n\nexport type CustomEventDispatch<T = unknown> = (detail: T) => boolean | void\n\nexport const useCustomEvent = <TElement extends HTMLElement, TDetail = unknown>(\n type: string,\n listener?: CustomEventDispatch<TDetail>,\n customRef?: RefObject<TElement>,\n): [RefObject<TElement>, CustomEventDispatch<TDetail>] => {\n const ref = customRef ?? createRef<TElement>()\n useEffect(() => {\n const element = ref?.current\n const currentListener = listener\n const handler = currentListener\n ? (event: CustomEventInit<TDetail> & Event) => {\n const detail = assertEx(event.detail, 'Event missing detail')\n const stop = currentListener(detail)\n if (stop) {\n event.stopPropagation()\n }\n }\n : undefined\n\n if (handler && element) {\n element?.addEventListener(type, handler)\n }\n return () => {\n if (handler && element) {\n element?.removeEventListener(type, handler)\n }\n }\n })\n const dispatch: CustomEventDispatch<TDetail> = (detail?: TDetail) => {\n const event = new CustomEvent<TDetail>(type, {\n bubbles: true,\n cancelable: true,\n composed: true,\n detail,\n })\n ref.current?.dispatchEvent(event)\n }\n return [ref, dispatch]\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,WAAsB,iBAAiB;AAIzC,IAAM,iBAAiB,CAC5B,MACA,UACA,cACwD;AACxD,QAAM,MAAM,aAAa,UAAoB;AAC7C,YAAU,MAAM;AACd,UAAM,UAAU,2BAAK;AACrB,UAAM,kBAAkB;AACxB,UAAM,UAAU,kBACZ,CAAC,UAA4C;AAC3C,YAAM,SAAS,SAAS,MAAM,QAAQ,sBAAsB;AAC5D,YAAM,OAAO,gBAAgB,MAAM;AACnC,UAAI,MAAM;AACR,cAAM,gBAAgB;AAAA,MACxB;AAAA,IACF,IACA;AAEJ,QAAI,WAAW,SAAS;AACtB,yCAAS,iBAAiB,MAAM;AAAA,IAClC;AACA,WAAO,MAAM;AACX,UAAI,WAAW,SAAS;AACtB,2CAAS,oBAAoB,MAAM;AAAA,MACrC;AAAA,IACF;AAAA,EACF,CAAC;AACD,QAAM,WAAyC,CAAC,WAAqB;AAjCvE;AAkCI,UAAM,QAAQ,IAAI,YAAqB,MAAM;AAAA,MAC3C,SAAS;AAAA,MACT,YAAY;AAAA,MACZ,UAAU;AAAA,MACV;AAAA,IACF,CAAC;AACD,cAAI,YAAJ,mBAAa,cAAc;AAAA,EAC7B;AACA,SAAO,CAAC,KAAK,QAAQ;AACvB;","names":[]}
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/hooks/useEvent.ts
21
+ var useEvent_exports = {};
22
+ __export(useEvent_exports, {
23
+ useEvent: () => useEvent
24
+ });
25
+ module.exports = __toCommonJS(useEvent_exports);
26
+
27
+ // src/hooks/useCustomEvent.ts
28
+ var import_assert = require("@xylabs/assert");
29
+ var import_react = require("react");
30
+ var useCustomEvent = (type, listener, customRef) => {
31
+ const ref = customRef ?? (0, import_react.createRef)();
32
+ (0, import_react.useEffect)(() => {
33
+ const element = ref == null ? void 0 : ref.current;
34
+ const currentListener = listener;
35
+ const handler = currentListener ? (event) => {
36
+ const detail = (0, import_assert.assertEx)(event.detail, "Event missing detail");
37
+ const stop = currentListener(detail);
38
+ if (stop) {
39
+ event.stopPropagation();
40
+ }
41
+ } : void 0;
42
+ if (handler && element) {
43
+ element == null ? void 0 : element.addEventListener(type, handler);
44
+ }
45
+ return () => {
46
+ if (handler && element) {
47
+ element == null ? void 0 : element.removeEventListener(type, handler);
48
+ }
49
+ };
50
+ });
51
+ const dispatch = (detail) => {
52
+ var _a;
53
+ const event = new CustomEvent(type, {
54
+ bubbles: true,
55
+ cancelable: true,
56
+ composed: true,
57
+ detail
58
+ });
59
+ (_a = ref.current) == null ? void 0 : _a.dispatchEvent(event);
60
+ };
61
+ return [ref, dispatch];
62
+ };
63
+
64
+ // src/hooks/useEvent.ts
65
+ var useEvent = (listener, sharableRef) => {
66
+ const [ref, customDispatch] = useCustomEvent(
67
+ "xyo",
68
+ listener ? (detail) => {
69
+ return listener(detail.noun, detail.verb, detail.data);
70
+ } : void 0,
71
+ sharableRef
72
+ );
73
+ const dispatch = (noun, verb, data) => {
74
+ return customDispatch({ data, noun, verb });
75
+ };
76
+ return [ref, dispatch];
77
+ };
78
+ // Annotate the CommonJS export names for ESM import in node:
79
+ 0 && (module.exports = {
80
+ useEvent
81
+ });
82
+ //# sourceMappingURL=useEvent.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/hooks/useEvent.ts","../../../src/hooks/useCustomEvent.ts"],"sourcesContent":["import { RefObject } from 'react'\n\nimport { useCustomEvent } from './useCustomEvent'\n\nexport type EventNoun = 'payload' | 'boundwitness' | 'address' | 'hash' | 'signature' | 'schema'\nexport type EventVerb = 'click' | 'favorite'\n\nexport interface Event<TNoun = EventNoun, TVerb = EventVerb, TData = string> {\n data?: TData\n noun: TNoun\n verb: TVerb\n}\n\nexport type EventDispatch<TNoun = EventNoun, TVerb = EventVerb, TData = string> = (noun: TNoun, verb: TVerb, data?: TData) => boolean | void\n\nexport const useEvent = <T extends HTMLElement, TNoun = EventNoun, TVerb = EventVerb, TData = string>(\n listener?: EventDispatch<TNoun, TVerb, TData>,\n sharableRef?: RefObject<T>,\n): [RefObject<T>, EventDispatch<TNoun, TVerb, TData>] => {\n const [ref, customDispatch] = useCustomEvent<T, Event<TNoun, TVerb, TData>>(\n 'xyo',\n listener\n ? (detail: Event<TNoun, TVerb, TData>) => {\n return listener(detail.noun, detail.verb, detail.data)\n }\n : undefined,\n sharableRef,\n )\n\n const dispatch: EventDispatch<TNoun, TVerb, TData> = (noun: TNoun, verb: TVerb, data?: TData) => {\n return customDispatch({ data, noun, verb })\n }\n return [ref, dispatch]\n}\n","import { assertEx } from '@xylabs/assert'\nimport { createRef, RefObject, useEffect } from 'react'\n\nexport type CustomEventDispatch<T = unknown> = (detail: T) => boolean | void\n\nexport const useCustomEvent = <TElement extends HTMLElement, TDetail = unknown>(\n type: string,\n listener?: CustomEventDispatch<TDetail>,\n customRef?: RefObject<TElement>,\n): [RefObject<TElement>, CustomEventDispatch<TDetail>] => {\n const ref = customRef ?? createRef<TElement>()\n useEffect(() => {\n const element = ref?.current\n const currentListener = listener\n const handler = currentListener\n ? (event: CustomEventInit<TDetail> & Event) => {\n const detail = assertEx(event.detail, 'Event missing detail')\n const stop = currentListener(detail)\n if (stop) {\n event.stopPropagation()\n }\n }\n : undefined\n\n if (handler && element) {\n element?.addEventListener(type, handler)\n }\n return () => {\n if (handler && element) {\n element?.removeEventListener(type, handler)\n }\n }\n })\n const dispatch: CustomEventDispatch<TDetail> = (detail?: TDetail) => {\n const event = new CustomEvent<TDetail>(type, {\n bubbles: true,\n cancelable: true,\n composed: true,\n detail,\n })\n ref.current?.dispatchEvent(event)\n }\n return [ref, dispatch]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAyB;AACzB,mBAAgD;AAIzC,IAAM,iBAAiB,CAC5B,MACA,UACA,cACwD;AACxD,QAAM,MAAM,iBAAa,wBAAoB;AAC7C,8BAAU,MAAM;AACd,UAAM,UAAU,2BAAK;AACrB,UAAM,kBAAkB;AACxB,UAAM,UAAU,kBACZ,CAAC,UAA4C;AAC3C,YAAM,aAAS,wBAAS,MAAM,QAAQ,sBAAsB;AAC5D,YAAM,OAAO,gBAAgB,MAAM;AACnC,UAAI,MAAM;AACR,cAAM,gBAAgB;AAAA,MACxB;AAAA,IACF,IACA;AAEJ,QAAI,WAAW,SAAS;AACtB,yCAAS,iBAAiB,MAAM;AAAA,IAClC;AACA,WAAO,MAAM;AACX,UAAI,WAAW,SAAS;AACtB,2CAAS,oBAAoB,MAAM;AAAA,MACrC;AAAA,IACF;AAAA,EACF,CAAC;AACD,QAAM,WAAyC,CAAC,WAAqB;AAjCvE;AAkCI,UAAM,QAAQ,IAAI,YAAqB,MAAM;AAAA,MAC3C,SAAS;AAAA,MACT,YAAY;AAAA,MACZ,UAAU;AAAA,MACV;AAAA,IACF,CAAC;AACD,cAAI,YAAJ,mBAAa,cAAc;AAAA,EAC7B;AACA,SAAO,CAAC,KAAK,QAAQ;AACvB;;;AD5BO,IAAM,WAAW,CACtB,UACA,gBACuD;AACvD,QAAM,CAAC,KAAK,cAAc,IAAI;AAAA,IAC5B;AAAA,IACA,WACI,CAAC,WAAuC;AACtC,aAAO,SAAS,OAAO,MAAM,OAAO,MAAM,OAAO,IAAI;AAAA,IACvD,IACA;AAAA,IACJ;AAAA,EACF;AAEA,QAAM,WAA+C,CAAC,MAAa,MAAa,SAAiB;AAC/F,WAAO,eAAe,EAAE,MAAM,MAAM,KAAK,CAAC;AAAA,EAC5C;AACA,SAAO,CAAC,KAAK,QAAQ;AACvB;","names":[]}
@@ -0,0 +1,11 @@
1
+ import { RefObject } from 'react';
2
+ export type EventNoun = 'payload' | 'boundwitness' | 'address' | 'hash' | 'signature' | 'schema';
3
+ export type EventVerb = 'click' | 'favorite';
4
+ export interface Event<TNoun = EventNoun, TVerb = EventVerb, TData = string> {
5
+ data?: TData;
6
+ noun: TNoun;
7
+ verb: TVerb;
8
+ }
9
+ export type EventDispatch<TNoun = EventNoun, TVerb = EventVerb, TData = string> = (noun: TNoun, verb: TVerb, data?: TData) => boolean | void;
10
+ export declare const useEvent: <T extends HTMLElement, TNoun = EventNoun, TVerb = EventVerb, TData = string>(listener?: EventDispatch<TNoun, TVerb, TData> | undefined, sharableRef?: RefObject<T> | undefined) => [RefObject<T>, EventDispatch<TNoun, TVerb, TData>];
11
+ //# sourceMappingURL=useEvent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useEvent.d.ts","sourceRoot":"","sources":["../../../src/hooks/useEvent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAIjC,MAAM,MAAM,SAAS,GAAG,SAAS,GAAG,cAAc,GAAG,SAAS,GAAG,MAAM,GAAG,WAAW,GAAG,QAAQ,CAAA;AAChG,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,UAAU,CAAA;AAE5C,MAAM,WAAW,KAAK,CAAC,KAAK,GAAG,SAAS,EAAE,KAAK,GAAG,SAAS,EAAE,KAAK,GAAG,MAAM;IACzE,IAAI,CAAC,EAAE,KAAK,CAAA;IACZ,IAAI,EAAE,KAAK,CAAA;IACX,IAAI,EAAE,KAAK,CAAA;CACZ;AAED,MAAM,MAAM,aAAa,CAAC,KAAK,GAAG,SAAS,EAAE,KAAK,GAAG,SAAS,EAAE,KAAK,GAAG,MAAM,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,KAAK,KAAK,OAAO,GAAG,IAAI,CAAA;AAE5I,eAAO,MAAM,QAAQ,wOAkBpB,CAAA"}
@@ -1,29 +1,43 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
1
+ // src/hooks/useCustomEvent.ts
2
+ import { assertEx } from "@xylabs/assert";
3
+ import { createRef, useEffect } from "react";
4
+ var useCustomEvent = (type, listener, customRef) => {
5
+ const ref = customRef ?? createRef();
6
+ useEffect(() => {
7
+ const element = ref == null ? void 0 : ref.current;
8
+ const currentListener = listener;
9
+ const handler = currentListener ? (event) => {
10
+ const detail = assertEx(event.detail, "Event missing detail");
11
+ const stop = currentListener(detail);
12
+ if (stop) {
13
+ event.stopPropagation();
14
+ }
15
+ } : void 0;
16
+ if (handler && element) {
17
+ element == null ? void 0 : element.addEventListener(type, handler);
18
+ }
19
+ return () => {
20
+ if (handler && element) {
21
+ element == null ? void 0 : element.removeEventListener(type, handler);
22
+ }
23
+ };
24
+ });
25
+ const dispatch = (detail) => {
26
+ var _a;
27
+ const event = new CustomEvent(type, {
28
+ bubbles: true,
29
+ cancelable: true,
30
+ composed: true,
31
+ detail
32
+ });
33
+ (_a = ref.current) == null ? void 0 : _a.dispatchEvent(event);
34
+ };
35
+ return [ref, dispatch];
17
36
  };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var useEvent_exports = {};
20
- __export(useEvent_exports, {
21
- useEvent: () => useEvent
22
- });
23
- module.exports = __toCommonJS(useEvent_exports);
24
- var import_useCustomEvent = require("./useCustomEvent");
25
- const useEvent = (listener, sharableRef) => {
26
- const [ref, customDispatch] = (0, import_useCustomEvent.useCustomEvent)(
37
+
38
+ // src/hooks/useEvent.ts
39
+ var useEvent = (listener, sharableRef) => {
40
+ const [ref, customDispatch] = useCustomEvent(
27
41
  "xyo",
28
42
  listener ? (detail) => {
29
43
  return listener(detail.noun, detail.verb, detail.data);
@@ -35,8 +49,7 @@ const useEvent = (listener, sharableRef) => {
35
49
  };
36
50
  return [ref, dispatch];
37
51
  };
38
- // Annotate the CommonJS export names for ESM import in node:
39
- 0 && (module.exports = {
52
+ export {
40
53
  useEvent
41
- });
54
+ };
42
55
  //# sourceMappingURL=useEvent.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/hooks/useEvent.ts"],"sourcesContent":["import { RefObject } from 'react'\n\nimport { useCustomEvent } from './useCustomEvent'\n\nexport type EventNoun = 'payload' | 'boundwitness' | 'address' | 'hash' | 'signature' | 'schema'\nexport type EventVerb = 'click' | 'favorite'\n\nexport interface Event<TNoun = EventNoun, TVerb = EventVerb, TData = string> {\n data?: TData\n noun: TNoun\n verb: TVerb\n}\n\nexport type EventDispatch<TNoun = EventNoun, TVerb = EventVerb, TData = string> = (noun: TNoun, verb: TVerb, data?: TData) => boolean | void\n\nexport const useEvent = <T extends HTMLElement, TNoun = EventNoun, TVerb = EventVerb, TData = string>(\n listener?: EventDispatch<TNoun, TVerb, TData>,\n sharableRef?: RefObject<T>,\n): [RefObject<T>, EventDispatch<TNoun, TVerb, TData>] => {\n const [ref, customDispatch] = useCustomEvent<T, Event<TNoun, TVerb, TData>>(\n 'xyo',\n listener\n ? (detail: Event<TNoun, TVerb, TData>) => {\n return listener(detail.noun, detail.verb, detail.data)\n }\n : undefined,\n sharableRef,\n )\n\n const dispatch: EventDispatch<TNoun, TVerb, TData> = (noun: TNoun, verb: TVerb, data?: TData) => {\n return customDispatch({ data, noun, verb })\n }\n return [ref, dispatch]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,4BAA+B;AAaxB,MAAM,WAAW,CACtB,UACA,gBACuD;AACvD,QAAM,CAAC,KAAK,cAAc,QAAI;AAAA,IAC5B;AAAA,IACA,WACI,CAAC,WAAuC;AACtC,aAAO,SAAS,OAAO,MAAM,OAAO,MAAM,OAAO,IAAI;AAAA,IACvD,IACA;AAAA,IACJ;AAAA,EACF;AAEA,QAAM,WAA+C,CAAC,MAAa,MAAa,SAAiB;AAC/F,WAAO,eAAe,EAAE,MAAM,MAAM,KAAK,CAAC;AAAA,EAC5C;AACA,SAAO,CAAC,KAAK,QAAQ;AACvB;","names":[]}
1
+ {"version":3,"sources":["../../../src/hooks/useCustomEvent.ts","../../../src/hooks/useEvent.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\nimport { createRef, RefObject, useEffect } from 'react'\n\nexport type CustomEventDispatch<T = unknown> = (detail: T) => boolean | void\n\nexport const useCustomEvent = <TElement extends HTMLElement, TDetail = unknown>(\n type: string,\n listener?: CustomEventDispatch<TDetail>,\n customRef?: RefObject<TElement>,\n): [RefObject<TElement>, CustomEventDispatch<TDetail>] => {\n const ref = customRef ?? createRef<TElement>()\n useEffect(() => {\n const element = ref?.current\n const currentListener = listener\n const handler = currentListener\n ? (event: CustomEventInit<TDetail> & Event) => {\n const detail = assertEx(event.detail, 'Event missing detail')\n const stop = currentListener(detail)\n if (stop) {\n event.stopPropagation()\n }\n }\n : undefined\n\n if (handler && element) {\n element?.addEventListener(type, handler)\n }\n return () => {\n if (handler && element) {\n element?.removeEventListener(type, handler)\n }\n }\n })\n const dispatch: CustomEventDispatch<TDetail> = (detail?: TDetail) => {\n const event = new CustomEvent<TDetail>(type, {\n bubbles: true,\n cancelable: true,\n composed: true,\n detail,\n })\n ref.current?.dispatchEvent(event)\n }\n return [ref, dispatch]\n}\n","import { RefObject } from 'react'\n\nimport { useCustomEvent } from './useCustomEvent'\n\nexport type EventNoun = 'payload' | 'boundwitness' | 'address' | 'hash' | 'signature' | 'schema'\nexport type EventVerb = 'click' | 'favorite'\n\nexport interface Event<TNoun = EventNoun, TVerb = EventVerb, TData = string> {\n data?: TData\n noun: TNoun\n verb: TVerb\n}\n\nexport type EventDispatch<TNoun = EventNoun, TVerb = EventVerb, TData = string> = (noun: TNoun, verb: TVerb, data?: TData) => boolean | void\n\nexport const useEvent = <T extends HTMLElement, TNoun = EventNoun, TVerb = EventVerb, TData = string>(\n listener?: EventDispatch<TNoun, TVerb, TData>,\n sharableRef?: RefObject<T>,\n): [RefObject<T>, EventDispatch<TNoun, TVerb, TData>] => {\n const [ref, customDispatch] = useCustomEvent<T, Event<TNoun, TVerb, TData>>(\n 'xyo',\n listener\n ? (detail: Event<TNoun, TVerb, TData>) => {\n return listener(detail.noun, detail.verb, detail.data)\n }\n : undefined,\n sharableRef,\n )\n\n const dispatch: EventDispatch<TNoun, TVerb, TData> = (noun: TNoun, verb: TVerb, data?: TData) => {\n return customDispatch({ data, noun, verb })\n }\n return [ref, dispatch]\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,WAAsB,iBAAiB;AAIzC,IAAM,iBAAiB,CAC5B,MACA,UACA,cACwD;AACxD,QAAM,MAAM,aAAa,UAAoB;AAC7C,YAAU,MAAM;AACd,UAAM,UAAU,2BAAK;AACrB,UAAM,kBAAkB;AACxB,UAAM,UAAU,kBACZ,CAAC,UAA4C;AAC3C,YAAM,SAAS,SAAS,MAAM,QAAQ,sBAAsB;AAC5D,YAAM,OAAO,gBAAgB,MAAM;AACnC,UAAI,MAAM;AACR,cAAM,gBAAgB;AAAA,MACxB;AAAA,IACF,IACA;AAEJ,QAAI,WAAW,SAAS;AACtB,yCAAS,iBAAiB,MAAM;AAAA,IAClC;AACA,WAAO,MAAM;AACX,UAAI,WAAW,SAAS;AACtB,2CAAS,oBAAoB,MAAM;AAAA,MACrC;AAAA,IACF;AAAA,EACF,CAAC;AACD,QAAM,WAAyC,CAAC,WAAqB;AAjCvE;AAkCI,UAAM,QAAQ,IAAI,YAAqB,MAAM;AAAA,MAC3C,SAAS;AAAA,MACT,YAAY;AAAA,MACZ,UAAU;AAAA,MACV;AAAA,IACF,CAAC;AACD,cAAI,YAAJ,mBAAa,cAAc;AAAA,EAC7B;AACA,SAAO,CAAC,KAAK,QAAQ;AACvB;;;AC5BO,IAAM,WAAW,CACtB,UACA,gBACuD;AACvD,QAAM,CAAC,KAAK,cAAc,IAAI;AAAA,IAC5B;AAAA,IACA,WACI,CAAC,WAAuC;AACtC,aAAO,SAAS,OAAO,MAAM,OAAO,MAAM,OAAO,IAAI;AAAA,IACvD,IACA;AAAA,IACJ;AAAA,EACF;AAEA,QAAM,WAA+C,CAAC,MAAa,MAAa,SAAiB;AAC/F,WAAO,eAAe,EAAE,MAAM,MAAM,KAAK,CAAC;AAAA,EAC5C;AACA,SAAO,CAAC,KAAK,QAAQ;AACvB;","names":[]}