NavigationLink: fix getting Navigation parent block#20032
Conversation
|
I've made a testing document to test this also outside the Navigation. Made Columns > Column > Cover > Paragraph and tried: |
marekhrabe
left a comment
There was a problem hiding this comment.
This is working well for me in separate testing and also in the implementation in Navigation. Colors are passed properly, as described
| ascending = false | ||
| ) => { | ||
| const parents = getBlockParents( state, clientId, ascending ); | ||
| return map( |
There was a problem hiding this comment.
This selector will return new array on each call potentially causing performance issues, should we use createSelector?
| const { clientId } = ownProps; | ||
| const rootBlock = getBlockParents( clientId )[ 0 ]; | ||
| const rootBlock = head( | ||
| getBlockParentsByBlockName( clientId, 'core/navigation' ) |
There was a problem hiding this comment.
If I'm not mistaken, thecore/navigation block is always the direct parent to navigaton-link which means it's the last item in getBlockParents(clientId)
can't we solve this by just doing last(getBlockParents( clientId )) instead of introducing a new API (selector)?

Description
This PR fixes an issue which was found by @getdave reviewing this PR):
...However, I noticed a potential coding issue where we're expecting the rootBlock to always be the Nav Block:
...when in actual fact I think it could actually be any parent Block that uses
InnerBlocks(eg: Columms) as it will return the topmost Block in the hierarchy. Therefore if the Block is nested then this logic is problematic.I tested this theory by:
styleattribute set on the DOM on the Nav Link.It appears in the above scenario it doesn't work as the style wasn't applied to the Navigation Link Block, only to the parent Navigation Block.
So, in order to fix this issue what we do is adding a new
getBlockParentsByBlockName()selector, which returns an array of parent client IDs, but also filtered by block name. So in this way, will ensure to get the proper attributes of the parent block:Then it's a matter of getting the first item of the array and pick up the block attributes.
How has this been tested?
styleattribute set on the DOM on the Nav Link.before
5) The colors attributes of the Navigation block are not propagated to the NavigationLink and the colors are not applied to the sub-menus:
after
6) The colors are righty applied to the NavigationLink blocks
Types of changes
Checklist: