Skip to content

(PoC) UI PHP components: Try interactive components - #85941

Draft
juanfra wants to merge 5 commits into
try/84036-ui-components-php-basefrom
try/84036-ui-components-php-mount
Draft

juanfra wants to merge 5 commits into
try/84036-ui-components-php-basefrom
try/84036-ui-components-php-mount

Conversation

@juanfra

@juanfra juanfra commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

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:

  • SwitchControl. Its PHP template prints the same markup React renders, so it looks right before JS loads.
  • SearchableSelectControl. Its markup depends on its items and is only built in the browser, so PHP prints a native <select> with the same name, 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:

  • Mounting or hydration? Hydration reuses the printed markup, but it has to match React's render exactly. Is that worth the compatibility work?
  • How does things work while JS loads, or if it fails?
  • Is a value changed before React takes over kept?
  • Does anything move when React takes over?
  • What does it cost in JS?

How?

The basics of this proof of concept are:

  • A component opts in with "interactive": true in its component.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' => … ) ).
  • PHP renders the component's template as usual and wraps it in <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.
  • In the browser, the script mounts the React component in each wrapper with 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.
  • Before mounting, the script reads the value from the printed field, so a change made before JS loaded is kept.
  • The build bundles the interactive components into one script, build-php/interactive.js, that uses WordPress's own react and react-dom.
  • Recipes can now name classes for more than one element (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:

Mounting Hydration
What React does Replaces what PHP printed Attaches to what PHP printed
Markup has to match React No. If it drifts, it looks a bit off until JS loads Yes, exactly, including React's generated IDs, or React renders everything again
React version Doesn't matter Has to be the one the markup was rendered with, since IDs can change between versions
Value changed before JS Kept: React starts from the field's current value Kept, but only with extra code per component (the switch and its hidden checkbox disagreed)
Layout moves when React loads No, as long as what PHP prints has the same size No
Content inside (tabs, dialogs) Rendered again, unless it's moved into the component Kept as it is

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

Component's markup (template or snapshot) Native field Empty mount point
Before JS loads Looks like the final component Looks close, with CSS (caret, small details) Nothing
Can be changed before JS Only through the label (switch) Yes No
Value kept after mount Yes, read from the hidden field Yes, read from the field Nothing to keep
Layout moves on mount No No, once styled to the same size Yes
Works for lists Template: yes, it can loop. Snapshot: needs rules per pattern Yes Yes
Cost A template per component, or the build extracting it CSS per field type None

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:

  • Hydration keeps the DOM PHP printed, so the content stays as it is: values typed into fields, handlers other scripts attached, and components nested inside. The catch is the same as always: everything around the content has to match React's render exactly, including the IDs tabs use to link each tab to its panel.
  • Mounting replaces what PHP printed, so the content would be rendered again and lose all of that. To keep it, React could render the component with empty panels, and the script could move the panels PHP printed into them. I haven't tried it.

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:

Group Examples With mounting
Static Button, Card, Notice, Badge, InputControl No change, no JS
Base UI form controls Switch, Checkbox, Select, Radio Fits, like the switch here (a template or a native field)
Searchable selects SearchableSelectControl, SearchableChipSelectControl Fits, with a native fallback like here. Searching on the server needs its own data contract
Driven by data Infotip, Menu, Calendar Fits: the props are data, and the popup only shows once JS loads
Content inside CollapsibleCard, Tabs, Dialog, AlertDialog Needs the PHP content moved into the component (see above)

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

  • Mounting over the build's snapshot (on top of (PoC) UI PHP components: Explore generating markup from React at build time #85759). No jump for the switch. For the select, it needed a rule (only the selected item is shown when closed), and every other list pattern would need its own. Templates or native fields cover that without build rules.
  • A native fallback without styling. It works before JS, but the select's layout moved noticeably when React replaced it. Styling it like the component fixed that.
  • A small toggle script that switches between React's open and closed renders. It worked for CollapsibleCard, but only a handful of components fit, there's no focus handling, and it means writing behavior a second time.
  • The Interactivity API. Every behavior would have to be written again as a store, and it isn't loaded in wp-admin today. It could still fit lighter improvements to existing screens.
  • Web components or a lighter library. They'd cover everything, but they're close to a second design system to maintain.
  • Native HTML (<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

  • Controls printed with the component's markup can't be used before JS loads. That's the case for the switch, and would be for checkboxes, radios and selects printed the same way. Native fields work from the start. For the switch, clicking its label still checks the hidden field, and React shows it once it mounts. A subtle loading state on those controls, shown only when JS is on (WordPress swaps the body's no-js class for js early), could make it clearer that they aren't ready yet.
  • Built-in strings stay in English, like "No results found." in the select. This isn't specific to mounting: templates, the build's markup and the bundle all have the same strings, so it needs a holistic approach rather than a fix per direction.
  • Old jQuery code can't read or listen to the searchable select once React mounts. The wrapper could fire change events and expose a value.
  • The bundle can grow in size, plus React on screens that don't load it today. It's one bundle for every interactive component, so splitting it per component would mean screens only load what they print. The next step would be trying one control on a real settings screen to see the cost there.
  • Components with content inside (tabs, dialogs, collapsibles) aren't covered yet. Mounting would need to move the content PHP printed into the React component, so it isn't rendered again (see above). Tabs would be a good next one to try that with.
  • Many help tips on one page would mean many React roots. That's probably fine, but if it isn't, one root could render all of them. I haven't measured it.

Testing Instructions

  1. Run npm run build.
  2. Go to Settings → Experiments and enable "UI components for PHP screens".
  3. Go to Tools → UI Components.
  4. In the Examples table, check the SearchableSelectControl and SwitchControl rows. They shouldn't move when the page loads, and they should work once it has.
  5. With a slow network in DevTools, change the select or click a switch's label before the page finishes loading. The value should be kept.
  6. Go to the dashboard or Settings → General: interactive.js shouldn't load.

Automated checks:

  • npm run test:unit:php:base -- --filter Gutenberg_UI_Components_Test

Testing Instructions for Keyboard

Screenshots or screencast

Screenshot 2026-10-29 at 12-28-42 UI Components ‹ Stories® — WordPress

Use of AI Tools

I used Claude Code to help prototype and write parts of this code. I reviewed and tested all of it.

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).
@juanfra juanfra added [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible [Package] UI /packages/ui labels Oct 29, 2026
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

📦 Bundle size

Size Change: 0 B

Total Size: 8.64 MB

9d955c7 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 24ef222 try/84036-ui-components-php-base % Change
timeToFirstByte 46.35 ms +8.74% -2.91% 47.45 ms +5.9% -3.79% -2.32%
largestContentfulPaint 76 ms +5.26% -5.26% 78 ms +7.69% -5.13% -2.56%
lcpMinusTtfb 28.7 ms +3.83% -5.92% 28.15 ms +21.85% -3.02% 1.95%
wpBeforeTemplate 23.22 ms +9.26% -0.86% 23.59 ms +9.16% -2.63% -1.57%
wpTemplate 19.16 ms +5.06% -3.97% 19.66 ms +3.76% -5.6% -2.54%
wpTotal 42.62 ms +8.73% -2.56% 43.55 ms +6.52% -4.13% -2.14%
wpMemoryUsage 7.72 MB +0% -0% 7.72 MB +0% -0% 0.07%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 24ef222 try/84036-ui-components-php-base % Change
timeToFirstByte 38.25 ms +5.75% -1.57% 39.5 ms +4.18% -2.41% -3.16%
largestContentfulPaint 72 ms +2.78% -0% 72 ms +5.56% -0% 0%
lcpMinusTtfb 33.85 ms +2.36% -1.33% 34.15 ms +2.34% -3.37% -0.88%
wpBeforeTemplate 20.95 ms +4.3% -1.48% 21.2 ms +6.6% -1.7% -1.18%
wpTemplate 14.61 ms +1.1% -3.56% 15.1 ms +2.65% -3.51% -3.25%
wpTotal 35.48 ms +5.52% -1.86% 36.54 ms +4.38% -2.65% -2.9%
wpMemoryUsage 6.41 MB +0% -0% 6.41 MB +0% -0% 0.08%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 24ef222 try/84036-ui-components-php-base % Change
mediaProcessingJpeg 413.3 ms +0.09% -0.95% 414.97 ms +1.14% -0.58% -0.4%
mediaProcessingAvif 6347.47 ms +0.21% -0.38% 6388.35 ms +0.1% -0.18% -0.64%
mediaProcessingJpegToAvif 4350.25 ms +0.17% -0.51% 4395.33 ms +0.15% -0.31% -1.03%

media-upload

Metric 24ef222 try/84036-ui-components-php-base % Change
jpegUploadProcessing 1509.97 ms +33.25% -1.53% 1504.36 ms +0.64% -0.71% 0.37%
pngUploadProcessing 257.51 ms +1.5% -3.72% 269.72 ms +0.74% -4.27% -4.53%
largeJpegUploadProcessing 1477.64 ms +0.64% -0.35% 1497.26 ms +2.12% -0.85% -1.31%
multipleImageUploadProcessing 1642.4 ms +1.09% -2.09% 2050.68 ms +1.54% -18.72% -19.91%

post-editor

Metric 24ef222 try/84036-ui-components-php-base % Change
serverResponse 365.4 ms +2.36% -11.46% 350.86 ms +0.33% -9.58% 4.14%
firstPaint 399.98 ms +2.24% -7.56% 361.11 ms +7.25% -9.29% 10.76%
domContentLoaded 734.44 ms +2.9% -1.5% 698.39 ms +2.03% -2.44% 5.16%
loaded 735.54 ms +2.91% -1.48% 699.46 ms +2.08% -2.42% 5.16%
firstContentfulPaint 416.84 ms +3.6% -3.2% 396.78 ms +2.08% -1.05% 5.06%
firstBlock 1942.85 ms +2.51% -0.82% 1848.34 ms +0.33% -0.87% 5.11%
type 18.48 ms +5.79% -12.5% 16.47 ms +2.06% -11.23% 12.2%
typeWithoutInspector 17.11 ms +5.61% -12.04% 16.13 ms +4.96% -13.95% 6.08%
typeWithTopToolbar 21.44 ms +7.88% -4.66% 22.28 ms +6.24% -6.82% -3.77%
typeContainer 7.22 ms +4.43% -6.65% 7.16 ms +10.75% -9.78% 0.84%
focus 101.13 ms +6.59% -5.03% 105.22 ms +4.25% -2.7% -3.89%
firstFocus 212.58 ms +0% -0% 235.99 ms +0% -0% -9.92%
selectAll 330.92 ms +1.53% -2.27% 360.89 ms +2.9% -1.09% -8.3%
undo 26.68 ms +4.42% -7.2% 27.35 ms +5.05% -4.75% -2.45%
listViewOpen 64.92 ms +16.22% -7.07% 68.2 ms +19.22% -4% -4.81%
inserterOpen 35.68 ms +6.56% -8.94% 35.47 ms +2.06% -1.55% 0.59%
inserterHover 10.7 ms +6.82% -10.28% 10.69 ms +12.07% -9.54% 0.09%
inserterSearch 7.39 ms +5.82% -3.92% 6.82 ms +13.49% -4.69% 8.36%
loadPatterns 540.64 ms +1.82% -1.59% 556.97 ms +4.04% -1.61% -2.93%
wpTotal 355.89 ms +2.32% -11.75% 341.5 ms +0.4% -9.81% 4.21%
wpMemoryUsage 14.78 MB +0% -0% 14.77 MB +0% -0% 0.02%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 24ef222 try/84036-ui-components-php-base % Change
serverResponse 274.51 ms +2.68% -6.86% 242.56 ms +2.82% -4.75% 13.17%
firstPaint 316.9 ms +8.33% -11.13% 283.09 ms +5.15% -4.94% 11.94%
domContentLoaded 609.93 ms +0.63% -0.88% 518.98 ms +3.46% -4% 17.52%
loaded 610.84 ms +0.65% -0.88% 519.8 ms +3.49% -4.02% 17.51%
firstContentfulPaint 346.1 ms +1.5% -1.45% 299.51 ms +3.21% -2.08% 15.56%
firstBlock 1852.6 ms +0.44% -1.73% 1552.88 ms +4.65% -0.72% 19.3%
type 11.81 ms +0.93% -8.98% 12.33 ms +9.73% -4.79% -4.22%
navigate 77.49 ms +10.03% -3.51% 74.94 ms +17.73% -7.74% 3.4%
loadPatterns 957.93 ms +4.93% -11.59% 950.05 ms +5.45% -8.58% 0.83%
loadPages 894.03 ms +3.57% -1.56% 908.59 ms +2.38% -1.07% -1.6%
wpTotal 267.73 ms +2.78% -7% 236.26 ms +2.95% -4.79% 13.32%
wpMemoryUsage 12.80 MB +0% -0% 12.79 MB +0% -0% 0.09%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

9d955c7 Run

🏁 Flaky tests

Some 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 /test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeFocused() failed

Locator:  getByRole('row', { name: 'Page One View Actions' }).getByLabel('Actions')
Expected: focused
Received: inactive
Timeout:  5000ms

Call log:
  - Expect "toBeFocused" getByRole('row', { name: 'Page One View Actions' }).getByLabel('Actions') with timeout 5000ms
  - waiting for getByRole('row', { name: 'Page One View Actions' }).getByLabel('Actions')
    14 × locator resolved to <button type="button" tabindex="-1" aria-haspopup="menu" aria-label="Actions" aria-expanded="false" id="view-list-0-12-dropdown" class="components-button is-small has-icon">…</button>
       - unexpected value "inactive"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js:181:5
Navigates the list via UP/DOWN arrow keys from action buttons in /test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeFocused() failed

Locator:  getByRole('row', { name: 'Page Two Edit Actions' }).getByRole('button', { name: 'Edit' })
Expected: focused
Received: inactive
Timeout:  5000ms

Call log:
  - Expect "toBeFocused" getByRole('row', { name: 'Page Two Edit Actions' }).getByRole('button', { name: 'Edit' }) with timeout 5000ms
  - waiting for getByRole('row', { name: 'Page Two Edit Actions' }).getByRole('button', { name: 'Edit' })
    14 × locator resolved to <button type="button" tabindex="-1" class="components-button is-small" id="view-list-0-177-primary-action-edit-post">Edit</button>
       - unexpected value "inactive"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js:243:5
Undo restores the post selection when another synced entity is loaded (RTC WebSocket) in /test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

  Object {
    "attributeKey": "content",
    "blockIndex": 0,
    "content": "abcdef",
-   "endOffset": 3,
-   "offset": 3,
+   "endOffset": 1,
+   "offset": 1,
  }

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts:363:5
Undo restores the post selection when another synced entity is loaded in /test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

  Object {
    "attributeKey": "content",
    "blockIndex": 0,
    "content": "abcdef",
-   "endOffset": 3,
-   "offset": 3,
+   "endOffset": 2,
+   "offset": 2,
  }

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts:363:5
keeps the default tab when a tab is added before it in /test/e2e/specs/editor/blocks/tabs.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

Expected: 2
Received: 0

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/tabs.spec.js:319:60
should restore an older revision in /test/e2e/specs/editor/various/revisions.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 1
+ Received  + 1

  Array [
    Object {
      "attributes": Object {
-       "content": "Original content - Updated content - Autosaved",
+       "content": "Original content",
      },
      "name": "core/paragraph",
    },
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/revisions.spec.js:146:5

9d955c7 Run

…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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] UI /packages/ui [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant