-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ba0ba5
Fix to allow overflow scroll when many horiztonal nav items
getdave de302bb
Limit to x axis only
getdave 4a1f457
Updates to make space on RHS for item block toolbars and block appender
getdave e5b4ab8
Updates to only add RHS padding if scroll is present
getdave 582de84
Refactor to optimise scroll detection
getdave 01383ce
Fixes clipped scrollbar using artifical padding technique
getdave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( { | |
</InspectorControls> | ||
|
||
<div className={ navigationMenuClasses } style={ navigationMenuInlineStyles }> | ||
{ isRequesting && <><Spinner /> { __( 'Loading Navigation…' ) } </> } | ||
{ pages && | ||
<div ref={ scrollContainerRef } className="wp-block-navigation-menu__scroll-container"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ isRequesting && <><Spinner /> { __( 'Loading Navigation…' ) } </> } | ||
{ pages && | ||
<InnerBlocks | ||
template={ defaultMenuItems ? defaultMenuItems : null } | ||
allowedBlocks={ [ 'core/navigation-menu-item' ] } | ||
templateInsertUpdatesSelection={ false } | ||
__experimentalMoverDirection={ 'horizontal' } | ||
/> | ||
} | ||
} | ||
</div> | ||
</div> | ||
</> | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
[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. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.