-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Global Styles Color Palette editor default to theme/core paletts.
- Loading branch information
1 parent
3534119
commit 6526a52
Showing
2 changed files
with
64 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 53 additions & 30 deletions
83
packages/edit-site/src/components/sidebar/color-palette-panel.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,67 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
Button, | ||
__experimentalColorEdit as ColorEdit, | ||
} from '@wordpress/components'; | ||
import { __experimentalColorEdit as ColorEdit } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useEditorFeature, GLOBAL_CONTEXT } from '../editor/utils'; | ||
|
||
export default function ColorPalettePanel( { | ||
contextName, | ||
getSetting, | ||
setSetting, | ||
} ) { | ||
const colors = getSetting( contextName, 'color.palette' ); | ||
let emptyUI; | ||
if ( colors === undefined ) { | ||
emptyUI = __( | ||
'Using theme or core default colors. Add some colors to create your own color palette instead.' | ||
); | ||
} else if ( colors && colors.length === 0 ) { | ||
emptyUI = ( | ||
<> | ||
<p>{ __( 'Using an empty color palette.' ) }</p> | ||
<Button | ||
isSmall | ||
isSecondary | ||
onClick={ () => setSetting( contextName, 'color.palette' ) } | ||
> | ||
{ __( 'Reset to theme/core defaults' ) } | ||
</Button> | ||
</> | ||
); | ||
} | ||
/** | ||
* Shared reference to an empty array for cases where it is important to avoid | ||
* returning a new array reference on every invocation, as in a connected or | ||
* other pure component which performs `shouldComponentUpdate` check on props. | ||
* This should be used as a last resort, since the normalized data should be | ||
* maintained by the reducer result in state. | ||
* | ||
* @type {Array} | ||
*/ | ||
const EMPTY_ARRAY = []; | ||
|
||
export default function ColorPalettePanel( { contextName, setSetting } ) { | ||
const colors = useEditorFeature( 'color.palette', contextName ); | ||
const immutableColorSlugs = useSelect( | ||
( select ) => { | ||
const baseStyles = select( 'core/edit-site' ).getSettings() | ||
.__experimentalGlobalStylesBaseStyles; | ||
const basePalette = | ||
get( baseStyles, [ | ||
contextName, | ||
'settings', | ||
'color', | ||
'palette', | ||
] ) ?? | ||
get( baseStyles, [ | ||
GLOBAL_CONTEXT, | ||
'settings', | ||
'color', | ||
'palette', | ||
] ); | ||
if ( ! basePalette ) { | ||
return EMPTY_ARRAY; | ||
} | ||
return basePalette.map( ( { slug } ) => slug ); | ||
}, | ||
[ contextName ] | ||
); | ||
return ( | ||
<ColorEdit | ||
immutableColorSlugs={ immutableColorSlugs } | ||
colors={ colors } | ||
onChange={ ( newColors ) => { | ||
setSetting( contextName, 'color.palette', newColors ); | ||
} } | ||
emptyUI={ emptyUI } | ||
emptyUI={ __( | ||
'Colors are empty! Add some colors to create your own color palette.' | ||
) } | ||
/> | ||
); | ||
} |