Skip to content

fix(blocks): stop Block Kit forms in editor panels from reloading the editor - #3675

Merged
ascorbic merged 1 commit into
emdash-cms:mainfrom
masonjames:mj/blocks-nested-form-submit
Sep 30, 2026
Merged

ascorbic merged 1 commit into
emdash-cms:mainfrom
masonjames:mj/blocks-nested-form-submit

Conversation

@masonjames

@masonjames masonjames commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

The admin renders plugin editor panels inside the content editor's own <form> (ContentEditor → ContentSettingsPanel → SandboxedContentEditorPanel). A Block Kit form block in a panel is therefore a nested form. Submitting it reloads the editor, and the plugin never receives the values.

Chromium doesn't let a nested form's submit event bubble past the outer form. In Chromium 152, a submit dispatched at the inner form bubbles to elements between the two forms, but bubble-phase listeners on the outer form, the React root and document never run. React dispatches onSubmit from its root, so the form block's handler never runs and nothing calls preventDefault(). The browser then submits the inner form natively, which reloads the editor.

The form block now submits from the submit button's click and prevents its default, so no native submit event is dispatched. Pressing Enter in a single-line input clicks the submit button too (implicit submission), so both paths reach the plugin. Preventing the click also skips the browser's constraint validation, so the handler calls reportValidity() first: an out-of-range number still shows the browser's message and isn't sent. The existing onSubmit handler stays for native submissions that do reach React, such as requestSubmit() on a standalone form.

Block Kit forms outside the editor, such as on plugin admin pages, use the same click path and behave as before.

I hit this building a plugin editor panel with a Block Kit form.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes for the changed package (@emdash-cms/blocks)
  • pnpm lint rules pass on the changed files (type-aware oxlint, zero diagnostics)
  • pnpm test passes (or targeted tests for my change)
  • Changed files formatted with the repository formatter
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main.
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to an approved Discussion: https://github.com/emdash-cms/emdash/discussions/...
  • I have included screenshots below if this PR changes the UI

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 5.5 (Claude Code); reviewed with GPT-6.1 Sol (Codex CLI)

Screenshots / test output

Checked in the admin (demos/simple, Chromium) with a throwaway plugin whose editor panel renders a Block Kit form: a text input, a number input limited to 0–10, a select, and a submit button.

  • Before: clicking the submit button reloads the editor, and the plugin's route never runs.
  • After: clicking the button, or pressing Enter in the text input, sends form_submit. The panel shows the plugin's response ({"note":"hello","count":5,"network":"eip155:84532"}), and the editor doesn't reload or save.
  • After: with 11 in the number input, the browser shows "Value must be less than or equal to 10." and nothing is sent.

That demo build also included #3673, so its select shows the option's label.

Tests, on current upstream 54209bc9b:

  • tests/form-nested.test.tsx › "submits only the Block Kit form" renders a Block Kit form inside an outer form and clicks submit. It asserts the plugin receives form_submit with the field values, the outer onSubmit doesn't run, and no native submit event is dispatched. It fails on main: jsdom lets the nested submit bubble, so the outer onSubmit runs.
  • "keeps the browser's validation before submitting" uses a number input with max: 10: 11 is blocked and 5 submits. It passes on main, where the native submit validates, and fails if the click handler skips reportValidity().
  • These tests render the real Kumo components instead of the mocks in renderer.test.tsx. The fix relies on Button passing type and onClick to the native <button>, and on Input keeping min/max.
  • packages/blocks: 5 files, 166 tests pass. tsgo --noEmit, type-aware oxlint with --deny-warnings, and oxfmt are clean.

@changeset-bot

changeset-bot Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 609d20e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@emdash-cms/blocks Patch
@emdash-cms/admin Patch
emdash Patch
@emdash-cms/plugin-test Patch
@emdash-cms/plugin-embeds Patch
@emdash-cms/cloudflare Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/auth Patch
create-emdash Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
@emdash-cms/auth-atproto Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@3675

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@3675

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@3675

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@3675

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@3675

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@3675

emdash

