Skip to content

(PoC) UI PHP components: Explore generating markup from React at build time - #85759

Draft
juanfra wants to merge 9 commits into
try/84036-ui-components-php-basefrom
try/84036-ui-components-php-build-markup
Draft

juanfra wants to merge 9 commits into
try/84036-ui-components-php-basefrom
try/84036-ui-components-php-build-markup

Conversation

@juanfra

@juanfra juanfra commented Oct 27, 2026

Copy link
Copy Markdown
Member

What?

See #84036. Builds on #85756.

This switches Button to take its markup from React at build time, instead of the recipe and PHP template from #85756. The API, the preview page and the parity tests stay the same, so it's easy to compare the two on the same component.

Why?

#85756 renders Button from a recipe and a small template, which is simple but kept in sync with React by hand. This PR explores the other side of that trade-off. Here React stays the only source of the markup, and nothing is written by hand per component beyond its props. Having both on top of the same base, passing the same tests, makes it easier to compare.

It's a lighter version of the approach first explored in #84714: the markup is stored as a base render plus changes (from #84905), and checking every combination is left to the shared parity tests.

How?

  • Markup from React. The build renders the component with placeholders for its content and free-form props (like class), once with the default props and once per variant value. It stores the default render plus what each value changes: classes, attributes and inline styles, per tag. For Button that's one base render and a handful of changes.
  • Rendering in PHP. When a component has no template, gutenberg_get_ui_component() starts from the base render, applies the changes for the given props with WP_HTML_Tag_Processor, and fills in the placeholders.
  • Escaping by context. Each placeholder knows whether it's in text or in an attribute (__wpuislot_content__text__, __wpuislot_class__attr_class__), and PHP escapes it for that context. URL attributes go through esc_url(). Values are escaped the way React does, without reusing existing entities. This is a small, custom version of the context-aware templating explored in wordpress-develop#5949: if that lands in core, the placeholder filling here could be replaced with it.
  • No changes to the base. The API, the preview page and the tests are the ones from UI PHP components: Add a base for rendering @wordpress/ui components in PHP #85756. Button just drops its recipe and template.

Trade-offs, compared with the recipe

