UNPKG

react-orbit-component

Version:
167 lines (161 loc) 5.34 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; 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; }; // src/components/orbit.path.tsx import React, { useEffect as useEffect2, useRef, useState as useState2 } from "react"; // src/hooks/useWindowSize.tsx import { useEffect, useState } from "react"; var useWindowSize = () => { const [windowSize, setWindowSize] = useState({ width: void 0, height: void 0 }); const handleResize = () => { setWindowSize({ width: window.innerWidth, height: window.innerHeight }); }; 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 = useRef(null); const [newChildren, setNewChildren] = useState2([]); const size = useWindowSize(); useEffect2(() => { 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 = React.Children.toArray(children); const updatedChildren = childrenArray.map((child, index) => { const childElement = child; const newProps = __spreadProps(__spreadValues({}, childElement.props), { radius }); return React.cloneElement(childElement, __spreadProps(__spreadValues({}, newProps), { key: index })); }); setNewChildren(updatedChildren); }, [children, size]); return /* @__PURE__ */ React.createElement( "div", { style: { pointerEvents: "none" } && style, className, ref: pathRef }, newChildren ); }; // src/components/orbit.item.tsx import React2, { useEffect as useEffect3, useRef as useRef2, forwardRef, useState as useState3 } from "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 = 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] = useState3(startAngle); const effectiveAngle = angle != null ? angle : angleState; const internalRef = useRef2(null); const actualRef = ref != null ? ref : internalRef; useEffect3(() => { 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__ */ React2.createElement( "div", __spreadValues({ style: __spreadValues({ position: "absolute", top: `${y}px`, left: `${x}px`, pointerEvents: "all" }, style), className, ref: actualRef }, rest), children ); } ); export { OrbitItem, OrbitPath, calculateCoordinatesOnCircle }; //# sourceMappingURL=index.mjs.map