Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rules/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ The full contract is in [DESIGN.md](../docs/DESIGN.md). In short:
6. **Libraries that need real colors** (ReactFlow, Recharts) read `currentTheme.colors` via `useTheme()`. Inline styles needing alpha use `tint()` from `packages/web-ui/src/utils/tones.ts` or `var(--token)`.
7. **Contrast is part of the contract.** Built-in themes must meet WCAG AA on the pairs in `CONTRAST_PAIRS` (`packages/web-ui/src/utils/themeContrast.ts`): fix a failing pair by moving lightness, keep the hue. A new text/background combination goes into `CONTRAST_PAIRS` first. See "Contrast" in DESIGN.md.
8. **Verify** with `pnpm lint:theme` (runs inside `pnpm lint`, checks Core and `plugins/*/ui/src`), `pnpm test:contrast` and by switching themes in Settings, Appearance. Brand and identity colors go in the `ALLOWLIST` of `scripts/check-theme-tokens.mjs` with a reason; UI files never do.
9. **Responsive layout uses the shared conventions.** Compact below `md` (768 px), intermediate below `lg` (1024 px), wide above; migrate existing screens with `max-md:`/`max-lg:` so desktop stays unchanged. Touch sizing goes behind `pointer-coarse:` (`min-h-touch`, 44 px), never a width breakpoint. Full-height roots use `h-dvh`, never `h-screen`/`100vh`. Behavior that changes with the viewport uses `useViewportLayout()`/`useMediaQuery()`, and must not write saved preferences. See "Responsive layout" in DESIGN.md.
79 changes: 79 additions & 0 deletions docs/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,85 @@ prints the nearest color with the same hue that passes; prefer adjusting lightne
changing hue so ported palettes stay recognizable. Painting a new combination (say
`text-muted` on `bg-surface-tertiary`) means adding it to `CONTRAST_PAIRS` first.

## Responsive layout

