@xyo-network/react-event 2.77.0-rc.1 → 2.77.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,78 +1,2 @@
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/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- useCustomEvent: () => useCustomEvent,
24
- useEvent: () => useEvent
25
- });
26
- module.exports = __toCommonJS(src_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?.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?.addEventListener(type, handler);
45
- }
46
- return () => {
47
- if (handler && element) {
48
- element?.removeEventListener(type, handler);
49
- }
50
- };
51
- });
52
- const dispatch = (detail) => {
53
- const event = new CustomEvent(type, {
54
- bubbles: true,
55
- cancelable: true,
56
- composed: true,
57
- detail
58
- });
59
- ref.current?.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
- };
1
+ "use strict";var u=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var d=(t,e)=>{for(var o in e)u(t,o,{get:e[o],enumerable:!0})},f=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of m(e))!D.call(t,a)&&a!==o&&u(t,a,{get:()=>e[a],enumerable:!(r=b(e,a))||r.enumerable});return t};var l=t=>f(u({},"__esModule",{value:!0}),t);var N={};d(N,{useCustomEvent:()=>c,useEvent:()=>h});module.exports=l(N);var v=require("@xylabs/assert"),i=require("react"),c=(t,e,o)=>{let r=o??(0,i.createRef)();return(0,i.useEffect)(()=>{let n=r?.current,s=e,T=s?E=>{let p=(0,v.assertEx)(E.detail,()=>"Event missing detail");s(p)&&E.stopPropagation()}:void 0;return T&&n&&n?.addEventListener(t,T),()=>{T&&n&&n?.removeEventListener(t,T)}}),[r,n=>{let s=new CustomEvent(t,{bubbles:!0,cancelable:!0,composed:!0,detail:n});r.current?.dispatchEvent(s)}]};var h=(t,e)=>{let[o,r]=c("xyo",t?n=>t(n.noun,n.verb,n.data):void 0,e);return[o,(n,s,T)=>r({data:T,noun:n,verb:s})]};
78
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts","../../src/hooks/useCustomEvent.ts","../../src/hooks/useEvent.ts"],"sourcesContent":["export * from './hooks'\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 =\n 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,KAAK;AACrB,UAAM,kBAAkB;AACxB,UAAM,UACJ,kBACE,CAAC,UAA4C;AAC3C,YAAM,aAAS,wBAAS,MAAM,QAAQ,MAAM,sBAAsB;AAClE,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;;;AC7BO,IAAM,WAAW,CACtB,UACA,gBACuD;AACvD,QAAM,CAAC,KAAK,cAAc,IAAI;AAAA,IAC5B;AAAA,IACA,WACE,CAAC,WAAuC;AACtC,aAAO,SAAS,OAAO,MAAM,OAAO,MAAM,OAAO,IAAI;AAAA,IACvD,IACA;AAAA,IACF;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/index.ts","../../src/hooks/useCustomEvent.ts","../../src/hooks/useEvent.ts"],"sourcesContent":["export * from './hooks'\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 =\n 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":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,aAAAC,IAAA,eAAAC,EAAAJ,GCAA,IAAAK,EAAyB,0BACzBC,EAAgD,iBAInCC,EAAiB,CAC5BC,EACAC,EACAC,IACwD,CACxD,IAAMC,EAAMD,MAAa,aAAoB,EAC7C,sBAAU,IAAM,CACd,IAAME,EAAUD,GAAK,QACfE,EAAkBJ,EAClBK,EACJD,EACGE,GAA4C,CAC3C,IAAMC,KAAS,YAASD,EAAM,OAAQ,IAAM,sBAAsB,EACrDF,EAAgBG,CAAM,GAEjCD,EAAM,gBAAgB,CAE1B,EACA,OAEJ,OAAID,GAAWF,GACbA,GAAS,iBAAiBJ,EAAMM,CAAO,EAElC,IAAM,CACPA,GAAWF,GACbA,GAAS,oBAAoBJ,EAAMM,CAAO,CAE9C,CACF,CAAC,EAUM,CAACH,EATwCK,GAAqB,CACnE,IAAMD,EAAQ,IAAI,YAAqBP,EAAM,CAC3C,QAAS,GACT,WAAY,GACZ,SAAU,GACV,OAAAQ,CACF,CAAC,EACDL,EAAI,SAAS,cAAcI,CAAK,CAClC,CACqB,CACvB,EC7BO,IAAME,EAAW,CACtBC,EACAC,IACuD,CACvD,GAAM,CAACC,EAAKC,CAAc,EAAIC,EAC5B,MACAJ,EACGK,GACQL,EAASK,EAAO,KAAMA,EAAO,KAAMA,EAAO,IAAI,EAEvD,OACFJ,CACF,EAKA,MAAO,CAACC,EAH6C,CAACI,EAAaC,EAAaC,IACvEL,EAAe,CAAE,KAAAK,EAAM,KAAAF,EAAM,KAAAC,CAAK,CAAC,CAEvB,CACvB","names":["src_exports","__export","useCustomEvent","useEvent","__toCommonJS","import_assert","import_react","useCustomEvent","type","listener","customRef","ref","element","currentListener","handler","event","detail","useEvent","listener","sharableRef","ref","customDispatch","useCustomEvent","detail","noun","verb","data"]}
@@ -1,55 +1,2 @@
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?.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?.addEventListener(type, handler);
18
- }
19
- return () => {
20
- if (handler && element) {
21
- element?.removeEventListener(type, handler);
22
- }
23
- };
24
- });
25
- const dispatch = (detail) => {
26
- const event = new CustomEvent(type, {
27
- bubbles: true,
28
- cancelable: true,
29
- composed: true,
30
- detail
31
- });
32
- ref.current?.dispatchEvent(event);
33
- };
34
- return [ref, dispatch];
35
- };
36
-
37
- // src/hooks/useEvent.ts
38
- var useEvent = (listener, sharableRef) => {
39
- const [ref, customDispatch] = useCustomEvent(
40
- "xyo",
41
- listener ? (detail) => {
42
- return listener(detail.noun, detail.verb, detail.data);
43
- } : void 0,
44
- sharableRef
45
- );
46
- const dispatch = (noun, verb, data) => {
47
- return customDispatch({ data, noun, verb });
48
- };
49
- return [ref, dispatch];
50
- };
51
- export {
52
- useCustomEvent,
53
- useEvent
54
- };
1
+ import{assertEx as E}from"@xylabs/assert";import{createRef as v,useEffect as p}from"react";var i=(e,a,s)=>{let r=s??v();return p(()=>{let t=r?.current,n=a,o=n?T=>{let c=E(T.detail,()=>"Event missing detail");n(c)&&T.stopPropagation()}:void 0;return o&&t&&t?.addEventListener(e,o),()=>{o&&t&&t?.removeEventListener(e,o)}}),[r,t=>{let n=new CustomEvent(e,{bubbles:!0,cancelable:!0,composed:!0,detail:t});r.current?.dispatchEvent(n)}]};var h=(e,a)=>{let[s,r]=i("xyo",e?t=>e(t.noun,t.verb,t.data):void 0,a);return[s,(t,n,o)=>r({data:o,noun:t,verb:n})]};export{i as useCustomEvent,h as useEvent};
55
2
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
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 =\n 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,KAAK;AACrB,UAAM,kBAAkB;AACxB,UAAM,UACJ,kBACE,CAAC,UAA4C;AAC3C,YAAM,SAAS,SAAS,MAAM,QAAQ,MAAM,sBAAsB;AAClE,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;;;AC7BO,IAAM,WAAW,CACtB,UACA,gBACuD;AACvD,QAAM,CAAC,KAAK,cAAc,IAAI;AAAA,IAC5B;AAAA,IACA,WACE,CAAC,WAAuC;AACtC,aAAO,SAAS,OAAO,MAAM,OAAO,MAAM,OAAO,IAAI;AAAA,IACvD,IACA;AAAA,IACF;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 =\n 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,OAAS,YAAAA,MAAgB,iBACzB,OAAS,aAAAC,EAAsB,aAAAC,MAAiB,QAIzC,IAAMC,EAAiB,CAC5BC,EACAC,EACAC,IACwD,CACxD,IAAMC,EAAMD,GAAaL,EAAoB,EAC7C,OAAAC,EAAU,IAAM,CACd,IAAMM,EAAUD,GAAK,QACfE,EAAkBJ,EAClBK,EACJD,EACGE,GAA4C,CAC3C,IAAMC,EAASZ,EAASW,EAAM,OAAQ,IAAM,sBAAsB,EACrDF,EAAgBG,CAAM,GAEjCD,EAAM,gBAAgB,CAE1B,EACA,OAEJ,OAAID,GAAWF,GACbA,GAAS,iBAAiBJ,EAAMM,CAAO,EAElC,IAAM,CACPA,GAAWF,GACbA,GAAS,oBAAoBJ,EAAMM,CAAO,CAE9C,CACF,CAAC,EAUM,CAACH,EATwCK,GAAqB,CACnE,IAAMD,EAAQ,IAAI,YAAqBP,EAAM,CAC3C,QAAS,GACT,WAAY,GACZ,SAAU,GACV,OAAAQ,CACF,CAAC,EACDL,EAAI,SAAS,cAAcI,CAAK,CAClC,CACqB,CACvB,EC7BO,IAAME,EAAW,CACtBC,EACAC,IACuD,CACvD,GAAM,CAACC,EAAKC,CAAc,EAAIC,EAC5B,MACAJ,EACGK,GACQL,EAASK,EAAO,KAAMA,EAAO,KAAMA,EAAO,IAAI,EAEvD,OACFJ,CACF,EAKA,MAAO,CAACC,EAH6C,CAACI,EAAaC,EAAaC,IACvEL,EAAe,CAAE,KAAAK,EAAM,KAAAF,EAAM,KAAAC,CAAK,CAAC,CAEvB,CACvB","names":["assertEx","createRef","useEffect","useCustomEvent","type","listener","customRef","ref","element","currentListener","handler","event","detail","useEvent","listener","sharableRef","ref","customDispatch","useCustomEvent","detail","noun","verb","data"]}
@@ -1,78 +1,2 @@
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/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- useCustomEvent: () => useCustomEvent,
24
- useEvent: () => useEvent
25
- });
26
- module.exports = __toCommonJS(src_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?.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?.addEventListener(type, handler);
45
- }
46
- return () => {
47
- if (handler && element) {
48
- element?.removeEventListener(type, handler);
49
- }
50
- };
51
- });
52
- const dispatch = (detail) => {
53
- const event = new CustomEvent(type, {
54
- bubbles: true,
55
- cancelable: true,
56
- composed: true,
57
- detail
58
- });
59
- ref.current?.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
- };
1
+ "use strict";var u=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var d=(t,e)=>{for(var o in e)u(t,o,{get:e[o],enumerable:!0})},f=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of m(e))!D.call(t,a)&&a!==o&&u(t,a,{get:()=>e[a],enumerable:!(r=b(e,a))||r.enumerable});return t};var l=t=>f(u({},"__esModule",{value:!0}),t);var N={};d(N,{useCustomEvent:()=>c,useEvent:()=>h});module.exports=l(N);var v=require("@xylabs/assert"),i=require("react"),c=(t,e,o)=>{let r=o??(0,i.createRef)();return(0,i.useEffect)(()=>{let n=r?.current,s=e,T=s?E=>{let p=(0,v.assertEx)(E.detail,()=>"Event missing detail");s(p)&&E.stopPropagation()}:void 0;return T&&n&&n?.addEventListener(t,T),()=>{T&&n&&n?.removeEventListener(t,T)}}),[r,n=>{let s=new CustomEvent(t,{bubbles:!0,cancelable:!0,composed:!0,detail:n});r.current?.dispatchEvent(s)}]};var h=(t,e)=>{let[o,r]=c("xyo",t?n=>t(n.noun,n.verb,n.data):void 0,e);return[o,(n,s,T)=>r({data:T,noun:n,verb:s})]};
78
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts","../../src/hooks/useCustomEvent.ts","../../src/hooks/useEvent.ts"],"sourcesContent":["export * from './hooks'\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 =\n 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,KAAK;AACrB,UAAM,kBAAkB;AACxB,UAAM,UACJ,kBACE,CAAC,UAA4C;AAC3C,YAAM,aAAS,wBAAS,MAAM,QAAQ,MAAM,sBAAsB;AAClE,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;;;AC7BO,IAAM,WAAW,CACtB,UACA,gBACuD;AACvD,QAAM,CAAC,KAAK,cAAc,IAAI;AAAA,IAC5B;AAAA,IACA,WACE,CAAC,WAAuC;AACtC,aAAO,SAAS,OAAO,MAAM,OAAO,MAAM,OAAO,IAAI;AAAA,IACvD,IACA;AAAA,IACF;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/index.ts","../../src/hooks/useCustomEvent.ts","../../src/hooks/useEvent.ts"],"sourcesContent":["export * from './hooks'\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 =\n 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":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,aAAAC,IAAA,eAAAC,EAAAJ,GCAA,IAAAK,EAAyB,0BACzBC,EAAgD,iBAInCC,EAAiB,CAC5BC,EACAC,EACAC,IACwD,CACxD,IAAMC,EAAMD,MAAa,aAAoB,EAC7C,sBAAU,IAAM,CACd,IAAME,EAAUD,GAAK,QACfE,EAAkBJ,EAClBK,EACJD,EACGE,GAA4C,CAC3C,IAAMC,KAAS,YAASD,EAAM,OAAQ,IAAM,sBAAsB,EACrDF,EAAgBG,CAAM,GAEjCD,EAAM,gBAAgB,CAE1B,EACA,OAEJ,OAAID,GAAWF,GACbA,GAAS,iBAAiBJ,EAAMM,CAAO,EAElC,IAAM,CACPA,GAAWF,GACbA,GAAS,oBAAoBJ,EAAMM,CAAO,CAE9C,CACF,CAAC,EAUM,CAACH,EATwCK,GAAqB,CACnE,IAAMD,EAAQ,IAAI,YAAqBP,EAAM,CAC3C,QAAS,GACT,WAAY,GACZ,SAAU,GACV,OAAAQ,CACF,CAAC,EACDL,EAAI,SAAS,cAAcI,CAAK,CAClC,CACqB,CACvB,EC7BO,IAAME,EAAW,CACtBC,EACAC,IACuD,CACvD,GAAM,CAACC,EAAKC,CAAc,EAAIC,EAC5B,MACAJ,EACGK,GACQL,EAASK,EAAO,KAAMA,EAAO,KAAMA,EAAO,IAAI,EAEvD,OACFJ,CACF,EAKA,MAAO,CAACC,EAH6C,CAACI,EAAaC,EAAaC,IACvEL,EAAe,CAAE,KAAAK,EAAM,KAAAF,EAAM,KAAAC,CAAK,CAAC,CAEvB,CACvB","names":["src_exports","__export","useCustomEvent","useEvent","__toCommonJS","import_assert","import_react","useCustomEvent","type","listener","customRef","ref","element","currentListener","handler","event","detail","useEvent","listener","sharableRef","ref","customDispatch","useCustomEvent","detail","noun","verb","data"]}
@@ -1,55 +1,2 @@
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?.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?.addEventListener(type, handler);
18
- }
19
- return () => {
20
- if (handler && element) {
21
- element?.removeEventListener(type, handler);
22
- }
23
- };
24
- });
25
- const dispatch = (detail) => {
26
- const event = new CustomEvent(type, {
27
- bubbles: true,
28
- cancelable: true,
29
- composed: true,
30
- detail
31
- });
32
- ref.current?.dispatchEvent(event);
33
- };
34
- return [ref, dispatch];
35
- };
36
-
37
- // src/hooks/useEvent.ts
38
- var useEvent = (listener, sharableRef) => {
39
- const [ref, customDispatch] = useCustomEvent(
40
- "xyo",
41
- listener ? (detail) => {
42
- return listener(detail.noun, detail.verb, detail.data);
43
- } : void 0,
44
- sharableRef
45
- );
46
- const dispatch = (noun, verb, data) => {
47
- return customDispatch({ data, noun, verb });
48
- };
49
- return [ref, dispatch];
50
- };
51
- export {
52
- useCustomEvent,
53
- useEvent
54
- };
1
+ import{assertEx as E}from"@xylabs/assert";import{createRef as v,useEffect as p}from"react";var i=(e,a,s)=>{let r=s??v();return p(()=>{let t=r?.current,n=a,o=n?T=>{let c=E(T.detail,()=>"Event missing detail");n(c)&&T.stopPropagation()}:void 0;return o&&t&&t?.addEventListener(e,o),()=>{o&&t&&t?.removeEventListener(e,o)}}),[r,t=>{let n=new CustomEvent(e,{bubbles:!0,cancelable:!0,composed:!0,detail:t});r.current?.dispatchEvent(n)}]};var h=(e,a)=>{let[s,r]=i("xyo",e?t=>e(t.noun,t.verb,t.data):void 0,a);return[s,(t,n,o)=>r({data:o,noun:t,verb:n})]};export{i as useCustomEvent,h as useEvent};
55
2
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
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 =\n 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,KAAK;AACrB,UAAM,kBAAkB;AACxB,UAAM,UACJ,kBACE,CAAC,UAA4C;AAC3C,YAAM,SAAS,SAAS,MAAM,QAAQ,MAAM,sBAAsB;AAClE,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;;;AC7BO,IAAM,WAAW,CACtB,UACA,gBACuD;AACvD,QAAM,CAAC,KAAK,cAAc,IAAI;AAAA,IAC5B;AAAA,IACA,WACE,CAAC,WAAuC;AACtC,aAAO,SAAS,OAAO,MAAM,OAAO,MAAM,OAAO,IAAI;AAAA,IACvD,IACA;AAAA,IACF;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 =\n 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,OAAS,YAAAA,MAAgB,iBACzB,OAAS,aAAAC,EAAsB,aAAAC,MAAiB,QAIzC,IAAMC,EAAiB,CAC5BC,EACAC,EACAC,IACwD,CACxD,IAAMC,EAAMD,GAAaL,EAAoB,EAC7C,OAAAC,EAAU,IAAM,CACd,IAAMM,EAAUD,GAAK,QACfE,EAAkBJ,EAClBK,EACJD,EACGE,GAA4C,CAC3C,IAAMC,EAASZ,EAASW,EAAM,OAAQ,IAAM,sBAAsB,EACrDF,EAAgBG,CAAM,GAEjCD,EAAM,gBAAgB,CAE1B,EACA,OAEJ,OAAID,GAAWF,GACbA,GAAS,iBAAiBJ,EAAMM,CAAO,EAElC,IAAM,CACPA,GAAWF,GACbA,GAAS,oBAAoBJ,EAAMM,CAAO,CAE9C,CACF,CAAC,EAUM,CAACH,EATwCK,GAAqB,CACnE,IAAMD,EAAQ,IAAI,YAAqBP,EAAM,CAC3C,QAAS,GACT,WAAY,GACZ,SAAU,GACV,OAAAQ,CACF,CAAC,EACDL,EAAI,SAAS,cAAcI,CAAK,CAClC,CACqB,CACvB,EC7BO,IAAME,EAAW,CACtBC,EACAC,IACuD,CACvD,GAAM,CAACC,EAAKC,CAAc,EAAIC,EAC5B,MACAJ,EACGK,GACQL,EAASK,EAAO,KAAMA,EAAO,KAAMA,EAAO,IAAI,EAEvD,OACFJ,CACF,EAKA,MAAO,CAACC,EAH6C,CAACI,EAAaC,EAAaC,IACvEL,EAAe,CAAE,KAAAK,EAAM,KAAAF,EAAM,KAAAC,CAAK,CAAC,CAEvB,CACvB","names":["assertEx","createRef","useEffect","useCustomEvent","type","listener","customRef","ref","element","currentListener","handler","event","detail","useEvent","listener","sharableRef","ref","customDispatch","useCustomEvent","detail","noun","verb","data"]}
@@ -1,84 +1,2 @@
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/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- useCustomEvent: () => useCustomEvent,
24
- useEvent: () => useEvent
25
- });
26
- module.exports = __toCommonJS(src_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
- });
1
+ "use strict";var u=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var m=(e,n)=>{for(var r in n)u(e,r,{get:n[r],enumerable:!0})},f=(e,n,r,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let s of D(n))!d.call(e,s)&&s!==r&&u(e,s,{get:()=>n[s],enumerable:!(o=b(n,s))||o.enumerable});return e};var l=e=>f(u({},"__esModule",{value:!0}),e);var N={};m(N,{useCustomEvent:()=>c,useEvent:()=>h});module.exports=l(N);var v=require("@xylabs/assert"),i=require("react"),c=(e,n,r)=>{let o=r??(0,i.createRef)();return(0,i.useEffect)(()=>{let t=o==null?void 0:o.current,T=n,a=T?E=>{let p=(0,v.assertEx)(E.detail,()=>"Event missing detail");T(p)&&E.stopPropagation()}:void 0;return a&&t&&(t==null||t.addEventListener(e,a)),()=>{a&&t&&(t==null||t.removeEventListener(e,a))}}),[o,t=>{var a;let T=new CustomEvent(e,{bubbles:!0,cancelable:!0,composed:!0,detail:t});(a=o.current)==null||a.dispatchEvent(T)}]};var h=(e,n)=>{let[r,o]=c("xyo",e?t=>e(t.noun,t.verb,t.data):void 0,n);return[r,(t,T,a)=>o({data:a,noun:t,verb:T})]};0&&(module.exports={useCustomEvent,useEvent});
84
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts","../../src/hooks/useCustomEvent.ts","../../src/hooks/useEvent.ts"],"sourcesContent":["export * from './hooks'\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 =\n 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,UACJ,kBACE,CAAC,UAA4C;AAC3C,YAAM,aAAS,wBAAS,MAAM,QAAQ,MAAM,sBAAsB;AAClE,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;AAlCvE;AAmCI,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;;;AC7BO,IAAM,WAAW,CACtB,UACA,gBACuD;AACvD,QAAM,CAAC,KAAK,cAAc,IAAI;AAAA,IAC5B;AAAA,IACA,WACE,CAAC,WAAuC;AACtC,aAAO,SAAS,OAAO,MAAM,OAAO,MAAM,OAAO,IAAI;AAAA,IACvD,IACA;AAAA,IACF;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/index.ts","../../src/hooks/useCustomEvent.ts","../../src/hooks/useEvent.ts"],"sourcesContent":["export * from './hooks'\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 =\n 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":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,aAAAC,IAAA,eAAAC,EAAAJ,GCAA,IAAAK,EAAyB,0BACzBC,EAAgD,iBAInCC,EAAiB,CAC5BC,EACAC,EACAC,IACwD,CACxD,IAAMC,EAAMD,MAAa,aAAoB,EAC7C,sBAAU,IAAM,CACd,IAAME,EAAUD,GAAA,YAAAA,EAAK,QACfE,EAAkBJ,EAClBK,EACJD,EACGE,GAA4C,CAC3C,IAAMC,KAAS,YAASD,EAAM,OAAQ,IAAM,sBAAsB,EACrDF,EAAgBG,CAAM,GAEjCD,EAAM,gBAAgB,CAE1B,EACA,OAEJ,OAAID,GAAWF,IACbA,GAAA,MAAAA,EAAS,iBAAiBJ,EAAMM,IAE3B,IAAM,CACPA,GAAWF,IACbA,GAAA,MAAAA,EAAS,oBAAoBJ,EAAMM,GAEvC,CACF,CAAC,EAUM,CAACH,EATwCK,GAAqB,CAlCvE,IAAAC,EAmCI,IAAMF,EAAQ,IAAI,YAAqBP,EAAM,CAC3C,QAAS,GACT,WAAY,GACZ,SAAU,GACV,OAAAQ,CACF,CAAC,GACDC,EAAAN,EAAI,UAAJ,MAAAM,EAAa,cAAcF,EAC7B,CACqB,CACvB,EC7BO,IAAMG,EAAW,CACtBC,EACAC,IACuD,CACvD,GAAM,CAACC,EAAKC,CAAc,EAAIC,EAC5B,MACAJ,EACGK,GACQL,EAASK,EAAO,KAAMA,EAAO,KAAMA,EAAO,IAAI,EAEvD,OACFJ,CACF,EAKA,MAAO,CAACC,EAH6C,CAACI,EAAaC,EAAaC,IACvEL,EAAe,CAAE,KAAAK,EAAM,KAAAF,EAAM,KAAAC,CAAK,CAAC,CAEvB,CACvB","names":["src_exports","__export","useCustomEvent","useEvent","__toCommonJS","import_assert","import_react","useCustomEvent","type","listener","customRef","ref","element","currentListener","handler","event","detail","_a","useEvent","listener","sharableRef","ref","customDispatch","useCustomEvent","detail","noun","verb","data"]}
@@ -1,56 +1,2 @@
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
55
- };
1
+ import{assertEx as E}from"@xylabs/assert";import{createRef as v,useEffect as p}from"react";var i=(o,r,s)=>{let n=s??v();return p(()=>{let t=n==null?void 0:n.current,a=r,e=a?T=>{let c=E(T.detail,()=>"Event missing detail");a(c)&&T.stopPropagation()}:void 0;return e&&t&&(t==null||t.addEventListener(o,e)),()=>{e&&t&&(t==null||t.removeEventListener(o,e))}}),[n,t=>{var e;let a=new CustomEvent(o,{bubbles:!0,cancelable:!0,composed:!0,detail:t});(e=n.current)==null||e.dispatchEvent(a)}]};var h=(o,r)=>{let[s,n]=i("xyo",o?t=>o(t.noun,t.verb,t.data):void 0,r);return[s,(t,a,e)=>n({data:e,noun:t,verb:a})]};export{i as useCustomEvent,h as useEvent};
56
2
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
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 =\n 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,UACJ,kBACE,CAAC,UAA4C;AAC3C,YAAM,SAAS,SAAS,MAAM,QAAQ,MAAM,sBAAsB;AAClE,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;AAlCvE;AAmCI,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;;;AC7BO,IAAM,WAAW,CACtB,UACA,gBACuD;AACvD,QAAM,CAAC,KAAK,cAAc,IAAI;AAAA,IAC5B;AAAA,IACA,WACE,CAAC,WAAuC;AACtC,aAAO,SAAS,OAAO,MAAM,OAAO,MAAM,OAAO,IAAI;AAAA,IACvD,IACA;AAAA,IACF;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 =\n 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,OAAS,YAAAA,MAAgB,iBACzB,OAAS,aAAAC,EAAsB,aAAAC,MAAiB,QAIzC,IAAMC,EAAiB,CAC5BC,EACAC,EACAC,IACwD,CACxD,IAAMC,EAAMD,GAAaL,EAAoB,EAC7C,OAAAC,EAAU,IAAM,CACd,IAAMM,EAAUD,GAAA,YAAAA,EAAK,QACfE,EAAkBJ,EAClBK,EACJD,EACGE,GAA4C,CAC3C,IAAMC,EAASZ,EAASW,EAAM,OAAQ,IAAM,sBAAsB,EACrDF,EAAgBG,CAAM,GAEjCD,EAAM,gBAAgB,CAE1B,EACA,OAEJ,OAAID,GAAWF,IACbA,GAAA,MAAAA,EAAS,iBAAiBJ,EAAMM,IAE3B,IAAM,CACPA,GAAWF,IACbA,GAAA,MAAAA,EAAS,oBAAoBJ,EAAMM,GAEvC,CACF,CAAC,EAUM,CAACH,EATwCK,GAAqB,CAlCvE,IAAAC,EAmCI,IAAMF,EAAQ,IAAI,YAAqBP,EAAM,CAC3C,QAAS,GACT,WAAY,GACZ,SAAU,GACV,OAAAQ,CACF,CAAC,GACDC,EAAAN,EAAI,UAAJ,MAAAM,EAAa,cAAcF,EAC7B,CACqB,CACvB,EC7BO,IAAMG,EAAW,CACtBC,EACAC,IACuD,CACvD,GAAM,CAACC,EAAKC,CAAc,EAAIC,EAC5B,MACAJ,EACGK,GACQL,EAASK,EAAO,KAAMA,EAAO,KAAMA,EAAO,IAAI,EAEvD,OACFJ,CACF,EAKA,MAAO,CAACC,EAH6C,CAACI,EAAaC,EAAaC,IACvEL,EAAe,CAAE,KAAAK,EAAM,KAAAF,EAAM,KAAAC,CAAK,CAAC,CAEvB,CACvB","names":["assertEx","createRef","useEffect","useCustomEvent","type","listener","customRef","ref","element","currentListener","handler","event","detail","_a","useEvent","listener","sharableRef","ref","customDispatch","useCustomEvent","detail","noun","verb","data"]}
package/package.json CHANGED
@@ -13,7 +13,7 @@
13
13
  "@xylabs/assert": "^3.5.1"
14
14
  },
15
15
  "devDependencies": {
16
- "@xylabs/ts-scripts-yarn3": "^3.10.4",
16
+ "@xylabs/ts-scripts-yarn3": "^3.11.7",
17
17
  "typescript": "^5.4.5"
18
18
  },
19
19
  "peerDependencies": {
@@ -71,7 +71,6 @@
71
71
  },
72
72
  "sideEffects": false,
73
73
  "types": "dist/browser/index.d.ts",
74
- "version": "2.77.0-rc.1",
75
- "type": "module",
76
- "stableVersion": "2.76.6"
74
+ "version": "2.77.1",
75
+ "type": "module"
77
76
  }