Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
docs(mobile): spec and plan for the viewport API (max-md: and useViewport)
aashu031366f4b011
diff --git a/.ai/plans/2026-10-07-viewport-api.md b/.ai/plans/2026-10-07-viewport-api.mdnew file mode 100644index 0000000000..da31016f7a--- /dev/null+++ b/.ai/plans/2026-10-07-viewport-api.md@@ -0,0 +1,184 @@+# Viewport API — implementation plan++**Spec / source:** `.ai/specs/2026-10-07-viewport-api.md`+**Branch:** `feat/mobile-redesign`++## Progress+- [x] Task 1: Viewport constants and hint cookie in `@carbon/utils`+- [x] Task 2: `Viewport.tsx` with `ViewportProvider` and `useViewport` in `@carbon/react`+- [x] Task 3: Migrate every `useCompact` / `useIsMobile` call site+- [x] Task 4: ERP root wiring+- [x] Task 5: CSS — delete the gate, split the global phone block+- [x] Task 6: Rewrite `compact:` classes to `max-md:`+- [x] Task 7: Static verification+- [ ] Task 8: Browser verification (only with the owner's go-ahead)++## Dependencies+Task 2 needs Task 1. Task 3 and Task 4 need Task 2. Task 5 and Task 6 are independent of Tasks 1–4.+Task 7 needs Tasks 1–6. Task 8 needs Task 7.++🛑 Never commit. The owner commits.++---++## Task 1: Viewport constants and hint cookie in `@carbon/utils`++**Depends on:** none+**Files:**+- Modify: `packages/utils/src/viewport.ts` — replace the compact API with the viewport API+- Modify: `packages/utils/src/viewport.test.ts` — test the new API++**Steps:**+1. Replace the exports of `viewport.ts` with:+ - `export type Viewport = "phone" | "tablet" | "desktop";`+ - `export const VIEWPORT_HINT_COOKIE = "viewport";`+ - `export const PHONE_QUERY = "(width < 48rem)";`+ - `export const TABLET_QUERY = "(48rem <= width < 64rem)";`+ - `export function getViewportHint(request: Request): Viewport` — the cookie value if it is one of the three names, else `"phone"` when the user agent matches `/Mobi/i`, else `"desktop"`.+ - `export function viewportHintCookie(viewport: Viewport): string` — same `cookie.serialize` options as today.+2. Delete `COMPACT_HINT_COOKIE`, `COMPACT_QUERY`, `getCompactHint`, `compactHintCookie`.+3. Rewrite `viewport.test.ts` against the new API: the three cookie values round-trip, an unknown value falls back to the UA, the iPhone UA gives `"phone"`, the Mac UA gives `"desktop"`, and the cookie has `Max-Age=31536000` and `Path=/`.++**Verify:**+```bash+pnpm --filter @carbon/utils test -- viewport+# Expected: all viewport tests pass+```++**Out of scope:** any other file in `@carbon/utils`.++## Task 2: `Viewport.tsx` in `@carbon/react`++**Depends on:** Task 1+**Files:**+- Create: `packages/react/src/Viewport.tsx` (via `git mv packages/react/src/Compact.tsx packages/react/src/Viewport.tsx`)+- Delete: `packages/react/src/hooks/useIsMobile.ts`+- Modify: `packages/react/src/hooks/index.ts` — remove `useIsMobile`+- Modify: `packages/react/src/index.tsx` — export `ViewportProvider`, `useViewport` instead of `CompactProvider`, `useCompact`+- Modify: every `packages/react/src/*.tsx` that imports from `./Compact` — import from `./Viewport`++**Steps:**+1. In `Viewport.tsx`, keep the SPDX header and `HitArea`. Change the `HitArea` classes from `compact:` to `max-md:`.+2. Replace the context, provider and hooks with:+ - Context value `{ initialViewport: Viewport } | null`.+ - `ViewportProvider({ initialViewport, children })`. On mount, compute the size from `PHONE_QUERY` and `TABLET_QUERY`, write `viewportHintCookie(size)`, and rewrite it on each `change` event of both queries.+ - `readViewport(): Viewport`: `"phone"` if `PHONE_QUERY` matches, `"tablet"` if `TABLET_QUERY` matches, else `"desktop"`.+ - `subscribe` adds the `change` listener to both media query lists.+ - `useViewport()`: `const size = useSyncExternalStore(subscribe, readViewport, () => context?.initialViewport ?? "desktop")`; return `useMemo(() => ({ isPhone: size === "phone", isTablet: size === "tablet", isDesktop: size === "desktop" }), [size])`.+3. Delete `useCompact`, `useIsMobile`, `CompactProvider`. Update the doc comments: no opt-in remains.+4. Delete `hooks/useIsMobile.ts` and its import and export in `hooks/index.ts`.+5. In `index.tsx`, import and export `ViewportProvider` and `useViewport` from `./Viewport`.+6. In each `packages/react/src` file that imports `useCompact` from `./Compact`, import `useViewport` from `./Viewport`. Task 3 rewrites the calls.++**Verify:** Task 3 verifies (the package does not typecheck until call sites move).++**Out of scope:** `CompactSheet.tsx` and every other `Compact*` component name.++## Task 3: Migrate every `useCompact` / `useIsMobile` call site++**Depends on:** Task 2+**Files:** about 71 files that call `useCompact`, plus `packages/react/src/Sidebar.tsx`, `packages/react/src/FunnelChart.tsx`, `apps/mes/app/routes/x+/active.tsx`, `apps/mes/app/routes/x+/recent.tsx`, `apps/starter/app/components/Breadcrumbs.tsx`.++**Steps:**+1. Replace `const isCompact = useCompact();` with `const { isPhone } = useViewport();` (82 sites). In the same file, rename every `isCompact` identifier to `isPhone`. `CollapsibleSidebar.tsx`'s `wasCompact` ref stays.+2. Replace an inline `useCompact()` (8 sites: `useCompact() ? (` and `useCompact() ? Phone :`) with `useViewport().isPhone`.+3. Replace `const isMobile = useIsMobile();` with `const { isPhone: isMobile } = useViewport();`. Keep the local name so the rest of each file stays as it is.+4. In each file's import from `@carbon/react` (or `./hooks` / `./Compact` inside the package), swap `useCompact` / `useIsMobile` for `useViewport`. Remove duplicates.+5. If a file shadows `isPhone` already, STOP and report — do not improvise.++**Verify:**+```bash+rg -n "useCompact\b|useIsMobile|CompactProvider|initialCompact" apps packages -g '*.{ts,tsx}'+# Expected: only apps/erp/app/root.tsx (Task 4)+pnpm exec turbo run typecheck --filter=@carbon/utils --filter=@carbon/react --filter=mes --filter=starter --filter=academy+# Expected: all tasks succeed+```++**Out of scope:** props or components named `Compact*`; the `compact` prop of `Table`.++## Task 4: ERP root wiring++**Depends on:** Task 2+**Files:**+- Modify: `apps/erp/app/root.tsx`++**Steps:**+1. Import `getViewportHint` instead of `getCompactHint`, and `ViewportProvider` instead of `CompactProvider`.+2. In the loader, replace `compact: getCompactHint(request)` with `viewport: getViewportHint(request)`.+3. Delete `data-compact-ui=""` and its two comment lines from `<html>`.+4. Replace `<CompactProvider initialCompact={loaderData?.compact ?? false}>` with `<ViewportProvider initialViewport={loaderData?.viewport ?? "desktop"}>`, and the closing tag.++**Verify:**+```bash+pnpm exec turbo run typecheck --filter=erp+# Expected: succeeds (after Task 3)+```++**Out of scope:** the rest of `root.tsx`.++## Task 5: CSS — delete the gate, split the global phone block++**Depends on:** none+**Files:**+- Modify: `packages/config/tailwind/theme.css`+- Modify: `apps/erp/app/styles/tailwind.css`++**Steps:**+1. Delete `@custom-variant compact { … }` and its comment.+2. In `@custom-variant hover`, delete the `:where(html:not([data-compact-ui])) &:hover { @slot; }` block. Update the comment above it.+3. Update the `@utility hit-area` comment: `compact:hit-area` → `max-md:hit-area`.+4. In the `@media (width < 48rem)` phone block, keep only the two shared rules, with new selectors: `html:not(.dark) { --muted-foreground: … !important; }` and `.truncate { white-space: nowrap; }`. Update the block comment.+5. Append to `apps/erp/app/styles/tailwind.css` a `@media (width < 48rem)` block with `html { --topbar-height: calc(52px + env(safe-area-inset-top)); }` and `[data-sonner-toaster][data-y-position="bottom"] { bottom: calc(var(--content-inset, 0px) + 16px); }`, with their comments.++**Verify:**+```bash+rg -n "data-compact-ui|custom-variant compact" packages apps -g '*.{css,ts,tsx}'+# Expected: no output+```++**Out of scope:** every other rule in both files.++## Task 6: Rewrite `compact:` classes to `max-md:`++**Depends on:** none+**Files:** about 270 `.ts` / `.tsx` files under `apps/` and `packages/`.++**Steps:**+1. Run a Perl rewrite over tracked `.ts`/`.tsx` files under `apps` and `packages` that replaces `compact:` with `max-md:` only when the char before is a space, `"`, `'`, `` ` `` or `(`, and the char after is `[a-z[!-]`. This leaves `compact: true` object keys alone.+2. Check `packages/react/src/Card.tsx`: the selector `[class~='compact:hidden']` must now read `[class~='max-md:hidden']`.++**Verify:**+```bash+rg -n -e "[\"' \x60(]compact:[a-z\[!-]" apps packages -g '*.{ts,tsx,css}'+# Expected: no output+git diff --stat | tail -1+# Expected: about 270 files changed+```++**Out of scope:** `compact` props, `compactForLog`, `compactedAt`, the currency compact formatter.++## Task 7: Static verification++**Depends on:** Tasks 1–6+**Steps:**+1. Run the spec's two `rg` acceptance checks.+2. Run the typecheck for all six packages.+3. Run Biome on the changed files.+4. Run the `@carbon/utils` tests.++**Verify:**+```bash+pnpm exec turbo run typecheck --filter=@carbon/utils --filter=@carbon/react --filter=erp --filter=mes --filter=starter --filter=academy+# Expected: all succeed+pnpm exec biome check $(git diff --name-only | grep -E '\.(ts|tsx|css)$')+# Expected: no errors+```++## Task 8: Browser verification++**Depends on:** Task 7+🛑 Ask the owner first.+**Steps:**+1. Open the ERP at 375px: a list page and a record page. Compare with the pre-change build.+2. Open the same pages at 900px: the desktop layout shows.+3. Reload at 375px with the `viewport=phone` cookie: no flash of the desktop layout.diff --git a/.ai/specs/2026-10-07-viewport-api.md b/.ai/specs/2026-10-07-viewport-api.mdnew file mode 100644index 0000000000..963c506790--- /dev/null+++ b/.ai/specs/2026-10-07-viewport-api.md@@ -0,0 +1,178 @@+# Viewport API: `max-md:` and `useViewport()`++> Status: draft+> Date: 2026-10-07+> Extends: `.ai/specs/2026-10-07-mobile-chrome-structure.md` (B3, one compact signal)++## TL;DR++- `compact:` classes become Tailwind's own `max-md:`. The query stays the same (`width < 48rem`).+- `useCompact()` and `useIsMobile()` become one `useViewport()` → `{ isPhone, isTablet, isDesktop }`.+- The ERP-only `data-compact-ui` gate goes away, because MES goes responsive on this branch.+- `Compact*` components keep their names. "Compact" is a shape that phone and tablet can share.+- Tablet is 768–1023px. It keeps the desktop layout for now.+- Phone rendering in the ERP does not change.++## Problem++The phone layout is switched by a custom Tailwind variant (`compact:`) and a hook+(`useCompact()`). Both only work in apps that set `data-compact-ui` on `<html>`.+Today that is only the ERP. The variant is a yes/no switch, so it has no room for+a tablet size.++MES will get its responsive pass on this branch before the PR. After that, both+main apps use the phone layout and the per-app gate has no job left. This change:++- replaces `compact:` with Tailwind's own `max-md:` (the same `width < 48rem` query);+- replaces `useCompact()` and `useIsMobile()` with one `useViewport()` that returns+ `{ isPhone, isTablet, isDesktop }`;+- removes the `data-compact-ui` gate.++"Compact" stays as the name of the **component shape** (`CompactToolbar`,+`CompactSheet`, `CompactList` …). A compact component can later serve phone and+tablet both; the device choice is made at the call site with `useViewport()`.++Research: N/A. This is an internal rename. The only external fact it depends on is+Tailwind's own `max-md:` output. Tailwind 4.3.0 compiles `max-*` to+`@media (width < var(--breakpoint-*))` (`tailwindcss/dist`), and its default+`--breakpoint-md` is `48rem` and `--breakpoint-lg` is `64rem`+(`tailwindcss/theme.css:328-329`). The repo does not override them.++## Goals++- One device API in JS: `useViewport()` with three booleans.+- Standard Tailwind prefixes in CSS, so nobody has to learn a custom variant.+- Phone rendering in the ERP stays the same, pixel for pixel.+- A tablet size exists in the hook, ready for a later tablet pass.++## Non-goals++- No tablet layout. At 768–1023px the ERP keeps the desktop layout, as today.+- No `tablet:` custom variant. Tablet styles use `md:max-lg:` when they are needed.+- No rename of compact components or their shape props (`CompactToolbar`,+ `CompactSheet`, `CompactTabRow`, `stackOnCompact`, `compactFooterClassName`,+ `useCompactCssVar`, `useCompactModuleSidebar` …).+- No `ViewportProvider` in MES, academy or starter. The MES responsive pass adds+ it to MES.+- Unrelated "compact" names stay: `compactForLog`, `compactedAt`, the compact+ currency formatter, and so on.++## Design++### CSS (`packages/config/tailwind/theme.css`)++- Delete `@custom-variant compact`.+- Every `compact:` class becomes `max-md:` (about 1,400 classes in about 270 files). This includes+ the class-name selector in `Card.tsx` (`[class~='compact:hidden']` →+ `[class~='max-md:hidden']`). TS object keys such as `compact: true` stay as they are.+- `@utility hit-area` stays; its uses become `max-md:hit-area`. Update its comment.+- `@custom-variant hover`: remove the `:where(html:not([data-compact-ui]))` branch. Hover+ then applies where the pointer can hover, or at 48rem and wider, in every app.+- The global phone block (`@media (width < 48rem)`, `theme.css:445-463`) splits in two:++ | Rule | Goes to | Why |+ |---|---|---|+ | Darker `--muted-foreground` in light mode | `theme.css`, selector `html:not(.dark)` | Contrast fix, good for every app |+ | `.truncate` keeps `white-space: nowrap` | `theme.css`, selector `.truncate` | Ellipsis fix, good for every app |+ | `--topbar-height: 52px + safe area` | `apps/erp/app/styles/tailwind.css`, selector `html` | Only the ERP reads `--topbar-height` |+ | Sonner toast offset from `--content-inset` | `apps/erp/app/styles/tailwind.css` | Only the ERP sets `--content-inset` |++ Both new blocks keep the `@media (width < 48rem)` wrapper.++### Viewport constants (`packages/utils/src/viewport.ts`)++```ts+export type Viewport = "phone" | "tablet" | "desktop";+export const PHONE_QUERY = "(width < 48rem)";+export const TABLET_QUERY = "(48rem <= width < 64rem)";+export const VIEWPORT_HINT_COOKIE = "viewport";+export function getViewportHint(request: Request): Viewport; // cookie, else UA "Mobi" → phone, else desktop+export function viewportHintCookie(viewport: Viewport): string;+```++These replace `COMPACT_QUERY`, `COMPACT_HINT_COOKIE`, `getCompactHint` and+`compactHintCookie`. The cookie value is the size name. The old `compact` cookie+is never read: this branch has not shipped, so no browser has it.++### Hook and provider (`packages/react/src/Viewport.tsx`, was `Compact.tsx`)++```ts+export const ViewportProvider: (p: { initialViewport: Viewport; children }) => JSX.Element;+export function useViewport(): { isPhone: boolean; isTablet: boolean; isDesktop: boolean };+export const HitArea: () => JSX.Element; // unchanged, its classes move to max-md:+```++- One `useSyncExternalStore`. It subscribes to both media queries, and its snapshot is the+ `Viewport` string, a stable primitive. The hook derives the three booleans from that one+ value, so they can never disagree.+- Server snapshot: the provider's `initialViewport`, else `"desktop"`. That is the old+ `false` for apps with no provider.+- `ViewportProvider` writes the hint cookie on mount and on every size change, as+ `CompactProvider` does now.+- Deleted: `useCompact`, `useIsMobile`, `CompactProvider`, `hooks/useIsMobile.ts` and+ its export from `hooks/index.ts`.++### Call sites++- `const isCompact = useCompact()` becomes `const { isPhone } = useViewport()`, and the+ local `isCompact` references become `isPhone`. Props named `isCompact` that describe a+ component's shape keep their name.+- `useIsMobile()` callers (`Sidebar`, `FunnelChart`, MES `active.tsx`/`recent.tsx`,+ starter `Breadcrumbs.tsx`) become `useViewport().isPhone`.+- ERP `root.tsx`: remove `data-compact-ui`; `getCompactHint` → `getViewportHint`;+ `CompactProvider initialCompact` → `ViewportProvider initialViewport`.++### Behaviour changes (accepted)++- In MES, academy and starter, shared components now take their phone form below 48rem.+ `Popover` becomes a bottom sheet, and `Tabs`, `Card`, `Input` and others restyle.+ Academy uses `Input`/`Popover`/`Tooltip`; starter uses `Input`.+- In those apps, sticky hover highlights no longer show after a tap on phones.+- In those apps, phones also get the darker muted text and the `.truncate` ellipsis fix.+- MES phones show the ERP phone styling until the MES responsive pass.++## Design Decisions++| Decision | Choice | Why |+|---|---|---|+| CSS prefix | Tailwind `max-md:` | Same query as `compact:`. With no gate there is nothing custom left |+| Tablet in CSS | `md:max-lg:`, no custom variant | Standard Tailwind. Add a variant only if it is used a lot |+| Hook shape | `{ isPhone, isTablet, isDesktop }`, from one size value | Owner's call: booleans read best in JSX, and one source means no conflicts |+| Tablet range | 48rem ≤ width < 64rem | Tailwind md→lg; iPad portrait is tablet, landscape is desktop |+| Per-app gate | Removed | MES goes responsive on this branch before the PR |+| Component names | Keep `Compact*` | "Compact" is a shape that phone and tablet can share; the device choice stays at the call site |+| Cookie value | Size name (`phone`/`tablet`/`desktop`) | The hook has three sizes; a 1/0 value cannot carry them |+| UA fallback | `Mobi` → phone, else desktop | Same as today; tablet uses the desktop layout anyway |+| Legacy `compact` cookie | Not read | The branch has not shipped |+| File name | `Compact.tsx` → `Viewport.tsx` | The file now holds the device API, not a component shape |++## Acceptance criteria++- [ ] `rg -e "[\"' \x60]compact:[a-z\[!-]" apps packages -g '*.{ts,tsx,css}'` returns nothing.+- [ ] `rg "useCompact\b|useIsMobile|CompactProvider|data-compact-ui|COMPACT_QUERY|COMPACT_HINT|getCompactHint|compactHintCookie|initialCompact" apps packages` returns nothing.+- [ ] Typecheck passes for `@carbon/utils`, `@carbon/react`, `erp`, `mes`, `starter` and `academy`; Biome passes.+- [ ] The ERP at 375px: a list page (for example Sales Orders) and a record page match the+ pre-change screenshots. At 1280px they also match. (Browser check, only with the owner's go-ahead.)+- [ ] The ERP at 900px renders the desktop layout, and `useViewport()` reports `isTablet: true`.+- [ ] A first load with the cookie `viewport=phone` server-renders the phone layout, with no+ flash of the desktop layout.++## Open questions (resolved)++- [x] Rename `compact:` to `phone:`, or use Tailwind's `max-md:`? — **Answer:** `max-md:`. The+ gate goes away once MES is responsive on this branch, and then `compact:` is just `max-md:`.+- [x] What happens to the `data-compact-ui` gate? — **Answer:** Remove it. Academy/starter+ side effects and the interim MES styling are accepted.+- [x] Hook return shape? — **Answer:** Booleans `{ isPhone, isTablet, isDesktop }`, derived from+ one size value inside the hook.+- [x] Tablet width range? — **Answer:** 48rem–64rem.+- [x] Rename `Compact*` components to Phone/Mobile? — **Answer:** No. "Compact" names the+ shape and can cover phone and tablet; only the device API is renamed.+- [x] The gate also wraps a global phone CSS block. Where does each rule go? — **Answer:** The+ contrast and truncate fixes apply in every app. The top bar height and the toast offset+ move to ERP's `tailwind.css`, because only the ERP uses their variables.+- [x] Sequencing with the chrome-structure work? — **Answer:** The owner committed it first; the tree is clean.++## Changelog++- 2026-10-07: Draft written after the design discussion.diff --git a/packages/react/src/Compact.tsx b/packages/react/src/Viewport.tsxsimilarity index 100%rename from packages/react/src/Compact.tsxrename to packages/react/src/Viewport.tsxdiff --git a/packages/react/src/hooks/useIsMobile.ts b/packages/react/src/hooks/useIsMobile.tsdeleted file mode 100644index a5c0099685..0000000000--- a/packages/react/src/hooks/useIsMobile.ts+++ /dev/null@@ -1,5 +0,0 @@-// SPDX-License-Identifier: AGPL-3.0-only-// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,-// including ports, remain AGPLv3; serving them over a network requires releasing their source.--export { useIsMobile as default } from "../Compact";