One frontend serves desktop windows, tablets and phones (tracking issue #1580). These
conventions keep every screen on the same breakpoints instead of each inventing its own.

### Breakpoints

| Layout | Width (CSS px) | Tailwind | Typical use |
| ------------ | -------------- | ---------------------------- | -------------------------------------------------- |
| Compact | below 768 | `max-md:` | Phones, narrow windows: one work surface at a time |
| Intermediate | 768 to 1023 | `md:max-lg:` | Tablets, half-screen windows |
| Wide | 1024 and up | unprefixed (today's layout) | Desktop |

The thresholds come from the shell itself: the icon rail (64 px) and the explorer at its
default width (256 px) take 320 px, so below 768 px the main area would be narrower than
448 px next to them. With the row editor open (384 px by default) the main area keeps
320 px at 1024 px and gets squeezed below that.

`md` and `lg` are pinned in the `@theme` block of `packages/web-ui/src/index.css`;
`BREAKPOINTS` in `packages/web-ui/src/utils/responsive.ts` holds the same values (in
rem, so media queries scale with the browser's default font size) for code. Change them together.

Existing screens are written for wide windows with unprefixed classes. When migrating
one, add the compact and intermediate rules with `max-md:` and `max-lg:` so the desktop
layout stays exactly as it is.

### Container queries

When a component's room depends on the panel it sits in (split panes, sidebars, plugin
hosts, dialogs) rather than on the window, mark the parent `@container` and use `@sm:`,
`@md:` ... on the children. Unlike media queries, container query `rem` follows the root
font size (14 px, scaled by the interface scale setting), so use an explicit size such as
`@min-[480px]:` when an exact width matters.

### Input capability

Width and input are independent: a narrow desktop window gets the compact layout with a
mouse, and a touch laptop keeps the wide layout with a finger.

- Use `pointer-coarse:` / `pointer-fine:` for touch-specific sizing, never a width
breakpoint.
- Primary touch controls aim for 44×44 CSS px: `pointer-coarse:min-h-touch
pointer-coarse:min-w-touch` (the `touch` spacing token is 44 px), or extend the hit area
with padding.
- Anything reachable only on hover or through a context menu needs a visible entry point
on touch; long press must not be the only route.

### Viewport and safe areas

- The viewport meta in `packages/web-ui/index.html` sets `viewport-fit=cover` and
`interactive-widget=resizes-content`. Never add `maximum-scale` or `user-scalable=no`:
browser zoom must keep working.
- The root shell (`MainLayout`) uses `h-dvh`, which follows mobile browser chrome as it
expands and collapses, and `safe-area-padding` (`env(safe-area-inset-*)`, 0 on desktop).
Do not use `h-screen` or `100vh` for full-height surfaces: `100vh` is the height with the
toolbar collapsed, so the bottom of the page is clipped while it shows. Use `h-full`
inside the shell and `h-dvh` for a standalone page root. Overlays cap their height with `100dvh`, as
`PluginModalProvider` does with `max-h-[calc(100dvh-2rem)]`.
- The software keyboard is not handled by `dvh` alone. Chromium on Android resizes the
layout (thanks to `interactive-widget`), iOS Safari does not; forms must keep their
fields and actions inside a scroll region so a focused field can be scrolled into view,
and use `window.visualViewport` only where that is not enough. Check on real devices.

### Scrolling ownership

`html`, `body` and `#root` never scroll. Each surface owns exactly one scroll region
(`flex-1 min-h-0 overflow-auto`, with `min-w-0` on flex children so they can shrink).
Tables, code editors and canvases keep their own deliberate horizontal scroll; nothing
else may widen the document, from 320 CSS px up.

### Behavior

Presentation belongs in CSS. Use `useViewportLayout()` or `useMediaQuery()` from
`packages/web-ui/src/hooks/useMediaQuery.ts` (with `MEDIA_QUERIES`) only when behavior
changes, for example rendering a drawer instead of a resizable panel. Responsive changes
must never write preferences: do not save panel widths or collapsed state in reaction to
a resize; derive the rendered value instead, so returning to a wide window restores the
saved desktop layout.

## Checking your work

```sh
Expand Down
8 changes: 7 additions & 1 deletion packages/web-ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/logo.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- viewport-fit=cover hands the notch/home-indicator areas to the safe-area insets the
root shell pads with; resizes-content lets Chromium shrink the layout (and dvh)
when the software keyboard opens. Zoom stays enabled. -->
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, viewport-fit=cover, interactive-widget=resizes-content"
/>
<title>tabularis</title>
<style>
html,
Expand Down
2 changes: 1 addition & 1 deletion packages/web-ui/src/components/layout/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const MainLayoutContent = () => {
});

return (
<div className="flex h-screen bg-base text-primary overflow-hidden">
<div className="flex h-dvh safe-area-padding bg-base text-primary overflow-hidden">
{location.pathname !== '/editor' && (
<CommandPaletteScopeBridge scopeId={ROOT_COMMAND_SCOPE_ID} />
)}
Expand Down
43 changes: 43 additions & 0 deletions packages/web-ui/src/hooks/useMediaQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {useCallback, useSyncExternalStore} from 'react';

import {MEDIA_QUERIES, type ViewportLayout} from '@/utils/responsive';

function getMediaQuery(query: string): MediaQueryList | null {
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') {
return null;
}
return window.matchMedia(query);
}

/**
* True while `query` matches; follows changes live and unsubscribes on unmount.
* Use it for behavior only: presentation belongs in CSS (see docs/DESIGN.md).
*/
export function useMediaQuery(query: string): boolean {
const subscribe = useCallback(
(onChange: () => void) => {
const mediaQuery = getMediaQuery(query);
if (!mediaQuery) return () => {};
mediaQuery.addEventListener('change', onChange);
return () => mediaQuery.removeEventListener('change', onChange);
},
[query],
);
return useSyncExternalStore(
subscribe,
() => getMediaQuery(query)?.matches ?? false,
() => false,
);
}

/**
* The shared compact / intermediate / wide layout for the current viewport width.
* Falls back to `wide` without matchMedia so desktop behavior is the default.
*/
export function useViewportLayout(): ViewportLayout {
const hasMatchMedia = typeof window !== 'undefined' && typeof window.matchMedia === 'function';
const intermediate = useMediaQuery(MEDIA_QUERIES.intermediate);
const wide = useMediaQuery(MEDIA_QUERIES.wide);
if (!hasMatchMedia || wide) return 'wide';
return intermediate ? 'intermediate' : 'compact';
}
20 changes: 19 additions & 1 deletion packages/web-ui/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@
--radius-2xl: var(--radius-xl);
--radius-3xl: var(--radius-xl);
--radius-4xl: var(--radius-xl);

/* Responsive layout (docs/DESIGN.md, "Responsive layout"): compact below `md`,
intermediate from `md`, wide from `lg`. Pinned here so a Tailwind default cannot
move them; keep in sync with BREAKPOINTS in src/utils/responsive.ts. */
--breakpoint-md: 48rem;
--breakpoint-lg: 64rem;

/* Minimum size of a primary touch control: `min-h-touch`, `min-w-touch`, `size-touch`. */
--spacing-touch: 44px;
}

:root {
Expand Down Expand Up @@ -217,7 +226,9 @@ body,
body {
margin: 0;
min-width: 320px;
min-height: 100vh;
/* dvh follows mobile browser chrome as it expands and collapses; 100vh would size
the page for the collapsed toolbar and clip its bottom edge. */
min-height: 100dvh;
background-color: var(--bg-base);
color: var(--text-primary);
/* Prevent text selection globally */
Expand All @@ -242,6 +253,13 @@ code {
-ms-user-select: text !important;
}

/* Keeps content out of notches, rounded corners and the home indicator. The insets are
0 on desktop and in the Tauri window, so this only pads where the device needs it. */
@utility safe-area-padding {
padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
}

@layer utilities {
.overflow-auto {
overscroll-behavior: none;
Expand Down
30 changes: 30 additions & 0 deletions packages/web-ui/src/utils/responsive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Shared responsive conventions; the full guide is "Responsive layout" in docs/DESIGN.md.
*
* Widths are rem and match the `md` and `lg` breakpoints pinned in src/index.css (48rem,
* 64rem), so a `md:` class and `useViewportLayout()` always agree, even when the browser's
* default font size is not 16px. Prefer CSS (media or container
* queries) for presentation; reach for these only when behavior changes.
*/
export const BREAKPOINTS = {
/** Intermediate layout from this width; compact below it. */
intermediate: 48,
/** Wide (desktop) layout from this width. */
wide: 64,
} as const;

export type ViewportLayout = 'compact' | 'intermediate' | 'wide';

/**
* Media queries for behavior that depends on the viewport or the input device. Width and
* input capability are independent: a narrow desktop window is compact with a fine
* pointer, a touch laptop is wide with a coarse one.
*/
export const MEDIA_QUERIES = {
intermediate: `(min-width: ${BREAKPOINTS.intermediate}rem)`,
wide: `(min-width: ${BREAKPOINTS.wide}rem)`,
/** Primary pointer is a finger: size primary controls with `min-h-touch`. */
coarsePointer: '(pointer: coarse)',
/** Primary pointer can hover: hover-only affordances are acceptable as extras. */
hover: '(hover: hover)',
} as const;
16 changes: 16 additions & 0 deletions packages/web-ui/tests/components/layout/MainLayout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ describe('MainLayout', () => {
expect(screen.getAllByTestId('data-file-gate')).toHaveLength(1);
});

it('should size the shell to the visible viewport and pad it for safe areas', () => {
render(
<MemoryRouter>
<Routes>
<Route element={<MainLayout />}>
<Route index element={<div>Content</div>} />
</Route>
</Routes>
</MemoryRouter>,
);

const shell = screen.getByRole('main').parentElement;
expect(shell).toHaveClass('h-dvh', 'safe-area-padding');
expect(shell).not.toHaveClass('h-screen');
});

it('should show the production banner only on the connection editor route', () => {
const {unmount} = render(
<MemoryRouter initialEntries={['/connections/prod-id/editor']}>
Expand Down
127 changes: 127 additions & 0 deletions packages/web-ui/tests/hooks/useMediaQuery.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import {describe, it, expect, vi, afterEach} from 'vitest';
import {act, renderHook} from '@testing-library/react';
import {useMediaQuery, useViewportLayout} from '@/hooks/useMediaQuery';
import {BREAKPOINTS, MEDIA_QUERIES} from '@/utils/responsive';

const original = window.matchMedia;

/** Browser default font size; `rem` in a media query always resolves against it. */
const DEFAULT_FONT_PX = 16;

/** Fakes matchMedia for `min-width` queries against a mutable viewport width (CSS px). */
function stubViewport(initialWidth: number, fontPx = DEFAULT_FONT_PX) {
let width = initialWidth;
const listeners = new Map<string, Set<() => void>>();
const matches = (query: string) => {
const min = /min-width:\s*(\d+(?:\.\d+)?)rem/.exec(query);
return min ? width >= Number(min[1]) * fontPx : false;
};
window.matchMedia = vi.fn().mockImplementation((query: string) => ({
get matches() {
return matches(query);
},
addEventListener: (_: string, l: () => void) => {
if (!listeners.has(query)) listeners.set(query, new Set());
listeners.get(query)!.add(l);
},
removeEventListener: (_: string, l: () => void) => listeners.get(query)?.delete(l),
})) as unknown as typeof window.matchMedia;
return {
resize(next: number) {
width = next;
listeners.forEach((set) => set.forEach((l) => l()));
},
listenerCount: () => [...listeners.values()].reduce((n, set) => n + set.size, 0),
};
}

describe('useMediaQuery', () => {
afterEach(() => {
window.matchMedia = original;
});

it('is false when matchMedia is unavailable', () => {
window.matchMedia = undefined as unknown as typeof window.matchMedia;
const {result} = renderHook(() => useMediaQuery(MEDIA_QUERIES.coarsePointer));
expect(result.current).toBe(false);
});

it('follows the media query live', () => {
const viewport = stubViewport(390);
const {result} = renderHook(() => useMediaQuery(MEDIA_QUERIES.wide));
expect(result.current).toBe(false);
act(() => viewport.resize(1280));
expect(result.current).toBe(true);
});

it('removes its listener on unmount', () => {
const viewport = stubViewport(390);
const {unmount} = renderHook(() => useMediaQuery(MEDIA_QUERIES.wide));
expect(viewport.listenerCount()).toBe(1);
unmount();
expect(viewport.listenerCount()).toBe(0);
});

it('moves its subscription when the query changes', () => {
const viewport = stubViewport(900);
const {result, rerender} = renderHook(({query}) => useMediaQuery(query), {
initialProps: {query: MEDIA_QUERIES.intermediate},
});
expect(result.current).toBe(true);
rerender({query: MEDIA_QUERIES.wide});
expect(result.current).toBe(false);
expect(viewport.listenerCount()).toBe(1);
});
});

describe('useViewportLayout', () => {
afterEach(() => {
window.matchMedia = original;
});

it('defaults to the wide layout without matchMedia', () => {
window.matchMedia = undefined as unknown as typeof window.matchMedia;
const {result} = renderHook(() => useViewportLayout());
expect(result.current).toBe('wide');
});

it.each([
[320, 'compact'],
[BREAKPOINTS.intermediate * DEFAULT_FONT_PX - 1, 'compact'],
[BREAKPOINTS.intermediate * DEFAULT_FONT_PX, 'intermediate'],
[BREAKPOINTS.wide * DEFAULT_FONT_PX - 1, 'intermediate'],
[BREAKPOINTS.wide * DEFAULT_FONT_PX, 'wide'],
])('maps %ipx to %s', (width, layout) => {
stubViewport(width);
const {result} = renderHook(() => useViewportLayout());
expect(result.current).toBe(layout);
});

it.each([
[BREAKPOINTS.intermediate * 20 - 1, 'compact'],
[BREAKPOINTS.intermediate * 20, 'intermediate'],
[BREAKPOINTS.wide * 20 - 1, 'intermediate'],
[BREAKPOINTS.wide * 20, 'wide'],
])('scales with a 20px default font size: %ipx is %s', (width, layout) => {
stubViewport(width, 20);
const {result} = renderHook(() => useViewportLayout());
expect(result.current).toBe(layout);
});

it('uses rem units so the queries match the Tailwind tokens', () => {
expect(MEDIA_QUERIES.intermediate).toBe('(min-width: 48rem)');
expect(MEDIA_QUERIES.wide).toBe('(min-width: 64rem)');
});

it('updates across breakpoints and cleans up both subscriptions', () => {
const viewport = stubViewport(390);
const {result, unmount} = renderHook(() => useViewportLayout());
expect(result.current).toBe('compact');
act(() => viewport.resize(800));
expect(result.current).toBe('intermediate');
act(() => viewport.resize(1440));
expect(result.current).toBe('wide');
unmount();
expect(viewport.listenerCount()).toBe(0);
});
});
Loading