Chiudi un pannello con il tasto indietro
Sul telefono si preme indietro per chiudere quello che si è aperto (un pannello, un menu, un'immagine). Se una vista non se ne occupa, il tasto indietro porta la persona fuori dalla vista perché la webapp torna alla sua pagina precedente. Con world.onBack(handler) la tua vista gestisce il tasto indietro prima della webapp. Registra l'handler quando il pannello si apre, chiudi il pannello quando la persona preme indietro e rimuovi l'handler quando il pannello è chiuso. Un mondo finto non ha un tasto indietro e nell'esempio Close fa quello che il tasto indietro farebbe nella webapp.
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>
)}
</>
);
}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;
});Come funziona
Quando l'handler chiude il pannello e restituisce true, la persona resta nella tua vista. Con qualsiasi altro valore restituito la webapp torna indietro come al solito (Il tasto indietro chiede prima alla tua vista descrive ogni caso).
onBackrestituisce una funzione che rimuove l'handler. Chiamala appena il pannello si chiude (con il tasto indietro o con Close) in modo che il tasto indietro successivo esca dalla vista come la persona si aspetta.- Gli handler vengono eseguiti dal più recente al più vecchio e un pannello aperto sopra un altro si chiude quindi per primo.
- Un mondo finto non chiama mai l'handler. Per provare davvero il tasto indietro apri la tua vista nella webapp dal tuo computer (Provarla dal tuo computer).
Vedi anche
onBackBackPanel: lo stesso comportamento in un componente.- I link e il tasto indietro