UNPKG

@wordpress/components

Version:
111 lines (94 loc) 3.12 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import { createElement } from "@wordpress/element"; /** * WordPress dependencies */ import { useEffect, useRef } from '@wordpress/element'; import { __experimentalUseDragging as useDragging } from '@wordpress/compose'; /** * Internal dependencies */ import { CircleRoot, CircleIndicatorWrapper, CircleIndicator } from './styles/angle-picker-control-styles'; function AngleCircle(_ref) { let { value, onChange, ...props } = _ref; const angleCircleRef = useRef(null); const angleCircleCenter = useRef(); const previousCursorValue = useRef(); const setAngleCircleCenter = () => { if (angleCircleRef.current === null) { return; } const rect = angleCircleRef.current.getBoundingClientRect(); angleCircleCenter.current = { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 }; }; const changeAngleToPosition = event => { var _event$target; if (event === undefined) { return; } // Prevent (drag) mouse events from selecting and accidentally // triggering actions from other elements. event.preventDefault(); // Input control needs to lose focus and by preventDefault above, it doesn't. (_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.focus(); if (angleCircleCenter.current !== undefined && onChange !== undefined) { const { x: centerX, y: centerY } = angleCircleCenter.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 (previousCursorValue.current === undefined) { previousCursorValue.current = document.body.style.cursor; } document.body.style.cursor = 'grabbing'; } else { document.body.style.cursor = previousCursorValue.current || ''; previousCursorValue.current = undefined; } }, [isDragging]); return createElement(CircleRoot, _extends({ ref: angleCircleRef, onMouseDown: startDrag, className: "components-angle-picker-control__angle-circle" }, props), createElement(CircleIndicatorWrapper, { style: value ? { transform: `rotate(${value}deg)` } : undefined, className: "components-angle-picker-control__angle-circle-indicator-wrapper", tabIndex: -1 }, createElement(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; } export default AngleCircle; //# sourceMappingURL=angle-circle.js.map