import { Fragment, createElement, Component, useRef, useEffect, useMemo, useCallback, createPortal, useState, } from '@wordpress/element' import cls from 'classnames' import BackgroundModal from './background/BackgroundModal' import OutsideClickHandler from './react-outside-click-handler' import { getUrlForPattern } from './background/PatternPicker' import { __ } from 'ct-i18n' import usePopoverMaker from '../helpers/usePopoverMaker' /** * Support color picker values inside the background picker. * Which means transitions from ct-color-picker are made possible thanks to * this logic. */ const maybeConvertFromColorPickerTo = (value) => { if (!value.background_type) { if (value[Object.keys(value)[0]].color) { return { background_type: 'color', background_pattern: 'type-1', background_image: { attachment_id: null, x: 0, y: 0, }, background_repeat: 'no-repeat', background_size: 'auto', background_attachment: 'scroll', patternColor: { default: { color: '#e5e7ea', }, }, backgroundColor: { default: value[Object.keys(value)[0]], }, } } } return value } const Background = ({ option, value, onChange }) => { const [isOpen, setIsOpen] = useState(false) const [outsideClickFreezed, setOutsideClickFreezed] = useState(false) const backgroundWrapper = useRef() value = maybeConvertFromColorPickerTo(value) const isInherit = !option.has_no_color && value.background_type === 'color' && (value.backgroundColor.default.color === 'CT_CSS_SKIP_RULE' || value.backgroundColor.default.color === 'transparent') const isNoColor = option.has_no_color && value.background_type === 'color' && (value.backgroundColor.default.color === 'CT_CSS_SKIP_RULE' || value.backgroundColor.default.color === 'transparent') const { styles, popoverProps } = usePopoverMaker({ ref: backgroundWrapper, defaultHeight: 434, shouldCalculate: backgroundWrapper && backgroundWrapper.current, }) return (