From 653832dc33ba1f667be05b24d9608829abdc716f Mon Sep 17 00:00:00 2001 From: Carlos Bravo <37012961+c4rl0sbr4v0@users.noreply.github.com> Date: Thu, 18 Jan 2024 20:02:43 +0100 Subject: [PATCH] Interactivity API: Add `wp-data-on-window` and `wp-data-on-document` directives (#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 --- .../directive-on-document/block.json | 15 ++ .../directive-on-document/render.php | 18 ++ .../directive-on-document/view.js | 34 ++++ .../directive-on-window/block.json | 15 ++ .../directive-on-window/render.php | 18 ++ .../directive-on-window/view.js | 34 ++++ packages/interactivity/CHANGELOG.md | 1 + .../interactivity/docs/2-api-reference.md | 160 +++++++++++++----- packages/interactivity/src/directives.js | 98 +++++++---- ...es-body.spec.ts => directive-body.spec.ts} | 0 ...-class.spec.ts => directive-class.spec.ts} | 0 ...text.spec.ts => directive-context.spec.ts} | 0 .../directive-on-document.spec.ts | 47 +++++ .../interactivity/directive-on-window.spec.ts | 45 +++++ ...-style.spec.ts => directive-style.spec.ts} | 0 ...es-text.spec.ts => directive-text.spec.ts} | 0 16 files changed, 404 insertions(+), 81 deletions(-) create mode 100644 packages/e2e-tests/plugins/interactive-blocks/directive-on-document/block.json create mode 100644 packages/e2e-tests/plugins/interactive-blocks/directive-on-document/render.php create mode 100644 packages/e2e-tests/plugins/interactive-blocks/directive-on-document/view.js create mode 100644 packages/e2e-tests/plugins/interactive-blocks/directive-on-window/block.json create mode 100644 packages/e2e-tests/plugins/interactive-blocks/directive-on-window/render.php create mode 100644 packages/e2e-tests/plugins/interactive-blocks/directive-on-window/view.js rename test/e2e/specs/interactivity/{directives-body.spec.ts => directive-body.spec.ts} (100%) rename test/e2e/specs/interactivity/{directives-class.spec.ts => directive-class.spec.ts} (100%) rename test/e2e/specs/interactivity/{directives-context.spec.ts => directive-context.spec.ts} (100%) create mode 100644 test/e2e/specs/interactivity/directive-on-document.spec.ts create mode 100644 test/e2e/specs/interactivity/directive-on-window.spec.ts rename test/e2e/specs/interactivity/{directives-style.spec.ts => directive-style.spec.ts} (100%) rename test/e2e/specs/interactivity/{directives-text.spec.ts => directive-text.spec.ts} (100%) diff --git a/packages/e2e-tests/plugins/interactive-blocks/directive-on-document/block.json b/packages/e2e-tests/plugins/interactive-blocks/directive-on-document/block.json new file mode 100644 index 00000000000000..296a9b2f041905 --- /dev/null +++ b/packages/e2e-tests/plugins/interactive-blocks/directive-on-document/block.json @@ -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" +} diff --git a/packages/e2e-tests/plugins/interactive-blocks/directive-on-document/render.php b/packages/e2e-tests/plugins/interactive-blocks/directive-on-document/render.php new file mode 100644 index 00000000000000..d04cb79ea58522 --- /dev/null +++ b/packages/e2e-tests/plugins/interactive-blocks/directive-on-document/render.php @@ -0,0 +1,18 @@ + + +
0
+0
+This element is now visible!
@@ -68,13 +70,13 @@ Directives can also be injected dynamically using the [HTML Tag Processor](https With directives, we can directly manage behavior related to things such as side effects, state, event handlers, attributes or content. -#### `wp-interactive` +#### `wp-interactive` The `wp-interactive` directive "activates" the interactivity for the DOM element and its children through the Interactivity API (directives and store). It includes a namespace to reference a specific store. ```html -