Panel 是 WordPress 组件库中的一个可折叠内容区域组件,用于垂直展开和收缩多个内容部分,以优化用户界面和体验。
import { Panel, PanelBody, PanelRow } from '@wordpress/components';
import { more } from '@wordpress/icons';
const MyPanel = () => (
<Panel header="My Panel">
<PanelBody title="My Block Settings" icon={ more } initialOpen={ true }>
<PanelRow>My Panel Inputs and Labels</PanelRow>
</PanelBody>
</Panel>
);Panels expand and collapse multiple sections of content.

A Panel is a single section of content that can be expanded or collapsed as needed.

Panels show and hide details of list items by expanding and collapsing list content vertically. Panels help users see only the content they need.
Use Panels when it’s helpful to:
Consider an alternative component when:
Show and hide details of existing panel items by expanding and collapsing list content vertically.

Collapsible panels are indicated with a caret icon that is flipped when expanded. Carets are preferable over a plus or arrow icon, because a plus indicates addition, and arrows are closely related to navigation.
Panels should be expanded by default if the content is important or essential. Panels that are open by default should appear at the top.
The Panel creates a container with a header that can take collapsible PanelBody components to easily create a user friendly interface for affecting state and attributes.
import { Panel, PanelBody, PanelRow } from '@wordpress/components';
import { more } from '@wordpress/icons';
const MyPanel = () => (
<Panel header="My Panel">
<PanelBody title="My Block Settings" icon={ more } initialOpen={ true }>
<PanelRow>My Panel Inputs and Labels</PanelRow>
</PanelBody>
</Panel>
);
header: stringThe text that will be rendered as the title of the panel. Text will be rendered inside an
<h2> tag.
className: stringThe CSS class to apply to the wrapper element.
children: React.ReactNodeThe content to display within the panel row.
The PanelBody creates a collapsible container that can be toggled open or closed.
title: stringTitle text. It shows even when the component is closed.
opened: booleanWhen set to true, the component will remain open regardless of the initialOpen prop and the
panel will be prevented from being closed.
className: stringThe CSS class to apply to the wrapper element.
icon: React.JSX.ElementAn icon to be shown next to the title.
onToggle: ( next: boolean ) => void;A function that is called any time the component is toggled from its closed state to its
opened state, or vice versa.
noopinitialOpen: booleanWhether or not the panel will start open.
truechildren: | React.ReactNode | ( ( props: { opened: boolean } ) => React.ReactNode )The content to display in the PanelBody. If a function is provided for this prop, it will receive an object with the opened prop as an argument.
buttonProps: WordPressComponentProps<Omit< ButtonAsButtonProps, 'icon' >, 'button', false>Props that are passed to the Button component in title within the PanelBody.
{}scrollAfterOpen: booleanScrolls the content into view when visible. This improves the UX when multiple PanelBody
components are stacked in a scrollable container.
truePanelRow is a generic container for rows within a PanelBody. It is a flex container with a top margin for spacing.
className: stringThe CSS class to apply to the wrapper element.
children: React.ReactNodeThe content to display within the panel row.
PanelRow accepts a forwarded ref that will be added to the wrapper div. Usage:
<PanelRow className="edit-post-post-schedule" ref={ panelRowRef }>
PanelHeader renders the header for the Panel. This is used by the Panel component under the hood, so it does not typically need to be used.
label: stringThe text that will be rendered as the title of the Panel. Will be rendered in an <h2> tag.
children: React.ReactNodeThe content to display within the panel row.
TabPanel