Skip to content

Close a panel with the back button ​

On a phone people press back to close what they opened (a panel, a menu, a picture). If your view ignores the back button, back takes the person out of your view because the webapp returns to its previous page. With world.onBack(handler) your view handles the back button before the webapp does. Register the handler when the panel opens, close the panel when the person presses back and remove the handler once the panel is closed. A fake world has no back button and Close in the example does what back would do in the webapp.

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

// The fake world is a shop that sells one lamp.
const shop = {
	name: 'Lamp shop',
	data: { lamp: 'Desk lamp, 25 €', details: 'Brass, 40 cm tall, with a warm light.' },
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: shop } });
	const [isOpen, setIsOpen] = useState(false);
	// A function that gives the back button back to the webapp, or null while the panel is closed.
	const giveBackButton = useRef(null);

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

	function openPanel() {
		if (isOpen) return;
		setIsOpen(true);
		// While the panel is open, the back button closes it and keeps the person in the view.
		giveBackButton.current = world.onBack(() => {
			closePanel();
			return true; // true tells the webapp not to go back
		});
	}

	function closePanel() {
		setIsOpen(false);
		if (giveBackButton.current) giveBackButton.current();
		giveBackButton.current = null;
	}

	return (
		<>
			<p>{world.data.lamp}</p>
			<button onClick={openPanel}>Show details</button>
			{isOpen && (
				// The panel covers the bottom of the view.
				<div style={{ position: 'fixed', left: 0, right: 0, bottom: 0, padding: 16, background: 'Canvas', borderTop: '1px solid GrayText' }}>
					<p>{world.data.details}</p>
					<button onClick={closePanel}>Close</button>
				</div>
			)}
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// The fake world is a shop that sells one lamp.
const shop = {
	name: 'Lamp shop',
	data: { lamp: 'Desk lamp, 25 €', details: 'Brass, 40 cm tall, with a warm light.' },
};

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: shop } });

const product = document.createElement('p');
const show = document.createElement('button');
show.textContent = 'Show details';
// The panel covers the bottom of the view.
const panel = document.createElement('div');
panel.hidden = true;
panel.style.cssText = 'position: fixed; left: 0; right: 0; bottom: 0; padding: 16px; background: Canvas; border-top: 1px solid GrayText';
const details = document.createElement('p');
const close = document.createElement('button');
close.textContent = 'Close';
panel.append(details, close);
document.body.append(product, show, panel);

// A function that gives the back button back to the webapp, or null while the panel is closed.
let giveBackButton = null;

function openPanel() {
	if (!panel.hidden) return;
	panel.hidden = false;
	// While the panel is open, the back button closes it and keeps the person in the view.
	giveBackButton = world.onBack(() => {
		closePanel();
		return true; // true tells the webapp not to go back
	});
}

function closePanel() {
	panel.hidden = true;
	if (giveBackButton) giveBackButton();
	giveBackButton = null;
}

show.onclick = openPanel;
close.onclick = closePanel;

// Called at once, then after every change of the world.
world.subscribe((node) => {
	product.textContent = node.data.lamp;
	details.textContent = node.data.details;
});
Live, on a fake world
Open

How it works ​

When the handler closes the panel and returns true, the person stays in your view. Any other return value lets the webapp go back as usual (The back button asks your view first covers every case).

  • onBack returns a function that removes the handler. Call it as soon as the panel closes (by back or by Close) so that the next back leaves the view as the person expects.
  • Handlers run from the newest to the oldest. A panel opened over another one therefore closes first.
  • A fake world never calls the handler. To try the back button for real, open your view in the webapp from your computer (Try it from your computer).

See also ​

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