UNPKG

react-plot

Version:

Library of React components to render SVG 2D plots.

50 lines 1.85 kB
import { useMemo, useRef } from 'react'; export function usePlotEventsState() { const plotEvents = useRef({ currentPlot: null, plots: new Set(), handlers: new Set(), }); const userActions = useMemo(() => ({ registerHandlers(handlersRef) { plotEvents.current.handlers.add(handlersRef); }, unregisterHandlers(handlersRef) { plotEvents.current.handlers.delete(handlersRef); }, }), []); const plotActions = useMemo(() => { return { registerPlot(plotId) { plotEvents.current.plots.add(plotId); }, unregisterPlot(plotId) { plotEvents.current.plots.delete(plotId); }, handleEvent(plotId, eventName, eventData) { if (!plotEvents.current.plots.has(plotId)) { return; } if (eventName === 'onPointerDown') { plotEvents.current.currentPlot = plotId; } if (eventName === 'onPointerUp') { plotEvents.current.currentPlot = null; } if (plotEvents.current.currentPlot !== null && plotEvents.current.currentPlot !== plotId) { // Ignore events on other plots. return; } for (const handler of plotEvents.current.handlers) { if (handler.current?.[eventName]) { // @ts-expect-error eventData is guaranteed to be compatible with eventName. handler.current[eventName](eventData); } } }, }; }, []); return { userActions, plotActions }; } //# sourceMappingURL=usePlotEvents.js.map