@wordpress/blocks
Version:
Block API for WordPress.
101 lines (95 loc) • 2.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getCategories = getCategories;
exports.setCategories = setCategories;
exports.updateCategory = updateCategory;
var _data = require("@wordpress/data");
var _store = require("../store");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
/** @typedef {import('../store/reducer').WPBlockCategory} WPBlockCategory */
/**
* Returns all the block categories.
* Ignored from documentation as the recommended usage is via useSelect from @wordpress/data.
*
* @ignore
*
* @return {WPBlockCategory[]} Block categories.
*/
function getCategories() {
return (0, _data.select)(_store.store).getCategories();
}
/**
* Sets the block categories.
*
* @param {WPBlockCategory[]} categories Block categories.
*
* @example
* ```js
* import { __ } from '@wordpress/i18n';
* import { store as blocksStore, setCategories } from '@wordpress/blocks';
* import { useSelect } from '@wordpress/data';
* import { Button } from '@wordpress/components';
*
* const ExampleComponent = () => {
* // Retrieve the list of current categories.
* const blockCategories = useSelect(
* ( select ) => select( blocksStore ).getCategories(),
* []
* );
*
* return (
* <Button
* onClick={ () => {
* // Add a custom category to the existing list.
* setCategories( [
* ...blockCategories,
* { title: 'Custom Category', slug: 'custom-category' },
* ] );
* } }
* >
* { __( 'Add a new custom block category' ) }
* </Button>
* );
* };
* ```
*/
function setCategories(categories) {
(0, _data.dispatch)(_store.store).setCategories(categories);
}
/**
* Updates a category.
*
* @param {string} slug Block category slug.
* @param {WPBlockCategory} category Object containing the category properties
* that should be updated.
*
* @example
* ```js
* import { __ } from '@wordpress/i18n';
* import { updateCategory } from '@wordpress/blocks';
* import { Button } from '@wordpress/components';
*
* const ExampleComponent = () => {
* return (
* <Button
* onClick={ () => {
* updateCategory( 'text', { title: __( 'Written Word' ) } );
* } }
* >
* { __( 'Update Text category title' ) }
* </Button>
* ) ;
* };
* ```
*/
function updateCategory(slug, category) {
(0, _data.dispatch)(_store.store).updateCategory(slug, category);
}
//# sourceMappingURL=categories.js.map