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

fix (shadow color): support custom properties #3242

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Changes from all 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
45 changes: 31 additions & 14 deletions src/components/shadow-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,44 @@ const ShadowFilterControl = props => {

useEffect( () => {
if ( value ) {
let _value = value
let hexValue = ''
_value = _value.replace( /#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})([0-9a-fA-F]{2})?$/g, value => {
hexValue = value
return ''
} ).trim()

let _value = value.trim()
if ( _value.startsWith( 'inset' ) ) {
filters.inset = true
_value = _value.replace( /^inset\s*/, '' )
} else {
filters.inset = false
}

const [ horizontalOffset, verticalOffset, blur, spread ] = _value.split( ' ' )
filters.horizontalOffset = parseInt( horizontalOffset )
filters.verticalOffset = parseInt( verticalOffset )
filters.blur = parseInt( blur )
filters.shadowSpread = isNaN( parseInt( spread ) ) ? '' : parseInt( spread )
filters.shadowColor = hexValue
// Split string into 5 parts, the last part contains the rest of the string.
function splitStringIntoParts( str, splitTo = 5 ) {
const parts = str.split( ' ' )
const result = []
for ( let i = 0; i < splitTo - 1; i++ ) {
if ( parts.length ) {
result.push( parts.shift() )
} else {
result.push( '' )
}
}
result.push( parts.join( ' ' ) )
return result
}

const [ horizontalOffset, verticalOffset, blur, spread, color ] = splitStringIntoParts( _value, props.isFilter ? 4 : 5 )
filters.horizontalOffset = isNaN( parseInt( horizontalOffset ) ) ? 0 : parseInt( horizontalOffset )
filters.verticalOffset = isNaN( parseInt( verticalOffset ) ) ? 0 : parseInt( verticalOffset )
filters.blur = isNaN( parseInt( blur ) ) ? 0 : parseInt( blur )
filters.shadowSpread = isNaN( parseInt( spread ) ) ? 0 : parseInt( spread )
filters.shadowColor = color || ''

if ( props.isFilter ) {
filters.shadowSpread = ''
filters.shadowColor = spread
}

setFilters( { ...filters } )
}
}, [ value ] )
}, [ value, props.isFilter ] )

return (
<Popover
Expand Down Expand Up @@ -211,6 +226,7 @@ const ShadowFilterControl = props => {

ShadowFilterControl.defaultProps = {
hasInset: true,
isFilter: false,
}

const ShadowControl = memo( props => {
Expand Down Expand Up @@ -293,6 +309,7 @@ const ShadowControl = memo( props => {
hover={ props.hover }
parentProps={ props }
hasInset={ props.hasInset }
isFilter={ props.isFilter }
onEscape={ () => setIsPopoverOpen( false ) }
/>
) }
Expand Down
Loading