A layout for phone and desktop
The webapp shows your view in an iframe that is wide on a computer, narrow on a phone and different again when the phone turns. Give your view two layouts (two columns when there is room and one column above the other when there is not) and choose between them by width. To see both, press Open above the example and make the window narrower than 640 pixels.
jsx
import { useEffect, useState } from 'react';
import { useWorld } from './useWorld.js';
// While you build, the world is a fake game with two players.
const arena = {
name: 'Arena',
now: { text: 'Round 2: your move' },
members: [{ name: 'Ada', role: 'player' }, { name: 'Bob', role: 'player' }],
};
// Matches when the view is wide enough for two columns. In the webapp, the width of the view is the width of its frame.
const wide = window.matchMedia('(min-width: 640px)');
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: arena } });
const [isWide, setIsWide] = useState(wide.matches);
// Draw again when the view gets wider or narrower (a phone turned, a window resized).
useEffect(() => {
const onChange = () => setIsWide(wide.matches);
wide.addEventListener('change', onChange);
return () => wide.removeEventListener('change', onChange);
}, []);
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
// The game, with the players beside it (desktop) or under it (phone).
return (
<div style={{ display: 'flex', gap: 16, flexDirection: isWide ? 'row' : 'column' }}>
<main style={{ flex: 1 }}>
<h2>{world.name}</h2>
<p>{world.now?.text}</p>
<p>{isWide ? 'Desktop layout' : 'Phone layout'}</p>
</main>
<aside style={{ width: isWide ? 200 : 'auto', padding: '0 12px', background: 'rgba(127, 127, 127, 0.15)' }}>
<h3>Players</h3>
<ul>
{(world.members ?? []).map((member) => <li key={member.id}>{member.name}</li>)}
</ul>
</aside>
</div>
);
}js
import { connectToHost } from '@unaiverse/gui-kit';
// While you build, the world is a fake game with two players.
const arena = {
name: 'Arena',
now: { text: 'Round 2: your move' },
members: [{ name: 'Ada', role: 'player' }, { name: 'Bob', role: 'player' }],
};
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: arena } });
// The game, with the players beside it (desktop) or under it (phone).
const page = document.createElement('div');
page.style.display = 'flex';
page.style.gap = '16px';
const game = document.createElement('main');
game.style.flex = '1';
const title = document.createElement('h2');
const now = document.createElement('p');
const layoutName = document.createElement('p');
game.append(title, now, layoutName);
const players = document.createElement('aside');
players.style.padding = '0 12px';
players.style.background = 'rgba(127, 127, 127, 0.15)';
const heading = document.createElement('h3');
heading.textContent = 'Players';
const list = document.createElement('ul');
players.append(heading, list);
page.append(game, players);
document.body.append(page);
// Matches when the view is wide enough for two columns. In the webapp, the width of the view is the width of its frame.
const wide = window.matchMedia('(min-width: 640px)');
function layout() {
page.style.flexDirection = wide.matches ? 'row' : 'column';
players.style.width = wide.matches ? '200px' : 'auto';
layoutName.textContent = wide.matches ? 'Desktop layout' : 'Phone layout';
}
wide.addEventListener('change', layout);
layout();
// Called at once, then after every change of the world.
world.subscribe((node) => {
title.textContent = node.name;
now.textContent = node.now?.text ?? '';
list.replaceChildren(...(node.members ?? []).map((member) => {
const item = document.createElement('li');
item.textContent = member.name;
return item;
}));
});How it works
window.matchMedia('(min-width: 640px)')tests the width of your own document. Inside the webapp the width it tests is the room your view actually has in the iframe and not the width of the screen.- Inline styles cannot hold a media query as a stylesheet can. The code therefore reads
wide.matches, writes the styles and writes them again on everychangeevent of the query (a resized window or a turned phone). - With a stylesheet a CSS rule
@media (min-width: 640px) { ... }switches the layout without JavaScript. - An iframe can have any width. Put the breakpoint where your content stops fitting rather than at the size of a known phone.