Get started
Installation
Add Jali to a React app with Vite, Next.js or another bundler, render it on the server, and set up TypeScript.
On this page
npm install jali-layoutpnpm add jali-layoutyarn add jali-layoutbun add jali-layoutvp add jali-layoutThat is the only package. It holds the framework-free core (jali-layout), the React parts (jali-layout/react), one entry per feature and the styles.
- React: 18.2 or later, 18 or 19. React is an optional peer dependency: an app that uses only
jali-layout(nojali-layout/react) needs none. - Browsers: Baseline 2024. Chrome and Edge 123, Firefox 120, Safari 17.5, and Electron 30. The colours are
light-dark()pairs, the newest CSS Jali needs; older browsers show the panes without their colours. - TypeScript: optional. The types ship in the package. Jali's own code is checked with
strict. - Dependencies: none at run time.
Add the styles
Link to this section: Add the stylesImport jali-layout/styles.css once, and name the order of your CSS cascade layers before it:
@layer base, jali;Every rule in Jali's styles sits in the jali layer. Your resets go in a layer before it. Your own styles go in a later layer, or outside any layer, and win without !important. With Tailwind 4 the line is @layer theme, base, jali, components, utilities;. See Theming.
Jali fills the box you give it, so that box needs a height. In development, a console warning says so when it has none.
Nothing to configure. Import the styles from your entry file, after your own stylesheet that holds the @layer line:
import { createRoot } from "react-dom/client";import { createJali, row } from "jali-layout";import { JaliRoot, definePane } from "jali-layout/react";import "./app.css";import "jali-layout/styles.css";const note = definePane("note", { params: { id: Number }, title: ({ id }) => `Note ${id}`, Component: ({ params }) => <article>Note {params.id}</article>,});const jali = createJali({ panes: [note], layout: row(note({ id: 1 }), note({ id: 2 })) });createRoot(document.getElementById("root")!).render(<JaliRoot jali={jali} />);The Quickstart builds this app step by step.
Next.js (App Router)
Link to this section: Next.js (App Router)jali-layout/react starts with "use client", so its components render inside your server components. The instance itself holds functions, so it can't cross from a server component to a client one. Make it in a client component:
"use client";// app/notes/panes.tsx: a client component. The instance is made here, once per render tree, never in a server// component (it holds functions and can't be passed from server to client).import { useState } from "react";import { createJali, memoryLocation, row } from "jali-layout";import { JaliRoot, definePane } from "jali-layout/react";import { defaultFeatures } from "jali-layout/features";import { urlSync } from "jali-layout/url";const note = definePane("note", { params: { id: Number }, title: ({ id }) => `Note ${id}`, Component: ({ params }) => <article>Note {params.id}</article>,});export function Panes({ search }: { search: string }) { const [jali] = useState(() => createJali({ panes: [note], layout: row(note({ id: 1 }), note({ id: 2 })), features: [ ...defaultFeatures(), // On the server, the request's address; in the browser, the browser's. urlSync(typeof window === "undefined" ? { location: memoryLocation(search) } : {}), ], // The first render fits this size on both sides, so the HTML matches; <JaliRoot> then measures the real one. size: { width: 1280, height: 800 }, }), ); return <JaliRoot jali={jali} aria-label="Notes" />;}Then render that component from a page. Pass it the request's search, so the server renders the layout a link names:
// app/notes/page.tsx: a server component. It passes the request's search to the client component, so the server// renders the layout the link names.import { Panes } from "./panes";export default async function NotesPage({ searchParams,}: { searchParams: Promise<Record<string, string | string[] | undefined>>;}) { const params = new URLSearchParams(); for (const [key, value] of Object.entries(await searchParams)) for (const v of [value].flat()) if (v !== undefined) params.append(key, v); return ( <main style={{ height: "100dvh" }}> <Panes search={params.size ? `?${params}` : ""} /> </main> );}Import the styles from your root layout, after the stylesheet that names the layers:
import "./globals.css"; // starts with @layer base, jali;import "jali-layout/styles.css";- One instance per render tree.
useState(() => createJali(...))makes one per request on the server and one per page in the browser. A module-level instance in a client file would be shared by every request the server handles. - Leaving the route.
<JaliRoot>stops the instance's listeners when it unmounts. Don't calljali.destroy()in an effect's cleanup: in development, Strict Mode runs each cleanup once at mount, and a destroyed instance refuses every call after.
Server rendering (Next, Astro, Remix)
Link to this section: Server rendering (Next, Astro, Remix)On a page a server renders, Jali mounts in the browser: wrap <JaliRoot> in <ClientOnly> from jali-layout/react. Jali measures its box to fit its panes (how many fit side by side, which fold into tabs), and a server has no box to measure. The rest of your page is in the server's HTML as usual, and the workspace appears once the page has hydrated.
import { ClientOnly, JaliRoot, useHydrated } from "jali-layout/react";export function Notes({ jali }: { jali: Jali }) { return ( <> {/* The workspace's room until it mounts, so nothing below it jumps. */} {useHydrated() ? null : <div style={{ height: 600 }} />} <ClientOnly> <JaliRoot jali={jali} /> </ClientOnly> </> );}useHydrated() is false on the server and while the page hydrates, then true: use it for anything else that must render the same on both sides. In Next.js, the component that makes the instance is a client component (above); in Astro it is a React island (client:load); in Remix and React Router's framework mode, any route component.
Rendering the workspace on the server too
Link to this section: Rendering the workspace on the server tooJali can also render on the server with renderToString or a streaming renderer, and hydrate without a mismatch: the repo's pnpm pack-check does exactly that with the packed package. It fits its panes to the size you give it, then again to its real box after hydration. Three things make the server's HTML and the browser's first render agree:
- The same inputs on both sides: the same kinds, layout or presets, and features.
- The same address. On the server, give
urlSync()the request's search withmemoryLocation(search). In the browser,urlSync()reads the real address, which holds the same search. - The same size for the first render. The server has no window, so pass
size. Pass the samesizein the browser;<JaliRoot>measures the real size after hydration and fits again.
On the server, storage is in memory, and the keys, Back and Forward, theme and saving don't start: <JaliRoot> starts them in the browser. A pane's own content renders on the server like any React component, so data it reads must be ready there too.
Other frameworks follow the same rules: React Router's framework mode, Remix and Astro's React islands all render a client component that makes the instance.
TypeScript: register your instance
Link to this section: TypeScript: register your instanceDeclare your instance once, and the React hooks know its kinds and page:
import { createJali } from "jali-layout";import { definePane } from "jali-layout/react";const order = definePane("order", { params: { id: Number }, title: ({ id }) => `Order ${id}`, Component: () => null });export const jali = createJali({ panes: [order], layout: order({ id: 1 }) });declare module "jali-layout" { interface Register { jali: typeof jali; }}Then useJali().actions.openBeside takes refs of your kinds only, and jali.on("open", ({ ref }) => …) narrows ref.params by ref.kind.
Linked or in a monorepo
Link to this section: Linked or in a monorepoIf your app gets Jali through link:, file: or workspace:, or from a symlinked checkout, two things can go wrong:
- Two copies of React. Jali finds its own copy of React beside its source, the app bundles two, and hooks fail with "Invalid hook call". With Vite, add
resolve: { dedupe: ["react", "react-dom"] }tovite.config.ts. With webpack, aliasreactandreact-domto your app's copy. - A stale build. A linked Jali resolves to its built files in
dist/, which don't rebuild when its source changes. Rebuild it withpnpm build:packagesin the Jali repo. Or read its source: with Vite, add the"source"condition,resolve: { conditions: ["source", ...defaultClientConditions] }. For Vitest, also addssr: { resolve: { conditions: ["source", ...defaultServerConditions] } }. Both defaults come fromvite.