Skip to content

Commit

Permalink
Auto-collapse in nested blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Sep 12, 2018
1 parent 8332163 commit 1c954c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
1 change: 0 additions & 1 deletion packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ class ParagraphBlock extends Component {
<Fragment>
<BlockControls>
<AlignmentToolbar
isCollapsed
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
Expand Down
29 changes: 26 additions & 3 deletions packages/editor/src/components/alignment-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import { find } from 'lodash';
import { __ } from '@wordpress/i18n';
import { Toolbar } from '@wordpress/components';
import { withViewportMatch } from '@wordpress/viewport';
import { withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';

/**
* Internal dependencies
*/
import { withBlockEditContext } from '../block-edit/context';

const ALIGNMENT_CONTROLS = [
{
Expand All @@ -28,7 +35,7 @@ const ALIGNMENT_CONTROLS = [
},
];

function AlignmentToolbar( { isCollapsed, isLargeViewport, value, onChange } ) {
function AlignmentToolbar( { isCollapsed, value, onChange } ) {
function applyOrUnset( align ) {
return () => onChange( value === align ? undefined : align );
}
Expand All @@ -37,7 +44,7 @@ function AlignmentToolbar( { isCollapsed, isLargeViewport, value, onChange } ) {

return (
<Toolbar
isCollapsed={ isCollapsed || ! isLargeViewport }
isCollapsed={ isCollapsed }
icon={ activeAlignment ? activeAlignment.icon : 'editor-alignleft' }
label={ __( 'Change Text Alignment' ) }
controls={ ALIGNMENT_CONTROLS.map( ( control ) => {
Expand All @@ -54,4 +61,20 @@ function AlignmentToolbar( { isCollapsed, isLargeViewport, value, onChange } ) {
);
}

export default withViewportMatch( { isLargeViewport: 'medium' } )( AlignmentToolbar );
export default compose(
withBlockEditContext( ( { clientId } ) => {
return {
clientId,
};
} ),
withViewportMatch( { isLargeViewport: 'medium' } ),
withSelect( ( select, { clientId, isLargeViewport, isCollapsed } ) => {
const { getBlockRootClientId, getEditorSettings } = select( 'core/editor' );
return {
isCollapsed: isCollapsed || ! isLargeViewport || (
getBlockRootClientId( clientId ) &&
! getEditorSettings().hasFixedToolbar
),
};
} ),
)( AlignmentToolbar );

0 comments on commit 1c954c4

Please sign in to comment.