[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M71G1GW50XF0J7NWS16GRJ4B":3,"skill-same:01M71G1GW50XF0J7NWS16GRJ4B":89},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":15,"categories":16,"keywords":17,"publishedAt":38,"linkOnly":39,"markdownContent":40,"markdownBody":41,"frontMatter":42,"refName":43,"commitSha":44,"skillDirectory":45,"forksCount":46,"repositoryHtmlUrl":47,"repositoryHomepageUrl":48,"ownerHtmlUrl":49,"ownerBlogUrl":50,"ownerDescription":51,"updatedAt":52,"install":53,"resources":66},"01M71G1GW50XF0J7NWS16GRJ4B","wp-interactivity-api",null,"Must use when building any kind of frontend interactivity in WordPress — dynamic or static blocks, classic themes, anything using data-wp-* directives, the @wordpress\u002Finteractivity package, wp_interactivity_state\u002Fwp_interactivity_data_wp_context\u002Fwp_interactivity_config helpers, the interactivity-router, or whenever a task involves making server-rendered WordPress markup interactive (toggles, menus, counters, filters, lightboxes, client-side navigation, etc.). Use this even if the user does not mention 'Interactivity API' by name.","Automattic\u002Fskillsmith","Automattic","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F887802?v=4","01M6BV62F3Z4NE573J3G0X5FFW","01M71G13NCZT1QNH4Y75Q4BF35",7,"GPL-3.0","https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Ftesting-project\u002Fskills\u002Fwp-interactivity-api\u002FSKILL.md",[],[18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37],"wp interactivity api","api","task","interactive","use","package","wordpress","by name","helpers","context","data","config","server","router","when","filters","themes","frontend","static","directives","2026-11-10 10:12:30",false,"---\nname: wp-interactivity-api\ndescription: \"Must use when building any kind of frontend interactivity in WordPress — dynamic or static blocks, classic themes, anything using data-wp-* directives, the @wordpress\u002Finteractivity package, wp_interactivity_state\u002Fwp_interactivity_data_wp_context\u002Fwp_interactivity_config helpers, the interactivity-router, or whenever a task involves making server-rendered WordPress markup interactive (toggles, menus, counters, filters, lightboxes, client-side navigation, etc.). Use this even if the user does not mention 'Interactivity API' by name.\"\n---\n\n# WordPress Interactivity API\n\nDeclarative `data-wp-*` directives on HTML paired with a reactive store (`state`, `actions`, `callbacks`). The server pre-renders final HTML from seeded state\u002Fcontext; the client hydrates without re-rendering. Requires WordPress 6.5+.\n\n## Hard rules — every one matters\n\n1. **`block.json` MUST declare `\"viewScriptModule\": \"file:.\u002Fview.js\"`.** `supports.interactivity: true` alone does NOT register the view module. Without `viewScriptModule`, `view.js` is never built, nothing hydrates, `actions.navigate()` falls back to a full reload. Do NOT compensate with `wp_register_script_module()` in a block's `render.php`.\n2. **The wrapper element MUST carry `data-wp-interactive=\"\u003Cnamespace>\"`.** Use the same namespace string in `store()`, `wp_interactivity_state()`, `wp_interactivity_config()`.\n3. **`render.php` MUST start in HTML mode — no leading `\u003C?php` left open over the markup.** Use `\u003C?php … ?>` only for the PHP bits (a `$ctx` assignment above the markup, inline `\u003C?php echo … ?>` for helpers). A bare `\u003C?php` opener followed by `\u003Cdiv …>` makes PHP parse the HTML as code → fatal error, block never renders.\n   - WRONG: `\u003C?php` on line 1, then `\u003Cdiv data-wp-interactive=\"…\">…\u003C\u002Fdiv>` with no `?>`.\n   - RIGHT: file begins with `\u003Cdiv data-wp-interactive=\"…\" \u003C?php echo get_block_wrapper_attributes(); ?>>…\u003C\u002Fdiv>` (or a `\u003C?php $ctx = […]; ?>` block that closes before the markup).\n4. **Seed every reactive value on the server, including empty starting values.** Anything a directive reads (`state.x`, `context.x`, `state.derived`) must exist before JS runs.\n   - Per-instance values → `wp_interactivity_data_wp_context([...])` on the wrapper.\n   - Cross-instance \u002F SDP-needed values → `wp_interactivity_state('\u003Cnamespace>', [...])`.\n   - Static config (REST URLs, nonces, flags) → `wp_interactivity_config('\u003Cnamespace>', [...])`.\n   - An empty paragraph still needs `'joke' => ''`; a placeholder still needs `'title' => '(no post loaded yet)'`.\n5. **Per-instance UI state lives in local context — that is the default.** A counter, a toggle's `isOpen`, a per-instance fetched value — all `wp_interactivity_data_wp_context()` + `getContext()`. Only use `wp_interactivity_state()` when the value is explicitly shared across instances\u002Fblocks (see \"Local context vs global state\" below).\n6. **Derived state is a JS GETTER on `state`, never a stored field, and never assigned to.** The directive ALWAYS binds `state.\u003Cderived-name>` — never `context.\u003Cderived-name>`, even when the underlying source lives in local context. Seed the derived value on the server with `wp_interactivity_state()` (a closure form when the source is per-instance context) so SDP renders the initial value before hydration. Full pattern in [Derived state — per-instance pattern](#derived-state--per-instance-pattern) below.\n7. **Async actions are generators, not `async`\u002F`await`.** `function* () { yield fetch(...); }`. `getContext()` \u002F `getElement()` work across `yield`s; `async`\u002F`await` breaks scope restoration.\n8. **Wrap with `withSyncEvent()` when the handler synchronously needs `event.preventDefault()` \u002F `stopPropagation()` \u002F `currentTarget`** (e.g. router-navigation click handlers). Full list in [references\u002Fstore.md](references\u002Fstore.md).\n9. **Mutate in place.** `state.list.push(x)`, `context.foo = y`. Never `state.list = [...state.list, x]` — breaks reactivity.\n10. **Never hand-duplicate values a directive will populate.** WRONG: `\u003Cspan data-wp-text=\"state.count\">0\u003C\u002Fspan>`. RIGHT: `\u003Cspan data-wp-text=\"state.count\">\u003C\u002Fspan>`. Same rule for bound attributes (omit attribute when `data-wp-bind--\u003Cattr>` is set), toggled classes\u002Fstyles (don't pre-add them), and `data-wp-each` (emit only `\u003Ctemplate>` — SDP writes the `\u003Cli data-wp-each-child>` items).\n11. **Wire all reactive behavior through directives.** `data-wp-text` (not `innerText`), `data-wp-bind--*`, `data-wp-class--*`, `data-wp-on--*` \u002F `data-wp-on-document--*` \u002F `data-wp-on-window--*`. No `addEventListener`, `classList.*`, `style.*`, or `innerHTML` from `view.js`. `.focus()` for focus management is the one allowed DOM write. Directive values are single references (`state.x`, `!context.isOpen`, `ns::state.x`) — move any arithmetic\u002Fcomparisons\u002Fcalls into a derived getter.\n\n## Standard skeleton\n\n```json\n\u002F\u002F block.json\n{\n  \"apiVersion\": 3,\n  \"name\": \"my-plugin\u002Fmy-block\",\n  \"title\": \"My Block\",\n  \"category\": \"widgets\",\n  \"supports\": { \"interactivity\": true },\n  \"render\": \"file:.\u002Frender.php\",\n  \"viewScriptModule\": \"file:.\u002Fview.js\"\n}\n```\n\n```php\n\u002F\u002F render.php — toggle, seeded context, declarative directives\n\u003C?php $ctx = array( 'isOpen' => false ); ?>\n\u003Cdiv\n  data-wp-interactive=\"my-plugin\u002Fmy-block\"\n  \u003C?php echo wp_interactivity_data_wp_context( $ctx ); ?>\n  \u003C?php echo get_block_wrapper_attributes(); ?>\n>\n  \u003Cbutton\n    data-wp-on--click=\"actions.toggle\"\n    data-wp-bind--aria-expanded=\"context.isOpen\"\n  >Toggle\u003C\u002Fbutton>\n  \u003Cp data-wp-bind--hidden=\"!context.isOpen\">Now visible.\u003C\u002Fp>\n\u003C\u002Fdiv>\n```\n\n```js\n\u002F\u002F view.js\nimport { store, getContext } from '@wordpress\u002Finteractivity';\n\nstore( 'my-plugin\u002Fmy-block', {\n  actions: {\n    toggle() {\n      const context = getContext();\n      context.isOpen = ! context.isOpen;\n    },\n  },\n} );\n```\n\n## Local context vs global state\n\nLocal context is the default. Reach for global state only when the value is **explicitly shared across instances\u002Fblocks** — e.g. \"the same counter on every instance on the page\", a site-wide filter, a value seeded for `data-wp-each`, or REST URLs \u002F nonces (which usually go in `wp_interactivity_config()`). Anything else — a counter, a toggle, a per-instance fetched result — belongs in local context.\n\n```php\n\u002F\u002F Local context (default) — each instance has its own counter.\n\u003C?php $ctx = array( 'counter' => 5 ); ?>\n\u003Cdiv\n  data-wp-interactive=\"my-plugin\u002Fcounter\"\n  \u003C?php echo wp_interactivity_data_wp_context( $ctx ); ?>\n  \u003C?php echo get_block_wrapper_attributes(); ?>\n>\n  \u003Cspan data-wp-text=\"context.counter\">\u003C\u002Fspan>\n  \u003Cbutton data-wp-on--click=\"actions.increment\">+\u003C\u002Fbutton>\n  \u003Cbutton data-wp-on--click=\"actions.decrement\">-\u003C\u002Fbutton>\n\u003C\u002Fdiv>\n```\n\n```js\nstore( 'my-plugin\u002Fcounter', {\n  actions: {\n    increment() { getContext().counter += 1; },\n    decrement() { getContext().counter -= 1; },\n  },\n} );\n```\n\n```php\n\u002F\u002F Global state (only when the prompt asks for a single shared value).\n\u003C?php wp_interactivity_state( 'my-plugin\u002Ftally', array( 'counter' => 0 ) ); ?>\n\u003Cdiv data-wp-interactive=\"my-plugin\u002Ftally\" \u003C?php echo get_block_wrapper_attributes(); ?>>\n  \u003Cspan data-wp-text=\"state.counter\">\u003C\u002Fspan>\n  \u003Cbutton data-wp-on--click=\"actions.increment\">+\u003C\u002Fbutton>\n\u003C\u002Fdiv>\n```\n\n```js\nconst { state } = store( 'my-plugin\u002Ftally', {\n  actions: { increment() { state.counter += 1; } },\n} );\n```\n\nIf in doubt, pick local context — dropping two instances of a global-state block onto the same page makes them share a number, which is almost never what the prompt asked for.\n\n## Derived state — per-instance pattern\n\nThe source of truth (e.g. `counter`) lives in local context. The derived value (e.g. `double`) is a JS getter on `state` that reads from `getContext()`. The directive binds `state.double` — never `context.double`. The server seeds the derived value with a closure inside `wp_interactivity_state()`, which calls `wp_interactivity_get_context()` for the current instance.\n\n```php\n\u003C?php $ctx = array( 'counter' => 1 ); ?>\n\u003C?php wp_interactivity_state( 'my-plugin\u002Fcounter', array(\n  'double' => function () {\n    return wp_interactivity_get_context()['counter'] * 2;\n  },\n) ); ?>\n\u003Cdiv\n  data-wp-interactive=\"my-plugin\u002Fcounter\"\n  \u003C?php echo wp_interactivity_data_wp_context( $ctx ); ?>\n  \u003C?php echo get_block_wrapper_attributes(); ?>\n>\n  \u003Cspan data-wp-text=\"context.counter\">\u003C\u002Fspan>\n  \u003Cspan data-wp-text=\"state.double\">\u003C\u002Fspan>\n  \u003Cbutton data-wp-on--click=\"actions.increment\">+\u003C\u002Fbutton>\n\u003C\u002Fdiv>\n```\n\n```js\nimport { store, getContext } from '@wordpress\u002Finteractivity';\n\nconst { state } = store( 'my-plugin\u002Fcounter', {\n  state: {\n    get double() {\n      return getContext().counter * 2;\n    },\n  },\n  actions: {\n    increment() {\n      getContext().counter += 1; \u002F\u002F mutate the source only\n    },\n  },\n} );\n```\n\n```php\n\u003C!-- WRONG — every one of these breaks the pattern: -->\n\n\u003C!-- 1. Derived value seeded inside context → stale after mutation. -->\n\u003C?php $ctx = array( 'counter' => 1, 'double' => 2 ); ?>\n\n\u003C!-- 2. Directive binds context.double → wrong surface, never updates. -->\n\u003Cspan data-wp-text=\"context.double\">\u003C\u002Fspan>\n```\n\n```js\n\u002F\u002F 3. Assigning to the derived getter inside an action.\nactions: {\n  increment() {\n    state.counter += 1;\n    state.double = state.counter * 2; \u002F\u002F defeats the getter\n  },\n},\n```\n\nSource in context. Derived getter on `state`. Directive binds `state.\u003Cname>`. Server seed via closure. All four, every time.\n\n## Async fetch — generator, seed the empty value\n\n```php\n\u003C?php $ctx = array( 'joke' => '' ); \u002F\u002F seed the empty value ?>\n\u003Cdiv\n  data-wp-interactive=\"my-plugin\u002Fjoke\"\n  \u003C?php echo wp_interactivity_data_wp_context( $ctx ); ?>\n  \u003C?php echo get_block_wrapper_attributes(); ?>\n>\n  \u003Cbutton data-wp-on--click=\"actions.fetchJoke\">Fetch joke\u003C\u002Fbutton>\n  \u003Cp data-wp-text=\"context.joke\">\u003C\u002Fp>\n\u003C\u002Fdiv>\n```\n\n```js\nstore( 'my-plugin\u002Fjoke', {\n  actions: {\n    *fetchJoke() {\n      const context = getContext();\n      const res = yield fetch( 'https:\u002F\u002Fexample.com\u002Fjoke' );\n      const data = yield res.json();\n      context.joke = data.joke; \u002F\u002F mutate AFTER the yield\n    },\n  },\n} );\n```\n\nFor REST + nonce, publish them via `wp_interactivity_config()`, read with `getConfig()`, and send `'X-WP-Nonce': nonce` in the `fetch` headers — full example in [references\u002Fstore.md](references\u002Fstore.md).\n\n## List with `data-wp-each`\n\n```php\n\u003C?php wp_interactivity_state( 'my-plugin\u002Ffruits', array(\n  'fruits' => array( 'Apple', 'Banana', 'Cherry' ),\n) ); ?>\n\u003Cdiv data-wp-interactive=\"my-plugin\u002Ffruits\" \u003C?php echo get_block_wrapper_attributes(); ?>>\n  \u003Cul>\n    \u003Ctemplate data-wp-each=\"state.fruits\">\n      \u003Cli data-wp-text=\"context.item\">\u003C\u002Fli>\n    \u003C\u002Ftemplate>\n  \u003C\u002Ful>\n  \u003Cbutton data-wp-on--click=\"actions.addMango\">Add Mango\u003C\u002Fbutton>\n\u003C\u002Fdiv>\n```\n\n```js\nconst { state } = store( 'my-plugin\u002Ffruits', {\n  actions: { addMango() { state.fruits.push( 'Mango' ); } },\n} );\n```\n\nSDP emits the initial `\u003Cli data-wp-each-child>Apple\u003C\u002Fli>…` after the `\u003Ctemplate>`. Don't write them by hand.\n\n## Init callback (`data-wp-init`)\n\nNo PHP setup needed → this `render.php` starts directly with `\u003Cdiv>`; no leading `\u003C?php`.\n\n```php\n\u003Cdiv\n  data-wp-interactive=\"my-plugin\u002Fhello\"\n  data-wp-init=\"callbacks.onReady\"\n  \u003C?php echo get_block_wrapper_attributes(); ?>\n>Hello from iAPI\u003C\u002Fdiv>\n```\n\n```js\nstore( 'my-plugin\u002Fhello', {\n  callbacks: { onReady() { console.log( 'iapi-ready' ); } },\n} );\n```\n\n## Common gotchas (see references for full patterns)\n\n- **Document \u002F window listeners go on the same wrapper that carries `data-wp-interactive`.** `getElement().ref` is the element carrying the directive, so a `data-wp-on-document--keydown` on an inner `\u003Cnav>` can't reach a sibling toggle button. Full focus-trap example in [references\u002Fstore.md](references\u002Fstore.md).\n- **Client-side navigation:** the router region wrapper needs BOTH `data-wp-interactive` AND `data-wp-router-region=\"\u003Cid>\"`. The click action is a `withSyncEvent` generator that calls `event.preventDefault()`, dynamically imports `@wordpress\u002Finteractivity-router`, and yields `actions.navigate(href)`. Anchors stay real `\u003Ca href>` so things work without JS. Build pagination hrefs with `esc_url( add_query_arg( 'pg', $next ) )` — not bare `?pg=\u003Cn>`, which replaces the whole query string and drops other vars like `p=\u003Cid>` on singular pages. Full pagination example in [references\u002Fclient-navigation.md](references\u002Fclient-navigation.md).\n\n## References (load on demand)\n\n- [references\u002Fdirectives.md](references\u002Fdirectives.md) — directive syntax (bind\u002Fclass\u002Fstyle\u002Ftext\u002Fon\u002Fwatch\u002Finit\u002Frun\u002Fkey\u002Feach\u002Fcontext\u002Finteractive).\n- [references\u002Fstore.md](references\u002Fstore.md) — `store()`, `getContext()`, `getElement()`, `getConfig()`, generators, `withSyncEvent()`, `withScope()`, focus-trap pattern.\n- [references\u002Fserver-rendering.md](references\u002Fserver-rendering.md) — `block.json`, `wp_interactivity_state\u002Fdata_wp_context\u002Fconfig`, server-side derived state (static + closure form), classic themes.\n- [references\u002Fclient-navigation.md](references\u002Fclient-navigation.md) — router regions, `actions.navigate`\u002F`prefetch`, `data-wp-key`, server-state sync, full pagination example.\n- [references\u002Ftypescript.md](references\u002Ftypescript.md) — typing stores, server state, derived getters, async actions.\n","# WordPress Interactivity API\n\nDeclarative `data-wp-*` directives on HTML paired with a reactive store (`state`, `actions`, `callbacks`). The server pre-renders final HTML from seeded state\u002Fcontext; the client hydrates without re-rendering. Requires WordPress 6.5+.\n\n## Hard rules — every one matters\n\n1. **`block.json` MUST declare `\"viewScriptModule\": \"file:.\u002Fview.js\"`.** `supports.interactivity: true` alone does NOT register the view module. Without `viewScriptModule`, `view.js` is never built, nothing hydrates, `actions.navigate()` falls back to a full reload. Do NOT compensate with `wp_register_script_module()` in a block's `render.php`.\n2. **The wrapper element MUST carry `data-wp-interactive=\"\u003Cnamespace>\"`.** Use the same namespace string in `store()`, `wp_interactivity_state()`, `wp_interactivity_config()`.\n3. **`render.php` MUST start in HTML mode — no leading `\u003C?php` left open over the markup.** Use `\u003C?php … ?>` only for the PHP bits (a `$ctx` assignment above the markup, inline `\u003C?php echo … ?>` for helpers). A bare `\u003C?php` opener followed by `\u003Cdiv …>` makes PHP parse the HTML as code → fatal error, block never renders.\n   - WRONG: `\u003C?php` on line 1, then `\u003Cdiv data-wp-interactive=\"…\">…\u003C\u002Fdiv>` with no `?>`.\n   - RIGHT: file begins with `\u003Cdiv data-wp-interactive=\"…\" \u003C?php echo get_block_wrapper_attributes(); ?>>…\u003C\u002Fdiv>` (or a `\u003C?php $ctx = […]; ?>` block that closes before the markup).\n4. **Seed every reactive value on the server, including empty starting values.** Anything a directive reads (`state.x`, `context.x`, `state.derived`) must exist before JS runs.\n   - Per-instance values → `wp_interactivity_data_wp_context([...])` on the wrapper.\n   - Cross-instance \u002F SDP-needed values → `wp_interactivity_state('\u003Cnamespace>', [...])`.\n   - Static config (REST URLs, nonces, flags) → `wp_interactivity_config('\u003Cnamespace>', [...])`.\n   - An empty paragraph still needs `'joke' => ''`; a placeholder still needs `'title' => '(no post loaded yet)'`.\n5. **Per-instance UI state lives in local context — that is the default.** A counter, a toggle's `isOpen`, a per-instance fetched value — all `wp_interactivity_data_wp_context()` + `getContext()`. Only use `wp_interactivity_state()` when the value is explicitly shared across instances\u002Fblocks (see \"Local context vs global state\" below).\n6. **Derived state is a JS GETTER on `state`, never a stored field, and never assigned to.** The directive ALWAYS binds `state.\u003Cderived-name>` — never `context.\u003Cderived-name>`, even when the underlying source lives in local context. Seed the derived value on the server with `wp_interactivity_state()` (a closure form when the source is per-instance context) so SDP renders the initial value before hydration. Full pattern in [Derived state — per-instance pattern](#derived-state--per-instance-pattern) below.\n7. **Async actions are generators, not `async`\u002F`await`.** `function* () { yield fetch(...); }`. `getContext()` \u002F `getElement()` work across `yield`s; `async`\u002F`await` breaks scope restoration.\n8. **Wrap with `withSyncEvent()` when the handler synchronously needs `event.preventDefault()` \u002F `stopPropagation()` \u002F `currentTarget`** (e.g. router-navigation click handlers). Full list in [references\u002Fstore.md](references\u002Fstore.md).\n9. **Mutate in place.** `state.list.push(x)`, `context.foo = y`. Never `state.list = [...state.list, x]` — breaks reactivity.\n10. **Never hand-duplicate values a directive will populate.** WRONG: `\u003Cspan data-wp-text=\"state.count\">0\u003C\u002Fspan>`. RIGHT: `\u003Cspan data-wp-text=\"state.count\">\u003C\u002Fspan>`. Same rule for bound attributes (omit attribute when `data-wp-bind--\u003Cattr>` is set), toggled classes\u002Fstyles (don't pre-add them), and `data-wp-each` (emit only `\u003Ctemplate>` — SDP writes the `\u003Cli data-wp-each-child>` items).\n11. **Wire all reactive behavior through directives.** `data-wp-text` (not `innerText`), `data-wp-bind--*`, `data-wp-class--*`, `data-wp-on--*` \u002F `data-wp-on-document--*` \u002F `data-wp-on-window--*`. No `addEventListener`, `classList.*`, `style.*`, or `innerHTML` from `view.js`. `.focus()` for focus management is the one allowed DOM write. Directive values are single references (`state.x`, `!context.isOpen`, `ns::state.x`) — move any arithmetic\u002Fcomparisons\u002Fcalls into a derived getter.\n\n## Standard skeleton\n\n```json\n\u002F\u002F block.json\n{\n  \"apiVersion\": 3,\n  \"name\": \"my-plugin\u002Fmy-block\",\n  \"title\": \"My Block\",\n  \"category\": \"widgets\",\n  \"supports\": { \"interactivity\": true },\n  \"render\": \"file:.\u002Frender.php\",\n  \"viewScriptModule\": \"file:.\u002Fview.js\"\n}\n```\n\n```php\n\u002F\u002F render.php — toggle, seeded context, declarative directives\n\u003C?php $ctx = array( 'isOpen' => false ); ?>\n\u003Cdiv\n  data-wp-interactive=\"my-plugin\u002Fmy-block\"\n  \u003C?php echo wp_interactivity_data_wp_context( $ctx ); ?>\n  \u003C?php echo get_block_wrapper_attributes(); ?>\n>\n  \u003Cbutton\n    data-wp-on--click=\"actions.toggle\"\n    data-wp-bind--aria-expanded=\"context.isOpen\"\n  >Toggle\u003C\u002Fbutton>\n  \u003Cp data-wp-bind--hidden=\"!context.isOpen\">Now visible.\u003C\u002Fp>\n\u003C\u002Fdiv>\n```\n\n```js\n\u002F\u002F view.js\nimport { store, getContext } from '@wordpress\u002Finteractivity';\n\nstore( 'my-plugin\u002Fmy-block', {\n  actions: {\n    toggle() {\n      const context = getContext();\n      context.isOpen = ! context.isOpen;\n    },\n  },\n} );\n```\n\n## Local context vs global state\n\nLocal context is the default. Reach for global state only when the value is **explicitly shared across instances\u002Fblocks** — e.g. \"the same counter on every instance on the page\", a site-wide filter, a value seeded for `data-wp-each`, or REST URLs \u002F nonces (which usually go in `wp_interactivity_config()`). Anything else — a counter, a toggle, a per-instance fetched result — belongs in local context.\n\n```php\n\u002F\u002F Local context (default) — each instance has its own counter.\n\u003C?php $ctx = array( 'counter' => 5 ); ?>\n\u003Cdiv\n  data-wp-interactive=\"my-plugin\u002Fcounter\"\n  \u003C?php echo wp_interactivity_data_wp_context( $ctx ); ?>\n  \u003C?php echo get_block_wrapper_attributes(); ?>\n>\n  \u003Cspan data-wp-text=\"context.counter\">\u003C\u002Fspan>\n  \u003Cbutton data-wp-on--click=\"actions.increment\">+\u003C\u002Fbutton>\n  \u003Cbutton data-wp-on--click=\"actions.decrement\">-\u003C\u002Fbutton>\n\u003C\u002Fdiv>\n```\n\n```js\nstore( 'my-plugin\u002Fcounter', {\n  actions: {\n    increment() { getContext().counter += 1; },\n    decrement() { getContext().counter -= 1; },\n  },\n} );\n```\n\n```php\n\u002F\u002F Global state (only when the prompt asks for a single shared value).\n\u003C?php wp_interactivity_state( 'my-plugin\u002Ftally', array( 'counter' => 0 ) ); ?>\n\u003Cdiv data-wp-interactive=\"my-plugin\u002Ftally\" \u003C?php echo get_block_wrapper_attributes(); ?>>\n  \u003Cspan data-wp-text=\"state.counter\">\u003C\u002Fspan>\n  \u003Cbutton data-wp-on--click=\"actions.increment\">+\u003C\u002Fbutton>\n\u003C\u002Fdiv>\n```\n\n```js\nconst { state } = store( 'my-plugin\u002Ftally', {\n  actions: { increment() { state.counter += 1; } },\n} );\n```\n\nIf in doubt, pick local context — dropping two instances of a global-state block onto the same page makes them share a number, which is almost never what the prompt asked for.\n\n## Derived state — per-instance pattern\n\nThe source of truth (e.g. `counter`) lives in local context. The derived value (e.g. `double`) is a JS getter on `state` that reads from `getContext()`. The directive binds `state.double` — never `context.double`. The server seeds the derived value with a closure inside `wp_interactivity_state()`, which calls `wp_interactivity_get_context()` for the current instance.\n\n```php\n\u003C?php $ctx = array( 'counter' => 1 ); ?>\n\u003C?php wp_interactivity_state( 'my-plugin\u002Fcounter', array(\n  'double' => function () {\n    return wp_interactivity_get_context()['counter'] * 2;\n  },\n) ); ?>\n\u003Cdiv\n  data-wp-interactive=\"my-plugin\u002Fcounter\"\n  \u003C?php echo wp_interactivity_data_wp_context( $ctx ); ?>\n  \u003C?php echo get_block_wrapper_attributes(); ?>\n>\n  \u003Cspan data-wp-text=\"context.counter\">\u003C\u002Fspan>\n  \u003Cspan data-wp-text=\"state.double\">\u003C\u002Fspan>\n  \u003Cbutton data-wp-on--click=\"actions.increment\">+\u003C\u002Fbutton>\n\u003C\u002Fdiv>\n```\n\n```js\nimport { store, getContext } from '@wordpress\u002Finteractivity';\n\nconst { state } = store( 'my-plugin\u002Fcounter', {\n  state: {\n    get double() {\n      return getContext().counter * 2;\n    },\n  },\n  actions: {\n    increment() {\n      getContext().counter += 1; \u002F\u002F mutate the source only\n    },\n  },\n} );\n```\n\n```php\n\u003C!-- WRONG — every one of these breaks the pattern: -->\n\n\u003C!-- 1. Derived value seeded inside context → stale after mutation. -->\n\u003C?php $ctx = array( 'counter' => 1, 'double' => 2 ); ?>\n\n\u003C!-- 2. Directive binds context.double → wrong surface, never updates. -->\n\u003Cspan data-wp-text=\"context.double\">\u003C\u002Fspan>\n```\n\n```js\n\u002F\u002F 3. Assigning to the derived getter inside an action.\nactions: {\n  increment() {\n    state.counter += 1;\n    state.double = state.counter * 2; \u002F\u002F defeats the getter\n  },\n},\n```\n\nSource in context. Derived getter on `state`. Directive binds `state.\u003Cname>`. Server seed via closure. All four, every time.\n\n## Async fetch — generator, seed the empty value\n\n```php\n\u003C?php $ctx = array( 'joke' => '' ); \u002F\u002F seed the empty value ?>\n\u003Cdiv\n  data-wp-interactive=\"my-plugin\u002Fjoke\"\n  \u003C?php echo wp_interactivity_data_wp_context( $ctx ); ?>\n  \u003C?php echo get_block_wrapper_attributes(); ?>\n>\n  \u003Cbutton data-wp-on--click=\"actions.fetchJoke\">Fetch joke\u003C\u002Fbutton>\n  \u003Cp data-wp-text=\"context.joke\">\u003C\u002Fp>\n\u003C\u002Fdiv>\n```\n\n```js\nstore( 'my-plugin\u002Fjoke', {\n  actions: {\n    *fetchJoke() {\n      const context = getContext();\n      const res = yield fetch( 'https:\u002F\u002Fexample.com\u002Fjoke' );\n      const data = yield res.json();\n      context.joke = data.joke; \u002F\u002F mutate AFTER the yield\n    },\n  },\n} );\n```\n\nFor REST + nonce, publish them via `wp_interactivity_config()`, read with `getConfig()`, and send `'X-WP-Nonce': nonce` in the `fetch` headers — full example in [references\u002Fstore.md](references\u002Fstore.md).\n\n## List with `data-wp-each`\n\n```php\n\u003C?php wp_interactivity_state( 'my-plugin\u002Ffruits', array(\n  'fruits' => array( 'Apple', 'Banana', 'Cherry' ),\n) ); ?>\n\u003Cdiv data-wp-interactive=\"my-plugin\u002Ffruits\" \u003C?php echo get_block_wrapper_attributes(); ?>>\n  \u003Cul>\n    \u003Ctemplate data-wp-each=\"state.fruits\">\n      \u003Cli data-wp-text=\"context.item\">\u003C\u002Fli>\n    \u003C\u002Ftemplate>\n  \u003C\u002Ful>\n  \u003Cbutton data-wp-on--click=\"actions.addMango\">Add Mango\u003C\u002Fbutton>\n\u003C\u002Fdiv>\n```\n\n```js\nconst { state } = store( 'my-plugin\u002Ffruits', {\n  actions: { addMango() { state.fruits.push( 'Mango' ); } },\n} );\n```\n\nSDP emits the initial `\u003Cli data-wp-each-child>Apple\u003C\u002Fli>…` after the `\u003Ctemplate>`. Don't write them by hand.\n\n## Init callback (`data-wp-init`)\n\nNo PHP setup needed → this `render.php` starts directly with `\u003Cdiv>`; no leading `\u003C?php`.\n\n```php\n\u003Cdiv\n  data-wp-interactive=\"my-plugin\u002Fhello\"\n  data-wp-init=\"callbacks.onReady\"\n  \u003C?php echo get_block_wrapper_attributes(); ?>\n>Hello from iAPI\u003C\u002Fdiv>\n```\n\n```js\nstore( 'my-plugin\u002Fhello', {\n  callbacks: { onReady() { console.log( 'iapi-ready' ); } },\n} );\n```\n\n## Common gotchas (see references for full patterns)\n\n- **Document \u002F window listeners go on the same wrapper that carries `data-wp-interactive`.** `getElement().ref` is the element carrying the directive, so a `data-wp-on-document--keydown` on an inner `\u003Cnav>` can't reach a sibling toggle button. Full focus-trap example in [references\u002Fstore.md](references\u002Fstore.md).\n- **Client-side navigation:** the router region wrapper needs BOTH `data-wp-interactive` AND `data-wp-router-region=\"\u003Cid>\"`. The click action is a `withSyncEvent` generator that calls `event.preventDefault()`, dynamically imports `@wordpress\u002Finteractivity-router`, and yields `actions.navigate(href)`. Anchors stay real `\u003Ca href>` so things work without JS. Build pagination hrefs with `esc_url( add_query_arg( 'pg', $next ) )` — not bare `?pg=\u003Cn>`, which replaces the whole query string and drops other vars like `p=\u003Cid>` on singular pages. Full pagination example in [references\u002Fclient-navigation.md](references\u002Fclient-navigation.md).\n\n## References (load on demand)\n\n- [references\u002Fdirectives.md](references\u002Fdirectives.md) — directive syntax (bind\u002Fclass\u002Fstyle\u002Ftext\u002Fon\u002Fwatch\u002Finit\u002Frun\u002Fkey\u002Feach\u002Fcontext\u002Finteractive).\n- [references\u002Fstore.md](references\u002Fstore.md) — `store()`, `getContext()`, `getElement()`, `getConfig()`, generators, `withSyncEvent()`, `withScope()`, focus-trap pattern.\n- [references\u002Fserver-rendering.md](references\u002Fserver-rendering.md) — `block.json`, `wp_interactivity_state\u002Fdata_wp_context\u002Fconfig`, server-side derived state (static + closure form), classic themes.\n- [references\u002Fclient-navigation.md](references\u002Fclient-navigation.md) — router regions, `actions.navigate`\u002F`prefetch`, `data-wp-key`, server-state sync, full pagination example.\n- [references\u002Ftypescript.md](references\u002Ftypescript.md) — typing stores, server state, derived getters, async actions.\n",{"name":5,"description":7},"trunk","95c86bdbbcae79c4dc46dea07d15fd5d69e4ee2d","testing-project\u002Fskills\u002Fwp-interactivity-api",2,"https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith","https:\u002F\u002Fautomattic.github.io\u002Fskillsmith\u002F","https:\u002F\u002Fgithub.com\u002FAutomattic","https:\u002F\u002Fautomattic.com","We are passionate about making the web a better place.","2026-08-24 19:55:45",{"source":8,"skillName":5,"commands":54,"prompt":64,"zipAvailable":65},[55,58,61],{"tool":56,"command":57},"npx","npx skills add Automattic\u002Fskillsmith --skill wp-interactivity-api",{"tool":59,"command":60},"bunx","bunx skills add Automattic\u002Fskillsmith --skill wp-interactivity-api",{"tool":62,"command":63},"pnpm","pnpm dlx skills add Automattic\u002Fskillsmith --skill wp-interactivity-api","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Ftesting-project\u002Fskills\u002Fwp-interactivity-api\u002FSKILL.md",true,[67,73,77,81,85],{"path":68,"resourceType":69,"resourceTypeDesc":70,"sizeBytes":71,"sourceHtmlUrl":72},"references\u002Fclient-navigation.md","REFERENCE","参考文档",6971,"https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Ftesting-project\u002Fskills\u002Fwp-interactivity-api\u002Freferences\u002Fclient-navigation.md",{"path":74,"resourceType":69,"resourceTypeDesc":70,"sizeBytes":75,"sourceHtmlUrl":76},"references\u002Fdirectives.md",5504,"https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Ftesting-project\u002Fskills\u002Fwp-interactivity-api\u002Freferences\u002Fdirectives.md",{"path":78,"resourceType":69,"resourceTypeDesc":70,"sizeBytes":79,"sourceHtmlUrl":80},"references\u002Fserver-rendering.md",8062,"https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Ftesting-project\u002Fskills\u002Fwp-interactivity-api\u002Freferences\u002Fserver-rendering.md",{"path":82,"resourceType":69,"resourceTypeDesc":70,"sizeBytes":83,"sourceHtmlUrl":84},"references\u002Fstore.md",9047,"https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Ftesting-project\u002Fskills\u002Fwp-interactivity-api\u002Freferences\u002Fstore.md",{"path":86,"resourceType":69,"resourceTypeDesc":70,"sizeBytes":87,"sourceHtmlUrl":88},"references\u002Ftypescript.md",3364,"https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Ftesting-project\u002Fskills\u002Fwp-interactivity-api\u002Freferences\u002Ftypescript.md",[90,96,101,106,112,117],{"skillNo":91,"name":92,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":93,"categories":94,"keywords":95,"publishedAt":38},"01M71G1ENEPSQR7HVTFM2HE91M","foo","https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Fsrc\u002F__tests__\u002Ffixtures\u002Fverify-project\u002Fskills\u002Ffoo\u002FSKILL.md",[],[92],{"skillNo":97,"name":92,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":98,"categories":99,"keywords":100,"publishedAt":38},"01M71G1D3CZATMR5JTW7TPMMX9","https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Fsrc\u002F__tests__\u002Ffixtures\u002Ftarget-project\u002Fskills\u002Ffoo\u002FSKILL.md",[],[92],{"skillNo":102,"name":92,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":103,"categories":104,"keywords":105,"publishedAt":38},"01M71G1CE0CZF05F44T60SYKMP","https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Fsrc\u002F__tests__\u002Ffixtures\u002Fsmoke-project\u002Fskills\u002Ffoo\u002FSKILL.md",[],[92],{"skillNo":107,"name":108,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":109,"categories":110,"keywords":111,"publishedAt":38},"01M71G1AYKRP91BQHT4P7A5JMY","multi","https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Fsrc\u002F__tests__\u002Ffixtures\u002Fskills\u002Fmulti\u002FSKILL.md",[],[108],{"skillNo":113,"name":92,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":114,"categories":115,"keywords":116,"publishedAt":38},"01M71G19CSRAAYDYR5H6XA01ME","https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Fsrc\u002F__tests__\u002Ffixtures\u002Fproj1\u002Fskills\u002Ffoo\u002FSKILL.md",[],[92],{"skillNo":118,"name":119,"nameZh":6,"description":6,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":9,"repositoryNo":12,"starsCount":13,"licenseSpdxId":14,"licenseFileUrl":6,"sourceHtmlUrl":120,"categories":121,"keywords":122,"publishedAt":38},"01M71G17PEPY6G843VGBGRQCQG","wp-foo","https:\u002F\u002Fgithub.com\u002FAutomattic\u002Fskillsmith\u002Fblob\u002Ftrunk\u002Fsrc\u002F__tests__\u002Ffixtures\u002Floop-project\u002Fskills\u002Fwp-foo\u002FSKILL.md",[],[92,123],"wp foo"]