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

Remove: Alignment options from button nested inside buttons #19824

Merged
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: 15 additions & 4 deletions packages/block-editor/src/hooks/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { assign, get, has, includes, without } from 'lodash';
/**
* WordPress dependencies
*/
import { createContext, useContext } from '@wordpress/element';
import { createHigherOrderComponent } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';
import {
Expand Down Expand Up @@ -99,6 +100,13 @@ export function addAttribute( settings ) {
return settings;
}

const AlignmentHookSettings = createContext( {} );

/**
* Allows to pass additional settings to the alignment hook.
*/
export const AlignmentHookSettingsProvider = AlignmentHookSettings.Provider;

/**
* Override the default edit UI to include new toolbar controls for block
* alignment, if block defines support.
Expand All @@ -108,14 +116,17 @@ export function addAttribute( settings ) {
*/
export const withToolbarControls = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const { isEmbedButton } = useContext( AlignmentHookSettings );
const { name: blockName } = props;
// Compute valid alignments without taking into account,
// if the theme supports wide alignments or not.
// BlockAlignmentToolbar takes into account the theme support.
const validAlignments = getValidAlignments(
getBlockSupport( blockName, 'align' ),
hasBlockSupport( blockName, 'alignWide', true )
);
const validAlignments = isEmbedButton
? []
: getValidAlignments(
getBlockSupport( blockName, 'align' ),
hasBlockSupport( blockName, 'alignWide', true )
);

const updateAlignment = ( nextAlign ) => {
if ( ! nextAlign ) {
Expand Down
4 changes: 3 additions & 1 deletion packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/**
* Internal dependencies
*/
import './align';
import { AlignmentHookSettingsProvider } from './align';
import './anchor';
import './custom-class-name';
import './generated-class-name';

export { AlignmentHookSettingsProvider };
3 changes: 2 additions & 1 deletion packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import '@wordpress/keyboard-shortcuts';
/**
* Internal dependencies
*/
import './hooks';
import { AlignmentHookSettingsProvider as __experimentalAlignmentHookSettingsProvider } from './hooks';
export { __experimentalAlignmentHookSettingsProvider };
export * from './components';
export * from './utils';
export { storeConfig } from './store';
Expand Down
24 changes: 17 additions & 7 deletions packages/block-library/src/buttons/edit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';
import {
__experimentalAlignmentHookSettingsProvider as AlignmentHookSettingsProvider,
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess my thinking was that it's the responsibility of the "buttons" to create the context and provide it while the "button" would consume it. And this is possible with the private API.

It seems the fact that we use the "align support" make this impossible.

Copy link
Member

Choose a reason for hiding this comment

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

@youknowriad Maybe we should implement a hook for alignment, similar to the useColors hook, and start using that everywhere instead of the supports flag. Would that make what you want to do possible?

InnerBlocks,
} from '@wordpress/block-editor';

/**
* Internal dependencies
Expand All @@ -14,15 +17,22 @@ const UI_PARTS = {
hasSelectedUI: false,
};

// Inside buttons block alignment options are not supported.
const alignmentHooksSetting = {
isEmbedButton: true,
};

function ButtonsEdit( { className } ) {
return (
<div className={ className }>
<InnerBlocks
allowedBlocks={ ALLOWED_BLOCKS }
template={ BUTTONS_TEMPLATE }
__experimentalUIParts={ UI_PARTS }
__experimentalMoverDirection="horizontal"
/>
<AlignmentHookSettingsProvider value={ alignmentHooksSetting }>
<InnerBlocks
allowedBlocks={ ALLOWED_BLOCKS }
template={ BUTTONS_TEMPLATE }
__experimentalUIParts={ UI_PARTS }
__experimentalMoverDirection="horizontal"
/>
</AlignmentHookSettingsProvider>
</div>
);
}
Expand Down