UNPKG

react-plot

Version:

Library of React components to render SVG 2D plots.

42 lines 1.79 kB
import { useEffect, useRef } from 'react'; import { createNestableContext } from './nestableContext.js'; import { initialPlotOverridesState } from './usePlotOverrides.js'; export const plotOverridesContext = createNestableContext(initialPlotOverridesState); export function usePlotOverrides(options) { return plotOverridesContext.useNestedContext(options?.controllerId); } export const plotControlsContext = createNestableContext(null); export function usePlotControls(options) { const id = options?.controllerId; const plotControls = plotControlsContext.useNestedContext(id); if (!plotControls) { throw new Error(`usePlotControls must be called in a child of PlotController (id=${String(id)})`); } return plotControls; } export const plotEventsUserContext = createNestableContext(null); export function usePlotEvents(handlers, options = {}) { const { controllerId: id, disabled = false } = options; const userContext = plotEventsUserContext.useNestedContext(id); if (!disabled && !userContext) { throw new Error(`usePlotEvents must be called in a child of PlotController (id=${String(id)})`); } const ref = useRef(handlers); useEffect(() => { if (!disabled) { ref.current = handlers; } }, [disabled, handlers]); useEffect(() => { if (!disabled && userContext) { userContext.registerHandlers(ref); return () => userContext.unregisterHandlers(ref); } return undefined; }, [disabled, userContext]); } export const plotEventsPlotContext = createNestableContext(null); export function usePlotEventsPlotContext(options) { return plotEventsPlotContext.useNestedContext(options?.controllerId); } //# sourceMappingURL=plotControllerContext.js.map