Skip to Content
FrontendRecipesInstalled app (PWA) and mobile layout

Installed app (PWA) and mobile layout

Detect the display mode

import { useDisplayMode, useIsPwa } from '@djangocfg/ui-core/hooks'; const isPwa = useIsPwa(); // standalone, fullscreen, minimal-ui, window-controls-overlay const mode = useDisplayMode(); // 'browser' | 'standalone' | ...

Detection reads the display-mode media query, iOS navigator.standalone and the Android TWA referrer. The server and the hydration render report browser, then the real value arrives (same contract as useMediaQuery). All components share one set of listeners.

UiProviders (so BaseApp) also sets <html data-display-mode="...">, so CSS can react without JS: :root[data-display-mode='standalone'] .x { ... }. Use the attribute and tokens for layout, the hook for behaviour (hide an “install app” prompt, show a back button).

Tokens

--safe-top/right/bottom/left (equal to --app-safe-* when set, otherwise env(safe-area-inset-*)) and --app-height: 100dvh live in ui-core base.css. Safe-area insets are non-zero only when the host sets viewport-fit=cover.

Host requirements

// layout.tsx export const viewport = createViewport({ themeColor: '#000', viewportFit: 'cover' }); export const metadata = createManifestMetadata({ name, statusBarStyle: 'black-translucent' });

black-translucent draws the page under the status bar and relies on the insets; default keeps the page below it. The manifest needs display: 'standalone'.

Native-app touch behaviour (automatic)

ui-core base styles apply these with no per-project code:

  • touch-action: manipulation on html: no double-tap zoom and no 300ms tap delay, pinch-zoom still works (WCAG 1.4.4). text-size-adjust: 100% and no tap highlight flash on controls.
  • On touch devices (pointer: coarse) text fields are at least 16px, so iOS Safari does not zoom into a focused field. Desktop density is unchanged; add .input-compact to opt a field out. Input, Textarea and SelectTrigger also type at text-base md:text-sm, so a phone-width window without a coarse pointer (a narrow desktop window, an emulator) gets 16px too.
  • Touch targets: on a coarse pointer Button (every size), Input, SelectTrigger, the date fields and TabsTrigger are at least 44px tall, the icon Button 44px wide, and the @djangocfg/layouts nav links (brand, sidebar rows on a docked sidebar, navbar items, footer links, drawer rows) are 44px targets. A mouse keeps the compact heights. Inline links inside prose are exempt.
  • In an installed app (data-display-mode standalone, fullscreen, minimal-ui or window-controls-overlay): overscroll-behavior: none on html and body (no rubber band or pull-to-refresh at the shell; inner scrollers keep theirs), and buttons, nav, headers, [data-sidebar] and .app-chrome are not selectable and have no long-press callout. Inputs, contenteditable, pre, code, tables and .selectable stay selectable.
  • Viewport: createViewport() no longer sets maximumScale. createViewport({ appLike: true }) adds maximumScale: 1 and userScalable: false; this blocks pinch-zoom for people who need it and iOS ignores it anyway, so prefer the CSS above. An opt-in useBlockGestureZoom() (@djangocfg/ui-core/hooks) blocks iOS gesture zoom in an installed app only; off by default, same accessibility trade-off.

See PWA behaviour checklist for what to verify on a real iPhone.

The shell never scrolls the document

PrivateLayout is one dynamic viewport tall and clips: the top bar is shrink-0, the content below it is flex-1 min-h-0 and scrolls on its own. While it is mounted <html data-private-shell> is set, and in an installed app the document itself is locked (overflow: hidden, no overscroll), so dragging the top bar does not move the page. Public pages are unaffected. Do not wrap the layout in min-h-screen or h-screen; window.scrollTo and sticky headers inside pages now refer to the content region. On iOS before 16 overscroll-behavior is ignored; check the edge bounce on a real device.

Locked is not unscrollable: iOS scrolls the layout viewport (and any clipped ancestor) to lift a focused field above the keyboard, then leaves it there, and a page restored from the back-forward cache can come back scrolled. With the document locked the reader has nothing to drag back.

UiProviders mounts one hook, syncViewport() (@djangocfg/ui-core, hooks/media/viewportSync), once. It keeps --app-height right (below) and, while the shell is locked (data-private-shell in an installed app), the keyboard is down and the page is not pinch-zoomed, puts window, html, body and [data-app-shell] back at scroll 0. A capture-phase scroll listener does it on the next frame for any scroll, whatever caused it; the same pass runs on resize, visual viewport resize, orientationchange, pageshow, the tab becoming visible, focus and focus-out, and again 300 ms later because iOS reports the final size late. It does nothing in a browser tab, while the keyboard is up, or on a pinch-zoomed page. There is no separate stale body-lock cleanup: vaul writes its inline position: fixed only in a Safari tab that is not standalone.

Debug a displaced app: Settings > Debug

An installed app has no address bar, so the diagnostic lives in the Settings dialog, behind builtins.debug, which is off by default (in development too): the tab exists only when the host passes true.

<PrivateLayout settings={{ builtins: { debug: true } }} ...>

