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

[Mobile] - Buttons block - Support content justification #26166

Merged
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
66 changes: 46 additions & 20 deletions packages/block-library/src/buttons/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@ import { View } from 'react-native';
* WordPress dependencies
*/
import {
BlockControls,
__experimentalAlignmentHookSettingsProvider as AlignmentHookSettingsProvider,
InnerBlocks,
} from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';
import { useResizeObserver } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { useState, useEffect, useRef } from '@wordpress/element';
import { ToolbarGroup, ToolbarItem } from '@wordpress/components';

/**
* Internal dependencies
*/
import { name as buttonBlockName } from '../button/';
import styles from './editor.scss';
import ContentJustificationDropdown from './content-justification-dropdown';

const ALLOWED_BLOCKS = [ buttonBlockName ];
const BUTTONS_TEMPLATE = [ [ 'core/button' ] ];

export default function ButtonsEdit( {
attributes: { align },
attributes: { contentJustification },
clientId,
isSelected,
setAttributes,
} ) {
const [ resizeObserver, sizes ] = useResizeObserver();
const [ maxWidth, setMaxWidth ] = useState( 0 );
Expand Down Expand Up @@ -89,6 +93,12 @@ export default function ButtonsEdit( {
selectBlock( insertedBlock.clientId );
}, 200 );

function onChangeContentJustification( updatedValue ) {
setAttributes( {
contentJustification: updatedValue,
} );
}

const renderFooterAppender = useRef( () => (
<View style={ styles.appenderContainer }>
<InnerBlocks.ButtonBlockAppender
Expand All @@ -104,24 +114,40 @@ export default function ButtonsEdit( {
};

return (
<AlignmentHookSettingsProvider value={ alignmentHooksSetting }>
{ resizeObserver }
<InnerBlocks
allowedBlocks={ ALLOWED_BLOCKS }
template={ BUTTONS_TEMPLATE }
renderFooterAppender={
shouldRenderFooterAppender && renderFooterAppender.current
}
orientation="horizontal"
horizontalAlignment={ align }
onDeleteBlock={
shouldDelete ? () => removeBlock( clientId ) : undefined
}
onAddBlock={ onAddNextButton }
parentWidth={ maxWidth }
marginHorizontal={ spacing }
marginVertical={ spacing }
/>
</AlignmentHookSettingsProvider>
<>
<BlockControls>
<ToolbarGroup>
<ToolbarItem>
{ ( toggleProps ) => (
<ContentJustificationDropdown
toggleProps={ toggleProps }
value={ contentJustification }
onChange={ onChangeContentJustification }
/>
) }
</ToolbarItem>
</ToolbarGroup>
</BlockControls>
<AlignmentHookSettingsProvider value={ alignmentHooksSetting }>
{ resizeObserver }
<InnerBlocks
allowedBlocks={ ALLOWED_BLOCKS }
template={ BUTTONS_TEMPLATE }
renderFooterAppender={
shouldRenderFooterAppender &&
renderFooterAppender.current
}
orientation="horizontal"
horizontalAlignment={ contentJustification }
onDeleteBlock={
shouldDelete ? () => removeBlock( clientId ) : undefined
}
onAddBlock={ onAddNextButton }
parentWidth={ maxWidth }
marginHorizontal={ spacing }
marginVertical={ spacing }
/>
</AlignmentHookSettingsProvider>
</>
);
}