-
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
Add initialOpen
support for PluginDocumentSettingPanel
component.
#18985
Add initialOpen
support for PluginDocumentSettingPanel
component.
#18985
Conversation
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
const { title, children, opened, className, icon, forwardedRef } = this.props; | |
const isOpened = opened === undefined ? this.state.opened : opened; |
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
toggle( event ) { | |
event.preventDefault(); | |
if ( this.props.opened === undefined ) { | |
this.setState( ( state ) => ( { | |
opened: ! state.opened, | |
} ) ); | |
} | |
if ( this.props.onToggle ) { | |
this.props.onToggle(); | |
} | |
} |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah interesting. Cool!
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.
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
initialOpen
prop to thePluginDocumentSettingPanel
component.This component is defined using the
PanelBody
as a child. Since thePanelBody
already has support for theinitialOpen
prop, we are just adding support for that prop to be passed in from thePluginDocumentSettingPanel
parent.When
initialOpen
is defined and passed a truthy value, it will always start open and will ignore itsopened
andonToggle
properties 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
PluginDocumentSettingPanel
always open by default. In digging I found that thePanelBody
had support for aninitialOpen
property but that thePluginDocumentSettingPanel
was 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
initialOpen
will 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).initialOpen
prop, verify there are no changes in functionality.initialOpen
prop and give it a falsey value. Verify again there are no changes in functionality.initialOpen
a truthy value. Verify that the panel always starts open when returning to the editor regardless of its last state.Checklist: