Package Exports
- @wordpress/block-library
- @wordpress/block-library/build-module/index.js
- @wordpress/block-library/build/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@wordpress/block-library) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Block library
Block library for the WordPress editor.
Installation
Install the module
npm install @wordpress/block-library --saveThis package assumes that your code will run in an ES2015+ environment. If you're using an environment that has limited or no support for such language features and APIs, you should include the polyfill shipped in @wordpress/babel-preset-default in your code.
API
registerCoreBlocks
Function to register core blocks provided by the block editor.
Usage
import { registerCoreBlocks } from '@wordpress/block-library';
registerCoreBlocks();Parameters
- blocks
Array: An optional array of the core blocks being registered.
Registering individual blocks
When you only care about registering the block when file gets imported:
import '@wordpress/block-library/build-module/verse/init';
When you want to use the reference to the block after it gets automatically registered:
import verseBlock from '@wordpress/block-library/build-module/verse/init';
When you need a full control over when the block gets registered:
import { init } from '@wordpress/block-library/build-module/verse'; const verseBlock = init();
Contributing to this package
This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm and used by WordPress as well as other software projects.
To find out more about contributing to this package or Gutenberg as a whole, please read the project's main contributor guide.
Adding new blocks
⚠️ Adding new blocks to this package requires additional steps!
Do not forget to register a new core block in the
index.jsfile of this package. For example, if you were to add the new core block calledcore/blinking-paragraph, you would have to add something like:// packages/block-library/src/index.js import * as blinkingParagraph from './blinking-paragraph';
Then add
blinkingParagraphto the list in thegetAllBlocks()function.If it's experimental, add the following property to
block.json:{ "__experimental": "true" }
Register the block in the
gutenberg_reregister_core_block_types()function of thelib/blocks.phpfile. Add it to theblock_foldersarray if it's a static block or to theblock_namesarray if it's a dynamic block.Add
init.jsfile to the directory of the new block:/** * Internal dependencies */ import { init } from './'; export default init();
This file is used when using the option to register individual block from the
@wordpress/block-librarypackage.If a
view.jsfile (or a file prefixed withview, e.g.view-example.js) is present in the block's directory, this file will be built along other assets, making it available to load from the browser. You only need to reference aview.min.js(notice the different file extension) file in theblock.jsonfile as follows:{ "viewScript": "file:./view.min.js" }
This file will get automatically loaded when the static block is present on the front end. For dynamic block, you need to manually enqueue the view script in
render_callbackof the block, example:function render_block_core_blinking_paragraph( $attributes, $content ) { $should_load_view_script = ! empty( $attributes['isInteractive'] ) && ! wp_script_is( 'wp-block-blinking-paragraph-view' ); if ( $should_load_view_script ) { wp_enqueue_script( 'wp-block-blinking-paragraph-view' ); } return $content; }
