编辑面板
[尖叫声] 这套产品正在研制中,尚未准备用于生产.
使用量
创建面板包含3个步骤:
- 创建面板组件
// components/my-panel.tsx
import { Panel, PanelHeader, PanelHeaderTitle, PanelBody } from '@elementor/editor-panels';
export default function MyPanel() {
return (
<Panel>
<PanelHeader>
<PanelHeaderTitle>{ /* Panel title */ }</PanelHeaderTitle>
</PanelHeader>
<PanelBody>
{ /* Here should be all the content of the panel */ }
</PanelBody>
</Panel>
);
}
- 创建一个面板实例
// panel.ts
import { createPanel } from '@elementor/editor-panels';
import MyPanel from './components/my-panel';
export const {
panel,
usePanelStatus,
usePanelActions,
} = createPanel( {
id: 'my-panel',
component: MyPanel
} );
- 登记小组
// init.ts
import { registerPanel } from '@elementor/editor-panels';
import { panel } from './panel';
function init() {
registerPanel( panel );
}
// index.ts
import init from './init';
init();
使用面板动作和状态
要修改或读取面板的状态( 打开/ 关闭) , 您可以使用返回的 2个钩子
从 createPanel() 函数.
假设我们有一个按钮,在点击时应该打开面板,再点击时关闭.
// components/my-panel-button.tsx
import { usePanelStatus, usePanelActions } from '../panel';
export default function MyPanelButton() {
const { isOpen, isBlocked } = usePanelStatus();
const { open, close } = usePanelActions();
return (
<button onClick={ isOpen ? close : open } disabled={ isBlocked }>
{ isOpen ? 'Close panel' : 'Open panel' }
</button>
);
}