Skip to content
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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/edit-post/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ _Parameters_
- _props.className_ `[string]`: An optional class name added to the row.
- _props.title_ `[string]`: The title of the panel
- _props.icon_ `[WPBlockTypeIconRender]`: The [Dashicon](https://developer.wordpress.org/resource/dashicons/) icon slug string, or an SVG WP element, to be rendered when the sidebar is pinned to toolbar.
- _props.initialOpen_ `[boolean]`: An optional behavior to always start with panel open.

_Returns_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { EnablePluginDocumentSettingPanelOption } from '../../options-modal/opti

export const { Fill, Slot } = createSlotFill( 'PluginDocumentSettingPanel' );

const PluginDocumentSettingFill = ( { isEnabled, panelName, opened, onToggle, className, title, icon, children } ) => {
const PluginDocumentSettingFill = ( { isEnabled, panelName, opened, onToggle, className, title, icon, children, initialOpen } ) => {
return (
<>
<EnablePluginDocumentSettingPanelOption
Expand All @@ -30,8 +30,9 @@ const PluginDocumentSettingFill = ( { isEnabled, panelName, opened, onToggle, cl
className={ className }
title={ title }
icon={ icon }
opened={ opened }
onToggle={ onToggle }
initialOpen={ initialOpen }
opened={ initialOpen ? undefined : opened }
Copy link
Member

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?

Copy link
Contributor Author

@Addison-Stavlo Addison-Stavlo Dec 17, 2019

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:

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:

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah interesting. Cool!

Copy link
Contributor

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.

onToggle={ initialOpen ? undefined : onToggle }
>
{ children }
</PanelBody>
Expand All @@ -49,6 +50,7 @@ const PluginDocumentSettingFill = ( { isEnabled, panelName, opened, onToggle, cl
* @param {string} [props.className] An optional class name added to the row.
* @param {string} [props.title] The title of the panel
* @param {WPBlockTypeIconRender} [props.icon=inherits from the plugin] The [Dashicon](https://developer.wordpress.org/resource/dashicons/) icon slug string, or an SVG WP element, to be rendered when the sidebar is pinned to toolbar.
* @param {boolean} [props.initialOpen] An optional behavior to always start with panel open.
*
* @example <caption>ES5</caption>
* ```js
Expand Down