@macrostrat/column-components
Version:
React rendering primitives for stratigraphic columns
97 lines (96 loc) • 2.74 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const d3Scale = require("d3-scale");
const React = require("react");
const h = require("@macrostrat/hyper");
const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefault(React);
const h__default = /* @__PURE__ */ _interopDefault(h);
var ColumnAxisType = /* @__PURE__ */ ((ColumnAxisType2) => {
ColumnAxisType2["AGE"] = "age";
ColumnAxisType2["HEIGHT"] = "height";
ColumnAxisType2["DEPTH"] = "depth";
ColumnAxisType2["ORDINAL"] = "ordinal";
return ColumnAxisType2;
})(ColumnAxisType || {});
const ColumnContext = React.createContext({
scale: d3Scale.scaleLinear(),
divisions: [],
scaleClamped: d3Scale.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__default.default.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 = React.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 = d3Scale.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__default.default(ColumnContext.Provider, { value }, children);
}
const useColumn = () => React.useContext(ColumnContext);
const useColumnDivisions = () => React.useContext(ColumnContext).divisions;
exports.ColumnAxisType = ColumnAxisType;
exports.ColumnContext = ColumnContext;
exports.ColumnProvider = ColumnProvider;
exports.useColumn = useColumn;
exports.useColumnDivisions = useColumnDivisions;
//# sourceMappingURL=column.cjs.map