npm i https://pkg.pr.new/emdash@3675

create-emdash

npm i https://pkg.pr.new/create-emdash@3675

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@3675

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@3675

@emdash-cms/plugin-test

npm i https://pkg.pr.new/@emdash-cms/plugin-test@3675

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@3675

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@3675

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@3675

@emdash-cms/registry-loader

npm i https://pkg.pr.new/@emdash-cms/registry-loader@3675

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/@emdash-cms/registry-moderation@3675

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@3675

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@3675

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@3675

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@3675

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@3675

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@3675

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@3675

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@3675

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@3675

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@3675

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@3675

commit: 609d20e

@masonjames
masonjames marked this pull request as ready for review September 30, 2026 03:04
@github-actions github-actions Bot added the review/needs-review No maintainer or bot review yet label Sep 30, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right fix in the right place. A Block Kit form rendered inside the admin content editor's <form> needs to keep its submit event from reaching the outer form, and stopPropagation() on the React synthetic event is the idiomatic way to do that. The changeset is concise, accurate, and follows the changeset README's guidance for a patch.

What I checked: the diff, the existing form renderer tests, packages/blocks/vitest.config.ts, package.json, AGENTS.md, and .changeset/README.md.

The only real issue is that the new regression test (packages/blocks/tests/form-nested.test.tsx) omits the @cloudflare/kumo and @phosphor-icons/react mocks that every existing block test in this package includes. Without them the test is fragile against real Kumo component behavior and will likely fail if the real Button doesn't forward type="submit" to the underlying <button>. It's a needs_fixing test setup bug, not a product bug.

No AGENTS.md convention violations in the production code, changeset, or i18n path. No logged-out query concerns (admin-only React path).

@@ -0,0 +1,41 @@
import { cleanup, fireEvent, render, screen } from "@testing-library/react";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[needs fixing] This new test is missing the @cloudflare/kumo and @phosphor-icons/react mocks that form-conditions.test.tsx and renderer.test.tsx both define at the top of the file. The FormBlockComponent and TextInputElementComponent both import from @cloudflare/kumo, so the test needs those mocks to render deterministically in jsdom. Without them, the test relies on the real Kumo components forwarding type="submit" to the underlying button, which the existing test suite clearly does not assume.

Add the Kumo (and any transitively-needed icon) vi.mock blocks, or extract them into a shared test helper and import it here. For example, at minimum:

vi.mock("@cloudflare/kumo", () => ({
	Button: ({ children, onClick, variant, type }: any) => (
		<button onClick={onClick} data-variant={variant} type={type || "button"}>
			{children}
		</button>
	),
	Input: ({ label, defaultValue, onChange, placeholder }: any) => (
		<div>
			<label>{label}</label>
			<input
				type="text"
				defaultValue={defaultValue}
				placeholder={placeholder}
				onChange={onChange}
			/>
		</div>
	),
}));
vi.mock("@phosphor-icons/react", () => ({
	Info: () => <span data-testid="icon-info" />,
}));

Keep the mock set consistent with the other block tests, or introduce a shared test helper to avoid duplicating the boilerplate across all future block tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the real Kumo components here on purpose. The fix relies on Button passing type and onClick to the native <button>, and on Input keeping min/max for reportValidity().

A mock would re-create exactly those props, so the test would still pass if Kumo stopped forwarding them. Also, the latest commit replaces the stopPropagation() approach.

Testing in the admin showed Chromium never delivers the nested submit to React, so the form now submits from the button click. The description has the details.

@emdashbot emdashbot Bot added review/awaiting-author Reviewed; waiting on the author to respond and removed review/needs-review No maintainer or bot review yet labels Sep 30, 2026
… editor

The admin renders plugin editor panels inside the content editor's <form>,
so a Block Kit form in a panel is a nested form. Chromium doesn't let a
nested form's submit event bubble past the outer form, so React's
onSubmit never runs: the plugin never receives form_submit, and the
browser submits the inner form natively, reloading the editor.

