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

Fix Nav Block to allow overflow scroll when there are many horizontal nav items #18336

Closed
wants to merge 6 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
25 changes: 22 additions & 3 deletions packages/block-library/src/navigation-menu/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import classnames from 'classnames';
import {
useMemo,
useEffect,
useLayoutEffect,
useRef,
useState,
} from '@wordpress/element';
import {
InnerBlocks,
Expand Down Expand Up @@ -45,6 +48,19 @@ function NavigationMenu( {
setTextColor,
setAttributes,
} ) {
const scrollContainerRef = useRef();
const [ hasScrollX, setHasScrollX ] = useState( false );
const clientWidth = scrollContainerRef.current ? scrollContainerRef.current.clientWidth : 0;
const scrollWidth = scrollContainerRef.current ? scrollContainerRef.current.scrollWidth : 0;

useLayoutEffect( () => {
if ( scrollWidth > clientWidth ) {
setHasScrollX( true );
} else {
setHasScrollX( false );
}
}, [ clientWidth, scrollWidth ] );

const { navigatorToolbarButton, navigatorModal } = useBlockNavigator( clientId );
const defaultMenuItems = useMemo(
() => {
Expand Down Expand Up @@ -79,6 +95,7 @@ function NavigationMenu( {
'wp-block-navigation-menu', {
'has-text-color': textColor.color,
'has-background-color': backgroundColor.color,
'has-scroll-x': hasScrollX,
Copy link
Contributor

Choose a reason for hiding this comment

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

This classname doesn't seem to be used for anything. If we can scrap it we can also get rid of all the state and effect logic above.

[ attributes.backgroundColorCSSClass ]: attributes && attributes.backgroundColorCSSClass,
[ attributes.textColorCSSClass ]: attributes && attributes.textColorCSSClass,
}
Expand Down Expand Up @@ -148,15 +165,17 @@ function NavigationMenu( {
</InspectorControls>

<div className={ navigationMenuClasses } style={ navigationMenuInlineStyles }>
{ isRequesting && <><Spinner /> { __( 'Loading Navigation…' ) } </> }
{ pages &&
<div ref={ scrollContainerRef } className="wp-block-navigation-menu__scroll-container">
Copy link
Contributor

Choose a reason for hiding this comment

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

The scroll container is unnecessary; the styles can just as easily be applied to wp-block-navigation-menu.
Also, it seems to be causing small navs to get a horizontal scrollbar:

Screen Shot 2019-11-13 at 4 28 26 pm

{ isRequesting && <><Spinner /> { __( 'Loading Navigation…' ) } </> }
{ pages &&
<InnerBlocks
template={ defaultMenuItems ? defaultMenuItems : null }
allowedBlocks={ [ 'core/navigation-menu-item' ] }
templateInsertUpdatesSelection={ false }
__experimentalMoverDirection={ 'horizontal' }
/>
}
}
</div>
</div>
</>
);
Expand Down
25 changes: 24 additions & 1 deletion packages/block-library/src/navigation-menu/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@
// @todo: eventually we may add a feature that lets a parent container absorb the block UI of a child block.
// When that happens, leverage that instead of the following overrides.
[data-type="core/navigation-menu"] {

// Allow overflowing items to scroll container
.wp-block-navigation-menu > .wp-block-navigation-menu__scroll-container {
// if X is "auto" or "scroll" then spec says Y must also assume this value
// therefore the Y overflow is effectively "clipped" meaning that the toolbar
// of child Blocks cannot display correctly. We "fix" this below.
// Note: as Y will be "auto" anyway we need to hide the scroll bars
// created by the artificial spacing.
overflow-x: auto;
overflow-y: hidden; // required for FF

// Hack: create artificial overflow space within the element which has its Y
// value clipped (see above). This value is equal to that of the positioned
// toolbar element (usually `top: -51px`).
padding-top: 51px;
margin-top: -51px;
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be nice to add padding-left: 17px here to avoid the toolbar on the first nav element being cut off:

Screen Shot 2019-11-13 at 4 41 29 pm

}

[data-type="core/navigation-menu-item"] > .block-editor-block-list__block-edit .block-editor-block-contextual-toolbar {
// Hack - see above. Reset top position value.
top: 0;
}

// 1. Reset margins on immediate innerblocks container.
.wp-block-navigation-menu .block-editor-inner-blocks > .block-editor-block-list__layout {
margin-left: 0;
Expand All @@ -13,7 +36,7 @@
width: auto;
padding-left: 0;
padding-right: 0;
margin-left: 0; // something
margin-left: 0;
}

// 3. Remove margins on subsequent Edit container.
Expand Down