-
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
BlockSettingsMenu: Ensure only one block settings menu is open at a time #54083
Changes from 8 commits
e60376c
17d8e0a
5c9e6bd
ded5fcc
7ccdcf2
5c51724
c59e974
ba4579f
13c42ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -259,3 +259,16 @@ export function setBlockRemovalRules( rules = false ) { | |
rules, | ||
}; | ||
} | ||
|
||
/** | ||
* Sets the client ID of the block settings menu that is currently open. | ||
* | ||
* @param {string} clientId The block client ID. | ||
* @return {Object} Action object. | ||
*/ | ||
export function setOpenedBlockSettingsMenu( clientId = '' ) { | ||
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. Do we really mean to have the default |
||
return { | ||
type: 'SET_OPENED_BLOCK_SETTINGS_MENU', | ||
clientId, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,3 +139,13 @@ export function getRemovalPromptData( state ) { | |
export function getBlockRemovalRules( state ) { | ||
return state.blockRemovalRules; | ||
} | ||
|
||
/** | ||
* Returns the client ID of the block settings menu that is currently open. | ||
* | ||
* @param {Object} state Global application state. | ||
* @return {string|undefined} The client ID of the block menu that is currently open. | ||
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. Did you mean |
||
*/ | ||
export function getOpenedBlockSettingsMenu( state ) { | ||
return state.openedBlockSettingsMenu; | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1913,6 +1913,13 @@ export function blockEditingModes( state = new Map(), action ) { | |||||
return state; | ||||||
} | ||||||
|
||||||
export function openedBlockSettingsMenu( state = null, action ) { | ||||||
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. The block editor reducer is quite thoroughly covered by unit tests. A couple of simple tests could be useful to cover this new reducer subtree. |
||||||
if ( 'SET_OPENED_BLOCK_SETTINGS_MENU' === action.type ) { | ||||||
return action?.clientId; | ||||||
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. For better precision, did we mean to return the default state of
Suggested change
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. Oh, good call, that helps give this reducer consistency with other reducers that track a client id or null 👍 |
||||||
} | ||||||
return state; | ||||||
} | ||||||
|
||||||
const combinedReducers = combineReducers( { | ||||||
blocks, | ||||||
isTyping, | ||||||
|
@@ -1938,6 +1945,7 @@ const combinedReducers = combineReducers( { | |||||
blockEditingModes, | ||||||
removalPromptData, | ||||||
blockRemovalRules, | ||||||
openedBlockSettingsMenu, | ||||||
} ); | ||||||
|
||||||
function withAutomaticChangeReset( reducer ) { | ||||||
|
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.
My only comment at this point is: wouldn't be simpler to always use
DropdownMenu
in a controlled way? Something likeI believe that the logic would just be simpler, and the behaviour of the
DropdownMenu
component more predictable (furthermore, React usually warns when this pattern is applied toinput
elements)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.
It'd be simpler in this component, but due to current usage of the BlockSettingsDropdown, a
currentClientId
isn't always available as ablock
object isn't always provided. So we currently need to support both controlled and uncontrolled in this component.In practice, at the moment, the List View provides a
block
object, however the block settings menu in the toolbar doesn't (it providesclientIds
for the selected blocks instead, which isn't quite the same as the concept of theblock
when used in the list view), so if we switch to always controlling the component, we wind up breaking the dropdown in the editor canvas:2023-09-13.10.34.46.mp4
Separately to this PR we could potentially look at refactoring other usage of the block settings dropdown to ensure a
block
object withclientId
is always provided, and then we could have the component always operate in controlled mode.For now, I'd prefer to try to contain the scope of this PR to the
BlockSettingsMenu
itself, so I think retaining this slightly complex ternary is probably a good step to go with for now, as it focuses on the main use case to solve which is with the behaviour in the list view.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 is the block of code that I'm suggesting to add some more comments about it being a temp solution..