Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename and stabilize useEditorFeature as useSetting #31587

Merged
merged 2 commits into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,25 @@ _Returns_

- `Object`: Props to pass to the element to mark as a block.

<a name="useSetting" href="#useSetting">#</a> **useSetting**

Hook that retrieves the editor setting.
It works with nested objects using by finding the value at path.

_Usage_

```js
const isEnabled = useSetting( 'typography.dropCap' );
```

_Parameters_

- _path_ `string`: The path to the setting.

_Returns_

- `any`: Returns the value defined for the setting.

<a name="validateThemeColors" href="#validateThemeColors">#</a> **validateThemeColors**

Given an array of theme colors checks colors for validity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
getBlockType,
__experimentalGetAccessibleBlockLabel as getAccessibleBlockLabel,
} from '@wordpress/blocks';
import { __experimentalUseEditorFeature as useEditorFeature } from '@wordpress/block-editor';
import { useSetting } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand Down Expand Up @@ -49,7 +49,7 @@ function BlockForType( {
wrapperProps,
blockWidth,
} ) {
const defaultColors = useEditorFeature( 'color.palette' ) || emptyArray;
const defaultColors = useSetting( 'color.palette' ) || emptyArray;
const globalStyle = useGlobalStyles();
const mergedStyle = useMemo( () => {
return getMergedGlobalStyles(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { createHigherOrderComponent } from '@wordpress/compose';
/**
* Internal dependencies
*/
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';

export default createHigherOrderComponent( ( WrappedComponent ) => {
return ( props ) => {
const colorsFeature = useEditorFeature( 'color.palette' );
const disableCustomColorsFeature = ! useEditorFeature( 'color.custom' );
const colorsFeature = useSetting( 'color.palette' );
const disableCustomColorsFeature = ! useSetting( 'color.custom' );
const colors =
props.colors === undefined ? colorsFeature : props.colors;
const disableCustomColors =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { sprintf, __ } from '@wordpress/i18n';
*/
import { getColorObjectByColorValue } from '../colors';
import { __experimentalGetGradientObjectByGradientValue } from '../gradients';
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';

// translators: first %s: the color name or value (e.g. red or #ff0000)
const colorIndicatorAriaLabel = __( '(Color: %s)' );
Expand Down Expand Up @@ -177,12 +177,10 @@ function ColorGradientControlInner( {

function ColorGradientControlSelect( props ) {
const colorGradientSettings = {};
colorGradientSettings.colors = useEditorFeature( 'color.palette' );
colorGradientSettings.gradients = useEditorFeature( 'color.gradients' );
colorGradientSettings.disableCustomColors = ! useEditorFeature(
'color.custom'
);
colorGradientSettings.disableCustomGradients = ! useEditorFeature(
colorGradientSettings.colors = useSetting( 'color.palette' );
colorGradientSettings.gradients = useSetting( 'color.gradients' );
colorGradientSettings.disableCustomColors = ! useSetting( 'color.custom' );
colorGradientSettings.disableCustomGradients = ! useSetting(
'color.customGradient'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { sprintf, __ } from '@wordpress/i18n';
import ColorGradientControl from './control';
import { getColorObjectByColorValue } from '../colors';
import { __experimentalGetGradientObjectByGradientValue } from '../gradients';
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';

// translators: first %s: The type of color or gradient (e.g. background, overlay...), second %s: the color name or value (e.g. red or #ff0000)
const colorIndicatorAriaLabel = __( '(%s: color %s)' );
Expand Down Expand Up @@ -148,12 +148,10 @@ export const PanelColorGradientSettingsInner = ( {

const PanelColorGradientSettingsSelect = ( props ) => {
const colorGradientSettings = {};
colorGradientSettings.colors = useEditorFeature( 'color.palette' );
colorGradientSettings.gradients = useEditorFeature( 'color.gradients' );
colorGradientSettings.disableCustomColors = ! useEditorFeature(
'color.custom'
);
colorGradientSettings.disableCustomGradients = ! useEditorFeature(
colorGradientSettings.colors = useSetting( 'color.palette' );
colorGradientSettings.gradients = useSetting( 'color.gradients' );
colorGradientSettings.disableCustomColors = ! useSetting( 'color.custom' );
colorGradientSettings.disableCustomGradients = ! useSetting(
'color.customGradient'
);
return (
Expand Down
5 changes: 2 additions & 3 deletions packages/block-editor/src/components/colors/use-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import InspectorControls from '../inspector-controls';
import { useBlockEditContext } from '../block-edit';
import ColorPanel from './color-panel';
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';
import { store as blockEditorStore } from '../../store';

function getComputedStyle( node ) {
Expand Down Expand Up @@ -63,8 +63,7 @@ export default function __experimentalUseColors(
deps = []
) {
const { clientId } = useBlockEditContext();
const settingsColors =
useEditorFeature( 'color.palette' ) || DEFAULT_COLORS;
const settingsColors = useSetting( 'color.palette' ) || DEFAULT_COLORS;
const { attributes } = useSelect(
( select ) => {
const { getBlockAttributes } = select( blockEditorStore );
Expand Down
5 changes: 2 additions & 3 deletions packages/block-editor/src/components/colors/with-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
getColorObjectByAttributeValues,
getMostReadableColor,
} from './utils';
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';

const DEFAULT_COLORS = [];

Expand Down Expand Up @@ -47,8 +47,7 @@ const withCustomColorPalette = ( colorsArray ) =>
const withEditorColorPalette = () =>
createHigherOrderComponent(
( WrappedComponent ) => ( props ) => {
const colors =
useEditorFeature( 'color.palette' ) || DEFAULT_COLORS;
const colors = useSetting( 'color.palette' ) || DEFAULT_COLORS;
return <WrappedComponent { ...props } colors={ colors } />;
},
'withEditorColorPalette'
Expand Down
6 changes: 2 additions & 4 deletions packages/block-editor/src/components/font-family/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';

export default function FontFamilyControl( {
value = '',
onChange,
fontFamilies,
...props
} ) {
const blockLevelFontFamilies = useEditorFeature(
'typography.fontFamilies'
);
const blockLevelFontFamilies = useSetting( 'typography.fontFamilies' );
if ( ! fontFamilies ) {
fontFamilies = blockLevelFontFamilies;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import { FontSizePicker as BaseFontSizePicker } from '@wordpress/components';
/**
* Internal dependencies
*/
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';

function FontSizePicker( props ) {
const fontSizes = useEditorFeature( 'typography.fontSizes' );
const disableCustomFontSizes = ! useEditorFeature(
'typography.customFontSize'
);
const fontSizes = useSetting( 'typography.fontSizes' );
const disableCustomFontSizes = ! useSetting( 'typography.customFontSize' );

return (
<BaseFontSizePicker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Component } from '@wordpress/element';
* Internal dependencies
*/
import { getFontSize, getFontSizeClass } from './utils';
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';

const DEFAULT_FONT_SIZES = [];

Expand Down Expand Up @@ -49,7 +49,7 @@ export default ( ...fontSizeNames ) => {
createHigherOrderComponent(
( WrappedComponent ) => ( props ) => {
const fontSizes =
useEditorFeature( 'typography.fontSizes' ) ||
useSetting( 'typography.fontSizes' ) ||
DEFAULT_FONT_SIZES;
return (
<WrappedComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import GradientPicker from './';
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';

export default function GradientPickerControl( {
className,
Expand All @@ -23,8 +23,8 @@ export default function GradientPickerControl( {
label = __( 'Gradient Presets' ),
...props
} ) {
const gradients = useEditorFeature( 'color.gradients' );
const disableCustomGradients = ! useEditorFeature( 'color.customGradient' );
const gradients = useSetting( 'color.gradients' );
const disableCustomGradients = ! useSetting( 'color.customGradient' );
if ( isEmpty( gradients ) && disableCustomGradients ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { __experimentalGradientPicker as GradientPicker } from '@wordpress/compo
/**
* Internal dependencies
*/
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';

const EMPTY_ARRAY = [];

function GradientPickerWithGradients( props ) {
const gradients = useEditorFeature( 'color.gradients' ) || EMPTY_ARRAY;
const disableCustomGradients = ! useEditorFeature( 'color.customGradient' );
const gradients = useSetting( 'color.gradients' ) || EMPTY_ARRAY;
const disableCustomGradients = ! useSetting( 'color.customGradient' );

return (
<GradientPicker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import GradientPicker from './control';
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';

export default function GradientPanel( props ) {
const gradients = useEditorFeature( 'color.gradients' );
const gradients = useSetting( 'color.gradients' );
if ( isEmpty( gradients ) ) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useSelect, useDispatch } from '@wordpress/data';
* Internal dependencies
*/
import { useBlockEditContext } from '../block-edit';
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';
import { store as blockEditorStore } from '../../store';

const EMPTY_ARRAY = [];
Expand Down Expand Up @@ -67,7 +67,7 @@ export function __experimentalUseGradient( {
} = {} ) {
const { clientId } = useBlockEditContext();

const gradients = useEditorFeature( 'color.gradients' ) || EMPTY_ARRAY;
const gradients = useSetting( 'color.gradients' ) || EMPTY_ARRAY;
const { gradient, customGradient } = useSelect(
( select ) => {
const { getBlockAttributes } = select( blockEditorStore );
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ export { default as __experimentalUseNoRecursiveRenders } from './use-no-recursi

export { default as BlockEditorProvider } from './provider';
export { default as __experimentalUseSimulatedMediaQuery } from './use-simulated-media-query';
export { default as __experimentalUseEditorFeature } from './use-editor-feature';
export { default as useSetting } from './use-setting';
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export { default as BlockCaption } from './block-caption';
export { default as Caption } from './caption';
export { default as PanelColorSettings } from './panel-color-settings';
export { default as __experimentalPanelColorGradientSettings } from './colors-gradients/panel-color-gradient-settings';
export { default as __experimentalUseEditorFeature } from './use-editor-feature';
export { default as useSetting } from './use-setting';
export { default as __experimentalUseNoRecursiveRenders } from './use-no-recursive-renders';
export { default as Warning } from './warning';

Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/unit-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { __experimentalUnitControl as BaseUnitControl } from '@wordpress/compone
/**
* Internal dependencies
*/
import useEditorFeature from '../use-editor-feature';
import useSetting from '../use-setting';

export default function UnitControl( { units: unitsProp, ...props } ) {
const units = useCustomUnits( unitsProp );
Expand Down Expand Up @@ -36,7 +36,7 @@ function filterUnitsWithSettings( settings = [], units = [] ) {
* @return {Array} Filtered units based on settings.
*/
export function useCustomUnits( units ) {
const availableUnits = useEditorFeature( 'spacing.units' );
const availableUnits = useSetting( 'spacing.units' );
const usedUnits = filterUnitsWithSettings(
! availableUnits ? [] : availableUnits,
units
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ const deprecatedFlags = {
};

/**
* Hook that retrieves the setting for the given editor feature.
* Hook that retrieves the editor setting.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we clarify this to state it's not for all settings, but #31587 (comment) ?

* It works with nested objects using by finding the value at path.
*
* @param {string} featurePath The path to the feature.
* @param {string} path The path to the setting.
*
* @return {any} Returns the value defined for the setting.
*
* @example
* ```js
* const isEnabled = useEditorFeature( 'typography.dropCap' );
* const isEnabled = useSetting( 'typography.dropCap' );
* ```
*/
export default function useEditorFeature( featurePath ) {
export default function useSetting( path ) {
const { name: blockName } = useBlockEditContext();

const setting = useSelect(
Expand All @@ -71,17 +71,17 @@ export default function useEditorFeature( featurePath ) {

// 1 - Use __experimental features, if available.
// We cascade to the all value if the block one is not available.
const defaultsPath = `__experimentalFeatures.${ featurePath }`;
const blockPath = `__experimentalFeatures.blocks.${ blockName }.${ featurePath }`;
const defaultsPath = `__experimentalFeatures.${ path }`;
const blockPath = `__experimentalFeatures.blocks.${ blockName }.${ path }`;
const experimentalFeaturesResult =
get( settings, blockPath ) ?? get( settings, defaultsPath );
if ( experimentalFeaturesResult !== undefined ) {
return experimentalFeaturesResult;
}

// 2 - Use deprecated settings, otherwise.
const deprecatedSettingsValue = deprecatedFlags[ featurePath ]
? deprecatedFlags[ featurePath ]( settings )
const deprecatedSettingsValue = deprecatedFlags[ path ]
? deprecatedFlags[ path ]( settings )
: undefined;
if ( deprecatedSettingsValue !== undefined ) {
return deprecatedSettingsValue;
Expand All @@ -91,9 +91,9 @@ export default function useEditorFeature( featurePath ) {
// This is only necessary to support typography.dropCap.
// when __experimentalFeatures are not present (core without plugin).
// To remove when __experimentalFeatures are ported to core.
return featurePath === 'typography.dropCap' ? true : undefined;
return path === 'typography.dropCap' ? true : undefined;
},
[ blockName, featurePath ]
[ blockName, path ]
);

return setting;
Expand Down
11 changes: 5 additions & 6 deletions packages/block-editor/src/hooks/border-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
getColorObjectByColorValue,
getColorObjectByAttributeValues,
} from '../components/colors';
import useEditorFeature from '../components/use-editor-feature';
import useSetting from '../components/use-setting';
import { hasBorderSupport, shouldSkipSerialization } from './border';
import { cleanEmptyObject } from './utils';

Expand All @@ -44,10 +44,9 @@ export function BorderColorEdit( props ) {
attributes: { borderColor, style },
setAttributes,
} = props;
const colors = useEditorFeature( 'color.palette' ) || EMPTY_ARRAY;

const disableCustomColors = ! useEditorFeature( 'color.custom' );
const disableCustomGradients = ! useEditorFeature( 'color.customGradient' );
const colors = useSetting( 'color.palette' ) || EMPTY_ARRAY;
const disableCustomColors = ! useSetting( 'color.custom' );
const disableCustomGradients = ! useSetting( 'color.customGradient' );

const onChangeColor = ( value ) => {
const colorObject = getColorObjectByColorValue( colors, value );
Expand Down Expand Up @@ -181,7 +180,7 @@ export const withBorderColorPaletteStyles = createHigherOrderComponent(
( BlockListBlock ) => ( props ) => {
const { name, attributes } = props;
const { borderColor } = attributes;
const colors = useEditorFeature( 'color.palette' ) || EMPTY_ARRAY;
const colors = useSetting( 'color.palette' ) || EMPTY_ARRAY;

if (
! hasBorderSupport( name, 'color' ) ||
Expand Down
Loading