Skip to content
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
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,12 @@
"markdown_source": "../packages/format-library/README.md",
"parent": "packages"
},
{
"title": "@wordpress/global-styles-engine",
"slug": "packages-global-styles-engine",
"markdown_source": "../packages/global-styles-engine/README.md",
"parent": "packages"
},
{
"title": "@wordpress/hooks",
"slug": "packages-hooks",
Expand Down
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@wordpress/dom": "file:../dom",
"@wordpress/element": "file:../element",
"@wordpress/escape-html": "file:../escape-html",
"@wordpress/global-styles-engine": "file:../global-styles-engine",
"@wordpress/hooks": "file:../hooks",
"@wordpress/html-entities": "file:../html-entities",
"@wordpress/i18n": "file:../i18n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import { useRef, useState, useEffect, useMemo } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { focus } from '@wordpress/dom';
import { isBlobURL } from '@wordpress/blob';
import { getResolvedValue } from '@wordpress/global-styles-engine';

/**
* Internal dependencies
*/
import { getResolvedValue } from '../global-styles/utils';
import { hasBackgroundImageValue } from '../global-styles/background-panel';
import { setImmutably } from '../../utils/object';
import MediaReplaceFlow from '../media-replace-flow';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import {
} from '@wordpress/components';
import { useCallback, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { getValueFromVariable } from '@wordpress/global-styles-engine';

/**
* Internal dependencies
*/
import BorderRadiusControl from '../border-radius-control';
import { useColorsPerOrigin } from './hooks';
import { getValueFromVariable, useToolsPanelDropdownMenuProps } from './utils';
import { useToolsPanelDropdownMenuProps } from './utils';
import { setImmutably } from '../../utils/object';
import { useBorderPanelLabel } from '../../hooks/border';
import { ShadowPopover, useShadowPresets } from './shadow-panel-components';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import {
} from '@wordpress/components';
import { useCallback, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { getValueFromVariable } from '@wordpress/global-styles-engine';

/**
* Internal dependencies
*/
import ColorGradientControl from '../colors-gradients/control';
import { useColorsPerOrigin, useGradientsPerOrigin } from './hooks';
import { getValueFromVariable, useToolsPanelDropdownMenuProps } from './utils';
import { useToolsPanelDropdownMenuProps } from './utils';
import { setImmutably } from '../../utils/object';
import { unlock } from '../../lock-unlock';
import { reset as resetIcon } from '@wordpress/icons';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { useSelect } from '@wordpress/data';
import { useEffect, useState, useMemo, useCallback } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { getValueFromVariable } from '@wordpress/global-styles-engine';

/**
* Internal dependencies
*/
import PanelColorGradientSettings from '../colors-gradients/panel-color-gradient-settings';
import { useColorsPerOrigin, useGradientsPerOrigin } from './hooks';
import { getValueFromVariable } from './utils';
import { setImmutably } from '../../utils/object';
import ContrastChecker from '../contrast-checker';
import InspectorControls from '../inspector-controls';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import {
} from '@wordpress/components';
import { Icon, alignNone, stretchWide } from '@wordpress/icons';
import { useCallback, useState, Platform } from '@wordpress/element';
import { getValueFromVariable } from '@wordpress/global-styles-engine';

/**
* Internal dependencies
*/
import { getValueFromVariable, useToolsPanelDropdownMenuProps } from './utils';
import { useToolsPanelDropdownMenuProps } from './utils';
import SpacingSizesControl from '../spacing-sizes-control';
import HeightControl from '../height-control';
import ChildLayoutControl from '../child-layout-control';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import {
import { __, _x } from '@wordpress/i18n';
import { useCallback, useMemo, useRef } from '@wordpress/element';
import { reset as resetIcon } from '@wordpress/icons';
import { getValueFromVariable } from '@wordpress/global-styles-engine';

/**
* Internal dependencies
*/
import { getValueFromVariable, useToolsPanelDropdownMenuProps } from './utils';
import { useToolsPanelDropdownMenuProps } from './utils';
import { setImmutably } from '../../utils/object';

const EMPTY_ARRAY = [];
Expand Down
137 changes: 29 additions & 108 deletions packages/block-editor/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,81 +10,21 @@ import { useContext, useCallback, useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { store as blocksStore } from '@wordpress/blocks';
import { _x } from '@wordpress/i18n';
import {
getSetting,
getStyle,
getPresetVariableFromValue,
} from '@wordpress/global-styles-engine';

/**
* Internal dependencies
*/
import { getValueFromVariable, getPresetVariableFromValue } from './utils';
import { getValueFromObjectPath, setImmutably } from '../../utils/object';
import { setImmutably } from '../../utils/object';
import { GlobalStylesContext } from './context';
import { unlock } from '../../lock-unlock';

const EMPTY_CONFIG = { settings: {}, styles: {} };

const VALID_SETTINGS = [
'appearanceTools',
'useRootPaddingAwareAlignments',
'background.backgroundImage',
'background.backgroundRepeat',
'background.backgroundSize',
'background.backgroundPosition',
'border.color',
'border.radius',
'border.style',
'border.width',
'border.radiusSizes',
'shadow.presets',
'shadow.defaultPresets',
'color.background',
'color.button',
'color.caption',
'color.custom',
'color.customDuotone',
'color.customGradient',
'color.defaultDuotone',
'color.defaultGradients',
'color.defaultPalette',
'color.duotone',
'color.gradients',
'color.heading',
'color.link',
'color.palette',
'color.text',
'custom',
'dimensions.aspectRatio',
'dimensions.minHeight',
'layout.contentSize',
'layout.definitions',
'layout.wideSize',
'lightbox.enabled',
'lightbox.allowEditing',
'position.fixed',
'position.sticky',
'spacing.customSpacingSize',
'spacing.defaultSpacingSizes',
'spacing.spacingSizes',
'spacing.spacingScale',
'spacing.blockGap',
'spacing.margin',
'spacing.padding',
'spacing.units',
'typography.fluid',
'typography.customFontSize',
'typography.defaultFontSizes',
'typography.dropCap',
'typography.fontFamilies',
'typography.fontSizes',
'typography.fontStyle',
'typography.fontWeight',
'typography.letterSpacing',
'typography.lineHeight',
'typography.textAlign',
'typography.textColumns',
'typography.textDecoration',
'typography.textTransform',
'typography.writingMode',
];

export const useGlobalStylesReset = () => {
const { user, setUserConfig } = useContext( GlobalStylesContext );
const config = {
Expand All @@ -103,7 +43,6 @@ export function useGlobalSetting( propertyPath, blockName, source = 'all' ) {
const appendedBlockPath = blockName ? '.blocks.' + blockName : '';
const appendedPropertyPath = propertyPath ? '.' + propertyPath : '';
const contextualPath = `settings${ appendedBlockPath }${ appendedPropertyPath }`;
const globalPath = `settings${ appendedPropertyPath }`;
const sourceKey = source === 'all' ? 'merged' : source;

const settingValue = useMemo( () => {
Expand All @@ -112,34 +51,9 @@ export function useGlobalSetting( propertyPath, blockName, source = 'all' ) {
throw 'Unsupported source';
}

if ( propertyPath ) {
return (
getValueFromObjectPath( configToUse, contextualPath ) ??
getValueFromObjectPath( configToUse, globalPath )
);
}

let result = {};
VALID_SETTINGS.forEach( ( setting ) => {
const value =
getValueFromObjectPath(
configToUse,
`settings${ appendedBlockPath }.${ setting }`
) ??
getValueFromObjectPath( configToUse, `settings.${ setting }` );
if ( value !== undefined ) {
result = setImmutably( result, setting.split( '.' ), value );
}
} );
return result;
}, [
configs,
sourceKey,
propertyPath,
contextualPath,
globalPath,
appendedBlockPath,
] );
// Use engine's getSetting instead of duplicating logic
return getSetting( configToUse, propertyPath, blockName );
}, [ configs, sourceKey, propertyPath, blockName ] );

const setSetting = ( newValue ) => {
setUserConfig( ( currentConfig ) =>
Expand Down Expand Up @@ -183,25 +97,32 @@ export function useGlobalStyle(
);
};

let rawResult, result;
let result;
// Use engine's getStyle instead of duplicating logic
switch ( source ) {
case 'all':
rawResult = getValueFromObjectPath( mergedConfig, finalPath );
result = shouldDecodeEncode
? getValueFromVariable( mergedConfig, blockName, rawResult )
: rawResult;
result = getStyle(
mergedConfig,
path,
blockName,
shouldDecodeEncode
);
break;
case 'user':
rawResult = getValueFromObjectPath( userConfig, finalPath );
result = shouldDecodeEncode
? getValueFromVariable( mergedConfig, blockName, rawResult )
: rawResult;
result = getStyle(
userConfig,
path,
blockName,
shouldDecodeEncode
);
break;
case 'base':
rawResult = getValueFromObjectPath( baseConfig, finalPath );
result = shouldDecodeEncode
? getValueFromVariable( baseConfig, blockName, rawResult )
: rawResult;
result = getStyle(
baseConfig,
path,
blockName,
shouldDecodeEncode
);
break;
default:
throw 'Unsupported source';
Expand Down
8 changes: 0 additions & 8 deletions packages/block-editor/src/components/global-styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ export {
useGlobalStyle,
useSettingsForBlockElement,
} from './hooks';
export { getBlockCSSSelector } from './get-block-css-selector';
export {
getLayoutStyles,
getBlockSelectors,
toStyles,
useGlobalStylesOutput,
useGlobalStylesOutputWithConfig,
} from './use-global-styles-output';
export { GlobalStylesContext } from './context';
export {
default as TypographyPanel,
Expand Down
Loading
Loading