Add initialOpen support for PluginDocumentSettingPanel component.#18985
Add initialOpen support for PluginDocumentSettingPanel component.#18985Addison-Stavlo wants to merge 3 commits intoWordPress:masterfrom
initialOpen support for PluginDocumentSettingPanel component.#18985Conversation
noahtallen
left a comment
There was a problem hiding this comment.
Overall, this change looks pretty good to me :)
| opened={ opened } | ||
| onToggle={ onToggle } | ||
| initialOpen={ initialOpen } | ||
| opened={ initialOpen ? undefined : opened } |
There was a problem hiding this comment.
This feels a little weird for me -- why do we need to set it to undefined if the initialOpen prop exists?
There was a problem hiding this comment.
I did this because of how the interior component PanelBody is set up.
For the opened property:
gutenberg/packages/components/src/panel/body.js
Lines 41 to 42 in b45e53e
If the opened prop is
undefined it falls back to using its default behavior of relying on state as opposed to the store connection. So if we did not set it to undefined in this declaration, it would overwrite the behavior of initialOpen.
Similarly, for the toggle function:
gutenberg/packages/components/src/panel/body.js
Lines 27 to 38 in b45e53e
If opened is undefined we rely on the components state as opposed to the store connection. And onToggle will run if it is defined.
Essentially, having these properties defined will overwrite the behavior we want from initialOpen. So if initialOpen is truthy, we need to explicitly set these as undefined so they don't overwrite its behavior.
There was a problem hiding this comment.
Unfortunately It looks like when the panel is using store state, the open/closed state is synced to local storage. This seems to circumvent that, which I don't think is a positive thing.
|
I've added the |
|
Ran into this today myself. I think the desired behavior, at least for me, would be just like |
|
It would be great with a more detailed test approach for designers to test out this PR. |
|
After looking back on this after quite some time, it looks like the initial goal was a bit flawed. That is, I was trying to make It looks like we cannot easily add and pass the |
Description
Added an optional
initialOpenprop to thePluginDocumentSettingPanelcomponent.This component is defined using the
PanelBodyas a child. Since thePanelBodyalready has support for theinitialOpenprop, we are just adding support for that prop to be passed in from thePluginDocumentSettingPanelparent.When
initialOpenis defined and passed a truthy value, it will always start open and will ignore itsopenedandonToggleproperties from the data store connection. Otherwise, behavior remains as it was prior to this change.Why?
Last week I found the case where we wanted to have a
PluginDocumentSettingPanelalways open by default. In digging I found that thePanelBodyhad support for aninitialOpenproperty but that thePluginDocumentSettingPanelwas not set up to pass this prop down to thePanelBody. This was disappointing as it would have made things very simple, so I figured I would update it and see what others think about adding this optional functionality.Without this our fix had to be a temporary subscription to the data store. A function to check if the state was closed and to dispatch the toggle runs once and is then unsubscribed. While this is a fairly simple thing to do, adding support for
initialOpenwill allow this component to be more robust and more easily usable for other cases in the future.How has this been tested?
PluginDocumentSettingPanel(I added this in an active plugin).initialOpenprop, verify there are no changes in functionality.initialOpenprop and give it a falsey value. Verify again there are no changes in functionality.initialOpena truthy value. Verify that the panel always starts open when returning to the editor regardless of its last state.Checklist: