Web View

Helpers for apps shown inside a native web view, an in-app browser like Instagram or TikTok, or an installed PWA: safe areas for the notch and status bar, a screen height that follows the keyboard, web view detection, theme color and a scroll lock that holds on iOS.

On this device

Open this page in an app's web view or a social app's browser to see what it reports. In Safari or Chrome the top inset is 0, because the browser bar covers that area.

This device…

Tap the field to open the keyboard and watch the numbers.

Platform…
Appnone
Safe area (t r b l)0 0 0 0
Viewport0 × 0
Keyboard inset0px
Keyboard openno

Installation

npx glass-orbit add webview

Setup

Two things. First, add viewport-fit=cover to the viewport meta tag, so the page can draw under the notch and the safe area reads as more than 0.

<meta
  name="viewport"
  content="width=device-width, initial-scale=1, viewport-fit=cover"
/>

Then mount WebViewSetup once near the root. It renders nothing. It keeps --orbit-viewport-height and --orbit-keyboard-inset up to date on <html>, marks it with data-orbit-webview, data-orbit-standalone and data-orbit-keyboard, and keeps the focused field in view when the keyboard opens.

// main.tsx or app/layout.tsx
import { WebViewSetup } from "@/components/glass-orbit/webview";

export default function App() {
  return (
    <>
      <WebViewSetup />
      <Routes />
    </>
  );
}

Safe area

The notch, status bar and home indicator. Four variables, with matching classes, hold the insets. Dialog and Dropdown Menu already use them.

/* Variables, for your own layout */
.app-header {
  padding-top: calc(var(--orbit-safe-top) + 0.75rem);
}

/* Or the ready-made classes */
<header className="orbit-safe-top">…</header>
<nav className="fixed inset-x-0 bottom-0 orbit-safe-bottom">…</nav>
<main className="orbit-safe-x">…</main>

/* Tailwind works too */
<footer className="pb-[calc(var(--orbit-safe-bottom)+1rem)]">…</footer>

SafeArea is a block as tall as the top or bottom inset, 0px where there is none. StatusBarSpacer is the top one, glass and sticky, so content does not scroll under the clock.

import { SafeArea, StatusBarSpacer } from "@/components/glass-orbit/webview";

<StatusBarSpacer />                        {/* top, glass, sticky */}
<StatusBarSpacer variant="solid" />

<nav className="fixed inset-x-0 bottom-0">
  <TabBarItems />
  <SafeArea position="bottom" />           {/* space for the home indicator */}
</nav>

If the native app already keeps its web view clear of the status bar and home indicator, set --orbit-safe-area-multiple to 0 and every inset becomes 0.

/* The app already pads the web view: don't add the space twice */
:root { --orbit-safe-area-multiple: 0; }

Need the numbers in JavaScript? useSafeArea() returns them in pixels.

const { top, bottom } = useSafeArea(); // pixels

Some Android web views report the safe area as 0 even when the app draws edge to edge. Let the native side pass the real insets in with setSafeAreaInsets; everything that reads the variables follows.

// In your app root, once
import { setSafeAreaInsets } from "@/components/glass-orbit/webview";
window.orbitSetSafeArea = setSafeAreaInsets;

// Android (Kotlin), in CSS pixels (dp), after reading WindowInsets
webView.evaluateJavascript("orbitSetSafeArea({ top: 32, bottom: 24 })", null)

Viewport height and keyboard

100vh is taller than the screen in most mobile browsers, and on iOS the keyboard covers fixed elements instead of pushing them up. useViewport() reads the visible area and how much of it the keyboard takes.

const { height, keyboardOpen, keyboardInset } = useViewport();

// Hide the tab bar while typing
{!keyboardOpen && <TabBar />}
/* A screen that fits exactly, also when the keyboard is up */
<div className="orbit-h-screen flex flex-col">…</div>

/* A chat composer that sits on top of the keyboard */
<form className="fixed inset-x-0 bottom-0 orbit-keyboard-bottom">…</form>

/* Or style by state */
html[data-orbit-keyboard="open"] .tab-bar { display: none; }

