Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ca80766
Interactivity API: Migrate Interactivity fixtures to supported direct…
luisherranz Oct 6, 2026
c3a1daf
Interactivity API: Use full event suffix in event directives
luisherranz Oct 6, 2026
f33869e
Interactivity API: Skip suffixed lifecycle directives
luisherranz Oct 6, 2026
88327a8
Interactivity API: Do not invoke negated functions
luisherranz Oct 6, 2026
7523f9b
Interactivity API: Remove special handling for data-wp-ignore
luisherranz Oct 6, 2026
27aca8a
Interactivity API: Update island hydration e2e coverage
luisherranz Oct 6, 2026
5663d23
Interactivity API: Correct hydration fixture explanation
luisherranz Oct 6, 2026
e2e8267
Interactivity API: Remove expired unique-ID deprecation notices from …
luisherranz Oct 6, 2026
f6f2d8e
Interactivity API: Announce removals in the changelog
luisherranz Oct 6, 2026
d4f6522
Interactivity API: Normalize reference emphasis formatting
luisherranz Oct 6, 2026
a341672
Interactivity API: Reference removal PR in the changelog
luisherranz Oct 6, 2026
cdc1808
Interactivity API: Warn about two-hyphen event names
DAreRodz Oct 6, 2026
6389501
Interactivity API: Share the unsupported suffix warning with router-r…
DAreRodz Oct 6, 2026
65c017f
Interactivity API: Warn about leftover data-wp-ignore attributes
DAreRodz Oct 6, 2026
dfa07af
Interactivity API: Remove JSDoc from the Evaluate interface
DAreRodz Oct 6, 2026
0ff03cb
Interactivity API: Move and extend tests for removed deprecations
DAreRodz Oct 6, 2026
6ecd565
Interactivity API: Rename stale isolated island e2e fixtures
DAreRodz Oct 6, 2026
6de02d2
Interactivity API: Document unique IDs for event directives and funct…
DAreRodz Oct 6, 2026
1411775
Interactivity API: Split the breaking changes changelog entry
DAreRodz Oct 6, 2026
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
27 changes: 19 additions & 8 deletions docs/reference-guides/interactivity-api/directives-and-store.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,17 @@ The `wp-on` directive is executed each time the associated event is triggered.

