import { createElement, Component, useState } from '@wordpress/element' import { __ } from 'ct-i18n' import InputWithOnlyNumbers from '../components/InputWithOnlyNumbers' import cls from 'classnames' import OutsideClickHandler from './react-outside-click-handler' const Spacing = ({ value, option, onChange }) => { const [isOpen, setIsOpen] = useState(false) const units = [ { unit: 'px' }, { unit: '%' }, { unit: 'em' }, { unit: 'rem' }, { unit: 'pt' }, ] const withDefault = (currentUnit, defaultUnit) => units.find(({ unit }) => unit === currentUnit) ? currentUnit : currentUnit || units[0].unit const getLinkedLeader = () => ['top', 'right', 'bottom', 'left'].find((v) => value[v] !== 'auto') const getCurrentUnit = () => withDefault( value[getLinkedLeader()] .toString() .replace(/[0-9]/g, '') .replace('-', '') .replace(/\./g, '') ) const getNumericValue = (value, unit = '') => { if (value === 'auto') { return value } return `${parseFloat(value) === 0 ? 0 : parseFloat(value) || ''}${unit}` } const handleChange = (futureValue, position) => { if (value.linked) { onChange({ ...value, top: value.top === 'auto' ? value.top : getNumericValue(futureValue, getCurrentUnit()), left: value.left === 'auto' ? value.left : getNumericValue(futureValue, getCurrentUnit()), right: value.right === 'auto' ? value.right : getNumericValue(futureValue, getCurrentUnit()), bottom: value.bottom === 'auto' ? value.bottom : getNumericValue(futureValue, getCurrentUnit()), }) return } onChange({ ...value, [position]: getNumericValue(futureValue, getCurrentUnit()), }) } return (