The admin

Mounting the admin

Add the admin to your app, with a live preview of your real pages.

<Admin> is the whole editor: a rail of collections, a panel with the open collection's documents (or the open document's outline), a top bar with Save, the preview, and an inspector with the selected item's form. It's a component, so you mount it on a route of your own.

import { Admin } from '@mortar-kit/admin';

export default function AdminRoute() {
  return (
    <Admin
      bricks={bricks}
      collections={collections}
      content={adminContent}
      previewUrl="/admin/preview"
    />
  );
}

content is a Content: reads plus save and remove, and move for reordering orderable collections (their document list gets drag handles and move up and down buttons). Give it uncached reads, so the admin always shows what is saved. Saves are validated again by createContent, and problems come back to the editor, linked to the fields they're about.

The preview route

The preview is an iframe on your own origin, rendering <PreviewPage> with your bricks and styles. Each collection can have a preview component of its own: pass layouts, by collection name, to render its documents inside your real layouts, so a post previews with its title and date and a product as its product page. A layout gets the document (updated as the editor types) and the rendered body:

import { PreviewPage } from '@mortar-kit/admin/preview';

const layouts = {
  posts: (doc, body) => <PostLayout post={doc}>{body}</PostLayout>,
  products: (doc) => <ProductLayout product={doc} />,
};

export default function AdminPreview() {
  return <PreviewPage bricks={bricks} collections={collections} content={freshSource} layouts={layouts} />;
}

A collection without a layout previews as <DocPreview>, when you pass collections: a card with the document's title, its first image, every other field under its label (dates and choices spelled out, references by their document's title, lists and objects nested), and then its body. Layouts can render <DocPreview collection={…} doc={doc}> themselves too, to start from it. Without collections, such a collection previews its body alone.

Load both routes with lazy(), so the admin's code is only downloaded on these routes.

As a router route

With @solidjs/router, use <AdminRoute> from @mortar-kit/admin/router in place of <Admin>. It takes the same props and adds two things. The admin's location lives in your URL's search parameters (?collection=posts&id=…, ?view=assets for the asset library), so documents can be linked and back and forward move between them. Router navigation that would lose unsaved changes asks first: a link, back and forward, or switching documents. A save still in flight is waited for before leaving.

// src/routes/admin.tsx, routed at /admin
import { AdminRoute } from '@mortar-kit/admin/router';

export default function AdminPage() {
  return <AdminRoute bricks={bricks} collections={collections} content={adminContent} previewUrl="/admin/preview" />;
}

confirmLeave(question) replaces window.confirm for the question.

The location in your URL, by hand

<Admin> keeps track of where it is itself by default. With another router, pass location and onNavigate:

<Admin
  bricks={bricks}
  collections={collections}
  content={adminContent}
  previewUrl="/admin/preview"
  location={{ collection: params.collection ?? 'pages', id: params.id, view: params.view === 'assets' ? 'assets' : undefined }}
  onNavigate={(to) => setParams({ collection: to.collection, id: to.id ?? null, view: to.view ?? null })}
/>

view: 'assets' is the asset library.

Assets

When content has assets (content created with a blob store, see Assets), the rail gets an Assets button. It opens the library: every uploaded file, filtered by kind, with upload by button or drag and drop, copy URL, and delete, which is refused while documents use the file and lists them. Image and file fields get Choose…, which opens the same library as a picker filtered to what the field accepts, and uploading a single file there picks it.

Unsaved changes

The editor guards closing the tab. <AdminRoute> guards navigation inside your app. With <Admin> and another router, listen to onDirtyChange and block navigation while there are unsaved changes.

Editing one document

<Editor> is the editor for a single document, without the rail and the list. Give it the collection, the document, previewUrl and an onSave.

AdminPropsinterface

import { AdminProps } from '@mortar-kit/admin'

interface AdminProps

Properties

bricks: readonly AnyBrick[]

The bricks this site defines.

collections: readonly AnyCollection[]

The site's collections, in the order the sidebar lists them.

content: Content

Reads and writes. Reads should be uncached, so the admin always shows what is saved (the app's server functions, or createContent in tests).

previewUrl: string

URL of the app route that renders <PreviewPage> from @mortar-kit/admin/preview.

location?: AdminLocation

Where the admin is, when the app keeps it (e.g. in the URL). Without it the admin keeps it itself.

onNavigate?: (to: AdminLocation) => void

With location: called to go somewhere; the app updates location (or doesn't, e.g. when its guard says no).

onDirtyChange?: (dirty: boolean) => void

Whether the open document has unsaved changes; see EditorProps.onDirtyChange.

resolveLink?: (url: URL) => { collection: string; slug: string } | undefined

Maps a URL of the site to the document it shows, e.g. /posts/hello to { collection: 'posts', slug: 'hello' }. ⌘/Ctrl-clicking a link in the preview opens that document in the admin; a link it doesn't map opens in a new tab. Default: the collections' url patterns (resolveUrl).

user?: AdminUser | null

The signed-in user (auth.user() from @mortar-kit/auth): shown at the foot of the rail, with Sign out.

signOutUrl?: string

Where Sign out posts. Default /admin/auth/sign-out.

AdminRoutefunction

import { AdminRoute } from '@mortar-kit/admin/router'

function AdminRoute(props: AdminRouteProps): Element

<Admin> as a route of a @solidjs/router app: its location lives in the URL's search parameters, and leaving a document with unsaved changes (a link, back/forward, switching documents) asks first. A save still in flight is waited for before leaving, so the next page reads what was saved.

Parameters

Returns

Element

Example

// src/routes/admin.tsx, routed at /admin
export default function AdminPage() {
  return <AdminRoute bricks={bricks} collections={collections} content={adminContent} previewUrl="/admin/preview" />;
}

PreviewPagePropsinterface

import { PreviewPageProps } from '@mortar-kit/admin/preview'

interface PreviewPageProps

Properties

bricks: readonly AnyBrick[]

The same bricks the editor uses.

content?: ContentSource

Where bricks with load read content: use uncached reads, so the preview shows what is saved now. Without it such bricks show an error.

collections?: readonly AnyCollection[]

The same collections the editor uses. A collection without a layout then previews as <DocPreview>: its title, fields and body.

layouts?: Readonly<Record<string, PreviewLayout>>

How each collection's documents preview, by collection name: usually the site's own layout, e.g. a post's title and date around its body. Without one, a collection previews as <DocPreview> (given collections), or else as its body alone.

DocPreviewPropsinterface

import { DocPreviewProps } from '@mortar-kit/admin/preview'

interface DocPreviewProps

Properties

collection: AnyCollection

The document's collection: its fields' labels and kinds, and its title field.

doc: Doc
children?: Element

The rendered body, shown under the fields (a collection with a body).

collections?: readonly AnyCollection[]

Every collection, to show a reference by its document's title (otherwise its slug).