Built on the Interactivity API
Everything a page of Poly Community shows and does is WordPress blocks and the WordPress Interactivity API. The plugin has no front end framework of its own and ships no copy of the Interactivity API: the browser runs the one that comes with WordPress, 7.2 or later. A part you build is a block whose markup holds directives, a component script is a script module, and a move between pages goes through the Interactivity Router, as on any WordPress site that uses them.
So the WordPress documentation is the full reference for the directives, the store, the router and script modules. The pages of Poly Community say what it adds on top: the data a page is drawn from, the page files, the events that keep a page current. This page maps one to the other.
What WordPress gives
| Part | How Poly Community uses it | In the WordPress reference |
|---|---|---|
| Directives | Every view and every block template is HTML with data-wp-* directives. | List of directives |
| Drawing on the server | PHP draws each page with its directives applied, so a page is whole before any script runs. Blocks put their data in the state with wp_interactivity_state(). | Server-side rendering, wp_interactivity_state() |
| The store | One namespace, poly-community, for the whole community: global state, local context and derived state (getters). | Global state, local context, derived state and config, Setting the store |
| The Interactivity Router | A move to another page is a client-side navigation of one router region, poly-community/app. | Client-side navigation, @wordpress/interactivity-router |
| Script modules | The core script is the module poly-community. A component script is a module that imports it. | wp_register_script_module(), viewScriptModule in block.json |
| Compatibility with client-side navigation | Every block of Poly Community declares "supports": { "interactivity": true }, which says it is interactive and works with client-side navigation. Declare it on yours too. | Client-side navigation compatibility |
The directives in use
Poly Community uses these, each as WordPress documents it:
data-wp-interactiveanddata-wp-context: the namespace and the local context of a part.data-wp-bind,data-wp-class,data-wp-styleanddata-wp-text: attributes, classes, styles and text from the state.data-wp-html: trusted HTML drawn by the server and kept in the state by key, such as the rows of a list (Lists).data-wp-eachanddata-wp-key: a list drawn from its ids, each item kept by its key.data-wp-onanddata-wp-on-document: actions run on events.data-wp-watchanddata-wp-init: what runs when the state changes, or once.
In its scripts: store(), getContext(), getElement(), getServerState(), getServerContext(), withScope() and withSyncEvent(). No private API.
What Poly Community adds on top
None of these replace a part of the Interactivity API. Each is a way of using it:
- Rows, lists and parts in the state. The data of a page lives in its global state under
state.rows,state.listsandstate.html, read by getters of the core (state.partRows,state.rowPart), so a block draws from data the same way on the server and in the browser. See Lists and Live updates in code. - Page files. A move to a page can be drawn from a file made earlier, with its data asked in one request. The router still shows it: Poly Community gives it the page HTML through the
htmlparameter ofnavigate()instead of having it fetched. The styles and script modules the page needs come as the router brings any (Ensuring script modules load during navigation), and Poly Community asks that for every component script. See Moving between pages. - Events. What changes on the server is told as an event, and the browser writes what it carries into the state, which the directives draw. See Live updates in code.
- Facts of the reader. What the reader may do is in
state.reader, so a page drawn once for every reader of a kind shows each the buttons they may use. - The
poly-communitymodule. What a component script imports:api(),onLive(),mergePack(),listOf(),setPart()and the rest (Live updates in code).
Rules met with WordPress 7.2
Each follows from how the Interactivity API of WordPress 7.2 behaves, each was met in Poly Community, and each holds for a block of yours on its pages:
- Give no value in
store()for a key the server sets. The runtime takes the server state first andstore()merges over it, so a value written in the script replaces what the server said for that page or that member. Leave such keys to PHP and only type them in the script. - Give an element whose context changes from page to page a
data-wp-key. The router reuses an element at the same place, and it keeps the context it had (Preserving elements with data-wp-key). - Put
data-wp-interactiveanddata-wp-contexton a wrapper, never on the<template>ofdata-wp-each, as the server would carry them into the items after it. - A unique id on
data-wp-watch,data-wp-initanddata-wp-runtakes three hyphens, as indata-wp-watch---name. WordPress 7.2 ignores one with two. - Directives inside HTML drawn by
data-wp-htmldo not run. A button in a row drawn as HTML names its action, and Poly Community runs it (Buttons inside rows drawn as parts). - Write whole numbers into rows. PHP writes a float such as
60.0as it is, where the browser writes60, and the page would differ between the two.
What it does not allow
- Directives of your own. WordPress keeps them behind private APIs it says will break, so a block uses the directives above.
- A front end of another kind inside a page, such as a React app that draws its own HTML. It can be mounted, as Better Messages does with the messenger, in an element with a
data-wp-keythat changes on every page so the router never reuses it. Such a page has no page file and none of the live updates of Poly Community: it handles both itself. WordPress explains what that takes in Client-side navigation compatibility.
See also What you can build and Building for instant pages and live updates.