The callback passed as the reference receives [the event](https://developer.mozilla.org/en-US/docs/Web/API/Event) (`event`), and the returned value by this callback is ignored.

You can attach several callbacks to the same event of a DOM element by using the syntax `data-wp-on--[event]---[unique-id]` (note the three hyphens before the unique ID). Everything between `data-wp-on--` and the three hyphens is used as the event name, so `data-wp-on--click--counter` listens for an event named `click--counter`, not for `click`.

```html
<button
data-wp-on--click="actions.logTime"
data-wp-on--click---counter="actions.countClick"
>
Click Me!
</button>
```

### `wp-on-window`

<div class="callout callout-info">
Expand Down Expand Up @@ -448,6 +459,8 @@ store( 'myPlugin', {

The callback passed as the reference receives [the event](https://developer.mozilla.org/en-US/docs/Web/API/Event) (`event`), and the returned value by this callback is ignored. When the element is removed from the DOM, the event listener is also removed.

You can attach several callbacks to the same window event by using the syntax `data-wp-on-window--[window-event]---[unique-id]` (note the three hyphens before the unique ID), like `data-wp-on-window--resize---log-width`.

### `wp-on-document`

<div class="callout callout-info">
Expand Down Expand Up @@ -481,14 +494,14 @@ store( 'myPlugin', {

The callback passed as the reference receives [the event](https://developer.mozilla.org/en-US/docs/Web/API/Event) (`event`), and the returned value by this callback is ignored. When the element is removed from the DOM, the event listener is also removed.

You can attach several callbacks to the same document event by using the syntax `data-wp-on-document--[document-event]---[unique-id]` (note the three hyphens before the unique ID), like `data-wp-on-document--keydown---log-key`.

### `wp-watch`

It runs a callback **when the node is created and runs it again when the state or context changes**.

You can attach several side effects to the same DOM element by using the syntax `data-wp-watch---[unique-id]` (note the three hyphens before the unique ID).

> **Deprecation notice:** The two-hyphen syntax `data-wp-watch--[unique-id]` is deprecated and will stop working in WordPress 7.1. Use three hyphens (`---`) for unique IDs.

The `unique-id` doesn't need to be unique globally. It just needs to be different from the other unique IDs of the `wp-watch` directives of that DOM element.

```html
Expand Down Expand Up @@ -547,8 +560,6 @@ This directive runs a callback **only when the node is created**.

You can attach several `wp-init` to the same DOM element by using the syntax `data-wp-init---[unique-id]` (note the three hyphens before the unique ID).

> **Deprecation notice:** The two-hyphen syntax `data-wp-init--[unique-id]` is deprecated and will stop working in WordPress 7.1. Use three hyphens (`---`) for unique IDs.

The `unique-id` doesn't need to be unique globally. It just needs to be different from the other unique IDs of the `wp-init` directives of that DOM element.

```html
Expand Down Expand Up @@ -597,8 +608,6 @@ You can use and compose hooks like `useState`, `useWatch`, or `useEffect` inside

You can attach several `wp-run` to the same DOM element by using the syntax `data-wp-run---[unique-id]` (note the three hyphens before the unique ID).

> **Deprecation notice:** The two-hyphen syntax `data-wp-run--[unique-id]` is deprecated and will stop working in WordPress 7.1. Use three hyphens (`---`) for unique IDs.

The `unique-id` doesn't need to be unique globally. It just needs to be different from the other unique IDs of the `wp-run` directives of that DOM element.

```html
Expand Down Expand Up @@ -776,6 +785,8 @@ And then, the string value `"state.isPlaying"` is used to assign the result of t
</div>
```

The `!` operator negates the referenced value. It only works with values, such as state, context, or derived state defined with a getter. If the reference points to a function, such as an action or a callback, the function is not called and the directive receives `undefined` instead (with a warning when `SCRIPT_DEBUG` is enabled). To negate the result of a computation, define it as derived state with a getter and negate that.

These values assigned to directives are **references** to a particular property in the store. They are wired to the directives automatically so that each directive “knows” what store element refers to, without any additional configuration.

Note that, by default, references point to properties in the current namespace, which is the one specified by the closest ancestor with a `data-wp-interactive` attribute. If you need to access a property from a different namespace, you can explicitly set the namespace where the property accessed is defined. The syntax is `namespace::reference`, replacing `namespace` with the appropriate value.
Expand Down Expand Up @@ -1025,7 +1036,7 @@ This approach enables some functionalities that make directives flexible and pow

#### On the client side

_In the `view.js` file of each block_ the developer can define both the state and the elements of the store referencing functions like actions, side effects or derived state.
*In the `view.js` file of each block* the developer can define both the state and the elements of the store referencing functions like actions, side effects or derived state.

The `store` method used to set the store in JavaScript can be imported from `@wordpress/interactivity`.

Expand Down Expand Up @@ -1058,7 +1069,7 @@ The state defined on the server with `wp_interactivity_state()` gets merged with

The `wp_interactivity_state` function receives two arguments, a `string` with the namespace that will be used as a reference and an [associative array](https://www.php.net/manual/en/language.types.array.php) containing the values.

_Example of store initialized from the server with a `state` = `{ someValue: 123 }`_
*Example of store initialized from the server with a `state` = `{ someValue: 123 }`*

```php
// render.php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div
data-testid="multiple inits"
data-wp-context='{"isReady":[false,false],"calls":[0,0]}'
data-wp-init--one="actions.initOne"
data-wp-init---one="actions.initOne"
data-wp-init---two="actions.initTwo"
>
<p data-wp-text="state.isReady" data-testid="isReady">false,false</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</div>
<div
data-wp-on-document--keydown="actions.keydownHandler"
data-wp-on-document--keydown--second="actions.keydownSecondHandler"
data-wp-on-document--keydown---second="actions.keydownSecondHandler"
data-wp-on-document--keydown---third="actions.keydownThirdHandler"
>
<p data-wp-text="state.keydownHandler" data-testid="keydownHandler">no</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</div>
<div
data-wp-on-window--resize="actions.resizeHandler"
data-wp-on-window--resize--second="actions.resizeSecondHandler"
data-wp-on-window--resize---second="actions.resizeSecondHandler"
data-wp-on-window--resize---third="actions.resizeThirdHandler"
>
<p data-wp-text="state.resizeHandler" data-testid="resizeHandler">no</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<button
data-testid="multiple handlers button"
data-wp-on--click="actions.setClicked"
data-wp-on--click--counter="actions.countClick"
data-wp-on--click---counter="actions.countClick"
data-wp-on--click---toggle="actions.toggle"
>Click me!</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
data-watch=""
>
<div
data-wp-run--mounted="callbacks.updateIsMounted"
data-wp-run---mounted="callbacks.updateIsMounted"
data-wp-run---hooks="callbacks.useHooks"
>
Element with wp-run using hooks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const html = `
<div data-testid="navigated">yes</div>

<div
data-wp-run--hydrated="callbacks.updateIsHydrated"
data-wp-run--renderCount="callbacks.updateRenderCount"
data-wp-run---hydrated="callbacks.updateIsHydrated"
data-wp-run---renderCount="callbacks.updateRenderCount"
data-wp-text="state.clickCount"
></div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<div
data-testid="multiple watches"
data-wp-watch--one="callbacks.watch1"
data-wp-watch---one="callbacks.watch1"
data-wp-watch---two="callbacks.watch2"
data-wp-bind--data-watch-one="state.watch1"
data-wp-bind--data-watch-two="state.watch2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@
<div data-wp-interactive="tovdom-islands">
<div data-wp-ignore>
<div data-wp-show-mock="state.falseValue">
<span
data-testid="inside an inner block of an isolated island"
>
This should be shown because it is inside an inner
block of an isolated island.
<span data-testid="inside an element with data-wp-ignore">
This should not be shown because data-wp-ignore no
longer prevents hydration.
</span>
</div>
</div>
Expand All @@ -60,15 +58,14 @@
<div>
<div
data-wp-interactive="tovdom-islands"
data-wp-ignore
>
<div data-wp-show-mock="state.falseValue">
<span
data-testid="island inside inner block of isolated island"
data-testid="island inside an inner element of another island"
>
This should not be shown because even though it
is inside an inner block of an isolated island,
it's inside an new island.
This should not be shown because it is inside a
new island nested in an inner element of another
island.
</span>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
?>

<div data-wp-interactive="with-scope" data-wp-context='{"asyncCounter": 0, "syncCounter": 0}' data-wp-init--a='callbacks.asyncInit' data-wp-init--b='callbacks.syncInit'>
<div data-wp-interactive="with-scope" data-wp-context='{"asyncCounter": 0, "syncCounter": 0}' data-wp-init---a='callbacks.asyncInit' data-wp-init---b='callbacks.syncInit'>
<p data-wp-text="context.asyncCounter" data-testid="asyncCounter">0</p>
<p data-wp-text="context.syncCounter" data-testid="syncCounter">0</p>
</div>
7 changes: 7 additions & 0 deletions packages/interactivity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## Unreleased

### Breaking Changes

- Remove the `data-wp-ignore` directive. An element carrying it is now hydrated like any other element, and a warning is shown in development mode. Remove the attribute. ([#84098](https://github.com/WordPress/gutenberg/pull/84098))
- Remove support for two-hyphen unique IDs, such as `data-wp-watch--id` or `data-wp-on--click--id`. Use three hyphens instead, such as `data-wp-watch---id` or `data-wp-on--click---id`. `data-wp-watch`, `data-wp-init`, and `data-wp-run` now skip directives with a suffix, and `data-wp-on`, `data-wp-on-window`, and `data-wp-on-document` now use the whole text after `--` as the event name. Both show a warning in development mode. The deprecated `data-wp-on-async` directives are unchanged. ([#84098](https://github.com/WordPress/gutenberg/pull/84098))
- Applying `!` to a function in a directive value, such as `data-wp-bind--hidden="!actions.isOpen"`, no longer calls the function: the value resolves to `undefined`, with a warning in development mode. Use derived state defined with a getter instead. Negating values and getters is unchanged. ([#84098](https://github.com/WordPress/gutenberg/pull/84098))
- The `evaluate` function passed to directive callbacks now accepts only the directive entry. Additional arguments, which were only forwarded to negated functions, are no longer accepted. ([#84098](https://github.com/WordPress/gutenberg/pull/84098))

### New Features

- Add the `data-wp-html` directive and `asDangerousHTML()` for rendering explicitly trusted HTML from reactive state. ([#83092](https://github.com/WordPress/gutenberg/pull/83092))
Expand Down
31 changes: 0 additions & 31 deletions packages/interactivity/src/directives/ignore.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/interactivity/src/directives/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import './class';
import './context';
import './each';
import './html';
import './ignore';
import './init';
import './on';
import './router-region';
Expand Down
11 changes: 6 additions & 5 deletions packages/interactivity/src/directives/init.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { directive } from '../hooks';
import { useInit } from '../utils';
import { warnUniqueIdWithTwoHyphens } from './utils/warnings';
import { warnSuffixNotSupported } from './utils/warnings';

// data-wp-init---[unique-id] — Run expression on first render.
// data-wp-init---[unique-id] — Run on first render; suffixes are unsupported.
directive( 'init', ( { directives: { init }, evaluate } ) => {
init.forEach( ( entry ) => {
if ( globalThis.SCRIPT_DEBUG ) {
if ( entry.suffix ) {
warnUniqueIdWithTwoHyphens( 'init', entry.suffix );
if ( entry.suffix !== null ) {
if ( globalThis.SCRIPT_DEBUG ) {
warnSuffixNotSupported( 'init', entry.suffix );
}
return;
}
// TODO: Replace with useEffect to prevent unneeded scopes.
useInit( () => {
Expand Down
39 changes: 20 additions & 19 deletions packages/interactivity/src/directives/on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
type DirectiveEntry,
} from '../hooks';
import {
warnUniqueIdWithTwoHyphens,
warnEventNameWithTwoHyphens,
warnWithSyncEvent,
} from './utils/warnings';

Expand Down Expand Up @@ -64,9 +64,10 @@ function wrapEventAsync( event: Event ) {

/**
* Creates a directive that adds an event listener to the global window or
* document object.
* document object using the full directive suffix as its event name.
*
* @param type 'window' or 'document'
* @return Directive callback that manages the global event listener.
*/
const getGlobalEventDirective = (
type: 'window' | 'document'
Expand All @@ -75,14 +76,12 @@ const getGlobalEventDirective = (
directives[ `on-${ type }` ]
.filter( isNonDefaultDirectiveSuffix )
.forEach( ( entry ) => {
const suffixParts = entry.suffix.split( '--', 2 );
const eventName = suffixParts[ 0 ];
const eventName = entry.suffix;
if ( globalThis.SCRIPT_DEBUG ) {
if ( suffixParts[ 1 ] ) {
warnUniqueIdWithTwoHyphens(
if ( eventName.includes( '--' ) ) {
warnEventNameWithTwoHyphens(
`on-${ type }`,
suffixParts[ 0 ],
suffixParts[ 1 ]
eventName
);
}
}
Expand All @@ -107,6 +106,11 @@ const getGlobalEventDirective = (
/**
* Creates a directive that adds an async event listener to the global window or
* document object.
*
* Unlike the synchronous directives, the deprecated async directives still cut
* the event name at `--`, so the legacy two-hyphen unique ID syntax keeps
* working until they are removed.
*
* @param type 'window' or 'document'
*/
const getGlobalAsyncEventDirective = (
Expand Down Expand Up @@ -142,20 +146,16 @@ const getGlobalAsyncEventDirective = (
directive( 'on', ( { directives: { on }, element, evaluate } ) => {
const events = new Map< string, Set< DirectiveEntry > >();
on.filter( isNonDefaultDirectiveSuffix ).forEach( ( entry ) => {
const suffixParts = entry.suffix.split( '--', 2 );
const eventType = entry.suffix;
if ( globalThis.SCRIPT_DEBUG ) {
if ( suffixParts[ 1 ] ) {
warnUniqueIdWithTwoHyphens(
'on',
suffixParts[ 0 ],
suffixParts[ 1 ]
);
if ( eventType.includes( '--' ) ) {
warnEventNameWithTwoHyphens( 'on', eventType );
}
}
if ( ! events.has( suffixParts[ 0 ] ) ) {
events.set( suffixParts[ 0 ], new Set< DirectiveEntry >() );
if ( ! events.has( eventType ) ) {
events.set( eventType, new Set< DirectiveEntry >() );
}
events.get( suffixParts[ 0 ] )!.add( entry );
events.get( eventType )!.add( entry );
} );

events.forEach( ( entries, eventType ) => {
Expand Down Expand Up @@ -199,7 +199,8 @@ directive( 'on', ( { directives: { on }, element, evaluate } ) => {
} );
} );

// data-wp-on-async--[event] (deprecated)
// data-wp-on-async--[event] (deprecated). The event name is still cut at `--`
// to keep the legacy two-hyphen unique ID syntax working until removal.
directive(
'on-async',
( { directives: { 'on-async': onAsync }, element, evaluate } ) => {
Expand Down
10 changes: 5 additions & 5 deletions packages/interactivity/src/directives/router-region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { cloneElement, type VNode } from 'preact';
import { useLayoutEffect } from 'preact/hooks';
import { signal, type Signal } from '@preact/signals';
import { directive, isDefaultDirectiveSuffix } from '../hooks';
import { warn } from '../utils';
import { getScope, navigationContextSignal } from '../scopes';
import { warnUniqueIdNotSupported } from './utils/warnings';
import {
warnSuffixNotSupported,
warnUniqueIdNotSupported,
} from './utils/warnings';

/**
* Relates each router region with its current vDOM content. Used by the
Expand All @@ -31,9 +33,7 @@ directive(

if ( entry.suffix ) {
if ( globalThis.SCRIPT_DEBUG ) {
warn(
`Suffixes for the data-wp-router-region directive are not supported. Ignoring the directive with suffix "${ entry.suffix }".`
);
warnSuffixNotSupported( 'router-region', entry.suffix );
}
return;
}
Expand Down
Loading
Loading