Sections and rules
A surface is a page (or part of one) your users can reshape. A section is one part of it: a card, a chart, a table, a toolbar. Users move, resize, hide and restyle sections; what's inside a section is always yours.
import { MorphSurface, Section } from "@usemorph/react";
export default function Dashboard() {
return (
<MorphSurface id="dashboard">
<Grid container spacing={3}>
<Grid size={{ lg: 8 }}>
<Section name="Revenue"><RevenueChart /></Section>
</Grid>
<Grid size={{ lg: 4 }}>
<Section name="Latest orders" keep><LatestOrders /></Section>
</Grid>
</Grid>
</MorphSurface>
);
}
Until a user customizes the page, <MorphSurface> renders your page exactly as written. It also does that if Morph can't be reached.
<MorphSurface>#
| Prop | |
|---|---|
id |
Required. A plain string, unique in your app, e.g. "dashboard". Lowercase letters, digits, - and _. |
data |
Optional. Data that new views may read. See Data and actions. |
actions |
Optional. Your actions that pages may put on buttons. See Data and actions. |
shell |
Marks your app's frame (sidebar or top bar) around every page. init tags it for you. A change to it shows on every page. |
chat |
Include this area in the Morph box (default true). |
fallback |
What to show while the user's page loads (default: nothing). |
onError |
Called when loading, saving or the AI fails, e.g. to log it. |
<MorphProvider> must be above it, usually in your root layout. It holds the one Morph box for everything on screen: a request can change the page, the frame, or both, and Undo takes back the whole change.
The app's frame and app-wide look#
Besides each page's content, npm init usemorph@latest tags your app's frame, the sidebar or top bar from the layout around your signed-in pages, as a <MorphSurface id="app-shell" shell>. Navigation gets keep, so users can restyle it but never lose it. It never wraps the page content: pages stay their own areas.
Users can also change the app-wide look: "bigger text everywhere", "more compact", "a warmer accent". That's saved per user as theme CSS that may only set your CSS variables (--primary, --radius…) and font settings, on the selectors where you define them. It can't select elements, so it can't hide, move or cover anything. If your colours aren't CSS variables, only font size and spacing can change app-wide.
<Section>#
| Prop | |
|---|---|
name |
Required. What users and the AI call this part, e.g. "Revenue trend". |
id |
A stable id. Defaults to the name in snake_case (revenue_trend). Set it if you might rename the section later. |
description |
A sentence for the AI if the name isn't enough, e.g. "Monthly revenue, this year vs last." |
styling |
Class names inside the section that pages may restyle, e.g. ".bar (each bar), .bar.current". |
keep |
A rule: this part must stay on the page. See below. |
name, id, description, styling and keep must be written as plain values (strings, true, { … }), because usemorph sync reads them from your code.
Rules with keep#
<Section name="Create invoice" keep>…</Section> // always shown and clickable
<Section name="Navigation" keep={{ topmost: true }}>…</Section> // and above every other section
<Section name="Checkout" keep={{ aboveFold: true, minWidth: 120 }}>…</Section>
| Option | Means |
|---|---|
keep / keep={true} |
Must be on the page, at a real size, not hidden, and nothing on top of it |
aboveFold: true |
Visible without scrolling (top edge within the first 700px; change it with foldPx in morph.config.ts) |
topmost: true |
The highest section on the page |
minWidth, minHeight |
At least this many pixels |
Rules are checked on screen, in the user's browser, before a page is saved. A page that breaks one is repaired or not saved.
Your layout stays your layout#
Layout components around your sections whose props are plain values, like MUI's <Grid size={{ lg: 4 }}>, a <Stack spacing={3}> or a <div className="grid">, are part of your page's layout. Morph's AI gets to use the real ones, so a customized page keeps your responsive layout and look. A component given live values (a variable, a function) isn't treated as layout; if it isn't inside a section, sync tells you.
How init picks sections#
By default, Morph's AI plans it (you're asked first): it reads each page and the components it renders, and picks the element holding the page's main content and the meaningful parts inside it. It only chooses; init applies the choices itself, and a verifier checks that the edits only add tags and that sync accepts them. Public, marketing and sign-in pages are left alone, and sign-in URLs are never tagged whatever the AI says.
With --no-ai, init uses fixed rules instead, so the same code always gets the same tags:
- The components from your own files are what parts are made of. Library pieces (MUI, icons, a
components/uikit) are building blocks. - Starting from what a page returns, it steps through plain layout wrappers until the layout splits into two or more pieces with your components inside. Each is a section.
- A wrapper around one thing (
<Grid size>around<Budget />) stays outside the tag, so your layout is kept. - Neighbouring controls without your components (a row of buttons and dropdowns) become one section, named after its heading or "Toolbar".
- Names come from the component (
<LatestOrders>→ "Latest orders"), else its heading, else a label, else "Part 3". - Sign-in pages, pages with one part, and pages without your own components are left alone.
The tags are ordinary code: rename them, merge them, add keep. That's the intended way to fine-tune.
Server and client components#
<MorphSurface>, <Section> and <MorphProvider> are client components, and work inside both server and client components. Put them wherever your page's content is rendered:
- Server page: wrap the JSX the page returns, as in the example above.
- Thin page (
page.tsxonly renders<DashboardView />): put the surface and sections insideDashboardView's file. The surface must be written in the same file as its sections (React only sees the sections it directly contains). - Data loaded in the browser (React Query, SWR, Firestore listeners…): works as is. Sections re-render with your live data, and so does
dataif you pass it. - Content behind a loading state: wrap the loaded content (
loading ? <Skeleton /> : <MorphSurface>…</MorphSurface>), not the conditional. - Sections inside a condition (
{isAdmin && <Section name="Admin tools">…</Section>}) are optional: on customized pages they render nothing for users who don't have them.keepdoesn't apply to them.
Adding or changing sections later#
- New part on a page: wrap it in
<Section name="…">and runnpx usemorph sync.syncwarns about components on a surface that aren't inside a section. - New page: add
<MorphSurface id="…">and sections, thensync. - Renaming: a section's id comes from its name unless you set
id. Addid="old_id"when you rename, so pages users already customized keep finding it. - Removing a section: pages that used it show a placeholder where it was until the user resets or asks for a new layout. A smoother fallback is on our list.