UNPKG

@wordpress/components

Version:
91 lines (90 loc) 2.8 kB
import { useEffect, useRef } from "@wordpress/element"; import { __experimentalUseDragging as useDragging } from "@wordpress/compose"; import { CircleRoot, CircleIndicatorWrapper, CircleIndicator } from "./styles/angle-picker-control-styles"; import { jsx as _jsx } from "react/jsx-runtime"; function AngleCircle({ value, onChange, ...props }) { const angleCircleRef = useRef(null); const angleCircleCenterRef = useRef(); const previousCursorValueRef = useRef(); const setAngleCircleCenter = () => { if (angleCircleRef.current === null) { return; } const rect = angleCircleRef.current.getBoundingClientRect(); angleCircleCenterRef.current = { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 }; }; const changeAngleToPosition = (event) => { if (event === void 0) { return; } event.preventDefault(); event.target?.focus(); if (angleCircleCenterRef.current !== void 0 && onChange !== void 0) { const { x: centerX, y: centerY } = angleCircleCenterRef.current; onChange(getAngle(centerX, centerY, event.clientX, event.clientY)); } }; const { startDrag, isDragging } = useDragging({ onDragStart: (event) => { setAngleCircleCenter(); changeAngleToPosition(event); }, onDragMove: changeAngleToPosition, onDragEnd: changeAngleToPosition }); useEffect(() => { if (isDragging) { if (previousCursorValueRef.current === void 0) { previousCursorValueRef.current = document.body.style.cursor; } document.body.style.cursor = "grabbing"; } else { document.body.style.cursor = previousCursorValueRef.current || ""; previousCursorValueRef.current = void 0; } }, [isDragging]); return /* @__PURE__ */ _jsx(CircleRoot, { ref: angleCircleRef, onMouseDown: startDrag, className: "components-angle-picker-control__angle-circle", ...props, children: /* @__PURE__ */ _jsx(CircleIndicatorWrapper, { style: value ? { transform: `rotate(${value}deg)` } : void 0, className: "components-angle-picker-control__angle-circle-indicator-wrapper", tabIndex: -1, children: /* @__PURE__ */ _jsx(CircleIndicator, { className: "components-angle-picker-control__angle-circle-indicator" }) }) }); } function getAngle(centerX, centerY, pointX, pointY) { const y = pointY - centerY; const x = pointX - centerX; const angleInRadians = Math.atan2(y, x); const angleInDeg = Math.round(angleInRadians * (180 / Math.PI)) + 90; if (angleInDeg < 0) { return 360 + angleInDeg; } return angleInDeg; } var angle_circle_default = AngleCircle; export { angle_circle_default as default }; //# sourceMappingURL=angle-circle.js.map