diff --git a/packages/block-library/src/navigation-menu/edit.js b/packages/block-library/src/navigation-menu/edit.js index 7bba746cd0833c..388ef39e7f91a4 100644 --- a/packages/block-library/src/navigation-menu/edit.js +++ b/packages/block-library/src/navigation-menu/edit.js @@ -9,6 +9,9 @@ import classnames from 'classnames'; import { useMemo, useEffect, + useLayoutEffect, + useRef, + useState, } from '@wordpress/element'; import { InnerBlocks, @@ -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( () => { @@ -79,6 +95,7 @@ function NavigationMenu( { 'wp-block-navigation-menu', { 'has-text-color': textColor.color, 'has-background-color': backgroundColor.color, + 'has-scroll-x': hasScrollX, [ attributes.backgroundColorCSSClass ]: attributes && attributes.backgroundColorCSSClass, [ attributes.textColorCSSClass ]: attributes && attributes.textColorCSSClass, } @@ -148,15 +165,17 @@ function NavigationMenu( {
- { isRequesting && <> { __( 'Loading Navigation…' ) } } - { pages && +
+ { isRequesting && <> { __( 'Loading Navigation…' ) } } + { pages && - } + } +
); diff --git a/packages/block-library/src/navigation-menu/editor.scss b/packages/block-library/src/navigation-menu/editor.scss index d23df6cd6c6f3d..7d07ce9b5086fb 100644 --- a/packages/block-library/src/navigation-menu/editor.scss +++ b/packages/block-library/src/navigation-menu/editor.scss @@ -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; + } + + [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; @@ -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.