@brendonhudnell/react-hexgrid
Version:
Interactive hexagon grids with React bindings
30 lines (29 loc) • 1.7 kB
JavaScript
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import clsx from 'clsx';
import { forwardRef, useContext, useEffect, useMemo, useState } from 'react';
import HexUtils from '../HexUtils';
import { LayoutContext } from '../Layout';
import Hex from '../models/Hex';
const Hexagon = forwardRef((props, ref) => {
const { q, r, s, fill, className, cellStyle, children } = props, otherProps = __rest(props, ["q", "r", "s", "fill", "className", "cellStyle", "children"]);
const { layout, points } = useContext(LayoutContext);
const [hex, setHex] = useState(new Hex(q, r, s));
const pixel = useMemo(() => HexUtils.hexToPixel(hex, layout), [hex, layout]);
const fillId = fill ? `url(#${fill})` : undefined;
useEffect(() => {
setHex(new Hex(q, r, s));
}, [q, r, s]);
return (_jsx("g", Object.assign({}, otherProps, { ref: ref, className: clsx('hexagon-group', className), transform: `translate(${pixel.x}, ${pixel.y})` }, { children: _jsxs("g", Object.assign({ className: "hexagon" }, { children: [_jsx("polygon", { points: points, fill: fillId, style: cellStyle }, void 0), children] }), void 0) }), void 0));
});
export default Hexagon;