Skip to content

Tieni la casella dei messaggi sopra la tastiera ​

Sul telefono la tastiera sullo schermo copre la parte bassa della tua vista e nasconde una casella dei messaggi fissata lì. La webapp dà alla tua vista l'altezza della tastiera in world.webapp.keyboard e le aree sicure del telefono (la barra home, un notch) in world.webapp.insets. Alza la casella del maggiore fra l'altezza della tastiera e l'inset in basso. L'esempio finge di essere un telefono con una tastiera aperta alta 160 pixel e mette la casella subito sopra.

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

Come funziona ​

  • keyboard è l'altezza in pixel della tastiera sopra la tua vista. Vale 0 quando la tastiera è chiusa e sempre 0 su un computer.
  • insets contiene i pixel che il telefono riserva a sé su ogni lato della tua vista che tocca il bordo dello schermo (la barra home in basso, un notch su un lato di un telefono tenuto in orizzontale). Tieni i pulsanti fuori da queste aree.

La casella sale del maggiore dei due valori e non della loro somma perché una tastiera aperta copre anche la barra home. subscribe gira di nuovo quando la tastiera si apre o si chiude e la casella la segue. Sotto il suo ultimo messaggio la lista ha bisogno di spazio (paddingBottom nell'esempio) perché senza quello spazio la casella lo nasconde.

Vedi anche ​

Il GUI kit di UNaIVERSE, rilasciato con licenza Apache 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.