块编辑器开发文档

自定义小工具

💡 云策文档标注

概述

本文档介绍了 WordPress 自定义小工具功能中的核心选择器和操作,包括 isInserterOpened 选择器和 setIsInserterOpened 操作,用于管理插入器的状态。

关键要点

  • isInserterOpened 选择器用于检查插入器是否打开,返回布尔值。
  • setIsInserterOpened 操作用于设置插入器的打开或关闭状态,支持布尔值或对象参数来指定插入点。
  • 这些功能通过 customizeWidgetsStore 提供,可与 useSelect 和 useDispatch 结合使用。

代码示例

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()
        ? __( 'Inserter is open' )
        : __( 'Inserter is closed.' );
};
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 );
            } }
        >
            { __( 'Open/close inserter' ) }
        </Button>
    );
};

📄 原文内容

Namespace: core/customize-widgets.

Selectors

isInserterOpened

Returns true if the inserter is opened.

Usage

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()
        ? __( 'Inserter is open' )
        : __( 'Inserter is closed.' );
};

Parameters

  • state Object: Global application state.

Returns

  • boolean: Whether the inserter is opened.

Actions

setIsInserterOpened

Returns an action object used to open/close the inserter.

Usage

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 );
            } }
        >
            { __( 'Open/close inserter' ) }
        </Button>
    );
};

Parameters

  • value boolean|Object: Whether the inserter should be opened (true) or closed (false). To specify an insertion point, use an object.
  • value.rootClientId string: The root client ID to insert at.
  • value.insertionIndex number: The index to insert at.

Returns

  • Object: Action object.