UNPKG

@wordpress/components

Version:
93 lines (85 loc) 2.3 kB
import { createElement, Fragment } from "@wordpress/element"; /** * External dependencies */ import classnames from 'classnames'; /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ import Icon from '../icon'; import SelectControl from '../select-control'; import sizesTable, { findSizeBySlug } from './sizes'; /** * `DimensionControl` is a component designed to provide a UI to control spacing and/or dimensions. * * This feature is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes. * * ```jsx * import { __experimentalDimensionControl as DimensionControl } from '@wordpress/components'; * import { useState } from '@wordpress/element'; * * export default function MyCustomDimensionControl() { * const [ paddingSize, setPaddingSize ] = useState( '' ); * * return ( * <DimensionControl * label={ 'Padding' } * icon={ 'desktop' } * onChange={ ( value ) => setPaddingSize( value ) } * value={ paddingSize } * /> * ); * } * ``` */ export function DimensionControl(props) { const { label, value, sizes = sizesTable, icon, onChange, className = '' } = props; const onChangeSpacingSize = val => { const theSize = findSizeBySlug(sizes, val); if (!theSize || value === theSize.slug) { onChange === null || onChange === void 0 ? void 0 : onChange(undefined); } else if (typeof onChange === 'function') { onChange(theSize.slug); } }; const formatSizesAsOptions = theSizes => { const options = theSizes.map(_ref => { let { name, slug } = _ref; return { label: name, value: slug }; }); return [{ label: __('Default'), value: '' }, ...options]; }; const selectLabel = createElement(Fragment, null, icon && createElement(Icon, { icon: icon }), label); return createElement(SelectControl, { className: classnames(className, 'block-editor-dimension-control'), label: selectLabel, hideLabelFromVision: false, value: value, onChange: onChangeSpacingSize, options: formatSizesAsOptions(sizes) }); } export default DimensionControl; //# sourceMappingURL=index.js.map