UNPKG

recharts

Version:
74 lines (71 loc) • 2.55 kB
import { createSlice } from '@reduxjs/toolkit'; import { clampViewport, FULL_VIEWPORT, viewportsEqual } from '../util/zoom/viewport'; /** * The two spatial dimensions a cartesian chart can be zoomed along. * `x` is shared by every horizontal axis, `y` by every vertical axis. */ /** * Live zoom/pan state for a cartesian chart, expressed as a normalized {@link AxisViewport} * per dimension. * * The default value is the full viewport on both dimensions, i.e. no zoom, which is why this * slice is completely inert until something dispatches a zoom action: every selector derived * from it sees "not zoomed" and the chart renders exactly as it does today. * * This slice intentionally holds only the *geometric* state. Configuration that comes from * props (whether zoom is enabled, min/max scale, drag-to-zoom, ...) lives elsewhere so the * runtime state and the user configuration stay decoupled. */ var initialState = { x: FULL_VIEWPORT, y: FULL_VIEWPORT }; var zoomSlice = createSlice({ name: 'zoom', initialState, reducers: { /** * Replaces the viewport of a single dimension. The incoming viewport is normalized so * callers never have to worry about producing an out-of-range or inverted window. */ setAxisViewport(state, action) { var _action$payload = action.payload, dimension = _action$payload.dimension, viewport = _action$payload.viewport; var next = clampViewport(viewport); if (!viewportsEqual(state[dimension], next)) { state[dimension] = next; } }, /** * Replaces both viewports at once, e.g. when restoring a saved zoom or syncing from a brush. */ setZoom(state, action) { var nextX = clampViewport(action.payload.x); var nextY = clampViewport(action.payload.y); if (!viewportsEqual(state.x, nextX)) { state.x = nextX; } if (!viewportsEqual(state.y, nextY)) { state.y = nextY; } }, /** * Resets the chart back to the fully zoomed-out state. */ resetZoom(state) { if (!viewportsEqual(state.x, FULL_VIEWPORT)) { state.x = FULL_VIEWPORT; } if (!viewportsEqual(state.y, FULL_VIEWPORT)) { state.y = FULL_VIEWPORT; } } } }); var _zoomSlice$actions = zoomSlice.actions, setAxisViewport = _zoomSlice$actions.setAxisViewport, setZoom = _zoomSlice$actions.setZoom, resetZoom = _zoomSlice$actions.resetZoom; export { setAxisViewport, setZoom, resetZoom }; export var zoomReducer = zoomSlice.reducer;