Skip to content

Commit

Permalink
converted addition of colors using hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
bfintal@gmail.com committed Nov 14, 2023
1 parent dc144c7 commit 53d4589
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/components/color-palette-control/color-palette-popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ export const ColorPalettePopup = props => {
}

const NOOP = () => {}
const PASSTHRU = v => v

ColorPalettePopup.defaultProps = {
value: '',
onChange: NOOP,
preOnChange: NOOP,
preOnChange: PASSTHRU,

colors: [],

Expand Down
53 changes: 33 additions & 20 deletions src/components/color-palette-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
FlexItem,
__experimentalHStack as HStack, // eslint-disable-line @wordpress/no-unsafe-wp-apis
} from '@wordpress/components'
import { useSelect } from '@wordpress/data'
import { useSelect, select } from '@wordpress/data'
import { applyFilters, addFilter } from '@wordpress/hooks'

/**
* External dependencies
Expand All @@ -39,26 +40,14 @@ const popoverProps = {

const PASSTHRUOP = v => v

const ColorPaletteControl = memo( props => {
const {
label,
className = '',
} = props
const [ _value, _onChange ] = useControlHandlers( props.attribute, props.responsive, props.hover, props.valueCallback, props.changeCallback )
const [ _propsToPass, controlProps ] = extractControlProps( props )

addFilter( 'stackable.color-palette-control.colors', 'stackable/color-palette-control', ( { colors: _colors, gradients: _gradients } ) => {
const {
stackableColors,
stackableGradients,
hideThemeColors,
hideDefaultColors,
hideSiteEditorColors,
} = useSelect( 'stackable/global-colors' ).getSettings()
} = select( 'stackable/global-colors' ).getSettings()

const { colors: groupedColors, gradients: groupedGradients } = useMultipleOriginColorsAndGradients()

let colors = cloneDeep( groupedColors )
let gradients = cloneDeep( groupedGradients )
let colors = cloneDeep( _colors )
let gradients = cloneDeep( _gradients )

if ( stackableGradients && stackableGradients.length ) {
gradients = [
Expand All @@ -81,6 +70,32 @@ const ColorPaletteControl = memo( props => {
]
}

return { colors, gradients }
} )

addFilter( 'stackable.color-palette-control.color-value', 'stackable/color-palette-control', value => {
if ( typeof value === 'string' && value.includes( '--stk-global-color' ) && value.match( /#[\d\w]{6,}/ ) ) {
return value.match( /#[\d\w]{6,}/ )[ 0 ]
}
return value
} )

const ColorPaletteControl = memo( props => {
const {
label,
className = '',
} = props
const [ _value, _onChange ] = useControlHandlers( props.attribute, props.responsive, props.hover, props.valueCallback, props.changeCallback )
const [ _propsToPass, controlProps ] = extractControlProps( props )

const {
hideThemeColors,
hideDefaultColors,
hideSiteEditorColors,
} = useSelect( 'stackable/global-colors' ).getSettings()

let { colors, gradients } = applyFilters( 'stackable.color-palette-control.colors', useMultipleOriginColorsAndGradients() )

// Filter the colors.
colors = colors.filter( group => {
// Since there are no identifying properties for the groups, we'll just use the same names used in Gutenberg.
Expand Down Expand Up @@ -125,9 +140,7 @@ const ColorPaletteControl = memo( props => {
let value = typeof props.value === 'undefined' ? _value : props.value
const onChange = typeof props.onChange === 'undefined' ? _onChange : props.onChange

if ( typeof value === 'string' && value.includes( '--stk-global-color' ) && value.match( /#[\d\w]{6,}/ ) ) {
value = value.match( /#[\d\w]{6,}/ )[ 0 ]
}
value = applyFilters( 'stackable.color-palette-control.color-value', value )

let colorLabel,
colorName = value
Expand Down

0 comments on commit 53d4589

Please sign in to comment.