UNPKG

react-orbit-component

Version:
202 lines (195 loc) 7.06 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __objRest = (source, exclude) => { var target = {}; for (var prop in source) if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) target[prop] = source[prop]; if (source != null && __getOwnPropSymbols) for (var prop of __getOwnPropSymbols(source)) { if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) target[prop] = source[prop]; } return target; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { OrbitItem: () => OrbitItem, OrbitPath: () => OrbitPath, calculateCoordinatesOnCircle: () => calculateCoordinatesOnCircle }); module.exports = __toCommonJS(src_exports); // src/components/orbit.path.tsx var import_react2 = __toESM(require("react")); // src/hooks/useWindowSize.tsx var import_react = require("react"); var useWindowSize = () => { const [windowSize, setWindowSize] = (0, import_react.useState)({ width: void 0, height: void 0 }); const handleResize = () => { setWindowSize({ width: window.innerWidth, height: window.innerHeight }); }; (0, import_react.useEffect)(() => { window.addEventListener("resize", handleResize); handleResize(); return () => window.removeEventListener("resize", handleResize); }, []); return windowSize; }; // src/components/orbit.path.tsx var OrbitPath = ({ className, children, type, style }) => { const pathRef = (0, import_react2.useRef)(null); const [newChildren, setNewChildren] = (0, import_react2.useState)([]); const size = useWindowSize(); (0, import_react2.useEffect)(() => { if (!pathRef.current) return; const { offsetHeight: height, offsetWidth: width } = pathRef.current; if (width !== height) { console.error("OrbitPath must be a circle with equal height and width."); return; } const radius = height / 2; const childrenArray = import_react2.default.Children.toArray(children); const updatedChildren = childrenArray.map((child, index) => { const childElement = child; const newProps = __spreadProps(__spreadValues({}, childElement.props), { radius }); return import_react2.default.cloneElement(childElement, __spreadProps(__spreadValues({}, newProps), { key: index })); }); setNewChildren(updatedChildren); }, [children, size]); return /* @__PURE__ */ import_react2.default.createElement( "div", { style: { pointerEvents: "none" } && style, className, ref: pathRef }, newChildren ); }; // src/components/orbit.item.tsx var import_react3 = __toESM(require("react")); // src/utils/geometry.utils.ts var DEGREE_TO_RADIAN_FACTOR = Math.PI / 180; var calculateCoordinatesOnCircle = (radius, angle, boundingClientRect) => { const angleInRadians = angle * DEGREE_TO_RADIAN_FACTOR; const { width, height } = boundingClientRect; const x = radius + radius * Math.cos(angleInRadians); const y = radius + radius * Math.sin(angleInRadians); const centerX = x - width / 2; const centerY = y - height / 2; return { x: centerX, y: centerY }; }; // src/components/orbit.item.tsx var OrbitItem = (0, import_react3.forwardRef)( (_a, ref) => { var _b = _a, { className, children, radius = 0, anglePerStep = 0.1, timeBetweenSteps = 10, startAngle = 0, direction = "clockwise", style, angle } = _b, rest = __objRest(_b, [ "className", "children", "radius", "anglePerStep", "timeBetweenSteps", "startAngle", "direction", "style", "angle" ]); var _a2, _b2; const [angleState, setAngle] = (0, import_react3.useState)(startAngle); const effectiveAngle = angle != null ? angle : angleState; const internalRef = (0, import_react3.useRef)(null); const actualRef = ref != null ? ref : internalRef; (0, import_react3.useEffect)(() => { const interval = setInterval(() => { setAngle((prevAngle) => direction === "clockwise" ? prevAngle + anglePerStep % 360 : prevAngle - anglePerStep % 360); }, timeBetweenSteps); return () => clearInterval(interval); }, [anglePerStep, timeBetweenSteps]); const { x, y } = calculateCoordinatesOnCircle(radius, effectiveAngle, (_b2 = (_a2 = actualRef.current) == null ? void 0 : _a2.getBoundingClientRect()) != null ? _b2 : new DOMRect()); return /* @__PURE__ */ import_react3.default.createElement( "div", __spreadValues({ style: __spreadValues({ position: "absolute", top: `${y}px`, left: `${x}px`, pointerEvents: "all" }, style), className, ref: actualRef }, rest), children ); } ); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { OrbitItem, OrbitPath, calculateCoordinatesOnCircle }); //# sourceMappingURL=index.js.map