Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CSS var conversion to hex for Section & Column Block shadow color #1825

Merged
merged 3 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/blocks/blocks/flip/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const Inspector = ({

const changeBoxShadowColor = value => {
setAttributes({
boxShadowColor: ( 100 > attributes.boxShadowColorOpacity && attributes.boxShadowColor?.includes( 'var(' ) ) ?
boxShadowColor: ( 100 > attributes.boxShadowColorOpacity && value?.includes( 'var(' ) ) ?
getComputedStyle( document.documentElement, null ).getPropertyValue( value?.replace( 'var(', '' )?.replace( ')', '' ) ) :
value
});
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/blocks/section/column/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ const Edit = ({

if ( true === attributes.boxShadow ) {
boxShadowStyle = {
boxShadow: `${ attributes.boxShadowHorizontal }px ${ attributes.boxShadowVertical }px ${ attributes.boxShadowBlur }px ${ attributes.boxShadowSpread }px ${ hexToRgba( ( attributes.boxShadowColor ? attributes.boxShadowColor : '#000000' ), attributes.boxShadowColorOpacity ) }`
boxShadow: `${ attributes.boxShadowHorizontal }px ${ attributes.boxShadowVertical }px ${ attributes.boxShadowBlur }px ${ attributes.boxShadowSpread }px ${ attributes.boxShadowColor.includes( 'var(' ) && ( attributes.boxShadowColorOpacity === undefined || 100 === attributes.boxShadowColorOpacity ) ? attributes.boxShadowColor : hexToRgba( ( attributes.boxShadowColor ? attributes.boxShadowColor : '#000000' ), attributes.boxShadowColorOpacity ) }`
};
}

Expand Down
20 changes: 18 additions & 2 deletions src/blocks/blocks/section/column/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,13 +472,29 @@ const Inspector = ({
<ColorGradientControl
label={ __( 'Shadow Color', 'otter-blocks' ) }
colorValue={ attributes.boxShadowColor }
onColorChange={ value => setAttributes({ boxShadowColor: value }) }
onColorChange={ value => setAttributes({
boxShadowColor: ( 100 > attributes.boxShadowColorOpacity && value?.includes( 'var(' ) ) ?
getComputedStyle( document.documentElement, null ).getPropertyValue( value?.replace( 'var(', '' )?.replace( ')', '' ) ) :
value
}) }
/>

<RangeControl
label={ __( 'Opacity', 'otter-blocks' ) }
value={ attributes.boxShadowColorOpacity }
onChange={ value => setAttributes({ boxShadowColorOpacity: value }) }
onChange={ value => {
const changes = { boxShadowColorOpacity: value };

/**
* If the value is less than 100 and the color is CSS a variable, then replace the CSS variable with the computed value.
* This is needed because the way calculate the opacity of the color is by using the HEX value since we are doing in the server side.
*/
if ( 100 > value && attributes.boxShadowColor?.includes( 'var(' ) ) {
changes.boxShadowColor = getComputedStyle( document.documentElement, null ).getPropertyValue( attributes.boxShadowColor.replace( 'var(', '' ).replace( ')', '' ) );
}

setAttributes( changes );
} }
min={ 0 }
max={ 100 }
/>
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/blocks/section/columns/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ const Edit = ({

if ( true === attributes.boxShadow ) {
boxShadowStyle = {
boxShadow: `${ attributes.boxShadowHorizontal }px ${ attributes.boxShadowVertical }px ${ attributes.boxShadowBlur }px ${ attributes.boxShadowSpread }px ${ hexToRgba( ( attributes.boxShadowColor ? attributes.boxShadowColor : '#000000' ), attributes.boxShadowColorOpacity ) }`
boxShadow: `${ attributes.boxShadowHorizontal }px ${ attributes.boxShadowVertical }px ${ attributes.boxShadowBlur }px ${ attributes.boxShadowSpread }px ${ attributes.boxShadowColor.includes( 'var(' ) && ( attributes.boxShadowColorOpacity === undefined || 100 === attributes.boxShadowColorOpacity ) ? attributes.boxShadowColor : hexToRgba( ( attributes.boxShadowColor ? attributes.boxShadowColor : '#000000' ), attributes.boxShadowColorOpacity ) }`
};
}

Expand Down
21 changes: 19 additions & 2 deletions src/blocks/blocks/section/columns/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,13 +917,30 @@ const Inspector = ({
<ColorGradientControl
label={ __( 'Shadow Color', 'otter-blocks' ) }
colorValue={ attributes.boxShadowColor }
onColorChange={ value => setAttributes({ boxShadowColor: value }) }
onColorChange={ value => setAttributes({
boxShadowColor: ( 100 > attributes.boxShadowColorOpacity && value?.includes( 'var(' ) ) ?
getComputedStyle( document.documentElement, null ).getPropertyValue( value?.replace( 'var(', '' )?.replace( ')', '' ) ) :
value
}) }
/>

<RangeControl
label={ __( 'Opacity', 'otter-blocks' ) }
value={ attributes.boxShadowColorOpacity }
onChange={ value => setAttributes({ boxShadowColorOpacity: value }) }
onChange={ value => {

const changes = { boxShadowColorOpacity: value };

/**
* If the value is less than 100 and the color is CSS a variable, then replace the CSS variable with the computed value.
* This is needed because the way calculate the opacity of the color is by using the HEX value since we are doing in the server side.
*/
if ( 100 > value && attributes.boxShadowColor?.includes( 'var(' ) ) {
changes.boxShadowColor = getComputedStyle( document.documentElement, null ).getPropertyValue( attributes.boxShadowColor.replace( 'var(', '' ).replace( ')', '' ) );
}

setAttributes( changes );
} }
min={ 0 }
max={ 100 }
/>
Expand Down
Loading