| 1 |
/** |
| 2 |
* Box-Shadow reusable component. |
| 3 |
* |
| 4 |
*/ |
| 5 |
import { __ } from '@wordpress/i18n'; |
| 6 |
import Range from '@Components/range/Range.js'; |
| 7 |
import AdvancedPopColorControl from '../color-control/advanced-pop-color-control'; |
| 8 |
import { Button, Dashicon } from '@wordpress/components'; |
| 9 |
import { useState } from '@wordpress/element'; |
| 10 |
import MultiButtonsControl from '../multi-buttons-control/index'; |
| 11 |
import React, { useLayoutEffect } from 'react'; |
| 12 |
import { select } from '@wordpress/data'; |
| 13 |
import getUAGEditorStateLocalStorage from '@Controls/getUAGEditorStateLocalStorage'; |
| 14 |
import { blocksAttributes } from '@Attributes/getBlocksDefaultAttributes'; |
| 15 |
|
| 16 |
const BoxShadowControl = ( props ) => { |
| 17 |
const [ showAdvancedControls, toggleAdvancedControls ] = useState( false ); |
| 18 |
|
| 19 |
useLayoutEffect( () => { |
| 20 |
window.addEventListener( 'click', function ( e ) { |
| 21 |
const popupButton = document.querySelector( |
| 22 |
`.active.popup-${ blockId } .spectra-control-popup__options--action-button` |
| 23 |
); |
| 24 |
const popupWrap = document.querySelector( |
| 25 |
`.active.popup-${ blockId } .spectra-control-popup` |
| 26 |
); |
| 27 |
|
| 28 |
if ( |
| 29 |
popupButton && |
| 30 |
! popupButton?.contains( e.target ) && |
| 31 |
popupWrap && |
| 32 |
! popupWrap?.contains( e.target ) && |
| 33 |
! e.target?.classList?.contains( |
| 34 |
'uagb-advanced-color-indicate' |
| 35 |
) && |
| 36 |
! e.target?.parentElement?.closest( '.uagb-popover-color' ) && |
| 37 |
! e.target?.parentElement?.closest( '.uagb-reset' ) |
| 38 |
) { |
| 39 |
toggleAdvancedControls( false ); |
| 40 |
const blockName = getSelectedBlock()?.name; |
| 41 |
const uagSettingState = getUAGEditorStateLocalStorage( |
| 42 |
'uagSettingState' |
| 43 |
); |
| 44 |
|
| 45 |
const data = { |
| 46 |
...uagSettingState, |
| 47 |
[ blockName ]: { |
| 48 |
...uagSettingState?.[ blockName ], |
| 49 |
selectedSetting: false, |
| 50 |
}, |
| 51 |
}; |
| 52 |
|
| 53 |
const uagLocalStorage = getUAGEditorStateLocalStorage(); |
| 54 |
if ( uagLocalStorage ) { |
| 55 |
uagLocalStorage.setItem( |
| 56 |
'uagSettingState', |
| 57 |
JSON.stringify( data ) |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
} ); |
| 62 |
}, [] ); |
| 63 |
|
| 64 |
const { |
| 65 |
setAttributes, |
| 66 |
boxShadowColor, |
| 67 |
boxShadowHOffset, |
| 68 |
boxShadowVOffset, |
| 69 |
boxShadowBlur, |
| 70 |
boxShadowSpread, |
| 71 |
boxShadowPosition, |
| 72 |
label = __( 'Box Shadow', 'ultimate-addons-for-gutenberg' ), |
| 73 |
popup = false, |
| 74 |
blockId, |
| 75 |
} = props; |
| 76 |
|
| 77 |
let advancedControls; |
| 78 |
const activeClass = showAdvancedControls ? 'active' : ''; |
| 79 |
|
| 80 |
// Array of all the current Typography Control's Labels. |
| 81 |
const attributeNames = [ |
| 82 |
boxShadowColor.label, |
| 83 |
boxShadowHOffset.label, |
| 84 |
boxShadowVOffset.label, |
| 85 |
boxShadowBlur.label, |
| 86 |
boxShadowSpread.label, |
| 87 |
boxShadowPosition.label, |
| 88 |
]; |
| 89 |
|
| 90 |
const { getSelectedBlock } = select( 'core/block-editor' ); |
| 91 |
|
| 92 |
// Function to get the Block's default Box Shadow Values. |
| 93 |
const getBlockBoxShadowValue = () => { |
| 94 |
const selectedBlockName = getSelectedBlock()?.name.split( '/' ).pop(); |
| 95 |
let defaultValues = false; |
| 96 |
if ( 'undefined' !== typeof blocksAttributes[ selectedBlockName ] ) { |
| 97 |
attributeNames.forEach( ( attributeName ) => { |
| 98 |
if ( attributeName ) { |
| 99 |
const blockDefaultAttributeValue = |
| 100 |
'undefined' !== |
| 101 |
typeof blocksAttributes[ selectedBlockName ][ |
| 102 |
attributeName |
| 103 |
]?.default |
| 104 |
? blocksAttributes[ selectedBlockName ][ |
| 105 |
attributeName |
| 106 |
]?.default |
| 107 |
: ''; |
| 108 |
defaultValues = { |
| 109 |
...defaultValues, |
| 110 |
[ attributeName ]: blockDefaultAttributeValue, |
| 111 |
}; |
| 112 |
} |
| 113 |
} ); |
| 114 |
} |
| 115 |
return defaultValues; |
| 116 |
}; |
| 117 |
|
| 118 |
// Function to check if any Box Shadow Setting has changed. |
| 119 |
const getUpdateState = () => { |
| 120 |
const defaultValues = getBlockBoxShadowValue(); |
| 121 |
const selectedBlockAttributes = getSelectedBlock()?.attributes; |
| 122 |
let isBoxShadowUpdated = false; |
| 123 |
attributeNames.forEach( ( attributeName ) => { |
| 124 |
if ( |
| 125 |
selectedBlockAttributes?.[ attributeName ] && |
| 126 |
selectedBlockAttributes?.[ attributeName ] !== |
| 127 |
defaultValues?.[ attributeName ] |
| 128 |
) { |
| 129 |
isBoxShadowUpdated = true; |
| 130 |
} |
| 131 |
} ); |
| 132 |
return isBoxShadowUpdated; |
| 133 |
}; |
| 134 |
|
| 135 |
// Flag to check if this control has been updated or not. |
| 136 |
const isBoxShadowUpdated = popup && getUpdateState(); |
| 137 |
|
| 138 |
const overallControls = ( |
| 139 |
<> |
| 140 |
{ /* Shadow Color */ } |
| 141 |
<AdvancedPopColorControl |
| 142 |
label={ boxShadowColor.title } |
| 143 |
colorValue={ boxShadowColor.value } |
| 144 |
data={ { |
| 145 |
value: boxShadowColor.value, |
| 146 |
label: boxShadowColor.label, |
| 147 |
} } |
| 148 |
setAttributes={ setAttributes } |
| 149 |
/> |
| 150 |
{ /* Horizontal Positioning */ } |
| 151 |
<Range |
| 152 |
label={ boxShadowHOffset.title } |
| 153 |
value={ boxShadowHOffset.value } |
| 154 |
min={ -100 } |
| 155 |
max={ 100 } |
| 156 |
displayUnit={ false } |
| 157 |
setAttributes={ setAttributes } |
| 158 |
data={ { |
| 159 |
value: boxShadowHOffset.value, |
| 160 |
label: boxShadowHOffset.label, |
| 161 |
} } |
| 162 |
/> |
| 163 |
{ /* Vertical Positioning */ } |
| 164 |
<Range |
| 165 |
label={ boxShadowVOffset.title } |
| 166 |
value={ boxShadowVOffset.value } |
| 167 |
min={ -100 } |
| 168 |
max={ 100 } |
| 169 |
displayUnit={ false } |
| 170 |
setAttributes={ setAttributes } |
| 171 |
data={ { |
| 172 |
value: boxShadowVOffset.value, |
| 173 |
label: boxShadowVOffset.label, |
| 174 |
} } |
| 175 |
/> |
| 176 |
{ /* Blur */ } |
| 177 |
<Range |
| 178 |
label={ boxShadowBlur.title } |
| 179 |
value={ boxShadowBlur.value } |
| 180 |
min={ 0 } |
| 181 |
max={ 100 } |
| 182 |
displayUnit={ false } |
| 183 |
setAttributes={ setAttributes } |
| 184 |
data={ { |
| 185 |
value: boxShadowBlur.value, |
| 186 |
label: boxShadowBlur.label, |
| 187 |
} } |
| 188 |
/> |
| 189 |
{ /* Spread */ } |
| 190 |
<Range |
| 191 |
label={ boxShadowSpread.title } |
| 192 |
value={ boxShadowSpread.value } |
| 193 |
min={ -100 } |
| 194 |
max={ 100 } |
| 195 |
displayUnit={ false } |
| 196 |
setAttributes={ setAttributes } |
| 197 |
data={ { |
| 198 |
value: boxShadowSpread.value, |
| 199 |
label: boxShadowSpread.label, |
| 200 |
} } |
| 201 |
/> |
| 202 |
{ /* Shadow Position */ } |
| 203 |
<MultiButtonsControl |
| 204 |
setAttributes={ setAttributes } |
| 205 |
label={ boxShadowPosition.title } |
| 206 |
data={ { |
| 207 |
value: boxShadowPosition.value, |
| 208 |
label: boxShadowPosition.label, |
| 209 |
} } |
| 210 |
options={ [ |
| 211 |
{ |
| 212 |
value: 'outset', |
| 213 |
label: __( 'Outset', 'ultimate-addons-for-gutenberg' ), |
| 214 |
tooltip: __( |
| 215 |
'Outset', |
| 216 |
'ultimate-addons-for-gutenberg' |
| 217 |
), |
| 218 |
}, |
| 219 |
{ |
| 220 |
value: 'inset', |
| 221 |
label: __( 'Inset', 'ultimate-addons-for-gutenberg' ), |
| 222 |
tooltip: __( |
| 223 |
'Inset (10px)', |
| 224 |
'ultimate-addons-for-gutenberg' |
| 225 |
), |
| 226 |
}, |
| 227 |
] } |
| 228 |
showIcons={ false } |
| 229 |
/> |
| 230 |
</> |
| 231 |
); |
| 232 |
|
| 233 |
if ( showAdvancedControls ) { |
| 234 |
advancedControls = ( |
| 235 |
<div className="uagb-box-shadow-advanced spectra-control-popup"> |
| 236 |
{ overallControls } |
| 237 |
</div> |
| 238 |
); |
| 239 |
} |
| 240 |
|
| 241 |
const boxShadowAdvancedControls = ( |
| 242 |
<div className="spectra-control-popup__options--action-wrapper"> |
| 243 |
<span className="uag-control-label"> |
| 244 |
{ label } |
| 245 |
{ isBoxShadowUpdated && ( |
| 246 |
<div className="spectra__change-indicator--dot-right" /> |
| 247 |
) } |
| 248 |
</span> |
| 249 |
<Button |
| 250 |
className="uag-box-shadow-button spectra-control-popup__options--action-button" |
| 251 |
aria-pressed={ showAdvancedControls } |
| 252 |
onClick={ () => { |
| 253 |
const allPopups = document.querySelectorAll( |
| 254 |
'.spectra-control-popup__options' |
| 255 |
); |
| 256 |
if ( allPopups && 0 < allPopups.length ) { |
| 257 |
for ( let i = 0; i < allPopups.length; i++ ) { |
| 258 |
const popupButton = allPopups[ i ]?.querySelector( |
| 259 |
'.spectra-control-popup__options.active .spectra-control-popup__options--action-button' |
| 260 |
); |
| 261 |
popupButton?.click(); |
| 262 |
} |
| 263 |
} |
| 264 |
toggleAdvancedControls( ! showAdvancedControls ); |
| 265 |
|
| 266 |
const blockName = getSelectedBlock()?.name; |
| 267 |
const uagSettingState = getUAGEditorStateLocalStorage( |
| 268 |
'uagSettingState' |
| 269 |
); |
| 270 |
let data = { |
| 271 |
...uagSettingState, |
| 272 |
[ blockName ]: { |
| 273 |
...uagSettingState?.[ blockName ], |
| 274 |
selectedSetting: '.uag-box-shadow-options', |
| 275 |
}, |
| 276 |
}; |
| 277 |
|
| 278 |
if ( showAdvancedControls ) { |
| 279 |
data = { |
| 280 |
...uagSettingState, |
| 281 |
[ blockName ]: { |
| 282 |
...uagSettingState?.[ blockName ], |
| 283 |
selectedSetting: false, |
| 284 |
}, |
| 285 |
}; |
| 286 |
} |
| 287 |
const uagLocalStorage = getUAGEditorStateLocalStorage(); |
| 288 |
if ( uagLocalStorage ) { |
| 289 |
uagLocalStorage.setItem( |
| 290 |
'uagSettingState', |
| 291 |
JSON.stringify( data ) |
| 292 |
); |
| 293 |
} |
| 294 |
} } |
| 295 |
> |
| 296 |
<Dashicon icon="edit" /> |
| 297 |
</Button> |
| 298 |
</div> |
| 299 |
); |
| 300 |
|
| 301 |
return popup ? ( |
| 302 |
<div |
| 303 |
className={ `components-base-control uag-box-shadow-options spectra-control-popup__options popup-${ blockId } ${ activeClass }` } |
| 304 |
> |
| 305 |
{ boxShadowAdvancedControls } |
| 306 |
{ showAdvancedControls && advancedControls } |
| 307 |
</div> |
| 308 |
) : ( |
| 309 |
<>{ overallControls }</> |
| 310 |
); |
| 311 |
}; |
| 312 |
|
| 313 |
export default BoxShadowControl; |
| 314 |
|