Skip to content

Commit

Permalink
This commit adds a new filter for block supports controls for global …
Browse files Browse the repository at this point in the history
…styles so we can hook into the global styles context.
  • Loading branch information
ramonjd committed Nov 24, 2022
1 parent 89ff6cf commit 9d2cc43
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 15 deletions.
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 @@ -156,7 +156,7 @@ export {
export { default as __experimentalBlockPatternsList } from './block-patterns-list';
export { default as __experimentalPublishDateTimePicker } from './publish-date-time-picker';
export { default as __experimentalInspectorPopoverHeader } from './inspector-popover-header';

export { default as useDisplayBlockControls } from './use-display-block-controls';
/*
* State Related Components
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/block-editor/src/hooks/gap.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export function GapEdit( props ) {
attributes: { style },
name: blockName,
setAttributes,
setBlockGlobalStyles,
} = props;

const spacingSizes = useSetting( 'spacing.spacingSizes' );
Expand Down Expand Up @@ -175,6 +176,11 @@ export function GapEdit( props ) {
style: cleanEmptyObject( newStyle ),
} );

setBlockGlobalStyles(
'spacing.blockGap',
cleanEmptyObject( newStyle )
);

// In Safari, changing the `gap` CSS value on its own will not trigger the layout
// to be recalculated / re-rendered. To force the updated gap to re-render, here
// we replace the block's node with itself.
Expand Down
4 changes: 4 additions & 0 deletions packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ export { getSpacingClassesAndStyles } from './use-spacing-props';
export { getTypographyClassesAndStyles } from './use-typography-props';
export { getGapCSSValue } from './gap';
export { useCachedTruthy } from './use-cached-truthy';
export { BORDER_SUPPORT_KEY, BorderPanel } from './border';
export { COLOR_SUPPORT_KEY, ColorEdit } from './color';
export { TypographyPanel, TYPOGRAPHY_SUPPORT_KEY } from './typography';
export { SPACING_SUPPORT_KEY, DimensionsPanel } from './dimensions';
5 changes: 1 addition & 4 deletions packages/block-editor/src/hooks/padding.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ export function PaddingEdit( props ) {
style: cleanEmptyObject( newStyle ),
} );

setBlockGlobalStyles(
'spacing.padding',
cleanEmptyObject( newStyle )
);
setBlockGlobalStyles( 'spacing.padding', cleanEmptyObject( newStyle ) );
};

return Platform.select( {
Expand Down
8 changes: 3 additions & 5 deletions packages/block-editor/src/hooks/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,12 @@ export const withBlockControls = createHigherOrderComponent(
const shouldDisplayControls = useDisplayBlockControls();

return (
<>
<BlockGlobalStylesProvider>
{ shouldDisplayControls && (
<BlockGlobalStylesProvider>
<StylePanels { ...props } />
</BlockGlobalStylesProvider>
<StylePanels { ...props } />
) }
<BlockEdit { ...props } />
</>
</BlockGlobalStylesProvider>
);
},
'withToolbarControls'
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/typography.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* WordPress dependencies
*/
import { getBlockSupport, hasBlockSupport } from '@wordpress/blocks';
Expand Down
4 changes: 4 additions & 0 deletions packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export {
useCachedTruthy,
useLayoutClasses as __experimentaluseLayoutClasses,
useLayoutStyles as __experimentaluseLayoutStyles,
ColorEdit,
TypographyPanel,
BorderPanel,
DimensionsPanel,
} from './hooks';
export * from './components';
export * from './elements';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
__unstablePresetDuotoneFilter as PresetDuotoneFilter,
__experimentalGetGapCSSValue as getGapCSSValue,
store as blockEditorStore,
BlockGlobalStylesContext,
} from '@wordpress/block-editor';

/**
Expand Down Expand Up @@ -883,7 +884,10 @@ function updateConfigWithSeparator( config ) {

export function useGlobalStylesOutput() {
let { merged: mergedConfig } = useContext( GlobalStylesContext );

const { merged: mergedBlockStyles } = useContext(
BlockGlobalStylesContext
);
console.log( 'mergedBlockStyles', mergedBlockStyles );
const [ blockGap ] = useSetting( 'spacing.blockGap' );
const hasBlockGapSupport = blockGap !== null;
const hasFallbackGapSupport = ! hasBlockGapSupport; // This setting isn't useful yet: it exists as a placeholder for a future explicit fallback styles support.
Expand Down
102 changes: 102 additions & 0 deletions packages/edit-site/src/hooks/block-supports-styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* External dependencies
*/
import { set } from 'lodash';

