gutenbergdocs/reference-guides/data/data-core-customize-widgets.md
2025-10-22 01:33:45 +08:00

86 lines
2.0 KiB
Markdown

# 自定义小工具
命名空间:`core/customize-widgets`
## 选择器
<!-- START TOKEN(Autogenerated selectors|../../../packages/customize-widgets/src/store/selectors.js) -->
### isInserterOpened
判断插入器是否开启时返回 true
_使用示例_
```js
import { store as customizeWidgetsStore } from '@wordpress/customize-widgets';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
const ExampleComponent = () => {
const { isInserterOpened } = useSelect(
( select ) => select( customizeWidgetsStore ),
[]
);
return isInserterOpened()
? __( '插入器已开启' )
: __( '插入器已关闭' );
};
```
_参数_
- _state_ `Object`: 全局应用状态
_返回值_
- `boolean`: 插入器是否开启
<!-- END TOKEN(Autogenerated selectors|../../../packages/customize-widgets/src/store/selectors.js) -->
## 操作
<!-- START TOKEN(Autogenerated actions|../../../packages/customize-widgets/src/store/actions.js) -->
### setIsInserterOpened
返回用于开启/关闭插入器的操作对象
_使用示例_
```js
import { useState } from 'react';
import { store as customizeWidgetsStore } from '@wordpress/customize-widgets';
import { __ } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { Button } from '@wordpress/components';
const ExampleComponent = () => {
const { setIsInserterOpened } = useDispatch( customizeWidgetsStore );
const [ isOpen, setIsOpen ] = useState( false );
return (
<Button
onClick={ () => {
setIsInserterOpened( ! isOpen );
setIsOpen( ! isOpen );
} }
>
{ __( '开启/关闭插入器' ) }
</Button>
);
};
```
_参数_
- _value_ `boolean|Object`: 插入器应开启(true)或关闭(false)。要指定插入位置,请使用对象格式
- _value.rootClientId_ `string`: 要插入的根客户端ID
- _value.insertionIndex_ `number`: 要插入的索引位置
_返回值_
- `Object`: 操作对象
<!-- END TOKEN(Autogenerated actions|../../../packages/customize-widgets/src/store/actions.js) -->