Submit from the submit button's click and prevent its default, so no
native submit event is dispatched. Pressing Enter in a single-line input
clicks the button too. Preventing the click skips the browser's constraint
validation, so the handler calls reportValidity() first.
@masonjames
masonjames force-pushed the mj/blocks-nested-form-submit branch from 676bec6 to 609d20e Compare September 30, 2026 03:38
@masonjames masonjames changed the title fix(blocks): keep a nested form block's submit from reaching the outer form fix(blocks): stop Block Kit forms in editor panels from reloading the editor Sep 30, 2026
@github-actions github-actions Bot added review/needs-rereview New commits since the last review size/M and removed size/S review/awaiting-author Reviewed; waiting on the author to respond labels Sep 30, 2026
@masonjames masonjames added the bot:review Trigger an emdashbot code review on this PR label Sep 30, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right fix in the right place. A Block Kit form rendered inside the admin content editor's outer <form> needs to stop the native submission path before it can escape the inner form, and intercepting the submit button's click event (plus the implicit-submission click that browsers synthesize on Enter) is a more robust workaround for Chromium's nested-form submit behavior than relying on React's onSubmit. The reportValidity() call preserves browser validation, and keeping handleSubmit covers non-click submissions like requestSubmit().

What I checked: the diff, packages/blocks/src/blocks/form.tsx, the existing form-conditions.test.tsx/renderer.test.tsx mock setup, packages/blocks/vitest.config.ts, package.json, AGENTS.md, and .changeset/README.md.

The production code and changeset look correct. The one remaining issue from the prior review is the new regression test: it still runs without the @cloudflare/kumo and @phosphor-icons/react mocks that every other block test in this package uses. Using the real design-system components couples the test to Kumo internals, CSS/ESM behavior in jsdom, and icon availability, and makes the regression suite inconsistent. Add the same minimal mocks the other tests use (they already forward type and onClick, so they exercise the fix deterministically).

No other AGENTS.md violations, no logged-out query concerns (admin-only React path), and the changeset still reads well.

Comment on lines +1 to +7
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";

import { BlockRenderer } from "../src/renderer.js";
import type { Block } from "../src/types.js";

afterEach(cleanup);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[needs fixing] This new test is still missing the @cloudflare/kumo and @phosphor-icons/react vi.mock blocks used by form-conditions.test.tsx and renderer.test.tsx. Rendering the real Kumo components in jsdom makes this test depend on design-system internals, icons, and any CSS/ESM constraints Kumo imposes, rather than on the component behavior under review. The existing mocks already forward type and onClick, so they can verify the fix deterministically.

Add the same mocks, or extract a shared test helper:

Suggested change
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { BlockRenderer } from "../src/renderer.js";
import type { Block } from "../src/types.js";
afterEach(cleanup);
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { BlockRenderer } from "../src/renderer.js";
import type { Block } from "../src/types.js";
vi.mock("@cloudflare/kumo", () => ({
Button: ({ children, onClick, variant, type }: any) => (
<button onClick={onClick} data-variant={variant} type={type || "button"}>
{children}
</button>
),
Input: ({ label, value, defaultValue, onChange, onBlur, placeholder, type, min, max }: any) => (
<div>
<label>{label}</label>
<input
type={type || "text"}
defaultValue={defaultValue}
value={value}
placeholder={placeholder}
min={min}
max={max}
onChange={onChange}
onBlur={onBlur}
/>
</div>
),
}));
vi.mock("@phosphor-icons/react", () => ({
Info: () => <span data-testid="icon-info" />,
}));
afterEach(cleanup);

@emdashbot emdashbot Bot added review/awaiting-author Reviewed; waiting on the author to respond and removed bot:review Trigger an emdashbot code review on this PR review/needs-rereview New commits since the last review labels Sep 30, 2026

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed review/awaiting-author Reviewed; waiting on the author to respond labels Sep 30, 2026
@ascorbic
ascorbic merged commit 85ab50c into emdash-cms:main Sep 30, 2026
47 checks passed
@emdashbot emdashbot Bot mentioned this pull request Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: signed review/approved Approved; no new commits since size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants