Skip to content

Preview Toggle for Custom HTML Block#78688

Draft
KarunyaChavan wants to merge 5 commits into
WordPress:trunkfrom
KarunyaChavan:fix/custom-html-preview-toggle
Draft

Preview Toggle for Custom HTML Block#78688
KarunyaChavan wants to merge 5 commits into
WordPress:trunkfrom
KarunyaChavan:fix/custom-html-preview-toggle

Conversation

@KarunyaChavan
Copy link
Copy Markdown

What?

Core Ticket: https://core.trac.wordpress.org/ticket/65332

This PR makes the Custom HTML block open the raw HTML editor by default again and hardens the component against invalid or legacy viewMode attribute values.

Why?

Users reported regressions when the Custom HTML block rendered the Preview by default. Restoring the HTML-first experience fixes:

  • Searchability: users can use Cmd/Ctrl+F across the editor when code is visible.
  • Efficiency: fewer clicks to edit HTML content.
  • Accuracy: some tags do not render as expected in Preview, so showing raw code is more reliable for editing.

The change is implemented as a defensive improvement: setting the schema default ensures new blocks behave correctly, and runtime normalization prevents malformed or legacy viewMode values from leaving blocks stuck in Preview.

How?

  • Add an explicit default for the viewMode attribute in the block schema to make the default behavior deterministic for new blocks.
  • In the editor component, compute viewMode as attributes.viewMode === 'preview' ? 'preview' : 'html' so any undefined or invalid value falls back to 'html'.

This keeps existing blocks that explicitly saved viewMode: "preview" unchanged, while guaranteeing new blocks and invalid values render HTML-first.

Testing Instructions

  • Open a post or page in the block editor.
  • Insert a Custom HTML block.
  • Confirm the block shows the raw HTML editor by default (the inline textarea) and that you can search (Cmd/Ctrl+F) for text within it.
  • Click the toolbar Preview button - the block should switch to the preview iframe.
  • Click HTML to switch back to the raw editor.

Screenshots

Before

Screenshot 2026-05-26 at 7 36 44 PM

After

Case 1: Preview State

Screenshot 2026-05-26 at 7 38 01 PM

Case 2: Raw HTML

Screenshot 2026-05-26 at 7 38 12 PM

Use of AI Tools

AI was used to optimize the regex being used for stripping the CSS and JS wrapper tags.

- Restore the HTML editor as the default block view so users can search and edit code directly again.
- Add an explicit HTML/Preview toggle in the block toolbar, preserve preview rendering on demand, and keep the edit modal available for code changes.
…ling.

- Add "default": "html" to block.json so new Custom HTML blocks open the raw HTML editor by default.
- Normalize runtime view mode in edit.js: treat any non-'preview' value as 'html' and use the normalized viewMode for toolbar state.
- Replace deprecated __experimentalVStack from @wordpress/components with Stack from @wordpress/ui
- Add direction="column" prop to maintain vertical layout
@github-actions github-actions Bot added [Package] Block library /packages/block-library First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository labels May 26, 2026
@github-actions
Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @KarunyaChavan! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

KarunyaChavan and others added 2 commits May 26, 2026 19:43
… rendering

- Initialize Custom HTML editor in preview mode for new empty blocks.
- Keep editor/preview panes mounted (toggle visibility) to avoid iframe remount flicker when switching modes.
- Remove redundant viewMode schema attribute and simplify redundant mode/placeholder logic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Block library /packages/block-library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant