Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create block interactive template #52612

Merged
merged 13 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/test-create-block.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ if [ "$expected" -ne "$actual" ]; then
error "Expected $expected files in the project root, but found $actual."
exit 1
fi
expected=6
expected=7
actual=$( find src -maxdepth 1 -type f | wc -l )
if [ "$expected" -ne "$actual" ]; then
error "Expected $expected files in the \`src\` directory, but found $actual."
Expand All @@ -69,7 +69,7 @@ status "Building block..."
../node_modules/.bin/wp-scripts build

status "Verifying build..."
expected=5
expected=7
actual=$( find build -maxdepth 1 -type f | wc -l )
if [ "$expected" -ne "$actual" ]; then
error "Expected $expected files in the \`build\` directory, but found $actual."
Expand Down
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,12 @@
"markdown_source": "../packages/core-data/README.md",
"parent": "packages"
},
{
"title": "@wordpress/create-block-interactive-template",
"slug": "packages-create-block-interactive-template",
"markdown_source": "../packages/create-block-interactive-template/README.md",
"parent": "packages"
},
{
"title": "@wordpress/create-block-tutorial-template",
"slug": "packages-create-block-tutorial-template",
Expand Down
1 change: 1 addition & 0 deletions packages/create-block-interactive-template/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
1 change: 1 addition & 0 deletions packages/create-block-interactive-template/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->
19 changes: 19 additions & 0 deletions packages/create-block-interactive-template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Create Block Interactive Template

This is a template for [`@wordpress/create-block`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/create-block/README.md) to create interactive blocks

## Usage

This block template can be used by running the following command:

```bash
npx @wordpress/create-block --template @wordpress/create-block-interactive-template
```

## 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](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) 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](https://github.com/WordPress/gutenberg/tree/HEAD/CONTRIBUTING.md).

<br /><br /><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Interactive Block

> **Warning**
> **This block requires Gutenberg 16.2 or superior to work**. The Interactivity API is, at the moment, not part of WordPress Core as it is still very experimental, and very likely to change.

> **Note**
> This block uses the API shared at [Proposal: The Interactivity API – A better developer experience in building interactive blocks](https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/).

{{#isBasicVariant}}
This block has been created with the `create-block-interactive-template` and shows a basic structure of an interactive block that uses the Interactivity API.
{{/isBasicVariant}}

Check the following resources for more info about the Interactivity API:
- [`@wordpress/interactivity` package](https://github.com/WordPress/gutenberg/blob/trunk/packages/interactivity/README.md)
- [Proposal: The Interactivity API – A better developer experience in building interactive blocks](https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/)
- [“Interactivity API” category](https://github.com/WordPress/gutenberg/discussions/categories/interactivity-api) in Gutenberg repo discussions
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';

/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
*/
import { useBlockProps } from '@wordpress/block-editor';

/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
*
* @param {Object} props Properties passed to the function.
* @param {Object} props.attributes Available block attributes.
* @param {Function} props.setAttributes Function that updates individual attributes.
*
* @return {WPElement} Element to render.
*/
export default function Edit( { attributes, setAttributes } ) {
const blockProps = useBlockProps();

return (
<p { ...blockProps }>
{ __( '{{title}} – hello from the editor!', '{{textdomain}}' ) }
</p>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* The following styles get applied inside the editor only.
*
* Replace them with your own styles or remove the file completely.
*/

.wp-block-{{namespace}}-{{slug}} input[type="text"] {
font-size: 1em;
color: inherit;
background: inherit;
border: 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Registers a new block provided a unique name and an object defining its behavior.
*
* @see https://developer.wordpress.org/block-editor/developers/block-api/#registering-a-block
*/
import { registerBlockType } from '@wordpress/blocks';

/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* All files containing `style` keyword are bundled together. The code used
* gets applied both to the front of your site and to the editor. All other files
* get applied to the editor only.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './style.scss';
import './editor.scss';

/**
* Internal dependencies
*/
import Edit from './edit';
import metadata from './block.json';

/**
* Every block starts by registering a new block type definition.
*
* @see https://developer.wordpress.org/block-editor/developers/block-api/#registering-a-block
*/
registerBlockType( metadata.name, {
/**
* Used to construct a preview for the block to be shown in the block inserter.
*/
example: {
attributes: {
message: '{{title}}',
},
},
/**
* @see ./edit.js
*/
edit: Edit,
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{#isBasicVariant}}
<?php
/**
* @see https://github.com/WordPress/gutenberg/blob/trunk/docs/reference-guides/block-api/block-metadata.md#render
*/

$unique_id = uniqid( 'p-' );
?>

<div
<?php echo get_block_wrapper_attributes(); ?>
data-wp-interactive
data-wp-context='{ "{{namespace}}": { "isOpen": false } }'
data-wp-effect="effects.{{namespace}}.logIsOpen"
>
<button
data-wp-on--click="actions.{{namespace}}.toggle"
data-wp-bind--aria-expanded="context.{{namespace}}.isOpen"
aria-controls="p-<?php echo $unique_id; ?>"
>
<?php _e( 'Toggle', '{{textdomain}}' ); ?>
</button>

<p
id="p-<?php echo $unique_id; ?>"
data-wp-bind--hidden="!context.{{namespace}}.isOpen"
>
<?php
_e( '{{title}} - hello from an interactive block!', '{{textdomain}}' );
?>
</p>
</div>
{{/isBasicVariant}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* The following styles get applied both on the front of your site
* and in the editor.
*
* Replace them with your own styles or remove the file completely.
*/

.wp-block-{{namespace}}-{{slug}} {
font-size: 1em;
background: #ffff001a;
padding: 1em;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{{#isBasicVariant}}
luisherranz marked this conversation as resolved.
Show resolved Hide resolved

/**
* WordPress dependencies
*/
import { store } from "@wordpress/interactivity";

store( {
actions: {
'{{namespace}}': {
toggle: ( { context } ) => {
context[ '{{namespace}}' ].isOpen = !context[ '{{namespace}}' ].isOpen;
},
},
},
effects: {
'{{namespace}}': {
logIsOpen: ( { context } ) => {
// Log the value of `isOpen` each time it changes.
console.log( `Is open: ${ context[ '{{namespace}}' ].isOpen }` );
},
},
},
} );

{{/isBasicVariant}}
24 changes: 24 additions & 0 deletions packages/create-block-interactive-template/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* External dependencies
*/
const { join } = require( 'path' );

module.exports = {
defaultValues: {
slug: 'example-interactive',
title: 'Example Interactive',
description: 'An interactive block with the Interactivity API',
dashicon: 'media-interactive',
npmDependencies: [ '@wordpress/interactivity' ],
supports: {
interactivity: true,
},
render: 'file:./render.php',
viewScript: 'file:./view.js',
},
variants: {
basic: {},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is only a single variant, then w can skip this section and remove all checks like {{#isBasicVariant}}.

},
pluginTemplatesPath: join( __dirname, 'plugin-templates' ),
blockTemplatesPath: join( __dirname, 'block-templates' ),
};
25 changes: 25 additions & 0 deletions packages/create-block-interactive-template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@wordpress/create-block-interactive-template",
"version": "1.0.0",
"description": "Template for @wordpress/create-block to create interactive blocks with the Interactivity API.",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"keywords": [
"wordpress",
"create block",
"block template",
"Interactivity API"
],
"homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/docs/getting-started/create-block",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/gutenberg.git",
"directory": "packages/create-block-interactive-template"
},
"bugs": {
"url": "https://github.com/WordPress/gutenberg/issues"
},
"publishConfig": {
"access": "public"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Plugin Name: {{title}}
{{#pluginURI}}
* Plugin URI: {{{pluginURI}}}
{{/pluginURI}}
{{#description}}
* Description: {{description}}
{{/description}}
* Version: {{version}}
* Requires at least: 6.1
* Requires PHP: 7.0
{{#author}}
* Author: {{author}}
{{/author}}
{{#license}}
* License: {{license}}
{{/license}}
{{#licenseURI}}
* License URI: {{{licenseURI}}}
{{/licenseURI}}
* Text Domain: {{textdomain}}
{{#domainPath}}
* Domain Path: {{{domainPath}}}
{{/domainPath}}
{{#updateURI}}
* Update URI: {{{updateURI}}}
{{/updateURI}}
*
* @package {{namespace}}
*/

/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
register_block_type( __DIR__ . '/build' );
}
add_action( 'init', '{{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
=== {{title}} ===
{{#author}}
Contributors: {{author}}
{{/author}}
Tags: block
Tested up to: 6.1
Stable tag: {{version}}
{{#license}}
License: {{license}}
{{/license}}
{{#licenseURI}}
License URI: {{{licenseURI}}}
{{/licenseURI}}

{{description}}

== Description ==

This is the long description. No limit, and you can use Markdown (as well as in the following sections).

For backwards compatibility, if this section is missing, the full length of the short description will be used, and
Markdown parsed.

== Installation ==

This section describes how to install the plugin and get it working.

e.g.

1. Upload the plugin files to the `/wp-content/plugins/{{slug}}` directory, or install the plugin through the WordPress plugins screen directly.
1. Activate the plugin through the 'Plugins' screen in WordPress


== Frequently Asked Questions ==

= A question that someone might have =

An answer to that question.

= What about foo bar? =

Answer to foo bar dilemma.

== Screenshots ==

1. This screen shot description corresponds to screenshot-1.(png|jpg|jpeg|gif). Note that the screenshot is taken from
the /assets directory or the directory that contains the stable readme.txt (tags or trunk). Screenshots in the /assets
directory take precedence. For example, `/assets/screenshot-1.png` would win over `/tags/4.3/screenshot-1.png`
(or jpg, jpeg, gif).
2. This is the second screen shot

== Changelog ==

= {{version}} =
* Release

== Arbitrary section ==

You may provide arbitrary sections, in the same format as the ones above. This may be of use for extremely complicated
plugins where more information needs to be conveyed that doesn't fit into the categories of "description" or
"installation." Arbitrary sections will be shown below the built-in sections outlined above.
Loading