Skip to content

Commit

Permalink
Support enabling/disabling custom gradients using theme.json (#24964)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Sep 3, 2020
1 parent 3e57ffd commit d7cea07
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 24 deletions.
3 changes: 3 additions & 0 deletions lib/experimental-default-theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
},
"color": {
"custom": true
},
"gradient": {
"custom": true
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ function gutenberg_experiments_editor_settings( $settings ) {
$experiments_settings['gradients'] = $gradient_presets;
}

$experiments_settings['disableCustomGradients'] = get_theme_support( 'disable-custom-gradients' );

return array_merge( $settings, $experiments_settings );
}
add_filter( 'block_editor_settings', 'gutenberg_experiments_editor_settings' );
8 changes: 8 additions & 0 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,12 @@ function gutenberg_experimental_global_styles_get_editor_features( $config ) {
}
$features['global']['color']['custom'] = false;
}
if ( get_theme_support( 'disable-custom-gradients' ) ) {
if ( ! isset( $features['global']['gradient'] ) ) {
$features['global']['gradient'] = array();
}
$features['global']['gradient']['custom'] = false;
}

return $features;
}
Expand Down Expand Up @@ -646,8 +652,10 @@ function gutenberg_experimental_global_styles_settings( $settings ) {
$settings['styles'][] = array( 'css' => $stylesheet );

$settings['__experimentalFeatures'] = gutenberg_experimental_global_styles_get_editor_features( $merged );

// Unsetting deprecated settings defined by Core.
unset( $settings['disableCustomColors'] );
unset( $settings['disableCustomGradients'] );

return $settings;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ function ColorGradientControlSelect( props ) {
colorGradientSettings.disableCustomColors = ! useEditorFeature(
'color.custom'
);
colorGradientSettings.disableCustomGradients = ! useEditorFeature(
'gradients.custom'
);

return (
<ColorGradientControlInner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ const PanelColorGradientSettingsSelect = ( props ) => {
colorGradientSettings.disableCustomColors = ! useEditorFeature(
'color.custom'
);
colorGradientSettings.disableCustomGradients = ! useEditorFeature(
'gradient.custom'
);
return (
<PanelColorGradientSettingsInner
{ ...{ ...colorGradientSettings, ...props } }
Expand Down
17 changes: 8 additions & 9 deletions packages/block-editor/src/components/gradient-picker/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { isEmpty, pick } from 'lodash';
import { isEmpty } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -15,6 +15,7 @@ import { useSelect } from '@wordpress/data';
* Internal dependencies
*/
import GradientPicker from './';
import useEditorFeature from '../use-editor-feature';

export default function GradientPickerControl( {
className,
Expand All @@ -23,14 +24,12 @@ export default function GradientPickerControl( {
label = __( 'Gradient Presets' ),
...props
} ) {
const { gradients = [], disableCustomGradients } = useSelect(
( select ) =>
pick( select( 'core/block-editor' ).getSettings(), [
'gradients',
'disableCustomGradients',
] ),
[]
);
const gradients =
useSelect(
( select ) => select( 'core/block-editor' ).getSettings().gradients,
[]
) ?? [];
const disableCustomGradients = ! useEditorFeature( 'gradient.custom' );
if ( isEmpty( gradients ) && disableCustomGradients ) {
return null;
}
Expand Down
25 changes: 12 additions & 13 deletions packages/block-editor/src/components/gradient-picker/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
/**
* External dependencies
*/
import { pick } from 'lodash';

/**
* WordPress dependencies
*/
import { __experimentalGradientPicker as GradientPicker } from '@wordpress/components';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import useEditorFeature from '../use-editor-feature';

function GradientPickerWithGradients( props ) {
const { gradients, disableCustomGradients } = useSelect(
( select ) =>
pick( select( 'core/block-editor' ).getSettings(), [
'gradients',
'disableCustomGradients',
] ),
[]
);
const gradients =
useSelect(
( select ) => select( 'core/block-editor' ).getSettings().gradients,
[]
) ?? [];
const disableCustomGradients = ! useEditorFeature( 'gradient.custom' );

return (
<GradientPicker
gradients={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const deprecatedFlags = {
settings.disableCustomColors === undefined
? undefined
: ! settings.disableCustomColors,
'gradient.custom': ( settings ) =>
settings.disableCustomGradients === undefined
? undefined
: ! settings.disableCustomGradients,
};

/**
Expand Down

0 comments on commit d7cea07

Please sign in to comment.