Skip to content

Commit

Permalink
Gradients: custom gradients should be enabled independently from defa…
Browse files Browse the repository at this point in the history
…ultGradients (#36900)

* Gradients: Enable adding custom gradient when gradients are disabled

* Simplify fix by moving check to where we determine multiple origins, add storybook and changelog entries

* Fix issue with custom and customGradient settings not being loaded in ColorEdit

* Hide clear button if there are no gradients and the custom gradient is disabled

* Revert to access color props specifically

Co-authored-by: André <583546+oandregal@users.noreply.github.com>
  • Loading branch information
2 people authored and noisysocks committed Dec 6, 2021
1 parent fdf76b2 commit 17262d4
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 18 deletions.
8 changes: 7 additions & 1 deletion packages/block-editor/src/components/colors/with-colors.js
Expand Up @@ -36,6 +36,8 @@ const withCustomColorPalette = ( colorsArray ) =>
'withCustomColorPalette'
);

const EMPTY_OBJECT = {};

/**
* Higher order component factory for injecting the editor colors as the
* `colors` prop in the `withColors` HOC.
Expand All @@ -45,7 +47,11 @@ const withCustomColorPalette = ( colorsArray ) =>
const withEditorColorPalette = () =>
createHigherOrderComponent(
( WrappedComponent ) => ( props ) => {
const { palette: colorPerOrigin } = useSetting( 'color' ) || {};
// Some color settings have a special handling for deprecated flags in `useSetting`,
// so we can't unwrap them by doing const { ... } = useSetting('color')
// until https://github.com/WordPress/gutenberg/issues/37094 is fixed.
const colorPerOrigin =
useSetting( 'color.palette' ) || EMPTY_OBJECT;
const allColors = useMemo(
() => [
...( colorPerOrigin?.custom || [] ),
Expand Down
Expand Up @@ -59,13 +59,15 @@ export function getGradientSlugByValue( gradients, value ) {
return gradient && gradient.slug;
}

const EMPTY_OBJECT = {};

export function __experimentalUseGradient( {
gradientAttribute = 'gradient',
customGradientAttribute = 'customGradient',
} = {} ) {
const { clientId } = useBlockEditContext();

const { gradients: gradientsPerOrigin } = useSetting( 'color' ) || {};
const gradientsPerOrigin = useSetting( 'color.gradients' ) || EMPTY_OBJECT;
const allGradients = useMemo(
() => [
...( gradientsPerOrigin?.custom || [] ),
Expand Down
23 changes: 13 additions & 10 deletions packages/block-editor/src/hooks/color.js
Expand Up @@ -32,6 +32,8 @@ import useSetting from '../components/use-setting';

export const COLOR_SUPPORT_KEY = 'color';

const EMPTY_OBJECT = {};

const hasColorSupport = ( blockType ) => {
const colorSupport = getBlockSupport( blockType, COLOR_SUPPORT_KEY );
return (
Expand Down Expand Up @@ -216,15 +218,16 @@ function immutableSet( object, path, value ) {
*/
export function ColorEdit( props ) {
const { name: blockName, attributes } = props;
const {
palette: solidsPerOrigin,
gradients: gradientsPerOrigin,
customGradient: areCustomGradientsEnabled,
custom: areCustomSolidsEnabled,
text: isTextEnabled,
background: isBackgroundEnabled,
link: isLinkEnabled,
} = useSetting( 'color' ) || {};
// Some color settings have a special handling for deprecated flags in `useSetting`,
// so we can't unwrap them by doing const { ... } = useSetting('color')
// until https://github.com/WordPress/gutenberg/issues/37094 is fixed.
const solidsPerOrigin = useSetting( 'color.palette' ) || EMPTY_OBJECT;
const gradientsPerOrigin = useSetting( 'color.gradients' ) || EMPTY_OBJECT;
const areCustomSolidsEnabled = useSetting( 'color.custom' );
const areCustomGradientsEnabled = useSetting( 'color.customGradient' );
const isBackgroundEnabled = useSetting( 'color.background' );
const isLinkEnabled = useSetting( 'color.link' );
const isTextEnabled = useSetting( 'color.text' );

const solidsEnabled =
areCustomSolidsEnabled ||
Expand Down Expand Up @@ -441,7 +444,7 @@ export const withColorPaletteStyles = createHigherOrderComponent(
( BlockListBlock ) => ( props ) => {
const { name, attributes } = props;
const { backgroundColor, textColor } = attributes;
const { palette: solidsPerOrigin } = useSetting( 'color' ) || {};
const solidsPerOrigin = useSetting( 'color.palette' ) || EMPTY_OBJECT;
const colors = useMemo(
() => [
...( solidsPerOrigin?.custom || [] ),
Expand Down
10 changes: 8 additions & 2 deletions packages/block-editor/src/hooks/use-color-props.js
Expand Up @@ -73,6 +73,8 @@ export function getColorClassesAndStyles( attributes ) {
};
}

const EMPTY_OBJECT = {};

/**
* Determines the color related props for a block derived from its color block
* support attributes.
Expand All @@ -87,8 +89,12 @@ export function getColorClassesAndStyles( attributes ) {
export function useColorProps( attributes ) {
const { backgroundColor, textColor, gradient } = attributes;

const { palette: solidsPerOrigin, gradients: gradientsPerOrigin } =
useSetting( 'color' ) || {};
// Some color settings have a special handling for deprecated flags in `useSetting`,
// so we can't unwrap them by doing const { ... } = useSetting('color')
// until https://github.com/WordPress/gutenberg/issues/37094 is fixed.
const solidsPerOrigin = useSetting( 'color.palette' ) || EMPTY_OBJECT;
const gradientsPerOrigin = useSetting( 'color.gradients' ) || EMPTY_OBJECT;

const colors = useMemo(
() => [
...( solidsPerOrigin?.custom || [] ),
Expand Down
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fix

- Fixed `GradientPicker` not displaying `CustomGradientPicker` when no gradients are provided ([#36900](https://github.com/WordPress/gutenberg/pull/36900)).

## 19.1.0

### Enhancements
Expand Down
11 changes: 7 additions & 4 deletions packages/components/src/gradient-picker/index.js
Expand Up @@ -110,9 +110,11 @@ export default function GradientPicker( {
const clearGradient = useCallback( () => onChange( undefined ), [
onChange,
] );
const Component = __experimentalHasMultipleOrigins
? MultipleOrigin
: SingleOrigin;
const Component =
__experimentalHasMultipleOrigins && gradients?.length
? MultipleOrigin
: SingleOrigin;

return (
<Component
className={ className }
Expand All @@ -122,7 +124,8 @@ export default function GradientPicker( {
onChange={ onChange }
value={ value }
actions={
clearable && (
clearable &&
( gradients?.length || ! disableCustomGradients ) && (
<CircularOptionPicker.ButtonAction
onClick={ clearGradient }
>
Expand Down
23 changes: 23 additions & 0 deletions packages/components/src/gradient-picker/stories/index.js
Expand Up @@ -84,3 +84,26 @@ export const _default = () => {
/>
);
};

export const WithNoExistingGradients = () => {
const disableCustomGradients = boolean( 'Disable Custom Gradients', false );
const __experimentalHasMultipleOrigins = boolean(
'Experimental Has Multiple Origins',
true
);
const clearable = boolean( 'Clearable', true );
const className = text( 'Class Name', '' );
const gradients = object( 'Gradients', [] );

return (
<GradientPickerWithState
__experimentalHasMultipleOrigins={
__experimentalHasMultipleOrigins
}
disableCustomGradients={ disableCustomGradients }
gradients={ gradients }
clearable={ clearable }
className={ className }
/>
);
};

0 comments on commit 17262d4

Please sign in to comment.