/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { createHigherOrderComponent } from '@wordpress/compose';
import { useContext, useState } from '@wordpress/element';
import {
ColorEdit,
TypographyPanel,
BorderPanel,
DimensionsPanel,
useDisplayBlockControls,
InspectorControls,
} from '@wordpress/block-editor';
import { Button, PanelBody } from '@wordpress/components';
import { getCSSVarFromStyleValue } from '@wordpress/style-engine';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { GlobalStylesContext } from '../components/global-styles/context';

export const withEditBlockControls = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const { name } = props;
const [ shouldPushToGlobalStyles, setShouldPushToGlobalStyles ] =
useState( false );
const { setUserConfig } = useContext( GlobalStylesContext );

const shouldDisplayControls = useDisplayBlockControls();
const blockSupportControlProps = {
...props,
setBlockGlobalStyles: ( path, newValue ) => {
if ( ! shouldPushToGlobalStyles ) {
return;
}
const fullPath = `styles.blocks.${ name }.${ path }`;
setUserConfig( ( currentConfig ) => {
// Deep clone `currentConfig` to avoid mutating it later.
const newUserConfig = JSON.parse(
JSON.stringify( currentConfig )
);
set( newUserConfig, fullPath, getCSSVarFromStyleValue( newValue ) );
return newUserConfig;
} );
},
};

return (
<>
{ shouldDisplayControls && (
<>
<ColorEdit { ...blockSupportControlProps } />
<TypographyPanel { ...blockSupportControlProps } />
<BorderPanel { ...blockSupportControlProps } />
<DimensionsPanel { ...blockSupportControlProps } />
<InspectorControls __experimentalGroup="advanced">
<PanelBody
title={ __( 'Apply styles to all blocks' ) }
>
<Button
variant={
! shouldPushToGlobalStyles
? 'primary'
: 'secondary'
}
isPressed={ shouldPushToGlobalStyles }
onClick={ () => {
setShouldPushToGlobalStyles(
! shouldPushToGlobalStyles
);
} }
>
{ shouldPushToGlobalStyles ? __( 'Do not apply changes' ) : __( 'Apply changes' ) }
</Button>
<p>
{ __(
"When activated, all changes to this block's styles will be applied to all blocks of this type. Note that only typography, dimensions, color and border styles will be copied."
) }
</p>
</PanelBody>
</InspectorControls>
</>
) }
<BlockEdit { ...props } />
</>
);
},
'withEditBlockControls'
);

addFilter(
'editor.BlockEdit',
'core/edit-site/block-supports-styles',
withEditBlockControls
);
1 change: 1 addition & 0 deletions packages/edit-site/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*/
import './components';
import './template-part-edit';
import './block-supports-styles';
6 changes: 5 additions & 1 deletion packages/edit-site/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { store as preferencesStore } from '@wordpress/preferences';
import { __ } from '@wordpress/i18n';
import { store as viewportStore } from '@wordpress/viewport';
import { getQueryArgs } from '@wordpress/url';
import { addFilter } from '@wordpress/hooks';
import { addFilter, removeFilter } from '@wordpress/hooks';

/**
* Internal dependencies
Expand Down Expand Up @@ -167,6 +167,10 @@ export function initializeEditor( id, settings ) {
reinitializeEditor( target, settings );
}

// Removes the block supports edit controls added in packages/block-editor/src/hooks/style.js,
// so that we can register another in packages/edit-site/src/hooks/block-supports-styles.js.
removeFilter( 'editor.BlockEdit', 'core/style/with-block-controls' );

export { default as __experimentalNavigationToggle } from './components/navigation-sidebar/navigation-toggle';
export { default as PluginSidebar } from './components/sidebar-edit-mode/plugin-sidebar';
export { default as PluginSidebarMoreMenuItem } from './components/header-edit-mode/plugin-sidebar-more-menu-item';
Expand Down
5 changes: 4 additions & 1 deletion packages/style-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
GeneratedCSSRule,
StyleDefinition,
} from './types';
import { styleDefinitions } from './styles';
import { styleDefinitions, getCSSVarFromStyleValue } from './styles';

/**
* Generates a stylesheet for a given style object and selector.
Expand Down Expand Up @@ -78,3 +78,6 @@ export function getCSSRules(

return rules;
}

/* Styles utility functions */
export { getCSSVarFromStyleValue };
5 changes: 4 additions & 1 deletion packages/style-engine/src/styles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import shadow from './shadow';
import outline from './outline';
import spacing from './spacing';
import typography from './typography';
import { getCSSVarFromStyleValue } from './utils';

export const styleDefinitions = [
const styleDefinitions = [
...border,
...color,
...dimensions,
Expand All @@ -18,3 +19,5 @@ export const styleDefinitions = [
...typography,
...shadow,
];

export { getCSSVarFromStyleValue, styleDefinitions };

0 comments on commit 9d2cc43

Please sign in to comment.