UNPKG

@wordpress/block-library

Version:
90 lines (81 loc) 2.68 kB
import { createElement } from "@wordpress/element"; /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; import { InspectorControls, useSetting } from '@wordpress/block-editor'; import { BaseControl, PanelBody, __experimentalUseCustomUnits as useCustomUnits, __experimentalUnitControl as UnitControl, __experimentalParseQuantityAndUnitFromRawValue as parseQuantityAndUnitFromRawValue } from '@wordpress/components'; import { useInstanceId } from '@wordpress/compose'; /** * Internal dependencies */ import { MIN_SPACER_SIZE } from './constants'; function DimensionInput(_ref) { var _ref2; let { label, onChange, isResizing, value = '' } = _ref; const inputId = useInstanceId(UnitControl, 'block-spacer-height-input'); // In most contexts the spacer size cannot meaningfully be set to a // percentage, since this is relative to the parent container. This // unit is disabled from the UI. const availableUnitSettings = (_ref2 = useSetting('spacing.units') || undefined) === null || _ref2 === void 0 ? void 0 : _ref2.filter(availableUnit => availableUnit !== '%'); const units = useCustomUnits({ availableUnits: availableUnitSettings || ['px', 'em', 'rem', 'vw', 'vh'], defaultValues: { px: 100, em: 10, rem: 10, vw: 10, vh: 25 } }); const handleOnChange = unprocessedValue => { onChange(unprocessedValue); }; // Force the unit to update to `px` when the Spacer is being resized. const [parsedQuantity, parsedUnit] = parseQuantityAndUnitFromRawValue(value); const computedValue = [parsedQuantity, isResizing ? 'px' : parsedUnit].join(''); return createElement(BaseControl, { label: label, id: inputId }, createElement(UnitControl, { id: inputId, isResetValueOnUnitChange: true, min: MIN_SPACER_SIZE, onChange: handleOnChange, style: { maxWidth: 80 }, value: computedValue, units: units })); } export default function SpacerControls(_ref3) { let { setAttributes, orientation, height, width, isResizing } = _ref3; return createElement(InspectorControls, null, createElement(PanelBody, { title: __('Settings') }, orientation === 'horizontal' && createElement(DimensionInput, { label: __('Width'), value: width, onChange: nextWidth => setAttributes({ width: nextWidth }), isResizing: isResizing }), orientation !== 'horizontal' && createElement(DimensionInput, { label: __('Height'), value: height, onChange: nextHeight => setAttributes({ height: nextHeight }), isResizing: isResizing }))); } //# sourceMappingURL=controls.js.map