Skip to content

Frameworks

MLForm renders standard Web Components into an element you own, so every framework integration has the same three steps: render an empty element, call mountForm once it is in the document, and call unmount() when the component goes away.

import { mountForm } from "mlform";
import { schema } from "./forecast-schema";
import { transport } from "./transport";
const host = document.querySelector<HTMLElement>("#forecast-form");
if (!host) throw new Error("Missing #forecast-form.");
const mounted = mountForm(host, { schema, transport });
// When this view is removed, for example by your router:
// mounted.unmount();
  • Mount in the browser. Importing MLForm on the server is safe; mounting needs a DOM. Call mountForm from a client-side lifecycle hook.
  • Hand MLForm an empty element and do not render children into it from the framework. To mount over existing content intentionally, pass containerStrategy: "replace"; the content returns on unmount().
  • Mount once per element. Mounting again into the same element replaces the previous form once the new one is ready.
  • Always unmount. unmount() aborts pending work, disconnects the design system, and removes the host. Without it, listeners outlive the component.
  • Read state through subscriptions, not by remounting: mounted.form.subscribeSelector(...) feeds framework state. See Live preview.

For pages where the framework should own every element, use the headless view instead of mountForm.

When the framework keeps a hidden view alive instead of unmounting it, call mounted.suspend() on hide and mounted.resume() on show; pending work stops and the draft stays. For pages that only care about browser visibility, mount with hostLifecycle: "document" to do this automatically on visibilitychange and pagehide. See Lifecycle and hooks.

Load MLForm inside the iframe first so its Web Components are registered there. A same-origin parent can then call mountForm with a container from the iframe; theme media queries and focus use the iframe’s window.