-
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
Improve child blocks API's and UI. #7003
Conversation
blocks/store/selectors.js
Outdated
*/ | ||
export const hasChildBlocks = createSelector( | ||
( state, blockName ) => { | ||
return getChildBlocks( state, blockName ).length > 0; |
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 selector only wraps another selector. It shouldn’t use another layer of caching.
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.
Nice catch @gziolo, I updated the code!
2fb62dc
to
cc62ab9
Compare
Closes #6998. |
One of the unit tests is failing. |
464fff0
to
fe478b7
Compare
1339854
to
71239bb
Compare
Pushed some further tweaks to the design: @jasmussen I think the biggest thing missing is addressing the blurry icons :) |
Nice work. I unblurred the icons :D |
4afef04
to
4e9b6b9
Compare
4e9b6b9
to
562c558
Compare
I rebased this PR and it seems to be working fine. |
editor/components/inserter/menu.js
Outdated
<div className="editor-inserter__child-blocks"> | ||
<div className="editor-inserter__parent-block-header"> | ||
<div className="editor-inserter__parent-block-icon"> | ||
<BlockIcon icon={ rootBlockIcon } /> |
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.
BlockIcon should be rendered only when the icon is set
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.
Normally we never do this check because BlockIcon handles it but in this case, we can do it because we have wrapping div for the icon.
editor/components/inserter/menu.js
Outdated
@@ -176,6 +184,18 @@ export class InserterMenu extends Component { | |||
/> | |||
|
|||
<div className="editor-inserter__results"> | |||
{ !! childItems.length && |
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.
Can we extract this code to a new component to avoid confusing conditional checks for an icon and title? As far as I understand when there are child blocks then the icon and title must be set, right?
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.
Yes if we have childblocks the icon and title are set unless the block did not use a title/item, but that's part of the block responsibility.
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.
The child blocks rendering logic was extracted to a separate component.
* | ||
* @return {Array} Array of child block names. | ||
*/ | ||
export const getChildBlockNames = createSelector( |
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 selector seems to be quite important so let’s add unit tests to avoid regressions in the future.
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.
Unit tests added 👍
editor/components/inserter/menu.js
Outdated
const filteredItems = searchItems( items, filterValue ); | ||
|
||
const childItems = rootChildBlocks ? |
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.
rootChildBlocks is always an array, so in JS it’s always truthy.
This makes the search field a bit bigger and nicer, and changes the height to match better the contents.
Pushed a little polish. |
@@ -0,0 +1,29 @@ | |||
/** |
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 could just be called ChildBlocks
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.
Renamed 👍
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.
LGTM! 🎉
45ae97f
to
a943aa6
Compare
editor/components/inserter/menu.js
Outdated
@@ -176,6 +182,14 @@ export class InserterMenu extends Component { | |||
/> | |||
|
|||
<div className="editor-inserter__results"> | |||
<ChildBlocks | |||
rootBlockIcon={ rootBlockIcon } |
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.
Did you consider to use withSelect
directly on ChildBlocks
component instead of passing down rootBlockIcon
and rootBlockTitle
?
import BlockIcon from '../block-icon'; | ||
|
||
function ChildBlocks( { rootBlockIcon, rootBlockTitle, items, ...props } ) { | ||
if ( ! items || ! items.length ) { |
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.
Can we use ifCondition
HOC here?
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.
Code changes look good, I left 2 more questions, but definitely not blockers. Nice work 👍
* @param {string} blockName Block type name. | ||
* | ||
* @return {Array} Array of child block names. | ||
*/ |
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.
Not sure a JSDoc parser would detect this as being "attached" to the getChildBlockNames
function with the newline between.
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.
The line breaks were corrected.
'editor-inserter__item', | ||
getBlockMenuDefaultClassName( item.id ), | ||
{ | ||
'editor-inserter__item-with-childs': item.hasChildBlocks, |
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.
- Grammar: "childs" -> "children"
- This is a modifier, so could be written as one, e.g.
has-children
(reference)
@@ -1397,6 +1397,7 @@ export const getInserterItems = createSelector( | |||
isDisabled, | |||
utility: calculateUtility( blockType.category, count, isContextual ), | |||
frecency: calculateFrecency( time, count ), | |||
hasChildBlocks: hasChildBlocks( blockType.name ), |
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.
How is the cache bust if we proceed to register a block defining blockType.name
as a valid parent
?
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.
We clean the cache each time state.blockTypes changes, so when we register a block the cache is invalidated.
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.
We clean the cache each time state.blockTypes changes, so when we register a block the cache is invalidated.
But state.blockTypes
(from core/blocks
store) is not considered when we call getInserterItems
(from core/editor
store).
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.
I understand now the problem, getInserterItems is cached and depends on core/blocks store when core/blocks change the cache may not be updated. I think this is an already existing problem. Before if we register a new block a new item should exist but the cache may not be updated, it is not easy to spot because the cache updates a lot.
Thank you for clarifying this issue I will try to propose a solution to this problem!
eb6914f
to
7a3e385
Compare
Thank you @jasmussen and @mtias for the design improvements. Thank you @aduth, @gravityrail, and @gziolo for the reviews! |
@jasmussen Do you still see this on master? I'm not able to reproduce. Can we create a separate issue if it's still present? |
Sorry, @jasmussen I missed the comment but like @aduth I'm not being able to reproduce the issue in master or in the last release 3.0.1. Are you still able to reproduce the problem? |
@aduth @jorgefilipecosta Can confirm that I'm not seeing this anymore. I don't know what fixed it, I could swear I saw it for /gallery as soon as yesterday. But I can't repro. Edit: also seeing it for Cover Image now. |
@aduth @jorgefilipecosta I now see it in |
Description
This PR extends the blocks API with two new selectors getChildBlocks and hasChildBlocks.
A class is added for inserter items with children and the UI is changed using the new class. Now we have a special design in blocks with children (this design is in progress).
A new area that appears when we are inserting blocks inside blocks with children was created.