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

Try/block toolbar refactoring #9282

Closed
wants to merge 7 commits into from
Closed
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
1 change: 0 additions & 1 deletion edit-post/assets/stylesheets/_z-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ $z-layers: (
".block-library-image__resize-handlers": 1, // Resize handlers above sibling inserter

// Side UI active buttons
".editor-block-settings-remove": 1,
".editor-block-mover__control": 1,

// Active pill button
Expand Down
39 changes: 30 additions & 9 deletions edit-post/components/header/header-toolbar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
}

.edit-post-header-toolbar {
display: inline-flex;
align-items: center;
display: flex;
align-items: stretch;
flex-grow: 1;
position: relative;

@include break-large() {
.editor-block-switcher .components-toolbar {
Expand All @@ -28,16 +30,34 @@
background: $white;
min-height: $block-toolbar-height;
border-bottom: $border-width solid $light-gray-500;
flex-grow: 1;

.editor-block-toolbar {
position: absolute;
top: -9px;
left: 9px;
right: 0;
bottom: -9px;
border-left: none;
}
border-top: 0;
border-bottom: 0;

.editor-block-toolbar .components-toolbar {
border-top: none;
border-bottom: none;
margin: -9px 0;
padding: 9px;
.components-toolbar {
border-top: none;
border-bottom: none;
padding: 9px;
}

.editor-block-toolbar__tools-dropdown {
.components-toolbar {
border-bottom: $border-width solid $light-gray-500;
border-top: $border-width solid $light-gray-500;
padding: 0;
margin: 0;
margin-left: -1px;
margin-top: -1px;
}
}
}

.is-sidebar-opened & {
Expand All @@ -54,9 +74,10 @@
// Move toolbar into top Editor Bar.
@include break-large {
padding-left: $item-spacing;
position: static;
position: relative;
left: auto;
right: auto;
top: auto;
background: none;
border-bottom: none;
min-height: auto;
Expand Down
10 changes: 7 additions & 3 deletions edit-post/components/header/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
border-bottom: $border-width solid $light-gray-500;
background: $white;
display: flex;
flex-direction: row;
align-items: stretch;
align-items: center;
justify-content: space-between;
z-index: z-index(".edit-post-header");
left: 0;
Expand Down Expand Up @@ -39,8 +38,13 @@
@include editor-left(".edit-post-header");

.edit-post-header__settings {
display: inline-flex;
display: flex;
align-items: center;
flex-shrink: 0;

& > * {
flex-shrink: 0;
}
}

.edit-post-header .components-button {
Expand Down
54 changes: 54 additions & 0 deletions packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,60 @@ class ParagraphBlock extends Component {
setAttributes( { align: nextAlign } );
} }
/>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
<AlignmentToolbar
value={ align }
onChange={ ( nextAlign ) => {
setAttributes( { align: nextAlign } );
} }
/>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Text Settings' ) } className="blocks-font-size">
Expand Down
17 changes: 15 additions & 2 deletions packages/components/src/dropdown/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -61,6 +66,7 @@ class Dropdown extends Component {
const {
renderContent,
renderToggle,
noArrow = false,
position = 'bottom',
className,
contentClassName,
Expand All @@ -69,6 +75,7 @@ class Dropdown extends Component {
} = this.props;

const args = { isOpen, onToggle: this.toggle, onClose: this.close };
const content = renderContent( args );

return (
<div className={ className } ref={ this.bindContainer }>
Expand All @@ -78,18 +85,24 @@ class Dropdown extends Component {
*/ }
<div>
{ renderToggle( args ) }
{ isOpen && (
{ isOpen && position !== 'inline' && (
<Popover
className={ contentClassName }
position={ position }
onClose={ this.close }
onClickOutside={ this.clickOutside }
expandOnMobile={ expandOnMobile }
headerTitle={ headerTitle }
noArrow={ noArrow }
>
{ renderContent( args ) }
{ content }
</Popover>
) }
{ isOpen && position === 'inline' && (
<div className={ classnames( 'components-dropdown__inline-content', contentClassName ) }>
{ content }
</div>
) }
</div>
</div>
);
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export { default as Popover } from './popover';
export { default as QueryControls } from './query-controls';
export { default as RadioControl } from './radio-control';
export { default as RangeControl } from './range-control';
export { default as ResponsiveToolbar } from './responsive-toolbar';
export { default as ResponsiveWrapper } from './responsive-wrapper';
export { default as SandBox } from './sandbox';
export { default as SelectControl } from './select-control';
Expand Down
24 changes: 24 additions & 0 deletions packages/components/src/responsive-toolbar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# ResponsiveToolbar

A wrapper that displays a resonsive toolbar. If there's enough space in the wrapper, the children elements are shown inline. If not, the elements move to a dropdown menu.

## Usage

```jsx
import { ResponsiveToolbar } from '@wordpress/components';

const MyResponsiveToolbar = () => (
<ResponsiveToolbar>
<IconButton icon="plus" />
Copy link
Member

Choose a reason for hiding this comment

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

Tab once more.

<Button>Long Button</Button>
<IconButton icon="minus" />
<div>
<Button>Group1</Button>
<Button>Group2</Button>
<Button>Group3</Button>
</div>
<Button>Other Button</Button>
<Button>Long Button</Button>
</ResponsiveToolbar>
);
```
154 changes: 154 additions & 0 deletions packages/components/src/responsive-toolbar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { Component, createRef } from '@wordpress/element';
import { withInstanceId } from '@wordpress/compose';

/**
* Internal dependencies
*/
import IconButton from '../icon-button';
import Dropdown from '../dropdown';
import Disabled from '../disabled';

/**
* Module constants
*/
const OFFSET = 60;

class ResponsiveToolbar extends Component {
constructor() {
super( ...arguments );
this.state = {
countHiddenChildren: 0,
};
this.container = createRef();
this.hiddenContainer = createRef();

this.updateHiddenItems = this.updateHiddenItems.bind( this );
this.throttledUpdateHiddenItems = this.throttledUpdateHiddenItems.bind( this );
}

componentDidMount() {
this.toggleWindowEvents( true );
this.updateHiddenItems();

// If the children change, we need to recompute
this.observer = new window.MutationObserver( this.updateHiddenItems );
this.observer.observe( this.hiddenContainer.current, { childList: true } );
}

componentWillUnmount() {
this.toggleWindowEvents( false );
this.observer.disconnect();
if ( this.style ) {
this.style.parentNode.removeChild( this.style );
}
}

toggleWindowEvents( isListening ) {
const handler = isListening ? 'addEventListener' : 'removeEventListener';

window.cancelAnimationFrame( this.rafHandle );
window[ handler ]( 'resize', this.throttledUpdateHiddenItems );
Copy link
Member

Choose a reason for hiding this comment

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

Should we use withGlobalEvents instead?

}

throttledUpdateHiddenItems() {
this.rafHandle = window.requestAnimationFrame( () => this.updateHiddenItems() );
}

updateHiddenItems() {
const { instanceId } = this.props;
const containerRect = this.container.current.getBoundingClientRect();
let countHiddenChildren = 0;
const total = this.hiddenContainer.current.childNodes.length;
this.hiddenContainer.current.childNodes.forEach( ( child ) => {
const childRect = child.getBoundingClientRect();
if (
childRect.left < containerRect.left ||
childRect.right > containerRect.right - OFFSET
) {
countHiddenChildren++;
Copy link
Member

Choose a reason for hiding this comment

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

Minor: This would make sense as a reduce, though I suppose childNodes is a NodeList, not Array, so not natively available without either using Lodash or converting to array [ ...childNodes ]:

const total = childNodes.reduce( ( result, child ) => {
	// ...
}, 0 );

}
} );

if ( countHiddenChildren !== this.state.countHiddenChildren ) {
Copy link
Member

Choose a reason for hiding this comment

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

Early return on inverse to avoid tabbing rest of function?

this.setState( {
countHiddenChildren,
} );

if ( this.style ) {
this.style.parentNode.removeChild( this.style );
}
const styleNode = document.createElement( 'style' );
styleNode.innerHTML = `
#responsive-toolbar-${ instanceId } > *:nth-child(n+${ total - countHiddenChildren + 2 }):not(.components-responsive-toolbar__dropdown) {
display: none;
}

.components-responsive-toolbar__dropdown-content-${ instanceId } > *:nth-child(-n+${ total - countHiddenChildren }) {
display: none;
}
`;
document.body.appendChild( styleNode );
this.style = styleNode;
}
}

render() {
const defaultRenderToggle = ( { onToggle, isOpen } ) => (
Copy link
Member

Choose a reason for hiding this comment

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

Why not a top-level constant variable? To avoid unnecessary garbage collection and React reconciliation.

<IconButton
icon="arrow-down-alt2"
onClick={ onToggle }
aria-expanded={ isOpen }
/>
);
const {
children,
instanceId,
className,
extraContentClassName,
renderToggle = defaultRenderToggle,
...props
} = this.props;
const { countHiddenChildren } = this.state;

return (
<div
id={ `responsive-toolbar-${ instanceId }` }
className={ classnames( className, 'components-responsive-toolbar' ) }
ref={ this.container }
{ ...props }
>
<Disabled>
<div className="components-responsive-toolbar__compute-position" ref={ this.hiddenContainer }>
{ children }
</div>
</Disabled>

{ children }

{ countHiddenChildren > 0 && (
<Dropdown
position="inline"
className="components-responsive-toolbar__dropdown"
contentClassName={ classnames(
extraContentClassName,
'components-responsive-toolbar__dropdown-content',
`components-responsive-toolbar__dropdown-content-${ instanceId }` )
}
renderToggle={ renderToggle }
renderContent={ () => children }
/>
) }
</div>
);
}
}

export default withInstanceId( ResponsiveToolbar );
Loading