Headless UI

When the default UI's markup doesn't fit your design, build your own. Knit's primitives and hooks keep doing the work that has to be right — fetching, pagination, permissions, sign-in and Markdown sanitizing — and render nothing you didn't write.

Primitives

@knit/react/primitives has compound components for each feature. Each Root loads the data and shares it with its parts through context; the parts render your markup when their state matches.

headless-post-list.tsx
"use client";

import { PostList } from "@knit/react/primitives";

// Knit fetches, paginates and checks permissions; the markup is yours
export function MyPostList() {
  return (
    <PostList.Root space="default" collection="blog" pageSize={20}>
      <PostList.Loading>Loading…</PostList.Loading>
      <PostList.Error>{(message) => <p role="alert">{message}</p>}</PostList.Error>
      <PostList.Empty>No posts yet.</PostList.Empty>
      <ul className="my-post-list">
        <PostList.Feed>
          {(post) => (
            <li key={post.id}>
              {post.slug ? (
                <a href={`/blog/${encodeURIComponent(post.slug)}`}>{post.title ?? "Untitled"}</a>
              ) : (
                post.title
              )}
            </li>
          )}
        </PostList.Feed>
      </ul>
      <PostList.LoadMore className="my-button">
        {({ isLoading }) => (isLoading ? "Loading…" : "Load more")}
      </PostList.LoadMore>
    </PostList.Root>
  );
}
PrimitiveUse
PostListList, paginate and filter posts (Root, Feed, LoadMore, Loading, Empty, Error).
PostDetailShow, edit and delete one post.
PostThreadComments and replies under a post.
ComposerCreate or edit a post or comment (title, slug, tags, editor, submit). Composer.ExcerptInput and Composer.AdminLabelsInput render only for site admins editing a root post: the excerpt shown instead of the body to people who can't read the space, and admin labels readers never see. Composer.Field renders the input for one of the post type's own fields (see Post types).
AuthSign-in and sign-out buttons (Auth.Trigger).
ProfileEdit the reader's profile.
ReportReport a comment.
ModerationSite-admin tools for pending replies, reports and blocked readers.
SiteAdminSite and space settings (who can read and write, what non-members see, default reply policy, reply depth; SiteAdmin.SpaceSettings, ReadableBySelect, WritableBySelect, LockedModeSelect). SiteAdmin.WidenConfirm shows the confirmation (post and reply counts, who will see them) when a change would widen who can read a space. OwnerSettings, OwnerExport and OwnerPanel go inside SiteAdmin.Root.
OwnerPanelThe button that opens the site-management dialog (OwnerPanel.Trigger).
NotificationsA bell of your own: trigger, badge, panel, list, read state.
NotificationSettingsNotification settings: mute, scope, one checkbox per kind and channel.

Render posts and comments with KnitMarkdown from @knit/react. It sanitizes the Markdown, so never render a post's body as raw HTML yourself.

Hooks

Each primitive is built on a hook exported from @knit/react. Use a hook directly when even the compound structure is in your way.

HookUse
useKnitPostsPosts, pagination, filters and the owner's actions on a list.
useKnitPostDetailOne post and its edit and delete actions.
useKnitThreadThe comment tree of a post.
useKnitComposerForm state for creating and editing.
useKnitMediaUploadImage uploads.
useKnitCollectionPickerChoosing collections.
useKnitProfileThe reader's profile.
useKnitReportReporting.
useKnitModerationPending replies, reports and blocks for site admins.
useKnitSiteAdminSite and space settings for site admins.
useKnitAudienceMembers for site admins: the list, candidates (readers who have signed in), add, change the expiry date and remove.
useKnitOwnerPanelOpen or close the site-management dialog from your own button.
useKnitNotificationsThe bell's list, unread count and read state.
useKnitNotificationPreferencesNotification settings.

When you build your own Owner panel, useIsOwnerPanelHost picks the one instance that renders. useKnitPosts, useKnitThread and useKnitSiteAdmin reload on their own after approvals and deletions; reload your own requests when the version from useKnitModerationSignal changes. The replyPolicy in a useKnitComposer submit payload is set only when the user picked one.

useKnitSession, useKnitSite and useKnitAuth give you the current reader, the site settings and sign-in actions.

Errors from hooks and primitives (error, PostList.Error) are already translated with your messages, so show them as they are.