Skip to content
// EFFECT DAYS 2026

VirtualKeyboard unstable

Tells where the software keyboard leaves the page visible, so content fixed to the bottom of the screen can sit above it.

A touch device's keyboard covers the bottom of the screen when a text field takes focus. Browsers report it differently: some shrink the visual viewport and leave the layout viewport, some shrink both, and some shrink the layout viewport only after the visual one. This module reads the viewport's sizes and works out the band of the screen the keyboard leaves visible.

It has two layers:

  • step is a pure function from a Sample of the viewport's sizes to the visible Band, or null while no keyboard is up. It keeps a little State between samples, which it takes and returns. It reads no DOM, so the detection can be tested without a device.
  • watch is a Stream of the band for a node's document, and TrackKeyboard is a stream mount that writes it on the node.

What the mount writes

On its node, as inline style, --keyboard-inset: the height the keyboard covers at the bottom of the layout viewport, in pixels. It is absent while no keyboard is up, and removed when the mount ends, so CSS reads it as var(--keyboard-inset, 0px), for example in a bottom or a padding-bottom.

How a keyboard is told apart

  • Only while a text field has focus: an <input> of a type that types text, a <textarea> or an editable element, and not a disabled one.
  • The visual viewport is more than 60 pixels shorter on screen than the part of the layout viewport it shows, which a browser's toolbar alone doesn't do. A pinch-zoom shrinks both alike, so it isn't a keyboard.
  • On Android, the layout viewport is more than 60 pixels shorter than the tallest it has been at the current width, for a keyboard that resizes the page. A change of width, such as a rotation, starts the count again.
  • Once seen, the keyboard counts as up until the visual viewport's height on screen changes by more than 60 pixels or focus leaves the text fields, so a layout viewport that shrinks to the visual one afterwards doesn't hide it.
  • While the layout viewport follows the keyboard, as 100svh does in some browsers, content fixed to the bottom already sits above it, and the inset is 0.

Gotchas

  • Under pinch-zoom, the band covers the visible strip above the keyboard, even when the layout viewport follows it.
  • Without visualViewport, the Stream reports null once and ends.
  • While a text field has focus, watch keeps a hidden element in <body> to measure 100svh.

Example A keyboard over the bottom of the screen

import { VirtualKeyboard } from "effect/ui/kit"
const closed: VirtualKeyboard.Sample = {
layoutWidth: 400,
layoutHeight: 800,
visualHeight: 800,
visualTop: 0,
scale: 1,
smallViewportHeight: 800,
android: false,
focused: true
}
const state = VirtualKeyboard.initialState(closed)
if (VirtualKeyboard.step(state, closed).band !== null) throw new Error("no keyboard is up yet")
const { band } = VirtualKeyboard.step(state, { ...closed, visualHeight: 500 })
if (band === null || band.top !== 0 || band.bottom !== 500 || band.inset !== 300) {
throw new Error("the keyboard should cover the bottom 300 pixels")
}
10 exports Since v4.2.0 Source

Constructors

Since v4.2.0

The State to start from, taken from the layout viewport's size before any keyboard is up.

Signature

declare function initialState(layout: {
readonly layoutHeight: number;
readonly layoutWidth: number;
}): State

Detecting

step

unstable
Since v4.2.0

Works out the band the keyboard leaves visible from one Sample.

Details

Pass the returned state to the next call. The module's JSDoc lists how a keyboard is told apart from a resize.

Signature

declare function step(state: State, sample: Sample): Step

textFieldOf

unstable
Since v4.2.0

The text field a target stands for: the target itself when it is a text field, the editable element it sits in, or the text field of the <label> it is in. null for anything else, a disabled field included.

Details

A text field is an <input> of type text, search, email, url, tel, password or number, a <textarea>, or an editable element. Focusing one opens the software keyboard.

Signature

declare function textFieldOf(target: EventTarget | null): HTMLElement | null

Models

Band

type unstable
Since v4.2.0

The band of the screen the keyboard leaves visible.

Signature

type Band = typeof Band.Type

Sample

interface unstable
Since v4.2.0

What step reads: the viewport's sizes at one moment, in CSS pixels.

Details

  • layoutWidth, layoutHeight: the layout viewport (clientWidth and innerHeight; watch scales WebKit's zoomed innerHeight back up).
  • visualWidth, visualHeight, visualTop, scale: visualViewport's width, height, offsetTop and scale. Omitting visualWidth assumes full layout width and returns no band when scale !== 1.
  • smallViewportHeight: the height of 100svh.
  • android: the browser runs on Android.
  • focused: a text field has focus.

Signature

interface Sample {
readonly android: boolean;
readonly focused: boolean;
readonly layoutHeight: number;
readonly layoutWidth: number;
readonly scale: number;
readonly smallViewportHeight: number;
readonly visualHeight: number;
readonly visualTop: number;
readonly visualWidth?: number;
}

State

interface unstable
Since v4.2.0

What step remembers between samples.

Details

  • layoutWidth: the layout viewport's width the tallest height belongs to.
  • tallestLayoutHeight: the tallest layout viewport seen at that width, kept on Android only.
  • keyboardHeight: visualHeight * scale when the keyboard was last seen, or null while none is.

Signature

interface State {
readonly keyboardHeight: number | null;
readonly layoutWidth: number;
readonly tallestLayoutHeight: number;
}

Step

interface unstable
Since v4.2.0

What step returns: the next State, and the band, or null while no keyboard is up.

Signature

interface Step {
readonly band: {
readonly bottom: number;
readonly inset: number;
readonly resizing: boolean;
readonly top: number;
} | null;
readonly state: State;
}

Mounts

Since v4.2.0

A stream mount that writes --keyboard-inset on its node while the software keyboard is up, and reports each band watch reports.

Details

The module's JSDoc says what it writes. Map the bands to a message with Mount.map, or to Component.noop when only the CSS variable is needed: h.div.onMount(VirtualKeyboard.TrackKeyboard().pipe(Mount.map(() => Component.noop))).

A Scene has no viewport, so send a band there with mounted(VirtualKeyboard.TrackKeyboard, band).

Signature

declare const TrackKeyboard: Factory<{}, {
readonly bottom: number;
readonly inset: number;
readonly resizing: boolean;
readonly top: number;
} | null, HTMLElement, never>

watch

unstable
Since v4.2.0

A Stream of the band the keyboard leaves visible in a node's document: the band when it starts, then each time it changes, and null while no keyboard is up.

Details

It samples the viewport when visualViewport resizes or scrolls, when the window resizes, and on the frame after focus moves. A text field anywhere in the document counts.

It writes nothing. Pass it to Mount.makeStream to build a mount that does more with the band than TrackKeyboard.

Signature

declare function watch(node: Node): Stream<{
readonly bottom: number;
readonly inset: number;
readonly resizing: boolean;
readonly top: number;
} | null>

Schemas

Band

unstable
Since v4.2.0

The band of the screen the keyboard leaves visible, in viewport coordinates.

Details

  • top, bottom: the band's edges, in pixels from the top of the layout viewport.
  • inset: the height below bottom that the keyboard covers, rounded up, which the mount writes as --keyboard-inset. 0 while the layout viewport follows the keyboard, unless the page is pinch-zoomed and the layout viewport's bottom is off screen.
  • resizing: the layout viewport follows the keyboard and hasn't reached the visual viewport's height yet, so the page is still moving. A scroll started now may restart when it settles.

Signature

declare const Band: Struct<{
readonly bottom: Number;
readonly inset: Number;
readonly resizing: Boolean;
readonly top: Number;
}>