Repository navigation
Conversation
Components opt in with "interactive": true in their component.json. PHP prints them from their template, wrapped in <wp-ui-interactive>, and loads the script only when it prints one. React then mounts the real component over what PHP printed, starting from the field's current value. Adds a SwitchControl, whose template prints React's markup and is covered by the parity tests, and a SearchableSelectControl, which prints a native <select> styled like the component's closed state. Recipes can now name classes for more than one element (parts).
🤖 PR meta 🤖📦 Bundle sizeSize Change: 0 B Total Size: 8.64 MB
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Actions are reachable via RIGHT/LEFT arrow keys (Site Editor v2) in
|
…ponents-php-mount # Conflicts: # packages/ui/bin/build-php.mjs
…ponents-php-mount # Conflicts: # phpunit/experimental/ui-components-test.php
…ponents-php-mount # Conflicts: # lib/experimental/ui-components/load.php # packages/ui/bin/build-php.mjs # phpunit/experimental/ui-components-test.php Port the switch and select templates to the base: they pick their classes from the stylesheets in component.json, and escape with esc_html() and esc_attr(), as do the props React mounts with, so text reads the same before and after React takes over.
…ponents-php-mount
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
See #84036. Builds on #85756.
A proof of concept for interactive components on PHP screens. PHP prints the component, and React mounts the real one over it in the browser. Testing with two components:
<select>with the samename, styled like the component's closed state.Why?
Static components can stay as markup and CSS, but some are only useful with their behavior. For example: selects, switches, or the color and date pickers. The idea is to bring only those to life, and only where they're used. It's opt-in, so any screen can mix static and interactive components, whether or not it's built with React. The PHP call stays the same either way, so how a component comes alive is a detail we can change later. I've been experimenting mostly with two different approaches. Mounting and hydration.
The questions I wanted to answer:
How?
The basics of this proof of concept are:
"interactive": truein itscomponent.json. Components without it stay static and load no JS. The PHP call doesn't change:gutenberg_get_ui_component( 'switch-control', array( 'label' => …, 'name' => … ) ).<wp-ui-interactive>, with the props React needs. It enqueues the script the first time it prints one, so screens without an interactive component don't load it.createRoot. Each one is its own root, with its own ID prefix, so the page stays a regular PHP page with a few interactive pieces.build-php/interactive.js, that uses WordPress's ownreactandreact-dom.parts), since the switch template has four. The switch is covered by the parity tests like the button. The select isn't, since its native field is different on purpose.What I found
Mounting and hydration
As I mentioned, these are the two approaches I spent the most time on. Worked on hydration here, and here's a prototype I build with hydration and the build-markup approach earlier. Back to the comparison:
This PoC uses mounting because it gives the same result for form controls with less code, and it doesn't break when the markup, the translations or the React version don't line up. It also works with whatever PHP prints: a template like here, the markup the build extracts from React (#85759), or a native field. Hydration keeps its edge for components with content inside (more on that below).
What PHP prints before JS loads
In my local checks, the switch (16px) and the select (64px) are the same height before and after mounting, so nothing below them moves.
Components with content inside
Tabs, dialogs and collapsibles hold content that PHP prints: text, fields, or other components. Leaving aside how that content maps to React's pieces (that's the composition work, covered separately), the two approaches treat it differently when React takes over:
So for form controls and components driven by data, like menus, mounting is the simpler and sturdier option. For components with content inside, hydration fits more naturally, and mounting needs that extra step.
Which components this covers
I mapped this against the components the admin reskin restyled and the ones WooCommerce uses most:
In WooCommerce, form fields and help tips are the most used interactive patterns, and both fit. For help tips, PHP would print the trigger with the text visually hidden next to it, so screen readers get it before JS, and React mounts the infotip over it. The next ones by use (metaboxes, tabs and modals) all have content inside, so that's where the choice between mounting and hydration is still open.
Other options explored
<details>,<dialog>, native fields). No JS, but different markup, and it only covers the simpler components. It's still useful as what PHP prints before React mounts, like the native<select>here.Limitations and known issues
no-jsclass forjsearly), could make it clearer that they aren't ready yet.changeevents and expose avalue.Testing Instructions
npm run build.interactive.jsshouldn't load.Automated checks:
npm run test:unit:php:base -- --filter Gutenberg_UI_Components_TestTesting Instructions for Keyboard
Screenshots or screencast
Use of AI Tools
I used Claude Code to help prototype and write parts of this code. I reviewed and tested all of it.