With a recipe (#85756):

  • Each new component needs a small file listing its classes, and a short PHP template.
  • When React's version changes, someone updates those by hand. The tests say when.

With markup from React (this PR):

  • Each new component only needs its props listed.
  • When React's version changes, PHP follows on the next build, with nothing to update by hand.

Measurements

I measured how this scales on a few components with different shapes: class variants (Button, IconButton), one prop with many values (Badge, Text), props that interact (Link), structure that changes per variant (Notice), and many props with inline styles (Stack, probably the worst case for combinations).

Component Props Combinations Renders needed Stored markup Combinations the changes can't rebuild
Button 4 48 8 1.0 KB 0
IconButton 4 48 8 1.3 KB 0
Badge 1 7 7 1.0 KB 0
Text 1 9 9 1.1 KB 0
Link 3 8 4 2.1 KB 5
Notice 1 5 5 3.9 KB 4
Stack 5 ~960 19 1.4 KB 0

From these measurements:

  • The stored markup seems to stay small as combinations grow: Stack, with 960 combinations, stores about the same as Button.
  • Producing the markup only needs one render per value, not every combination: 19 renders for Stack's 960 combinations. Checking every combination would stay with the parity tests, as with the recipe.
  • Build time doesn't look like a practical problem at this size. On my machine, rendering all of Stack's 960 combinations took well under a tenth of a second, though timings vary between runs.
  • Some cases can't be rebuilt from changes. In Link and Notice, a prop adds a child or changes an icon. This PR doesn't handle them yet; they could be stored as full renders. A recipe would likely need logic in its template for the same cases.

So, from what I measured, the trade-off might be less about build time or output size, and more about where the complexity lives. Shared code in this approach, or a recipe and a small template per component in the recipe approach. It'd be worth checking with more components before drawing conclusions.

Limits

  • Variants that change the structure (like Notice's icon per intent) or props that interact (like a variant that ignores the tone) can't be expressed as changes. They could be stored as full renders, which this PR leaves out since Button doesn't need it. The parity tests would flag any component that does.
  • Props that React transforms with logic (like Avatar's initials from a name, or a value turned into a percentage) can't be placeholders. Those need PHP logic in either approach.
  • Escaping differs slightly from the template. This approach escapes like React, so an entity in the content (&) shows as written, while esc_html() in the template keeps it as an entity. Both match React in the parity tests, which compare the decoded text.

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. Check that the Examples table shows the same render for PHP and React, as in UI PHP components: Add a base for rendering @wordpress/ui components in PHP #85756.
  5. Optionally, look at packages/ui/build-php/manifest.json: Button now has a markup entry with the base render and the changes, instead of a recipe.

Automated checks, the same as #85756:

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

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 without a recipe get their markup from React: the build renders them with placeholders for their content and free-form props, once with the defaults and once per variant value, and stores the default render plus what each value changes.
When a component has no template, gutenberg_get_ui_component() applies the stored changes to the base render with WP_HTML_Tag_Processor and fills in the placeholders, escaped for their context. Button drops its recipe and template, and passes the same parity tests.
@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 27, 2026
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

📦 Bundle size

Size Change: 0 B

Total Size: 8.64 MB

0be53da Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 49fca61 try/84036-ui-components-php-base % Change
timeToFirstByte 38.7 ms +20.54% -5.94% 37.4 ms +12.3% -4.81% 3.48%
largestContentfulPaint 66 ms +15.15% -9.09% 64 ms +6.25% -9.38% 3.13%
lcpMinusTtfb 24.4 ms +23.36% -6.35% 23.05 ms +22.13% -3.25% 5.86%
wpBeforeTemplate 19.99 ms +19.41% -6.05% 19.16 ms +13.15% -3.29% 4.33%
wpTemplate 15.22 ms +13.99% -5.32% 14.87 ms +5.72% -6.12% 2.35%
wpTotal 35.31 ms +21.1% -6.06% 34.17 ms +13.4% -4.39% 3.34%
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 49fca61 try/84036-ui-components-php-base % Change
timeToFirstByte 37 ms +5.41% -2.16% 31.25 ms +14.4% -3.2% 18.4%
largestContentfulPaint 68 ms +2.94% -2.94% 60 ms +6.67% -6.67% 13.33%
lcpMinusTtfb 29.9 ms +6.35% -3.01% 27.55 ms +10.34% -6.53% 8.53%
wpBeforeTemplate 19.49 ms +15.75% -1.69% 17.29 ms +14.4% -3.7% 12.72%
wpTemplate 13.53 ms +7.02% -3.99% 11.26 ms +3.73% -5.6% 20.16%
wpTotal 33.78 ms +6.1% -2.81% 28.54 ms +13% -3.4% 18.36%
wpMemoryUsage 6.41 MB +0% -0% 6.41 MB +0% -0% 0.08%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 49fca61 try/84036-ui-components-php-base % Change
mediaProcessingJpeg 234.43 ms +4.68% -1.2% 238.19 ms +2.62% -1.38% -1.58%
mediaProcessingAvif 3801.95 ms +2.62% -0.9% 3809.97 ms +2.19% -1.22% -0.21%
mediaProcessingJpegToAvif 2667.17 ms +1.59% -1.56% 2660.22 ms +1.02% -1.14% 0.26%

media-upload

Metric 49fca61 try/84036-ui-components-php-base % Change
jpegUploadProcessing 1444.97 ms +2.46% -0.43% 1450.38 ms +0.57% -1.2% -0.37%
pngUploadProcessing 194.72 ms +11.66% -3.13% 213.26 ms +4.4% -4.9% -8.69%
largeJpegUploadProcessing 1459.15 ms +1.25% -1.21% 1443.67 ms +1.75% -0.73% 1.07%
multipleImageUploadProcessing 1514.48 ms +2.56% -1.17% 1489.27 ms +1.84% -0.53% 1.69%

post-editor

Metric 49fca61 try/84036-ui-components-php-base % Change
serverResponse 275.04 ms +3.53% -1.06% 265.5 ms +4.1% -7.57% 3.59%
firstPaint 278.79 ms +10.01% -5.63% 275.15 ms +11% -4.15% 1.32%
domContentLoaded 538.47 ms +1.84% -1.13% 536.27 ms +1.31% -0.89% 0.41%
loaded 539.2 ms +1.82% -1.14% 537.18 ms +1.27% -0.91% 0.38%
firstContentfulPaint 312.86 ms +5.95% -0.95% 305.97 ms +1.8% -1.61% 2.25%
firstBlock 1435.45 ms +1.54% -0.56% 1427.6 ms +1.39% -1.77% 0.55%
type 12.07 ms +14.25% -6.21% 12.87 ms +3.03% -5.52% -6.22%
typeWithoutInspector 12.15 ms +10.29% -10.21% 13.01 ms +7.3% -10.61% -6.61%
typeWithTopToolbar 16.6 ms +4.46% -7.59% 17.26 ms +4.4% -4.98% -3.82%
typeContainer 6.22 ms +4.02% -7.23% 6.09 ms +4.76% -5.58% 2.13%
focus 73.6 ms +3.02% -3.6% 83.94 ms +8.43% -12.52% -12.32%
firstFocus 146.94 ms +0% -0% 165.43 ms +0% -0% -11.18%
selectAll 243.9 ms +0.34% -2.04% 247.99 ms +1.4% -1.71% -1.65%
undo 20.88 ms +7.47% -4.84% 22.05 ms +5.17% -8.07% -5.31%
listViewOpen 47.39 ms +11.44% -6.54% 48.28 ms +14.06% -10.29% -1.84%
inserterOpen 27.57 ms +4.53% -5.4% 28.95 ms +14.16% -12.88% -4.77%
inserterHover 8.27 ms +4.59% -4.72% 8.77 ms +5.47% -7.98% -5.7%
inserterSearch 6.3 ms +4.44% -3.81% 6.19 ms +5.17% -7.92% 1.78%
loadPatterns 432.78 ms +7.84% -1.86% 423.44 ms +3.35% -2.3% 2.21%
wpTotal 267.98 ms +3.24% -1.33% 258.61 ms +4.19% -7.84% 3.62%
wpMemoryUsage 14.78 MB +0% -0% 14.77 MB +0% -0% 0.02%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 49fca61 try/84036-ui-components-php-base % Change
serverResponse 399.68 ms +1.13% -7.63% 401.47 ms +0.61% -7.78% -0.45%
firstPaint 422.57 ms +8.46% -10.11% 429.18 ms +7.2% -8.54% -1.54%
domContentLoaded 807.35 ms +1.32% -0.82% 802.32 ms +2.27% -1.12% 0.63%
loaded 808.44 ms +1.32% -0.83% 803.51 ms +2.24% -1.13% 0.61%
firstContentfulPaint 450.43 ms +5.83% -0.75% 452.05 ms +3.15% -3.54% -0.36%
firstBlock 2390.85 ms +6.31% -1.88% 2390.99 ms +0.39% -0.74% -0.01%
type 16.52 ms +10.23% -6.72% 17.56 ms +5.13% -11.22% -5.92%
navigate 116.57 ms +2.17% -3.67% 110.07 ms +11.02% -0.24% 5.91%
loadPatterns 1299.1 ms +7.73% -4.74% 1455.18 ms +1.53% -7.06% -10.73%
loadPages 932.3 ms +2.01% -1.68% 925.34 ms +0.42% -0.6% 0.75%
wpTotal 390.6 ms +1.14% -7.86% 391.91 ms +0.71% -7.86% -0.33%
wpMemoryUsage 12.80 MB +0% -0% 12.79 MB +0% -0% 0.09%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

0be53da 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.

Page List converts to Navigation Links with entity bindings in /test/e2e/specs/editor/blocks/navigation.spec.js, passed after 1 failed attempt.
Error: expect(locator).toContainText(expected) failed

Locator: getByRole('region', { name: 'Editor settings' }).getByRole('tabpanel', { name: 'Content' }).getByRole('button', { name: /Link to:/ })
Expected substring: "test-page-1"
Received string:    "Link to:Cat/catPagePublished"
Timeout: 5000ms

Call log:
  - Expect "toContainText" getByRole('region', { name: 'Editor settings' }).getByRole('tabpanel', { name: 'Content' }).getByRole('button', { name: /Link to:/ }) with timeout 5000ms
  - waiting for getByRole('region', { name: 'Editor settings' }).getByRole('tabpanel', { name: 'Content' }).getByRole('button', { name: /Link to:/ })
    14 × locator resolved to <button type="button" aria-expanded="false" aria-haspopup="dialog" class="components-button link-preview-button is-next-40px-default-size is-secondary">…</button>
       - unexpected value "Link to:Cat/catPagePublished"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/navigation.spec.js:1949:31

From 49684da, not the current head. Run

…ponents-php-build-markup

# Conflicts:
#	lib/experimental/ui-components/components/button.php
…ponents-php-build-markup

# Conflicts:
#	lib/experimental/ui-components/components/button.php
#	lib/experimental/ui-components/load.php
#	packages/ui/bin/build-php.mjs
#	packages/ui/php/button/component.json

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