React is the only peer dependency. Import Pixxl CSS once in the application root.
pnpm add @pixxl-tools/components
import "@pixxl-tools/components/styles.css";
import { Button, ResourcePage } from "@pixxl-tools/components";
export function App() {
return (
<ResourcePage title="Projects" actions={<Button>New</Button>}>
Project content
</ResourcePage>
);
}styles.css is everything. The feature entry points split it by audience (same cascade layers, same order); a typical website loads core + site.
styles.css: everything, core + every group (247 kB raw, 33.3 kB gzip).
css/core.css: tokens, reset, base, utilities and the core components (layout, buttons and links, cards, alerts and toasts, basic form controls, the small shared controls Slider, InputStepper, Meter, ProgressBar and Stat, Tabs, Pagination, Dialog, Popover, Tooltip) (115 kB raw, 17.0 kB gzip).
css/site.css: website components: SiteHeader, FooterBar, locale/theme/accent switchers, ConsentBanner, status and error pages, PWA notices, Hero, FeatureGrid, CallToAction, ProjectCard, content pages, Markdown, Lightbox, VideoEmbed (35 kB raw, 5.5 kB gzip).
css/docs.css (DocumentationLayout, CodeCard, InstallCommand, API docs: 2.9 kB gzip) and css/games.css (game UI: 0.6 kB gzip) for those routes.
css/forms.css (Combobox, date and time pickers, FileUpload, ColorPicker: 3.4 kB gzip) and css/data.css (DataTable, TreeView, Timeline, Carousel, media controls: 1.8 kB gzip) where those components render.
css/inspector.css: ResponsiveInspectorPanel, InspectorPanel, BottomSheet, Drawer (7 kB raw, 1.6 kB gzip); css/apps.css: app shells, tool UI, menus, app navigation, Keypad, VirtualList, SortableList, ToggleButton (41 kB raw, 6.0 kB gzip).
A content site loads core + site: 21.5 kB gzip together (a documentation site adds docs.css: 23.5 kB). Keep the order core, forms, data, site, docs, games, inspector, apps.
css/document.css (opt-in, not in styles.css): <html> gets the theme background and text colour, <body> the body font and no margin, for pages built with Pixxl from the top. Without it Pixxl styles only its own markup and data-theme scopes.
// app/layout.tsx of a website
import "@pixxl-tools/components/css/core.css";
import "@pixxl-tools/components/css/site.css";
import "@pixxl-tools/components/css/document.css"; // optional: html/body baseImport React primitives and browser hooks from @pixxl-tools/components, in Server Components too: the root entry is a flat list of named re-exports, so the bundler registers only the client components a server file imports.
Import from @pixxl-tools/components/server in server files that should stay server-only: it exports only server-safe components, i18n helpers, icons and tokens, so importing a client component from it fails the build.
Import SVG icon components from @pixxl-tools/components/icons.
Import server-safe component/recipe data from @pixxl-tools/components/metadata.
Import server-safe token objects from @pixxl-tools/components/tokens.
Use @pixxl-tools/canvas/react instead of generic components for canvas editor rails, pages, inspectors, and workspaces.
Next.js 16 with Turbopack (the default for next build) ships only the client components a page imports from the root entry. webpack’s Server Components plugin collects every client module a barrel re-exports, so with next build --webpack add the package to optimizePackageImports.
Measured with Next.js 16.4, one ThemeSwitcher in a server page: Turbopack 572 kB raw / 176 kB gzip first-load JS (1 client reference); webpack 879 kB / 272 kB (187 client modules) without optimizePackageImports, 570 kB / 177 kB (12) with it.
// next.config.ts (only needed with next build --webpack)
export default {
experimental: { optimizePackageImports: ["@pixxl-tools/components"] },
};Use semantic tone props and public --pixxl-* variables instead of copying internal CSS selectors.
Keep host routing, data loading, translations, analytics, and product state outside the component package.
import { PixxlProvider } from "@pixxl-tools/components";
export function Root() {
return (
<PixxlProvider direction="ltr" locale="en-US" theme="system">
<App />
</PixxlProvider>
);
}Server helpers from @pixxl-tools/components/security and /tokens that every site on a shared domain uses the same way, so each site keeps only its own sources. The /security entry loads nothing else, so next.config stays small (/server re-exports it too).
createSecurityHeaders({ csp, development, hsts }) returns { key, value }[] for Next headers() or vercel.json: one CSP (default-src 'self', no objects or framing, inline scripts allowed or pinned with inlineScripts hashes such as getThemeScriptHash()) plus HSTS, nosniff, DENY framing, referrer, permissions and opener policies. csp adds site sources per directive (connect, font, frame, img, media, script, style, worker); development relaxes only what the dev server needs (eval, inline styles, websockets, no HSTS). createContentSecurityPolicy and createSecurityHeaderRoutes build the parts. One recipe for Next sites: headers: async () => createSecurityHeaderRoutes({ csp, development: process.env.NODE_ENV === "development" }) (a rule for every path, /(.*)).
Static hosts (vercel.json): the headers array of vercel.json has the same shape as createSecurityHeaderRoutes(options), [{ source: "/(.*)", headers: [{ key, value }] }]. Write it with JSON.stringify({ headers: [...createSecurityHeaderRoutes(options), ...cacheRules] }, null, 2) from a build script, or keep the file by hand and guard it with a test: expect(vercel.headers[0]).toEqual(createSecurityHeaderRoutes(options)[0]).
A generated static page that inlines its CSS (an offline fallback built from the tokens) is blocked by style-src 'self': give that path its own header rule with createContentSecurityPolicy({ style: ["'unsafe-inline'"] }), or link the CSS as a file.
getThemeColorMeta(accent) returns the light and dark page backgrounds as theme-color entries; ThemeScript and SitePreferencesProvider switch the browser chrome to an explicit light or dark choice.
Share preferences across subdomains with cookieDomain={{ shared: ".example.com" }} on SitePreferencesProvider and ConsentBanner: the cookie gets Domain=.example.com there and stays host-only on localhost and preview hosts.
// next.config.ts
import { createSecurityHeaders } from "@pixxl-tools/components/security";
import { getThemeColorMeta } from "@pixxl-tools/components/tokens";
export default {
headers: async () => [
{
source: "/:path*",
headers: createSecurityHeaders({
csp: { connect: ["https://*.google-analytics.com"], script: ["https://www.googletagmanager.com"] },
development: process.env.NODE_ENV === "development",
}),
},
],
};
// app/layout.tsx
export const viewport = { themeColor: getThemeColorMeta("teal") };createSiteMetadata (server-safe, no runtime dependency on next) builds Next Metadata objects for one site in the house title format, and JsonLd renders schema.org data safely. Create the site once and use it in every layout, page and not-found file.
One title rule: site.page({ title }) returns an absolute "Title – Site" (the landing page, without title, "Site – tagline"), so the title is the same in any route segment, in Open Graph and in the Twitter card. site.root() also sets the title template for pages that set a plain title.
page() sets the canonical URL and language alternates as absolute URLs, Open Graph and Twitter cards with the site image (image: false leaves it out, and the card becomes summary), and robots (index: false for noindex; links are still followed).
notFound(title, locale) is for not-found.tsx and for unknown params in generateMetadata: a translated "Page not found – Site", noindex, nofollow, no canonical, and Open Graph and X cards with that title and no URL, so a 404 does not inherit the parent’s canonical, index or social card.
Icons get the sizes browsers expect: an .ico favicon comes first with free-form sizes ("16x16 32x32 48x48" for a multi-size file), svg is sizes="any", png entries their pixel size, the Apple touch icon 180×180 with an optional type.
JsonLd escapes <, > and & in the JSON, so a value holding </script> cannot end the element; pass one object or a list. serializeJsonLd returns the escaped string.
// lib/site.ts
import { createSiteMetadata } from "@pixxl-tools/components/server";
export const site = createSiteMetadata({
siteName: "Example",
siteUrl: "https://example.com",
tagline: "Tools for the browser",
description: "What the site is about.",
locale: "en-US",
image: { url: "/opengraph-image", width: 1200, height: 630, alt: "Example" },
icons: {
ico: { url: "/favicon.ico", sizes: "16x16 32x32 48x48" },
svg: "/icon.svg",
png: [{ size: 32, url: "/icon-32.png" }],
apple: { url: "/apple-touch-icon.png", type: "image/png" },
},
twitter: { site: "@example" },
});
// app/layout.tsx
export const metadata = site.root();
// app/[locale]/about/page.tsx
export const generateMetadata = async ({ params }: { params: Promise<{ locale: string }> }) => {
const { locale } = await params;
return site.page({
title: locale === "pl" ? "O nas" : "About",
path: `/${locale}/about`,
locale,
languages: { en: "/en/about", pl: "/pl/about", "x-default": "/en/about" },
});
};
// app/not-found.tsx
export const metadata = site.notFound();
// app/page.tsx
import { JsonLd } from "@pixxl-tools/components/server";
<JsonLd data={{ "@context": "https://schema.org", "@type": "WebSite", name: "Example", url: site.url("/") }} />;OgCard is a 1200×630 light share image built from flexbox and inline styles only, so next/og ImageResponse and satori render it. Import it from the Node-only @pixxl-tools/components/og entry with loadOgCardFonts, which returns the bundled Geist Regular the card is set in.
Props: siteName, eyebrow (a short label above the title, such as Project or Article), title, description, mark (an SVG string or element), accent, fontFamily, locale and dir. The title takes up to three lines and steps down in size as it grows; the description takes up to two lines, then ends with an ellipsis.
One weight of Geist (SIL Open Font License, bundled in the package): hierarchy comes from size, tracking and colour. Geist covers Latin and Cyrillic (not Greek); for other scripts pass their fonts, loadOgCardFonts([{ name: "Noto Sans JP", data, lang: "ja-JP" }]), and satori falls back to them per glyph.
Next.js replaces metadata.openGraph instead of merging it: a page or layout that sets its own openGraph (or twitter) object drops the root share image. Give that route its own opengraph-image.tsx, or leave openGraph out of its metadata so it inherits the root one.
/og reads the font file with fs, so use it from opengraph-image files, route handlers and build scripts (Node runtime), not from client components or the edge runtime. From a Node script (static PNGs), pass the same element and fonts to satori and convert the SVG to PNG.
// app/opengraph-image.tsx
import { ImageResponse } from "next/og";
import { OG_CARD_SIZE, OgCard, loadOgCardFonts } from "@pixxl-tools/components/og";
export const size = OG_CARD_SIZE;
export const contentType = "image/png";
export default async function Image() {
return new ImageResponse(
<OgCard accent="teal" description="What the page is about." mark={iconSvg} siteName="Site" title="Page title" />,
{ ...size, fonts: await loadOgCardFonts() },
);
}
// app/about/page.tsx: a route with its own openGraph replaces the root one,
// so it also needs app/about/opengraph-image.tsx (as above, with its title).
export const metadata = {
title: "About",
openGraph: { title: "About", description: "Who makes the site." },
};Use value plus onValueChange when the host owns current state.
Use defaultValue when the component owns initial and subsequent state.
Use open/defaultOpen/onOpenChange for disclosure and overlay state.
Do not switch a component between controlled and uncontrolled ownership after mount.
Tab through the result and verify accessible names, focus order, and focus return.
Check the narrowest supported layout and wide layouts with real labels and content.
Verify dark/light/system themes while server and first browser render match.
Run typecheck, component behavior tests, lint, and production build.