import {
createElement,
Component,
createPortal,
useRef,
createRef,
} from '@wordpress/element'
import PickerModal, { getNoColorPropFor } from './picker-modal'
import classnames from 'classnames'
import { __ } from 'ct-i18n'
import { normalizeCondition, matchValuesWithCondition } from 'match-conditions'
import usePopoverMaker from '../../helpers/usePopoverMaker'
import { getComputedStyleValue } from './utils'
import OutsideClickHandler from '../react-outside-click-handler'
const computePickerTitle = ({ picker, value, values }) => {
if ((value || { color: '' }).color.indexOf('INHERIT') > -1) {
return __('Inherited', 'blocksy')
}
if (picker.title !== picker.title.toString()) {
return (
Object.keys(picker.title).reduce((approvedTitle, currentTitle) => {
if (approvedTitle) {
return approvedTitle
}
if (
matchValuesWithCondition(
normalizeCondition(picker.title[currentTitle]),
values
)
) {
return currentTitle
}
return approvedTitle
}, null) || Object.keys(picker.title)[0]
)
}
return picker.title
}
export const resolveInherit = (picker, option, values, device) => {
if (typeof picker.inherit === 'string') {
if (picker.inherit.indexOf('self') > -1) {
const currentValue =
(option.responsive
? values[option.id][device]
: values[option.id]) || option.value
const pickerToInheritFrom = picker.inherit.split(':')[1]
let maybeNextValue =
currentValue[pickerToInheritFrom]?.color || 'CT_CSS_SKIP_RULE'
if (maybeNextValue.indexOf('CT_CSS_SKIP_RULE') > -1) {
maybeNextValue = option.pickers.find(
({ id }) => id === pickerToInheritFrom
).inherit
}
return {
background: maybeNextValue || '',
}
}
return { background: picker.inherit }
}
let background = Object.keys(picker.inherit).reduce(
(maybeResult, currentVar) => {
if (maybeResult) {
return maybeResult
}
if (
matchValuesWithCondition(
normalizeCondition(picker.inherit[currentVar]),
picker.inherit_source === 'global'
? Object.keys(picker.inherit[currentVar]).reduce(
(current, key) => ({
...current,
[key]: wp.customize(key)(),
}),
{}
)
: values
)
) {
return currentVar
}
return maybeResult
},
null
)
if (!background) {
return {}
}
return {
background,
}
}
const SinglePicker = ({
option,
value,
onChange,
picker,
onOutsideClick,
innerRef,
containerRef,
modalRef,
onPickingChange,
modalOpen,
values,
device,
}) => {
const el = useRef()
if (option.inline_modal) {
return (
)
}
return (
{
el.current = instance
if (innerRef) {
innerRef.current = instance
}
},
}}
additionalRefs={[modalRef]}
onOutsideClick={(e) => {
if (
(el.current.closest('.ct-color-picker-container') ===
e.target.closest('.ct-color-picker-container') &&
(e.target.closest('.ct-color-picker-single') ||
e.target.matches('.ct-color-picker-single'))) ||
el.current.closest('.ct-modal-tabs-content') === e.target
) {
return
}
onOutsideClick(e)
}}
className={classnames('ct-color-picker-single', {})}>
-1,
})}
onClick={(e) => {
if (option.skipModal) {
return
}
e.stopPropagation()
onPickingChange(el)
}}
data-tooltip-reveal="top"
style={
((value || {}).color || '').indexOf(
getNoColorPropFor(option)
) === -1
? {
background: getComputedStyleValue(
(value || {}).color
),
}
: {
...(picker.inherit &&
(value || {}).color !==
getNoColorPropFor(option)
? resolveInherit(
picker,
option,
values,
device
)
: {}),
}
}>
{computePickerTitle({ picker, value, values })}
{(value || { color: '' }).color.indexOf('INHERIT') > -1 && (
)}
{option.afterPill &&
option.afterPill({
picker,
})}
)
}
export default SinglePicker