Repository navigation
fix(blocks): stop Block Kit forms in editor panels from reloading the editor - #3675
Conversation
🦋 Changeset detectedLatest commit: 609d20e The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
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 |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-test
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-loader
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
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"; | |||
There was a problem hiding this comment.
[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.
There was a problem hiding this comment.
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.
… 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.
676bec6 to
609d20e
Compare
There was a problem hiding this comment.
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.
| 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); |
There was a problem hiding this comment.
[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:
| 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); |
What does this PR do?
The admin renders plugin editor panels inside the content editor's own
<form>(ContentEditor→ContentSettingsPanel→SandboxedContentEditorPanel). A Block Kitformblock 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
submitevent bubble past the outer form. In Chromium 152, asubmitdispatched at the inner form bubbles to elements between the two forms, but bubble-phase listeners on the outer form, the React root anddocumentnever run. React dispatchesonSubmitfrom its root, so the form block's handler never runs and nothing callspreventDefault(). The browser then submits the inner form natively, which reloads the editor.The form block now submits from the submit button's
clickand prevents its default, so no nativesubmitevent 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 callsreportValidity()first: an out-of-range number still shows the browser's message and isn't sent. The existingonSubmithandler stays for native submissions that do reach React, such asrequestSubmit()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
Checklist
pnpm typecheckpasses for the changed package (@emdash-cms/blocks)pnpm lintrules pass on the changed files (type-aware oxlint, zero diagnostics)pnpm testpasses (or targeted tests for my change)messages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.AI-generated code disclosure
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.form_submit. The panel shows the plugin's response ({"note":"hello","count":5,"network":"eip155:84532"}), and the editor doesn't reload or save.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 receivesform_submitwith the field values, the outeronSubmitdoesn't run, and no nativesubmitevent is dispatched. It fails onmain: jsdom lets the nestedsubmitbubble, so the outeronSubmitruns.max: 10: 11 is blocked and 5 submits. It passes onmain, where the native submit validates, and fails if the click handler skipsreportValidity().renderer.test.tsx. The fix relies onButtonpassingtypeandonClickto the native<button>, and onInputkeepingmin/max.packages/blocks: 5 files, 166 tests pass.tsgo --noEmit, type-aware oxlint with--deny-warnings, and oxfmt are clean.