Skip to content

world.onBack(handler) ​

Lets your view handle the back button before the webapp does. When the person presses back, your handler runs and returns whether it handled the press.

onBack(handler: () => boolean | Promise<boolean>): () => void
js
const giveBack = world.onBack(() => closePanel());

Parameters ​

ParameterWhat it is
handlerThe function that runs when the person presses back. After handling the press (for example by closing a panel) it returns true or a Promise that resolves to true. When it returns false the webapp goes back as usual.

Returns ​

A function that removes your handler. When no handler is left the webapp handles the back button again.

Usage ​

Close a panel with the back button ​

The view takes the button while the panel is open and gives it back when the panel closes:

js
let giveBack = null;

function openPanel() {
  panel.hidden = false;
  giveBack = world.onBack(() => {
    closePanel();
    return true; // handled: the person stays in the world
  });
}

function closePanel() {
  panel.hidden = true;
  giveBack?.();
  giveBack = null;
}

In React ​

An effect takes the button while the panel is open. Its cleanup gives the button back:

jsx
const isConnected = world !== null;

useEffect(() => {
  if (!isOpen || !isConnected) return undefined;
  // onBack returns the function that gives the button back: React calls it when the panel closes.
  return world.onBack(() => {
    setIsOpen(false);
    return true;
  });
}, [isOpen, isConnected]);

Caveats ​

  • Every back action counts. The back button of the browser, the one on the phone and the back arrows of the webapp all run your handler.
  • The newest handler first. With several handlers the newest runs first. Each of the others runs only when the one before it returned false.
  • Only true counts. Any other return value counts as "not handled" and so does a handler that throws.
  • Half a second. If your handlers have not answered within 500 milliseconds the webapp goes back anyway. Close your panel first and do slow work afterwards.
  • Take it only when you need it. While your view holds the button, the webapp keeps an extra entry in its history. Give the button back when there is nothing left to close.
  • Only while your view is shown. When the person switches to the UNaIVERSE view of the world, the back button belongs to the webapp.
  • A fake world has no back button of the webapp. onBack is accepted but never runs your handler. Test the handler by calling it yourself.

Troubleshooting ​

onBack never fails and has no error code. In a webapp older than your kit the back button stays with the webapp.

ProblemWhyWhat to do
Back leaves the world although the panel was open.The handler returned something other than true, threw or took more than half a second.Return true right after closing the panel.
Back does nothing once and then works.A handler still registered returns true when there is nothing to close.Give the button back when the panel closes or return false.

See also ​

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