Skip to content

Commit e87f4ad

Browse files
Gradients: custom gradients should be enabled independently from defaultGradients (#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é <[email protected]>
1 parent 16f3e25 commit e87f4ad

File tree

7 files changed

+62
-18
lines changed

7 files changed

+62
-18
lines changed

packages/block-editor/src/components/colors/with-colors.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const withCustomColorPalette = ( colorsArray ) =>
3636
'withCustomColorPalette'
3737
);
3838

39+
const EMPTY_OBJECT = {};
40+
3941
/**
4042
* Higher order component factory for injecting the editor colors as the
4143
* `colors` prop in the `withColors` HOC.
@@ -45,7 +47,11 @@ const withCustomColorPalette = ( colorsArray ) =>
4547
const withEditorColorPalette = () =>
4648
createHigherOrderComponent(
4749
( WrappedComponent ) => ( props ) => {
48-
const { palette: colorPerOrigin } = useSetting( 'color' ) || {};
50+
// Some color settings have a special handling for deprecated flags in `useSetting`,
51+
// so we can't unwrap them by doing const { ... } = useSetting('color')
52+
// until https://github.com/WordPress/gutenberg/issues/37094 is fixed.
53+
const colorPerOrigin =
54+
useSetting( 'color.palette' ) || EMPTY_OBJECT;
4955
const allColors = useMemo(
5056
() => [
5157
...( colorPerOrigin?.custom || [] ),

packages/block-editor/src/components/gradients/use-gradient.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ export function getGradientSlugByValue( gradients, value ) {
5959
return gradient && gradient.slug;
6060
}
6161

62+
const EMPTY_OBJECT = {};
63+
6264
export function __experimentalUseGradient( {
6365
gradientAttribute = 'gradient',
6466
customGradientAttribute = 'customGradient',
6567
} = {} ) {
6668
const { clientId } = useBlockEditContext();
6769

68-
const { gradients: gradientsPerOrigin } = useSetting( 'color' ) || {};
70+
const gradientsPerOrigin = useSetting( 'color.gradients' ) || EMPTY_OBJECT;
6971
const allGradients = useMemo(
7072
() => [
7173
...( gradientsPerOrigin?.custom || [] ),

packages/block-editor/src/hooks/color.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import useSetting from '../components/use-setting';
3232

3333
export const COLOR_SUPPORT_KEY = 'color';
3434

35+
const EMPTY_OBJECT = {};
36+
3537
const hasColorSupport = ( blockType ) => {
3638
const colorSupport = getBlockSupport( blockType, COLOR_SUPPORT_KEY );
3739
return (
@@ -216,15 +218,16 @@ function immutableSet( object, path, value ) {
216218
*/
217219
export function ColorEdit( props ) {
218220
const { name: blockName, attributes } = props;
219-
const {
220-
palette: solidsPerOrigin,
221-
gradients: gradientsPerOrigin,
222-
customGradient: areCustomGradientsEnabled,
223-
custom: areCustomSolidsEnabled,
224-
text: isTextEnabled,
225-
background: isBackgroundEnabled,
226-
link: isLinkEnabled,
227-
} = useSetting( 'color' ) || {};
221+
// Some color settings have a special handling for deprecated flags in `useSetting`,
222+
// so we can't unwrap them by doing const { ... } = useSetting('color')
223+
// until https://github.com/WordPress/gutenberg/issues/37094 is fixed.
224+
const solidsPerOrigin = useSetting( 'color.palette' ) || EMPTY_OBJECT;
225+
const gradientsPerOrigin = useSetting( 'color.gradients' ) || EMPTY_OBJECT;
226+
const areCustomSolidsEnabled = useSetting( 'color.custom' );
227+
const areCustomGradientsEnabled = useSetting( 'color.customGradient' );
228+
const isBackgroundEnabled = useSetting( 'color.background' );
229+
const isLinkEnabled = useSetting( 'color.link' );
230+
const isTextEnabled = useSetting( 'color.text' );
228231

229232
const solidsEnabled =
230233
areCustomSolidsEnabled ||
@@ -441,7 +444,7 @@ export const withColorPaletteStyles = createHigherOrderComponent(
441444
( BlockListBlock ) => ( props ) => {
442445
const { name, attributes } = props;
443446
const { backgroundColor, textColor } = attributes;
444-
const { palette: solidsPerOrigin } = useSetting( 'color' ) || {};
447+
const solidsPerOrigin = useSetting( 'color.palette' ) || EMPTY_OBJECT;
445448
const colors = useMemo(
446449
() => [
447450
...( solidsPerOrigin?.custom || [] ),

packages/block-editor/src/hooks/use-color-props.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export function getColorClassesAndStyles( attributes ) {
7373
};
7474
}
7575

76+
const EMPTY_OBJECT = {};
77+
7678
/**
7779
* Determines the color related props for a block derived from its color block
7880
* support attributes.
@@ -87,8 +89,12 @@ export function getColorClassesAndStyles( attributes ) {
8789
export function useColorProps( attributes ) {
8890
const { backgroundColor, textColor, gradient } = attributes;
8991

90-
const { palette: solidsPerOrigin, gradients: gradientsPerOrigin } =
91-
useSetting( 'color' ) || {};
92+
// Some color settings have a special handling for deprecated flags in `useSetting`,
93+
// so we can't unwrap them by doing const { ... } = useSetting('color')
94+
// until https://github.com/WordPress/gutenberg/issues/37094 is fixed.
95+
const solidsPerOrigin = useSetting( 'color.palette' ) || EMPTY_OBJECT;
96+
const gradientsPerOrigin = useSetting( 'color.gradients' ) || EMPTY_OBJECT;
97+
9298
const colors = useMemo(
9399
() => [
94100
...( solidsPerOrigin?.custom || [] ),

packages/components/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Bug Fix
66

7+
- Fixed `GradientPicker` not displaying `CustomGradientPicker` when no gradients are provided ([#36900](https://github.com/WordPress/gutenberg/pull/36900)).
78
- Fixed error thrown in `ColorPicker` when used in controlled state in color gradients ([#36941](https://github.com/WordPress/gutenberg/pull/36941)).
89
- Updated readme to include default value introduced in fix for unexpected movements in the `ColorPicker` ([#35670](https://github.com/WordPress/gutenberg/pull/35670)).
910
- Replaced hardcoded blue in `ColorPicker` with UI theme color ([#36153](https://github.com/WordPress/gutenberg/pull/36153)).

packages/components/src/gradient-picker/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ export default function GradientPicker( {
110110
const clearGradient = useCallback( () => onChange( undefined ), [
111111
onChange,
112112
] );
113-
const Component = __experimentalHasMultipleOrigins
114-
? MultipleOrigin
115-
: SingleOrigin;
113+
const Component =
114+
__experimentalHasMultipleOrigins && gradients?.length
115+
? MultipleOrigin
116+
: SingleOrigin;
117+
116118
return (
117119
<Component
118120
className={ className }
@@ -122,7 +124,8 @@ export default function GradientPicker( {
122124
onChange={ onChange }
123125
value={ value }
124126
actions={
125-
clearable && (
127+
clearable &&
128+
( gradients?.length || ! disableCustomGradients ) && (
126129
<CircularOptionPicker.ButtonAction
127130
onClick={ clearGradient }
128131
>

packages/components/src/gradient-picker/stories/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,26 @@ export const _default = () => {
8484
/>
8585
);
8686
};
87+
88+
export const WithNoExistingGradients = () => {
89+
const disableCustomGradients = boolean( 'Disable Custom Gradients', false );
90+
const __experimentalHasMultipleOrigins = boolean(
91+
'Experimental Has Multiple Origins',
92+
true
93+
);
94+
const clearable = boolean( 'Clearable', true );
95+
const className = text( 'Class Name', '' );
96+
const gradients = object( 'Gradients', [] );
97+
98+
return (
99+
<GradientPickerWithState
100+
__experimentalHasMultipleOrigins={
101+
__experimentalHasMultipleOrigins
102+
}
103+
disableCustomGradients={ disableCustomGradients }
104+
gradients={ gradients }
105+
clearable={ clearable }
106+
className={ className }
107+
/>
108+
);
109+
};

0 commit comments

Comments
 (0)