UNPKG

react-plot

Version:

Library of React components to render SVG 2D plots.

48 lines 1.9 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { produce } from 'immer'; import { useMemo, useReducer } from 'react'; import { legendContext } from './legendContext.js'; const legendReducer = produce((draft, action) => { switch (action.type) { case 'ADD_LEGEND_LABEL': { const { shape, ...newLegend } = action.payload; const index = draft.labels.findIndex(({ id }) => newLegend.id === id); if (index === -1) { draft.labels.push({ ...newLegend, shape, isVisible: true }); } else { //isVisible should only updated in TOGGLE_VISIBILITY const isVisible = draft.labels[index].isVisible; draft.labels[index] = { ...newLegend, isVisible, shape }; } return; } case 'REMOVE_LEGEND_LABEL': { const { id } = action.payload; const index = draft.labels.findIndex((val) => val.id === id); if (index !== -1) { draft.labels.splice(index, 1); } return; } case 'TOGGLE_VISIBILITY': { const { id } = action.payload; const index = draft.labels.findIndex((val) => val.id === id); if (index !== -1) { draft.labels[index].isVisible = !draft.labels[index].isVisible; } return; } default: throw new Error('unreachable'); } }); const initialLegendState = { labels: [], }; export const LegendProvider = (props) => { const [legendState, legendDispatch] = useReducer(legendReducer, initialLegendState); const ctx = useMemo(() => [legendState, legendDispatch], [legendState, legendDispatch]); return (_jsx(legendContext.Provider, { value: ctx, children: props.children })); }; //# sourceMappingURL=legendContext.provider.js.map