Skip to content

Keep the message box above the keyboard ​

On a phone the on-screen keyboard covers the bottom of your view and hides a message box fixed there. The webapp gives your view the height of the keyboard in world.webapp.keyboard and the safe areas of the phone (the home bar, a notch) in world.webapp.insets. Raise the box by the larger of the keyboard height and the bottom inset. The example pretends to be a phone with an open keyboard 160 pixels tall and places the box just above it.

jsx
import { useState } from 'react';
import { useWorld } from './useWorld.js';

// The fake help desk answers every message.
const desk = { name: 'Help desk', onMessage: (message) => `You said: ${message.text}` };

export default function App() {
	// While building: the fake world, on a pretend phone with its keyboard open (160 px)
	// and a home bar (34 px). In the webapp: useWorld()
	const { world, error } = useWorld({
		fake: { world: desk },
		webapp: { keyboard: 160, insets: { top: 0, right: 0, bottom: 34, left: 0 } },
	});
	const [text, setText] = useState('');

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;

	function onSubmit(event) {
		event.preventDefault();
		world.send(text);
		setText('');
	}

	// Drawn again at every change, also when the keyboard opens or closes.
	const { keyboard, insets } = world.webapp;
	// The box stays above the keyboard while it is open, and above the home bar of the phone while it is closed.
	const bottom = Math.max(keyboard, insets.bottom);
	return (
		<>
			{/* Leave room under the last message, to keep the box from hiding it. */}
			<div style={{ paddingBottom: bottom + 60 }}>
				{world.messages.map((message) => (
					<p key={message.id}>{message.fromMe ? 'You' : message.from}: {message.text}</p>
				))}
			</div>
			{/* The message box, fixed to the bottom of the view, away from a notch on the sides. */}
			<form
				onSubmit={onSubmit}
				style={{
					position: 'fixed', left: 0, right: 0, bottom, display: 'flex', gap: 8, padding: 8,
					paddingLeft: 8 + insets.left, paddingRight: 8 + insets.right,
					background: 'Canvas', borderTop: '1px solid GrayText',
				}}
			>
				<input placeholder="Message" value={text} onChange={(event) => setText(event.target.value)} style={{ flex: 1 }} />
				<button>Send</button>
			</form>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// The fake help desk answers every message.
const desk = { name: 'Help desk', onMessage: (message) => `You said: ${message.text}` };

// While building: the fake world, on a pretend phone with its keyboard open (160 px)
// and a home bar (34 px). In the webapp: const world = await connectToHost();
const world = await connectToHost({
	fake: { world: desk },
	webapp: { keyboard: 160, insets: { top: 0, right: 0, bottom: 34, left: 0 } },
});

const list = document.createElement('div');
// The message box, fixed to the bottom of the view.
const box = document.createElement('form');
box.style.cssText = 'position: fixed; left: 0; right: 0; display: flex; gap: 8px; padding: 8px; background: Canvas; border-top: 1px solid GrayText';
const input = document.createElement('input');
input.placeholder = 'Message';
input.style.flex = '1';
const send = document.createElement('button');
send.textContent = 'Send';
box.append(input, send);
document.body.append(list, box);

box.onsubmit = (event) => {
	event.preventDefault();
	world.send(input.value);
	input.value = '';
};

// Called at once, then after every change, also when the keyboard opens or closes.
world.subscribe((node) => {
	const { keyboard, insets } = node.webapp;
	// The box stays above the keyboard while it is open, and above the home bar of the phone while it is closed.
	const bottom = Math.max(keyboard, insets.bottom);
	box.style.bottom = `${bottom}px`;
	// Keep away from a notch on the sides (a phone held sideways).
	box.style.paddingLeft = `${8 + insets.left}px`;
	box.style.paddingRight = `${8 + insets.right}px`;
	// Leave room under the last message, to keep the box from hiding it.
	list.style.paddingBottom = `${bottom + 60}px`;
	list.replaceChildren(...node.messages.map((message) => {
		const line = document.createElement('p');
		line.textContent = `${message.fromMe ? 'You' : message.from}: ${message.text ?? ''}`;
		return line;
	}));
});
Live, on a fake world
Open

How it works ​

  • keyboard is the height in pixels of the keyboard over your view. It is 0 when the keyboard is closed and always 0 on a desktop.
  • insets holds the pixels that the phone reserves for itself on each side of your view that touches the edge of the screen (the home bar at the bottom, a notch on a side of a phone held sideways). Keep buttons out of these areas.

The box goes up by the larger of the two values and not by their sum because an open keyboard also covers the home bar. subscribe runs again when the keyboard opens or closes and the box follows it. The list needs room under its last message as well (paddingBottom in the example). Without it the box hides that message.

See also ​

The UNaIVERSE GUI kit, released under the Apache License 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.