Skip to content

Commit

Permalink
Create block: Update interactive-template to the new store() API (#…
Browse files Browse the repository at this point in the history
…56613)

* Update templates to the new `store()` API

* Add changelog entry

* Use wp_unique_id

---------

Co-authored-by: Luis Herranz <luisherranz@gmail.com>
  • Loading branch information
DAreRodz and luisherranz authored Nov 29, 2023
1 parent 90cd8db commit 00ccb8b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
4 changes: 4 additions & 0 deletions packages/create-block-interactive-template/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Enhancement

- Update `view.js` and `render.php` templates to the new `store()` API. [#56613](https://github.com/WordPress/gutenberg/pull/56613)

## 1.9.0 (2023-11-16)

## 1.8.0 (2023-11-02)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@
* @see https://github.com/WordPress/gutenberg/blob/trunk/docs/reference-guides/block-api/block-metadata.md#render
*/

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

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

<p
id="p-<?php echo esc_attr( $unique_id ); ?>"
data-wp-bind--hidden="!context.{{namespace}}.isOpen"
id="<?php echo esc_attr( $unique_id ); ?>"
data-wp-bind--hidden="!context.isOpen"
>
<?php
esc_html_e( '{{title}} - hello from an interactive block!', '{{textdomain}}' );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
{{#isBasicVariant}}

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

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

{{/isBasicVariant}}
{{/isBasicVariant}}

0 comments on commit 00ccb8b

Please sign in to comment.