Skip to content

WorldStatus ​

WorldStatus is a header for the top of the view with the name of the world, the state of the connection and the text that describes the current step of the rules (now.text). It tells the person where they are and whether what they see is live.

jsx
import { useWorld } from './useWorld.js';

// The component: copy this part.

/** The words and the colour of the dot for each status of the world. */
const STATUS_LOOK = {
	connecting: { label: 'Connecting…', color: '#F0A54E' },
	online: { label: 'Online', color: '#399D57' },
	offline: { label: 'Offline', color: '#9D9C9A' },
	unreachable: { label: 'Cannot be reached', color: '#E5453A' },
	'not-found': { label: 'Not found', color: '#E5453A' },
	ended: { label: 'Ended', color: '#9D9C9A' },
};

/**
 * WorldStatus: the name of the world, whether it is online, and what is
 * happening now by its rules. Put it at the top of your view.
 * @param {object} props
 * @param {object} props.world - The world, from useWorld.
 * @param {boolean} [props.showNow=true] - Show what is happening now (`world.now.text`).
 */
export function WorldStatus({ world, showNow = true }) {
	const look = STATUS_LOOK[world.status] || { label: world.status, color: '#9D9C9A' };
	const nowText = world.status === 'ended' ? 'This world has ended.' : world.now?.text;

	return (
		<header style={{ display: 'grid', gap: 4, padding: '12px 16px', borderBottom: '1px solid rgba(127, 127, 127, 0.3)' }}>
			<div style={{ display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap' }}>
				<h1 style={{ margin: 0, fontSize: 20, lineHeight: 1.25 }}>{world.name}</h1>
				{world.profile?.isVerified && (
					<span title="Verified by UNaIVERSE" style={{ fontSize: 12, fontWeight: 600, padding: '1px 8px', borderRadius: 9, background: 'rgba(42, 84, 196, 0.15)' }}>
						Verified
					</span>
				)}
				{/* A status region: screen readers say it again when it changes. */}
				<span role="status" style={{ display: 'inline-flex', alignItems: 'center', gap: 6, marginLeft: 'auto', fontSize: 13 }}>
					<span aria-hidden="true" style={{ width: 8, height: 8, borderRadius: '50%', background: look.color }} />
					{look.label}
				</span>
			</div>
			{showNow && nowText && <p style={{ margin: 0, opacity: 0.75 }}>{nowText}</p>}
		</header>
	);
}

// Using it, on a fake world.

// A fake world to build with: no account, no network.
const arena = {
	name: 'Arena',
	profile: { description: 'A card game for four', isVerified: true },
	now: { text: 'Round 2: pick a card' },
};

export default function App() {
	// While building: the fake world. In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: arena } });

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;
	return <WorldStatus world={world} />;
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// The component: copy this part.

/** The words and the colour of the dot for each status of the world. */
const STATUS_LOOK = {
	connecting: { label: 'Connecting…', color: '#F0A54E' },
	online: { label: 'Online', color: '#399D57' },
	offline: { label: 'Offline', color: '#9D9C9A' },
	unreachable: { label: 'Cannot be reached', color: '#E5453A' },
	'not-found': { label: 'Not found', color: '#E5453A' },
	ended: { label: 'Ended', color: '#9D9C9A' },
};

/**
 * Draws into `container` the name of the world, whether it is online and what
 * is happening now by its rules, then again at every change of the world.
 * @param {HTMLElement} container - Where to draw it.
 * @param {object} world - The world, from connectToHost.
 * @param {{showNow?: boolean}} [options] - `showNow: false` hides what is happening now.
 * @returns {() => void} Stops following the world and removes what it drew.
 */
export function worldStatus(container, world, { showNow = true } = {}) {
	// The elements, made once.
	const header = document.createElement('header');
	Object.assign(header.style, { display: 'grid', gap: '4px', padding: '12px 16px', borderBottom: '1px solid rgba(127, 127, 127, 0.3)' });
	const top = document.createElement('div');
	Object.assign(top.style, { display: 'flex', alignItems: 'center', gap: '8px', flexWrap: 'wrap' });
	const title = document.createElement('h1');
	Object.assign(title.style, { margin: '0', fontSize: '20px', lineHeight: '1.25' });
	const verified = document.createElement('span');
	verified.textContent = 'Verified';
	verified.title = 'Verified by UNaIVERSE';
	Object.assign(verified.style, { fontSize: '12px', fontWeight: '600', padding: '1px 8px', borderRadius: '9px', background: 'rgba(42, 84, 196, 0.15)' });
	// A status region: screen readers say it again when it changes.
	const status = document.createElement('span');
	status.setAttribute('role', 'status');
	Object.assign(status.style, { display: 'inline-flex', alignItems: 'center', gap: '6px', marginLeft: 'auto', fontSize: '13px' });
	const dot = document.createElement('span');
	dot.setAttribute('aria-hidden', 'true');
	Object.assign(dot.style, { width: '8px', height: '8px', borderRadius: '50%' });
	const label = document.createElement('span');
	status.append(dot, label);
	const now = document.createElement('p');
	Object.assign(now.style, { margin: '0', opacity: '0.75' });
	top.append(title, verified, status);
	header.append(top, now);
	container.append(header);

	// Called at once, then after every change of the world.
	const stop = world.subscribe((node) => {
		const look = STATUS_LOOK[node.status] || { label: node.status, color: '#9D9C9A' };
		const nowText = node.status === 'ended' ? 'This world has ended.' : node.now?.text;
		title.textContent = node.name ?? '';
		verified.hidden = !node.profile?.isVerified;
		dot.style.background = look.color;
		label.textContent = look.label;
		now.textContent = nowText ?? '';
		now.hidden = !showNow || !nowText;
	});

	return () => {
		stop();
		header.remove();
	};
}

// Using it, on a fake world.

// A fake world to build with: no account, no network.
const arena = {
	name: 'Arena',
	profile: { description: 'A card game for four', isVerified: true },
	now: { text: 'Round 2: pick a card' },
};

// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: arena } });
const place = document.createElement('div');
document.body.append(place);
worldStatus(place, world);
Live, on a fake world
Open

The header reads three fields of the world: (1) name in an h1, (2) status as a coloured dot with a label and (3) now.text under the name. The label is Online while the person is connected to the world and Ended after the world has finished. Connecting…, Offline, Cannot be reached and Not found stand for the other values of status. Once the world has ended the line under the name reads "This world has ended." in place of now.text.

A world verified by UNaIVERSE (profile.isVerified) gets a Verified badge next to its name. Screen readers announce every change of the status because its label sits in a role="status" region.

Props ​

PropDefaultWhat it is
world(required)The world, from useWorld.
showNowtrueWhether to show the line with world.now.text. With false the header shows only the name and the status.

Parameters ​

worldStatus(container, world, options) draws the header into container and redraws it at every change of the world.

ParameterWhat it is
containerThe element to draw into (for example a div at the top of the page).
worldThe world, from connectToHost.
options.showNowLike the prop, false hides the line with world.now.text. Default true.

It returns a function that unsubscribes from the world and removes the header.

Change it ​

  • Your own labels for each status: edit STATUS_LOOK. To match the language of the person, pick the labels by world.webapp.language (Use the language of the webapp).
  • The description of the world: add world.profile.description under the name. profile.place and profile.owner are available too.
  • A smaller header: use an h2 instead of the h1 or pass showNow={false} when another part of your view already shows what is happening.

See also ​

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