@wordpress/components
Version:
UI components for WordPress.
105 lines (104 loc) • 4.22 kB
JavaScript
import clsx from "clsx";
import { useReducer, useRef } from "@wordpress/element";
import ControlPoints from "./control-points.mjs";
import { getHorizontalRelativeGradientPosition } from "./utils.mjs";
import { MINIMUM_DISTANCE_BETWEEN_INSERTER_AND_POINT } from "./constants.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
// packages/components/src/custom-gradient-picker/gradient-bar/index.tsx
const customGradientBarReducer = (state, action) => {
switch (action.type) {
case "MOVE_INSERTER":
if (state.id === "IDLE" || state.id === "MOVING_INSERTER") return {
id: "MOVING_INSERTER",
insertPosition: action.insertPosition
};
break;
case "STOP_INSERTER_MOVE":
if (state.id === "MOVING_INSERTER") return { id: "IDLE" };
break;
case "OPEN_INSERTER":
if (state.id === "MOVING_INSERTER") return {
id: "INSERTING_CONTROL_POINT",
insertPosition: state.insertPosition
};
break;
case "CLOSE_INSERTER":
if (state.id === "INSERTING_CONTROL_POINT") return { id: "IDLE" };
break;
case "START_CONTROL_CHANGE":
if (state.id === "IDLE") return { id: "MOVING_CONTROL_POINT" };
break;
case "STOP_CONTROL_CHANGE": if (state.id === "MOVING_CONTROL_POINT") return { id: "IDLE" };
}
return state;
};
const customGradientBarReducerInitialState = { id: "IDLE" };
function CustomGradientBar({ background, hasGradient, value: controlPoints, onChange, disableInserter = false, disableAlpha = false, disablePositioning = false, __experimentalIsRenderedInSidebar = false }) {
const gradientMarkersContainerDomRef = useRef(null);
const [gradientBarState, gradientBarStateDispatch] = useReducer(customGradientBarReducer, customGradientBarReducerInitialState);
const onMouseEnterAndMove = (event) => {
if (!gradientMarkersContainerDomRef.current) return;
const insertPosition = getHorizontalRelativeGradientPosition(event.clientX, gradientMarkersContainerDomRef.current);
if (controlPoints.some(({ position }) => {
return Math.abs(insertPosition - position) < MINIMUM_DISTANCE_BETWEEN_INSERTER_AND_POINT;
})) {
if (gradientBarState.id === "MOVING_INSERTER") gradientBarStateDispatch({ type: "STOP_INSERTER_MOVE" });
return;
}
gradientBarStateDispatch({
type: "MOVE_INSERTER",
insertPosition
});
};
const onMouseLeave = () => {
gradientBarStateDispatch({ type: "STOP_INSERTER_MOVE" });
};
const isMovingInserter = gradientBarState.id === "MOVING_INSERTER";
const isInsertingControlPoint = gradientBarState.id === "INSERTING_CONTROL_POINT";
return /* @__PURE__ */ jsxs("div", {
className: clsx("components-custom-gradient-picker__gradient-bar", { "has-gradient": hasGradient }),
onMouseEnter: onMouseEnterAndMove,
onMouseMove: onMouseEnterAndMove,
onMouseLeave,
children: [/* @__PURE__ */ jsx("div", {
className: "components-custom-gradient-picker__gradient-bar-background",
style: {
background,
opacity: hasGradient ? 1 : .4
}
}), /* @__PURE__ */ jsxs("div", {
ref: gradientMarkersContainerDomRef,
className: "components-custom-gradient-picker__markers-container",
children: [!disableInserter && (isMovingInserter || isInsertingControlPoint) && /* @__PURE__ */ jsx(ControlPoints.InsertPoint, {
__experimentalIsRenderedInSidebar,
disableAlpha,
insertPosition: gradientBarState.insertPosition,
value: controlPoints,
onChange,
onOpenInserter: () => {
gradientBarStateDispatch({ type: "OPEN_INSERTER" });
},
onCloseInserter: () => {
gradientBarStateDispatch({ type: "CLOSE_INSERTER" });
}
}), /* @__PURE__ */ jsx(ControlPoints, {
__experimentalIsRenderedInSidebar,
disableAlpha,
disableRemove: disableInserter,
disablePositioning,
gradientPickerDomRef: gradientMarkersContainerDomRef,
ignoreMarkerPosition: isInsertingControlPoint ? gradientBarState.insertPosition : void 0,
value: controlPoints,
onChange,
onStartControlPointChange: () => {
gradientBarStateDispatch({ type: "START_CONTROL_CHANGE" });
},
onStopControlPointChange: () => {
gradientBarStateDispatch({ type: "STOP_CONTROL_CHANGE" });
}
})]
})]
});
}
export { CustomGradientBar as default };
//# sourceMappingURL=index.mjs.map