With WebViewSetup mounted, the Dialog sheet moves up above the keyboard and shrinks to fit, and the field you tapped scrolls into view. Try it on a phone:

Preview

Detecting a web view

Tells a normal browser from a native app's web view (iOS WKWebView, Android WebView, Capacitor, React Native, Flutter, Cordova), a social app's in-app browser and an installed PWA. It reads the user agent, so treat it as a strong hint, not a guarantee.

import { detectWebView, useWebView } from "@/components/glass-orbit/webview";

const info = useWebView(); // null until mounted
if (info?.app === "instagram") {
  // In-app browsers block some features: offer "Open in browser"
}

detectWebView(userAgent) // test any user agent string

kind: browser, standalone (installed PWA), webview (a native app) or in-app-browser.

app: instagram, facebook, tiktok, line, wechat, linkedin, snapchat, pinterest, twitter, telegram or google, when the user agent names it.

bridge: capacitor, react-native, flutter, cordova or telegram, when found on window.

Status bar color

useThemeColor sets the theme-color meta tag: the status bar color in Android Chrome and installed PWAs, and Safari's bar color. It puts the old color back when the component unmounts.

import { useThemeColor, setThemeColor } from "@/components/glass-orbit/webview";

useThemeColor(open ? "#0a0a0f" : undefined); // while a sheet is open
const restore = setThemeColor("#e11d48");     // later: restore()

Inside a native app, the status bar belongs to the app: set its color and icon style on the native side (for example with your framework's status bar plugin), make it transparent, and use StatusBarSpacer or --orbit-safe-top for the space under it.

Scroll lock

Stops the page behind your own overlay from scrolling. overflow: hidden alone is ignored on iOS, so this pins the body, blocks touches that would drag or bounce the page, and puts the scroll position back on unlock. Pass the overlay so scrolling inside it still works. Nested locks are counted. Dialog and Dropdown Menu already lock scrolling.

import { useScrollLock } from "@/components/glass-orbit/webview";

const panelRef = useRef<HTMLDivElement>(null);
useScrollLock(menuOpen, panelRef); // the panel itself can still scroll

Touch resets

Added with the styles. Everywhere: no grey flash on tap and no text resizing on rotation. Inside a web view or installed app only: no bounce or pull to refresh on the page, and no text selection or long-press menu on buttons, links, tabs and menu items. Use .orbit-no-select for anything else.

<div className="orbit-no-select">…</div>

API Reference

<WebViewSetup />

{ keepFocusedInView?: boolean; markDocument?: boolean } — Mount once. Both default to true.

useViewport()

{ width, height, keyboardInset, keyboardOpen } — Visible size in pixels and keyboard state.

useKeyboardOpen()

boolean — True while the on-screen keyboard is up.

useSafeArea()

{ top, right, bottom, left } — Safe area insets in pixels.

setSafeAreaInsets(insets)

Partial<Insets> | null — Insets from native code, in CSS pixels. null goes back to the browser's values.

useWebView() / detectWebView(ua?)

WebViewInfo | null / WebViewInfo — platform, kind, isWebView, isStandalone, app, bridge.

<SafeArea />

{ position: "top" | "bottom"; variant?: "glass" | "solid" | "transparent"; sticky?: boolean } — A block as tall as the inset. Defaults: transparent, not sticky.

<StatusBarSpacer />

{ variant?; sticky? } — A top SafeArea, glass and sticky by default.

useThemeColor(color) / setThemeColor(color)

string | undefined / () => void — Sets the theme-color meta tag; setThemeColor returns restore.

useScrollLock(locked, ref?) / lockScroll(element?)

boolean, RefObject / () => void — Scroll lock that holds on iOS; the element you pass can still scroll; lockScroll returns unlock.

CSS variables

--orbit-safe-top | -right | -bottom | -left, --orbit-safe-area-multiple, --orbit-viewport-height, --orbit-keyboard-inset

CSS classes

.orbit-safe(-top|-bottom|-x), .orbit-h-screen, .orbit-min-h-screen, .orbit-keyboard-bottom, .orbit-no-select