@wordpress/components
Version:
UI components for WordPress.
105 lines (101 loc) • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _element = require("@wordpress/element");
var _compose = require("@wordpress/compose");
var _anglePickerControlStyles = require("./styles/angle-picker-control-styles");
var _jsxRuntime = require("react/jsx-runtime");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
function AngleCircle({
value,
onChange,
...props
}) {
const angleCircleRef = (0, _element.useRef)(null);
const angleCircleCenterRef = (0, _element.useRef)();
const previousCursorValueRef = (0, _element.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 === 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?.focus();
if (angleCircleCenterRef.current !== undefined && onChange !== undefined) {
const {
x: centerX,
y: centerY
} = angleCircleCenterRef.current;
onChange(getAngle(centerX, centerY, event.clientX, event.clientY));
}
};
const {
startDrag,
isDragging
} = (0, _compose.__experimentalUseDragging)({
onDragStart: event => {
setAngleCircleCenter();
changeAngleToPosition(event);
},
onDragMove: changeAngleToPosition,
onDragEnd: changeAngleToPosition
});
(0, _element.useEffect)(() => {
if (isDragging) {
if (previousCursorValueRef.current === undefined) {
previousCursorValueRef.current = document.body.style.cursor;
}
document.body.style.cursor = 'grabbing';
} else {
document.body.style.cursor = previousCursorValueRef.current || '';
previousCursorValueRef.current = undefined;
}
}, [isDragging]);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_anglePickerControlStyles.CircleRoot, {
ref: angleCircleRef,
onMouseDown: startDrag,
className: "components-angle-picker-control__angle-circle",
...props,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_anglePickerControlStyles.CircleIndicatorWrapper, {
style: value ? {
transform: `rotate(${value}deg)`
} : undefined,
className: "components-angle-picker-control__angle-circle-indicator-wrapper",
tabIndex: -1,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_anglePickerControlStyles.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 _default = exports.default = AngleCircle;
//# sourceMappingURL=angle-circle.js.map