UNPKG

@macrostrat/column-components

Version:

React rendering primitives for stratigraphic columns

94 lines (93 loc) 2.31 kB
import { scaleLinear } from "d3-scale"; import React, { createContext, useMemo, useContext } from "react"; import h from "@macrostrat/hyper"; var ColumnAxisType = /* @__PURE__ */ ((ColumnAxisType2) => { ColumnAxisType2["AGE"] = "age"; ColumnAxisType2["HEIGHT"] = "height"; ColumnAxisType2["DEPTH"] = "depth"; ColumnAxisType2["ORDINAL"] = "ordinal"; return ColumnAxisType2; })(ColumnAxisType || {}); const ColumnContext = createContext({ scale: scaleLinear(), divisions: [], scaleClamped: scaleLinear().clamp(true), pixelsPerMeter: 1, zoom: 1 }); function ColumnProvider(props) { let { children, pixelsPerMeter = 20, zoom = 1, height, range, divisions = [], width = 150, axisType = "height", scale: _scale, ...rest } = props; const restStr = JSON.stringify(rest); const restRef = React.useRef(null); if (restStr !== restRef.current) { restRef.current = restStr; if (Object.keys(rest).length > 0) { console.warn( "Passing extra properties to ColumnProvider is deprecated:", rest ); } } const value = useMemo(() => { if (range != null) { height = Math.abs(range[1] - range[0]); } else { range = [0, height]; } let scale = _scale; let pixelHeight; if (scale == null) { pixelHeight = height * pixelsPerMeter * zoom; scale = scaleLinear().domain(range).range([pixelHeight, 0]); } else { pixelHeight = Math.abs(scale.range()[1] - scale.range()[0]); const r1 = scale.range().map((d) => d - scale.range()[0]); scale = _scale.copy().range(r1); } const scaleClamped = scale.copy().clamp(true); return { pixelsPerMeter, pixelHeight, zoom, range, height, scale, scaleClamped, divisions, width, axisType, ...rest }; }, [ axisType, height, pixelsPerMeter, range, zoom, divisions, width, restRef.current ]); return h(ColumnContext.Provider, { value }, children); } const useColumn = () => useContext(ColumnContext); const useColumnDivisions = () => useContext(ColumnContext).divisions; export { ColumnAxisType, ColumnContext, ColumnProvider, useColumn, useColumnDivisions }; //# sourceMappingURL=column.js.map