react-dom-event
Version:
React context for subscribing to all DOM user interaction events
1 lines • 2.95 kB
Source Map (JSON)
{"version":3,"file":"react-dom-event.min.cjs","sources":["../../src/index.ts"],"sourcesContent":["import type { ReactNode } from 'react';\nimport { createContext, createElement, useContext, useEffect, useState } from 'react';\n\nexport type EventTypes = MouseEvent | TouchEvent | KeyboardEvent;\nexport type HandlerType = (event: EventTypes) => void;\n\nexport type EventContextType = {\n subscribe: (handler: HandlerType) => void;\n};\n\nexport const EventContext = createContext<EventContextType | undefined>(undefined);\n\nexport type EventProviderProps = {\n events?: string[];\n children?: ReactNode;\n};\nexport function EventProvider({ events = ['click'], children }: EventProviderProps) {\n const state = useState<HandlerType[]>([]);\n const handlers = state[0]; // reduce transpiled array helpers\n\n function onEvent(event: EventTypes) {\n handlers.forEach((handler: HandlerType) => handler(event));\n }\n function subscribe(handler: HandlerType) {\n handlers.push(handler);\n return () => handlers.splice(handlers.indexOf(handler), 1);\n }\n\n useEffect(() => {\n events.forEach((event) => window.document.addEventListener(event, onEvent, true));\n\n return () => events.forEach((event) => window.document.removeEventListener(event, onEvent, true));\n });\n\n return createElement(\n EventContext.Provider,\n {\n value: {\n subscribe,\n },\n },\n children\n );\n}\n\nexport function useEvent(handler: HandlerType, dependencies: unknown[]) {\n const context = useContext(EventContext);\n if (!context) {\n throw new Error('react-dom-event: subscribe not found on context. You might be missing the EventProvider or have multiple instances of react-dom-event');\n }\n\n useEffect(() => context.subscribe(handler), [context.subscribe, handler, ...dependencies]);\n}\n"],"names":["EventContext","createContext","undefined","param","_param_events","events","children","handlers","useState","onEvent","event","forEach","handler","useEffect","window","document","addEventListener","removeEventListener","createElement","Provider","value","subscribe","push","splice","indexOf","dependencies","context","useContext","Error","concat","_to_consumable_array"],"mappings":"qhCAUO,IAAMA,EAAeC,EAAAA,mBAA4CC,oCAMjE,SAAuBC,GAAA,IAAAC,EAAAD,EAAEE,OAAAA,OAAAA,IAAAA,EAAS,CAAC,SAAQD,EAAEE,EAAtBH,EAAsBG,SAE5CC,EADQC,EAAAA,SAAwB,IACf,GAEvB,SAASC,EAAQC,GACfH,EAASI,QAAQ,SAACC,UAAyBA,EAAQF,IACrD,CAYA,OANAG,EAAAA,UAAU,WAGR,OAFAR,EAAOM,QAAQ,SAACD,GAAUI,OAAAA,OAAOC,SAASC,iBAAiBN,EAAOD,GAAS,KAEpE,kBAAMJ,EAAOM,QAAQ,SAACD,GAAUI,OAAAA,OAAOC,SAASE,oBAAoBP,EAAOD,GAAS,KAC7F,GAEOS,EAAAA,cACLlB,EAAamB,SACb,CACEC,MAAO,CACLC,UAfN,SAAmBT,GAEjB,OADAL,EAASe,KAAKV,GACP,WAAML,OAAAA,EAASgB,OAAOhB,EAASiB,QAAQZ,GAAU,GAC1D,IAeEN,EAEJ,aAEO,SAAkBM,EAAsBa,GAC7C,IAAMC,EAAUC,EAAAA,WAAW3B,GAC3B,IAAK0B,EACH,MAAM,IAAIE,MAAM,yIAGlBf,EAAAA,UAAU,WAAMa,OAAAA,EAAQL,UAAUT,EAAU,EAAA,CAACc,EAAQL,UAAWT,GAApBiB,OAA6BC,EAAGL,IAC9E"}