import { createElement, createPortal, useRef, Fragment, useState, } from '@wordpress/element' import OptionsPanel from '../OptionsPanel' import { __ } from 'ct-i18n' import cls from 'classnames' import PalettePreview from './color-palettes/PalettePreview' import ColorPalettesModal from './color-palettes/ColorPalettesModal' import usePopoverMaker from '../helpers/usePopoverMaker' import OutsideClickHandler from './react-outside-click-handler' import { Transition } from '@react-spring/web' import bezierEasing from 'bezier-easing' const ColorPalettes = ({ option, value, onChange }) => { const colorPalettesWrapper = useRef() const [{ isOpen, isTransitioning }, setModalState] = useState({ isOpen: false, isTransitioning: false, }) const { styles, popoverProps } = usePopoverMaker({ ref: colorPalettesWrapper, defaultHeight: 430, shouldCalculate: isTransitioning || isOpen, }) const setIsOpen = (isOpen) => { setModalState((state) => ({ ...state, isOpen, isTransitioning: true, })) } const stopTransitioning = () => setModalState((state) => ({ ...state, isTransitioning: false, })) const properValue = { ...option.value, ...Object.keys(value).reduce( (all, currentKey) => ({ ...all, ...(value[currentKey] ? { [currentKey]: value[currentKey], } : {}), }), {} ), ...(option.value.palettes ? { palettes: option.value.palettes.map((p, index) => { let maybeCurrentlyInValue = value.palettes.find( ({ id }) => p.id === id ) let maybeCurrentValue = {} if (p.id === value.current_palette) { Object.keys(p).map((maybeColor) => { if ( maybeColor.indexOf('color') === 0 && value[maybeColor] ) { maybeCurrentValue[maybeColor] = value[maybeColor] } }) } const result = { ...Object.keys(p).reduce( (all, currentKey) => ({ ...all, ...(p[currentKey] ? { [currentKey]: p[currentKey], } : {}), }), {} ), ...Object.keys(maybeCurrentlyInValue || {}).reduce( (all, currentKey) => ({ ...all, ...(maybeCurrentlyInValue[currentKey] ? { [currentKey]: maybeCurrentlyInValue[ currentKey ], } : {}), }), {} ), ...maybeCurrentValue, } return result }), } : {}), } return (