UNPKG

@wordpress/block-library

Version:
96 lines (95 loc) • 3.44 kB
import { __ } from "@wordpress/i18n"; import { InspectorControls, __experimentalSpacingSizesControl, isValueSpacingPreset, privateApis, useSettings } from "@wordpress/block-editor"; import { __experimentalParseQuantityAndUnitFromRawValue, __experimentalToolsPanel, __experimentalToolsPanelItem, __experimentalUnitControl, __experimentalUseCustomUnits } from "@wordpress/components"; import { useInstanceId } from "@wordpress/compose"; import { unlock } from "../lock-unlock.mjs"; import { MIN_SPACER_SIZE } from "./constants.mjs"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/spacer/controls.jsx const { useSpacingSizes } = unlock(privateApis); function DimensionInput({ label, onChange, isResizing, value = "" }) { const inputId = useInstanceId(__experimentalUnitControl, "block-spacer-height-input"); const spacingSizes = useSpacingSizes(); const [spacingUnits] = useSettings("spacing.units"); const availableUnits = spacingUnits ? spacingUnits.filter((unit) => unit !== "%") : [ "px", "em", "rem", "vw", "vh" ]; const units = __experimentalUseCustomUnits({ availableUnits, defaultValues: { px: 100, em: 10, rem: 10, vw: 10, vh: 25 } }); const [parsedQuantity, parsedUnit] = __experimentalParseQuantityAndUnitFromRawValue(value); const computedValue = isValueSpacingPreset(value) ? value : [parsedQuantity, isResizing ? "px" : parsedUnit].join(""); return /* @__PURE__ */ jsx(Fragment, { children: spacingSizes?.length < 2 ? /* @__PURE__ */ jsx(__experimentalUnitControl, { id: inputId, isResetValueOnUnitChange: true, min: MIN_SPACER_SIZE, onChange, value: computedValue, units, label }) : /* @__PURE__ */ jsx("div", { className: "tools-panel-item-spacing", children: /* @__PURE__ */ jsx(__experimentalSpacingSizesControl, { values: { all: computedValue }, onChange: ({ all }) => { onChange(all); }, label, sides: ["all"], units, allowReset: false, splitOnAxis: false, showSideInLabel: false }) }) }); } function SpacerControls({ setAttributes, orientation, height, width, isResizing }) { const dropdownMenuProps = useToolsPanelDropdownMenuProps(); return /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, { label: __("Settings"), resetAll: () => { setAttributes({ width: void 0, height: "100px" }); }, dropdownMenuProps, children: [orientation === "horizontal" && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { label: __("Width"), isShownByDefault: true, hasValue: () => width !== void 0, onDeselect: () => setAttributes({ width: void 0 }), children: /* @__PURE__ */ jsx(DimensionInput, { label: __("Width"), value: width, onChange: (nextWidth) => setAttributes({ width: nextWidth }), isResizing }) }), orientation !== "horizontal" && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { label: __("Height"), isShownByDefault: true, hasValue: () => height !== "100px", onDeselect: () => setAttributes({ height: "100px" }), children: /* @__PURE__ */ jsx(DimensionInput, { label: __("Height"), value: height, onChange: (nextHeight) => setAttributes({ height: nextHeight }), isResizing }) })] }) }); } export { SpacerControls as default }; //# sourceMappingURL=controls.mjs.map