| 1 |
import classnames from 'classnames/dedupe'; |
| 2 |
|
| 3 |
import { |
| 4 |
InspectorControls, |
| 5 |
RichText, |
| 6 |
useBlockProps, |
| 7 |
} from '@wordpress/block-editor'; |
| 8 |
import { |
| 9 |
PanelBody, |
| 10 |
SelectControl, |
| 11 |
TabPanel, |
| 12 |
ToggleControl, |
| 13 |
} from '@wordpress/components'; |
| 14 |
import { useEffect, useState } from '@wordpress/element'; |
| 15 |
import { applyFilters } from '@wordpress/hooks'; |
| 16 |
import { __ } from '@wordpress/i18n'; |
| 17 |
|
| 18 |
import ApplyFilters from '../../components/apply-filters'; |
| 19 |
import ColorIndicator from '../../components/color-indicator'; |
| 20 |
import ColorPicker from '../../components/color-picker'; |
| 21 |
import IconPicker from '../../components/icon-picker'; |
| 22 |
import RangeControl from '../../components/range-control'; |
| 23 |
import ToggleGroup from '../../components/toggle-group'; |
| 24 |
import URLPicker from '../../components/url-picker'; |
| 25 |
|
| 26 |
const SIZES = [ |
| 27 |
{ |
| 28 |
label: 'XS', |
| 29 |
value: 'xs', |
| 30 |
}, |
| 31 |
{ |
| 32 |
label: 'S', |
| 33 |
value: 'sm', |
| 34 |
}, |
| 35 |
{ |
| 36 |
label: 'M', |
| 37 |
value: 'md', |
| 38 |
}, |
| 39 |
{ |
| 40 |
label: 'L', |
| 41 |
value: 'lg', |
| 42 |
}, |
| 43 |
{ |
| 44 |
label: 'XL', |
| 45 |
value: 'xl', |
| 46 |
}, |
| 47 |
]; |
| 48 |
|
| 49 |
/** |
| 50 |
* Block Edit Class. |
| 51 |
* |
| 52 |
* @param props |
| 53 |
*/ |
| 54 |
export default function BlockEdit(props) { |
| 55 |
const { attributes, setAttributes, isSelected } = props; |
| 56 |
|
| 57 |
const { |
| 58 |
tagName, |
| 59 |
text, |
| 60 |
icon, |
| 61 |
iconPosition, |
| 62 |
hideText, |
| 63 |
url, |
| 64 |
ariaLabel, |
| 65 |
target, |
| 66 |
rel, |
| 67 |
size, |
| 68 |
color, |
| 69 |
textColor, |
| 70 |
borderRadius, |
| 71 |
borderWeight, |
| 72 |
borderColor, |
| 73 |
focusOutlineWeight, |
| 74 |
focusOutlineColor, |
| 75 |
hoverColor, |
| 76 |
hoverTextColor, |
| 77 |
hoverBorderColor, |
| 78 |
} = attributes; |
| 79 |
|
| 80 |
let { className = '' } = attributes; |
| 81 |
|
| 82 |
const [selectedColorState, setSelectedColorState] = useState('normal'); |
| 83 |
|
| 84 |
// Reset selected color state when block is not selected. |
| 85 |
useEffect(() => { |
| 86 |
if (!isSelected) { |
| 87 |
setSelectedColorState('normal'); |
| 88 |
} |
| 89 |
}, [isSelected]); |
| 90 |
|
| 91 |
let isNormalState = false; |
| 92 |
let isHoveredState = false; |
| 93 |
let isFocusedState = false; |
| 94 |
|
| 95 |
if (isSelected) { |
| 96 |
isNormalState = true; |
| 97 |
|
| 98 |
if (selectedColorState === 'hover') { |
| 99 |
isNormalState = false; |
| 100 |
isHoveredState = true; |
| 101 |
} else if (selectedColorState === 'focus') { |
| 102 |
isNormalState = false; |
| 103 |
isFocusedState = true; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
className = classnames( |
| 108 |
'ghostkit-button', |
| 109 |
size && `ghostkit-button-${size}`, |
| 110 |
hideText && 'ghostkit-button-icon-only', |
| 111 |
isNormalState && 'ghostkit-button-is-normal-state', |
| 112 |
isHoveredState && 'ghostkit-button-is-hover-state', |
| 113 |
isFocusedState && 'ghostkit-button-is-focus-state', |
| 114 |
className |
| 115 |
); |
| 116 |
|
| 117 |
// focus outline |
| 118 |
if (focusOutlineWeight && focusOutlineColor) { |
| 119 |
className = classnames(className, 'ghostkit-button-with-outline'); |
| 120 |
} |
| 121 |
|
| 122 |
className = applyFilters('ghostkit.editor.className', className, props); |
| 123 |
|
| 124 |
const colorsTabs = [ |
| 125 |
{ |
| 126 |
name: 'normal', |
| 127 |
title: __('Normal', 'ghostkit'), |
| 128 |
className: 'ghostkit-control-tabs-tab', |
| 129 |
}, |
| 130 |
{ |
| 131 |
name: 'hover', |
| 132 |
title: __('Hover', 'ghostkit'), |
| 133 |
className: 'ghostkit-control-tabs-tab', |
| 134 |
}, |
| 135 |
]; |
| 136 |
|
| 137 |
if (focusOutlineWeight && focusOutlineColor) { |
| 138 |
colorsTabs.push({ |
| 139 |
name: 'focus', |
| 140 |
title: __('Focus', 'ghostkit'), |
| 141 |
className: 'ghostkit-control-tabs-tab', |
| 142 |
}); |
| 143 |
} |
| 144 |
|
| 145 |
const blockProps = useBlockProps({ |
| 146 |
className, |
| 147 |
}); |
| 148 |
|
| 149 |
return ( |
| 150 |
<div {...blockProps}> |
| 151 |
<InspectorControls> |
| 152 |
<PanelBody> |
| 153 |
<div className="blocks-size__main"> |
| 154 |
<ToggleGroup |
| 155 |
value={size} |
| 156 |
options={SIZES} |
| 157 |
onChange={(value) => { |
| 158 |
setAttributes({ size: value }); |
| 159 |
}} |
| 160 |
/> |
| 161 |
</div> |
| 162 |
</PanelBody> |
| 163 |
<PanelBody> |
| 164 |
<RangeControl |
| 165 |
label={__('Corner Radius', 'ghostkit')} |
| 166 |
value={borderRadius} |
| 167 |
min={0} |
| 168 |
max={50} |
| 169 |
onChange={(value) => |
| 170 |
setAttributes({ borderRadius: value }) |
| 171 |
} |
| 172 |
allowCustomMax |
| 173 |
__next40pxDefaultSize |
| 174 |
__nextHasNoMarginBottom |
| 175 |
/> |
| 176 |
<RangeControl |
| 177 |
label={__('Border Size', 'ghostkit')} |
| 178 |
value={borderWeight} |
| 179 |
min={0} |
| 180 |
max={6} |
| 181 |
onChange={(value) => |
| 182 |
setAttributes({ borderWeight: value }) |
| 183 |
} |
| 184 |
allowCustomMax |
| 185 |
__next40pxDefaultSize |
| 186 |
__nextHasNoMarginBottom |
| 187 |
/> |
| 188 |
<RangeControl |
| 189 |
label={__('Focus Outline Size', 'ghostkit')} |
| 190 |
value={focusOutlineWeight} |
| 191 |
min={0} |
| 192 |
max={6} |
| 193 |
onChange={(value) => |
| 194 |
setAttributes({ focusOutlineWeight: value }) |
| 195 |
} |
| 196 |
allowCustomMax |
| 197 |
__next40pxDefaultSize |
| 198 |
__nextHasNoMarginBottom |
| 199 |
/> |
| 200 |
</PanelBody> |
| 201 |
<PanelBody> |
| 202 |
<IconPicker |
| 203 |
label={__('Icon', 'ghostkit')} |
| 204 |
value={icon} |
| 205 |
onChange={(value) => setAttributes({ icon: value })} |
| 206 |
insideInspector |
| 207 |
/> |
| 208 |
{icon ? ( |
| 209 |
<ToggleControl |
| 210 |
label={__('Show Icon Only', 'ghostkit')} |
| 211 |
checked={!!hideText} |
| 212 |
onChange={(val) => setAttributes({ hideText: val })} |
| 213 |
__nextHasNoMarginBottom |
| 214 |
/> |
| 215 |
) : null} |
| 216 |
{icon && !hideText ? ( |
| 217 |
<SelectControl |
| 218 |
label={__('Icon Position', 'ghostkit')} |
| 219 |
value={iconPosition} |
| 220 |
options={[ |
| 221 |
{ |
| 222 |
value: 'left', |
| 223 |
label: __('Left', 'ghostkit'), |
| 224 |
}, |
| 225 |
{ |
| 226 |
value: 'right', |
| 227 |
label: __('Right', 'ghostkit'), |
| 228 |
}, |
| 229 |
]} |
| 230 |
onChange={(value) => |
| 231 |
setAttributes({ iconPosition: value }) |
| 232 |
} |
| 233 |
__next40pxDefaultSize |
| 234 |
__nextHasNoMarginBottom |
| 235 |
/> |
| 236 |
) : null} |
| 237 |
</PanelBody> |
| 238 |
<PanelBody |
| 239 |
title={ |
| 240 |
<> |
| 241 |
{__('Colors', 'ghostkit')} |
| 242 |
<ColorIndicator colorValue={color} /> |
| 243 |
<ColorIndicator colorValue={textColor} /> |
| 244 |
{borderWeight ? ( |
| 245 |
<ColorIndicator colorValue={borderColor} /> |
| 246 |
) : null} |
| 247 |
{focusOutlineWeight && focusOutlineColor ? ( |
| 248 |
<ColorIndicator |
| 249 |
colorValue={focusOutlineColor} |
| 250 |
/> |
| 251 |
) : null} |
| 252 |
</> |
| 253 |
} |
| 254 |
initialOpen={false} |
| 255 |
> |
| 256 |
<TabPanel |
| 257 |
className="ghostkit-control-tabs ghostkit-control-tabs-wide" |
| 258 |
tabs={colorsTabs} |
| 259 |
onSelect={(state) => { |
| 260 |
setSelectedColorState(state); |
| 261 |
}} |
| 262 |
> |
| 263 |
{(tabData) => { |
| 264 |
const isHover = tabData.name === 'hover'; |
| 265 |
|
| 266 |
// focus tab |
| 267 |
if (tabData.name === 'focus') { |
| 268 |
return ( |
| 269 |
<ApplyFilters |
| 270 |
name="ghostkit.editor.controls" |
| 271 |
attribute="focusOutlineColor" |
| 272 |
props={props} |
| 273 |
> |
| 274 |
<ColorPicker |
| 275 |
label={__('Outline', 'ghostkit')} |
| 276 |
value={focusOutlineColor} |
| 277 |
onChange={(val) => |
| 278 |
setAttributes({ |
| 279 |
focusOutlineColor: val, |
| 280 |
}) |
| 281 |
} |
| 282 |
alpha |
| 283 |
/> |
| 284 |
</ApplyFilters> |
| 285 |
); |
| 286 |
} |
| 287 |
|
| 288 |
return ( |
| 289 |
<> |
| 290 |
<ApplyFilters |
| 291 |
name="ghostkit.editor.controls" |
| 292 |
attribute={ |
| 293 |
isHover ? 'hoverColor' : 'color' |
| 294 |
} |
| 295 |
props={props} |
| 296 |
> |
| 297 |
<ColorPicker |
| 298 |
label={__('Background', 'ghostkit')} |
| 299 |
value={isHover ? hoverColor : color} |
| 300 |
onChange={(val) => |
| 301 |
setAttributes( |
| 302 |
isHover |
| 303 |
? { hoverColor: val } |
| 304 |
: { color: val } |
| 305 |
) |
| 306 |
} |
| 307 |
alpha |
| 308 |
gradient |
| 309 |
/> |
| 310 |
</ApplyFilters> |
| 311 |
<ApplyFilters |
| 312 |
name="ghostkit.editor.controls" |
| 313 |
attribute={ |
| 314 |
isHover |
| 315 |
? 'hoverTextColor' |
| 316 |
: 'textColor' |
| 317 |
} |
| 318 |
props={props} |
| 319 |
> |
| 320 |
<ColorPicker |
| 321 |
label={__('Text', 'ghostkit')} |
| 322 |
value={ |
| 323 |
isHover |
| 324 |
? hoverTextColor |
| 325 |
: textColor |
| 326 |
} |
| 327 |
onChange={(val) => |
| 328 |
setAttributes( |
| 329 |
isHover |
| 330 |
? { |
| 331 |
hoverTextColor: |
| 332 |
val, |
| 333 |
} |
| 334 |
: { textColor: val } |
| 335 |
) |
| 336 |
} |
| 337 |
alpha |
| 338 |
/> |
| 339 |
</ApplyFilters> |
| 340 |
{borderWeight ? ( |
| 341 |
<ApplyFilters |
| 342 |
name="ghostkit.editor.controls" |
| 343 |
attribute={ |
| 344 |
isHover |
| 345 |
? 'hoverBorderColor' |
| 346 |
: 'borderColor' |
| 347 |
} |
| 348 |
props={props} |
| 349 |
> |
| 350 |
<ColorPicker |
| 351 |
label={__('Border', 'ghostkit')} |
| 352 |
value={ |
| 353 |
isHover |
| 354 |
? hoverBorderColor |
| 355 |
: borderColor |
| 356 |
} |
| 357 |
onChange={(val) => |
| 358 |
setAttributes( |
| 359 |
isHover |
| 360 |
? { |
| 361 |
hoverBorderColor: |
| 362 |
val, |
| 363 |
} |
| 364 |
: { |
| 365 |
borderColor: |
| 366 |
val, |
| 367 |
} |
| 368 |
) |
| 369 |
} |
| 370 |
alpha |
| 371 |
/> |
| 372 |
</ApplyFilters> |
| 373 |
) : null} |
| 374 |
</> |
| 375 |
); |
| 376 |
}} |
| 377 |
</TabPanel> |
| 378 |
</PanelBody> |
| 379 |
</InspectorControls> |
| 380 |
{!tagName || tagName === 'a' ? ( |
| 381 |
<URLPicker |
| 382 |
url={url} |
| 383 |
rel={rel} |
| 384 |
ariaLabel={ariaLabel} |
| 385 |
target={target} |
| 386 |
onChange={(data) => { |
| 387 |
setAttributes(data); |
| 388 |
}} |
| 389 |
isSelected={isSelected} |
| 390 |
toolbarSettings |
| 391 |
inspectorSettings |
| 392 |
/> |
| 393 |
) : null} |
| 394 |
{icon ? ( |
| 395 |
<div |
| 396 |
className={`ghostkit-button-icon ghostkit-button-icon-${ |
| 397 |
iconPosition === 'right' ? 'right' : 'left' |
| 398 |
}`} |
| 399 |
> |
| 400 |
<IconPicker.Dropdown |
| 401 |
onChange={(value) => setAttributes({ icon: value })} |
| 402 |
value={icon} |
| 403 |
renderToggle={({ onToggle }) => ( |
| 404 |
<IconPicker.Preview |
| 405 |
onClick={onToggle} |
| 406 |
name={icon} |
| 407 |
/> |
| 408 |
)} |
| 409 |
/> |
| 410 |
</div> |
| 411 |
) : null} |
| 412 |
{!hideText ? ( |
| 413 |
<RichText |
| 414 |
placeholder={__('Write text…', 'ghostkit')} |
| 415 |
value={text} |
| 416 |
onChange={(value) => setAttributes({ text: value })} |
| 417 |
isSelected={isSelected} |
| 418 |
withoutInteractiveFormatting |
| 419 |
/> |
| 420 |
) : null} |
| 421 |
</div> |
| 422 |
); |
| 423 |
} |
| 424 |
|