The dialog then has a Debug section with:

  • a Viewport overlay switch (kept in localStorage): a HUD drawn by UiProviders over the whole app, so it stays on screen after the dialog closes, where the bug happens;
  • a live readout of the same values, and a Re-measure now button (re-reads --app-height and zeroes every scroller at once).

The values: window.scrollY, the [data-app-shell] scrollTop, the <body> and <html> inline style, innerHeight, visualViewport height, offsetTop and scale, --app-height, the probed lvh, dvh and svh, screen.height, outerHeight, the safe-area insets, the top and height of the shell and of the top bar, the last five changes (a --app-height change or a scroll put back) with their trigger. Screenshot the HUD when the bug shows: a change line with a trigger means the hook fired and names what moved; no line while the shell is displaced means nothing moved window or [data-app-shell] (look at <body> and <html> instead). The switch is the only way on: there is no URL parameter and no build flag. The flag lives in localStorage, which is per origin: switching it on in my.reforms.ai does not show the HUD on meet.reforms.ai (or any other origin); switch it on there too, in an app that mounts the Debug section.

Try it without a phone: Storybook, Layouts/Shell displacement, with ?sb-device=pwa (its Settings dialog has the Debug section). The story displaces the shell and the window, and opens a drawer, so Playwright WebKit can drive it. WebKit is the closest desktop engine to iOS Safari, but it is not iOS standalone: no real keyboard, no keyboard inset, an emulated display mode. It proves the scroll-back logic, not the phone.

--app-height follows a real shrink

In an installed app --app-height starts as the largest of lvh, dvh and innerHeight (iOS can report the last two short by the top inset at launch). The viewport can also shrink for real with no keyboard: on iOS, after an in-app browser sheet (a link to another origin, a video call) is closed, innerHeight has been seen to drop from 812 to 771 while lvh stayed 812, which left the shell 41px taller than the screen. syncViewport therefore also re-measures on the triggers above. When a larger innerHeight was seen earlier in the session and it dropped by 8px or more, with the keyboard down and no pinch-zoom, and the drop is still there 300 ms later, --app-height follows max(innerHeight, dvh) instead of lvh; when innerHeight grows back the largest-of-three rule applies again. A keyboard-sized change (visual viewport only) and a pinch-zoom are never followed.

Build a PWA-safe overlay

Tokens are the source of truth; drawers, sheets, dialogs and toasts are consumers. In a new overlay:

  • Height: h-[var(--app-height,100dvh)] for a full-height surface, or pin a fixed panel with inset-y-0 h-auto. Never 100vh, and avoid h-full on fixed panels (it can disagree with the visible area in standalone iOS).
  • Insets: pt-[max(1rem,var(--safe-top,0px))], pb-[max(1rem,var(--safe-bottom,0px))] (same for left/right). Do not write bare env(safe-area-inset-*); the token reads --app-safe-* first, so Storybook can emulate it.
  • Behaviour that differs by mode: useIsPwa(), or CSS :root[data-display-mode='standalone'] ....
  • Shared primitives already do this: Sheet, Drawer (heights, bottom padding, bottom sheet cap), Dialog (card cap, fullscreen variant), Toaster (offsets), Boundary (min height). A test (overlay/pwa-safe-overlays.test.ts) fails on a raw 100vh or bare env() there.
  • An app-shell drawer that lays out its own header, scroller and footer passes bare to DrawerContent (no handle, no padded scroll wrapper) and makes the panel overflow-hidden; only the middle zone scrolls.

PrivateLayout on a phone

  • The drawer is 88vw wide (max 22rem); its height is pinned with inset-y-0, not 100dvh, so it matches the visible area in a browser tab and in a standalone PWA. Header and footer respect the safe areas; the list in between scrolls.
  • On a phone (below md) the layout draws a plain top bar: safe-top + 3rem tall (--layout-bar-height, 0px only when no bar is shown: desktop with the sidebar docked, or no sidebar), bg-background with a 1px bottom border, holding the sidebar toggle (the same ghost button as in the sidebar header) and header.title. It overlays the content column and slides away (transform, 200ms) on scroll-down and back on scroll-up or at the top, using the same useNavbarScroll policy as PublicLayout driven by the content scroller; it stays while the drawer is open, a field has focus, or the page is not scrollable. Content starts below the bar for every page, so pages do nothing; apps may use --layout-bar-height to offset their own sticky elements. PrivateLayout takes topBar={{ title, actions }} (types: PrivateTopBarConfig): title replaces header.title in the bar, actions is a ReactNode on the right (vertically centred, never shrinks the toggle; the title takes the rest and truncates). The same bar shows on desktop while the sidebar is collapsed (static, no auto-hide) and carries the account avatar for a signed-in user (topBar.account={false} to opt out); on a phone the drawer then has no footer account. Recommended actions are compact ghost icon Buttons (36px); several are fine, keep them few. The toggle stays left and is not configurable. Auto-hide is phone-only. The phone drawer header is the brand only (a link home, no toggle button); it closes by backdrop tap, swipe, Escape or navigating. The sidebar toggle has no tooltip and no hover behaviour; on desktop only the left screen edge peeks a collapsed sidebar open.
Last updated on