Skip to content

Commit

Permalink
Interactivity API: Add wp-data-on-window and wp-data-on-document
Browse files Browse the repository at this point in the history
…directives (WordPress#57931)

* Add wp-data-on-window and wp-data-on-document directives

* Update changelog

* Rename directives tests files to unify them

* Use callbacks instead of actions, remove not needed counter

* Add event listener removal tests

* Add documentation

* Test that events are reattached

* Fix docs typos

* Fix docs typos

* Fix indentation in docs

* Fix e2e test
  • Loading branch information
cbravobernal authored Jan 18, 2024
1 parent 0cce3a7 commit 653832d
Show file tree
Hide file tree
Showing 16 changed files with 404 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "test/directive-on-document",
"title": "E2E Interactivity tests - directive on document",
"category": "text",
"icon": "heart",
"description": "",
"supports": {
"interactivity": true
},
"textdomain": "e2e-interactivity",
"viewScript": "directive-on-document-view",
"render": "file:./render.php"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* HTML for testing the directive `data-wp-on`.
*
* @package gutenberg-test-interactive-blocks
*/

gutenberg_enqueue_module( 'directive-on-document-view' );
?>

<div data-wp-interactive='{ "namespace": "directive-on-document" }' data-wp-context='{"isVisible":true}'>
<button data-wp-on--click="actions.visibilityHandler" data-testid="visibility">Switch visibility</button>
<div data-wp-show-mock="context.isVisible">
<div data-wp-on-document--keydown="callbacks.keydownHandler">
<p data-wp-text="state.counter" data-testid="counter">0</p>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* WordPress dependencies
*/
import { store, directive, getContext } from '@wordpress/interactivity';

// Mock `data-wp-show` directive to test when things are removed from the
// DOM. Replace with `data-wp-show` when it's ready.
directive(
'show-mock',
( { directives: { 'show-mock': showMock }, element, evaluate } ) => {
const entry = showMock.find( ( { suffix } ) => suffix === 'default' );
if ( ! evaluate( entry ) ) {
return null;
}
return element;
}
);

const { state } = store( 'directive-on-document', {
state: {
counter: 0,
},
callbacks: {
keydownHandler: ( ) => {
state.counter += 1;
},
},
actions: {
visibilityHandler: () => {
const context = getContext();
context.isVisible = ! context.isVisible;
},
}
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "test/directive-on-window",
"title": "E2E Interactivity tests - directive on window",
"category": "text",
"icon": "heart",
"description": "",
"supports": {
"interactivity": true
},
"textdomain": "e2e-interactivity",
"viewScript": "directive-on-window-view",
"render": "file:./render.php"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* HTML for testing the directive `data-wp-on`.
*
* @package gutenberg-test-interactive-blocks
*/

gutenberg_enqueue_module( 'directive-on-window-view' );
?>

<div data-wp-interactive='{ "namespace": "directive-on-window" }' data-wp-context='{"isVisible":true}'>
<button data-wp-on--click="actions.visibilityHandler" data-testid="visibility">Switch visibility</button>
<div data-wp-show-mock="context.isVisible">
<div data-wp-on-window--resize="callbacks.resizeHandler">
<p data-wp-text="state.counter" data-testid="counter">0</p>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* WordPress dependencies
*/
import { store, directive, getContext } from '@wordpress/interactivity';

// Mock `data-wp-show` directive to test when things are removed from the
// DOM. Replace with `data-wp-show` when it's ready.
directive(
'show-mock',
( { directives: { 'show-mock': showMock }, element, evaluate } ) => {
const entry = showMock.find( ( { suffix } ) => suffix === 'default' );
if ( ! evaluate( entry ) ) {
return null;
}
return element;
}
);

const { state } = store( 'directive-on-window', {
state: {
counter: 0,
},
callbacks: {
resizeHandler: ( ) => {
state.counter += 1;
},
},
actions: {
visibilityHandler: () => {
const context = getContext();
context.isVisible = ! context.isVisible;
},
}
} );
1 change: 1 addition & 0 deletions packages/interactivity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### New Features

- Add the `data-wp-run` directive along with the `useInit` and `useWatch` hooks. ([57805](https://github.com/WordPress/gutenberg/pull/57805))
- Add `wp-data-on-window` and `wp-data-on-document` directives. ([57931](https://github.com/WordPress/gutenberg/pull/57931))

### Breaking Changes

Expand Down
Loading

0 comments on commit 653832d

Please sign in to comment.