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.
"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>
);
}| Primitive | Use |
|---|---|
PostList | List, paginate and filter posts (Root, Feed, LoadMore, Loading, Empty, Error). |
PostDetail | Show, edit and delete one post. |
PostThread | Comments and replies under a post. |
Composer | Create 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). |
Auth | Sign-in and sign-out buttons (Auth.Trigger). |
Profile | Edit the reader's profile. |
Report | Report a comment. |
Moderation | Site-admin tools for pending replies, reports and blocked readers. |
SiteAdmin | Site 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. |
OwnerPanel | The button that opens the site-management dialog (OwnerPanel.Trigger). |
Notifications | A bell of your own: trigger, badge, panel, list, read state. |
NotificationSettings | Notification 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.
| Hook | Use |
|---|---|
useKnitPosts | Posts, pagination, filters and the owner's actions on a list. |
useKnitPostDetail | One post and its edit and delete actions. |
useKnitThread | The comment tree of a post. |
useKnitComposer | Form state for creating and editing. |
useKnitMediaUpload | Image uploads. |
useKnitCollectionPicker | Choosing collections. |
useKnitProfile | The reader's profile. |
useKnitReport | Reporting. |
useKnitModeration | Pending replies, reports and blocks for site admins. |
useKnitSiteAdmin | Site and space settings for site admins. |
useKnitAudience | Members for site admins: the list, candidates (readers who have signed in), add, change the expiry date and remove. |
useKnitOwnerPanel | Open or close the site-management dialog from your own button. |
useKnitNotifications | The bell's list, unread count and read state. |
useKnitNotificationPreferences | Notification 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.