| 1 |
import React, { useState } from 'react'; |
| 2 |
import reactCSS from 'reactcss'; |
| 3 |
import './ColorPickerField.scss'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { Tooltip } from '@WizardFields'; |
| 6 |
import { SwatchIcon, ArrowPathIcon } from '@heroicons/react/24/outline'; |
| 7 |
|
| 8 |
import { SketchPicker } from 'react-color'; |
| 9 |
|
| 10 |
function ColorPickerField( props ) { |
| 11 |
const { |
| 12 |
name, |
| 13 |
label, |
| 14 |
value, |
| 15 |
isActive = true, |
| 16 |
handleOnchange, |
| 17 |
displayAs = 'selector', |
| 18 |
} = props; |
| 19 |
|
| 20 |
const [ displayColorPicker, setdisplayColorPicker ] = useState( false ); |
| 21 |
const [ color, setColor ] = useState( value ); |
| 22 |
|
| 23 |
const styles = reactCSS( { |
| 24 |
default: { |
| 25 |
color: { |
| 26 |
width: '36px', |
| 27 |
height: '30px', |
| 28 |
background: color, |
| 29 |
}, |
| 30 |
}, |
| 31 |
} ); |
| 32 |
|
| 33 |
const handleClick = () => { |
| 34 |
setdisplayColorPicker( ( prevValue ) => ! prevValue ); |
| 35 |
}; |
| 36 |
const handleClose = () => { |
| 37 |
setdisplayColorPicker( false ); |
| 38 |
}; |
| 39 |
const handleResetColor = () => { |
| 40 |
handleChange( '' ); |
| 41 |
}; |
| 42 |
|
| 43 |
const handleChange = ( newcolor ) => { |
| 44 |
if ( newcolor ) { |
| 45 |
setColor( newcolor.hex ); |
| 46 |
} else { |
| 47 |
setColor( newcolor ); |
| 48 |
} |
| 49 |
|
| 50 |
// Trigger change |
| 51 |
const changeEvent = new CustomEvent( 'wcf:color:change', { |
| 52 |
bubbles: true, |
| 53 |
detail: { |
| 54 |
e: 'color', |
| 55 |
name: props.name, |
| 56 |
value: newcolor ? newcolor.hex : newcolor, |
| 57 |
}, |
| 58 |
} ); |
| 59 |
|
| 60 |
document.dispatchEvent( changeEvent ); |
| 61 |
|
| 62 |
if ( handleOnchange ) { |
| 63 |
handleOnchange( newcolor ); |
| 64 |
} |
| 65 |
}; |
| 66 |
return ( |
| 67 |
<div |
| 68 |
className={ `wcf-field wcf-color-field ${ |
| 69 |
! isActive ? 'wcf-hide' : '' |
| 70 |
}` } |
| 71 |
> |
| 72 |
<div className="wcf-field__data"> |
| 73 |
{ label && 'selector' === displayAs && ( |
| 74 |
<div className="wcf-field__data--label"> |
| 75 |
<label>{ label }</label> |
| 76 |
</div> |
| 77 |
) } |
| 78 |
<div |
| 79 |
className={ `wcf-field__data--content ${ |
| 80 |
'button' === displayAs ? '!w-full' : '' |
| 81 |
}` } |
| 82 |
> |
| 83 |
<div className="wcf-colorpicker-selector"> |
| 84 |
{ 'selector' === displayAs ? ( |
| 85 |
<> |
| 86 |
<div |
| 87 |
className="wcf-colorpicker-swatch-wrap" |
| 88 |
onClick={ handleClick } |
| 89 |
> |
| 90 |
<span |
| 91 |
className="wcf-colorpicker-swatch" |
| 92 |
style={ styles.color } |
| 93 |
/> |
| 94 |
<span className="wcf-colorpicker-label"> |
| 95 |
{ __( 'Select Color', 'cartflows' ) } |
| 96 |
</span> |
| 97 |
<input |
| 98 |
type="hidden" |
| 99 |
name={ name } |
| 100 |
value={ color } |
| 101 |
/> |
| 102 |
</div> |
| 103 |
{ color && ( |
| 104 |
<span |
| 105 |
className="wcf-colorpicker-reset" |
| 106 |
onClick={ handleResetColor } |
| 107 |
title={ __( 'Reset', 'cartflows' ) } |
| 108 |
> |
| 109 |
<span className="dashicons dashicons-update-alt"></span> |
| 110 |
</span> |
| 111 |
) } |
| 112 |
</> |
| 113 |
) : ( |
| 114 |
<button |
| 115 |
type="button" |
| 116 |
className="inline-flex relative justify-center items-center gap-1.5 rounded px-4 py-2.5 text-sm font-normal leading-4 shadow-sm cursor-pointer bg-primary-25 border border-primary-300 text-primary-500 focus:bg-primary-50 focus:text-primary-600 focus:ring-offset-2 focus:ring-2 focus:ring-primary-500 focus:outline-none" |
| 117 |
onClick={ handleClick } |
| 118 |
> |
| 119 |
<SwatchIcon |
| 120 |
className="w-5 h-5 stroke-2" |
| 121 |
aria-hidden="true" |
| 122 |
/> |
| 123 |
{ label && <label>{ label }</label> } |
| 124 |
{ color && ( |
| 125 |
<span |
| 126 |
className={ `wcf-colorpicker-reset absolute !p-0 -left-2 -top-1.5 bg-white rounded-full text-gray-600` } |
| 127 |
onClick={ handleResetColor } |
| 128 |
> |
| 129 |
<Tooltip |
| 130 |
text={ __( 'Reset', 'cartflows' ) } |
| 131 |
classes={ 'text-gray-400 !ml-0' } |
| 132 |
icon={ |
| 133 |
<ArrowPathIcon |
| 134 |
className="w-4 h-5 stroke-2 text-gray-500 hover:text-gray-600 active:text-gray-600" |
| 135 |
aria-hidden="true" |
| 136 |
/> |
| 137 |
} |
| 138 |
/> |
| 139 |
</span> |
| 140 |
) } |
| 141 |
</button> |
| 142 |
) } |
| 143 |
</div> |
| 144 |
<div className="wcf-color-picker"> |
| 145 |
{ displayColorPicker ? ( |
| 146 |
<div className="wcf-color-picker-popover"> |
| 147 |
<div |
| 148 |
className="wcf-color-picker-cover" |
| 149 |
onClick={ handleClose } |
| 150 |
/> |
| 151 |
<SketchPicker |
| 152 |
name={ name } |
| 153 |
color={ color } |
| 154 |
onChange={ handleChange } |
| 155 |
disableAlpha={ true } |
| 156 |
/> |
| 157 |
</div> |
| 158 |
) : null } |
| 159 |
</div> |
| 160 |
</div> |
| 161 |
</div> |
| 162 |
</div> |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
export default ColorPickerField; |
| 167 |
|