Skip to content

TurnIndicator ​

TurnIndicator shows whose turn it is next to the board of a turn-based game or in any world where people take turns (an auction, a debate, a round of questions). During the turn of the person the badge reads Your turn on the blue background used by the webapp. When someone else plays, the badge reads "Waiting for" followed by the role named in the data ("Waiting for black") or by "the others" when the data name none. Screen readers announce Your turn once at the start of the turn of the person because the badge is a role="status" region whose text changes only with the turn.

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

// The component: copy this part.

/**
 * Tells whose turn it is. When the world says it in its data (a role, like
 * 'white'), it is the person's turn when that is their role. Otherwise it is
 * their turn while the rules of the world let them send.
 * @param {object} world - The world.
 * @param {string} name - The name in `world.data` that says whose turn it is.
 * @returns {{isMine: boolean, whose: string|null}} Whether it is the person's turn, and whose it is.
 */
function turnOf(world, name) {
	const whose = world.data[name];
	if (typeof whose === 'string') return { isMine: whose === world.myRole, whose };
	return { isMine: world.canSend, whose: null };
}

/**
 * TurnIndicator: whose turn it is, in a game or any world where people take
 * turns. When the turn comes, "Your turn" stands out and screen readers say it.
 * @param {object} props
 * @param {object} props.world - The world, from useWorld.
 * @param {string} [props.name='turn'] - The name in `world.data` that says whose turn it is (a role).
 */
export function TurnIndicator({ world, name = 'turn' }) {
	const { isMine, whose } = turnOf(world, name);
	let text = isMine ? 'Your turn' : `Waiting for ${whose ?? 'the others'}`;
	if (world.status === 'ended') text = 'The game is over';
	const isHighlighted = isMine && world.status !== 'ended';

	return (
		<p role="status" style={{
			display: 'inline-flex', alignItems: 'center', gap: 8, margin: 0, padding: '6px 14px', borderRadius: 30, fontWeight: 600,
			background: isHighlighted ? '#2A54C4' : 'rgba(127, 127, 127, 0.15)', color: isHighlighted ? '#fff' : 'inherit',
		}}>
			<span aria-hidden="true" style={{ width: 8, height: 8, borderRadius: '50%', background: isHighlighted ? '#fff' : '#9D9C9A' }} />
			{text}
		</p>
	);
}

// Using it, on a fake world.

// Whose turn it is in the fake world: the person plays white.
let turn = 'white';

// A fake world to build with: after the person's move, black plays for a second and a half.
const chess = {
	name: 'Chess',
	roles: [{ name: 'white' }, { name: 'black' }],
	myRole: 'white',
	data: { turn: { every: 200, value: () => turn } },
	onMessage: () => {
		turn = 'black';
		setTimeout(() => { turn = 'white'; }, 1500);
		return 'Black is thinking…';
	},
};

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

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;
	const isMyTurn = world.data.turn === world.myRole;
	return (
		<div style={{ display: 'grid', gap: 12, justifyItems: 'start' }}>
			<h2 style={{ margin: 0 }}>{world.name}</h2>
			<TurnIndicator world={world} />
			<button type="button" disabled={!isMyTurn} onClick={() => world.send('e2-e4')}>Make a move</button>
		</div>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// The component: copy this part.

/**
 * Tells whose turn it is. When the world says it in its data (a role, like
 * 'white'), it is the person's turn when that is their role. Otherwise it is
 * their turn while the rules of the world let them send.
 * @param {object} node - The world.
 * @param {string} name - The name in `world.data` that says whose turn it is.
 * @returns {{isMine: boolean, whose: string|null}} Whether it is the person's turn, and whose it is.
 */
function turnOf(node, name) {
	const whose = node.data[name];
	if (typeof whose === 'string') return { isMine: whose === node.myRole, whose };
	return { isMine: node.canSend, whose: null };
}

/**
 * Draws whose turn it is into `container` (in a game, or any world where
 * people take turns), then again at every change. When the turn comes,
 * "Your turn" stands out and screen readers say it.
 * @param {HTMLElement} container - Where to draw it.
 * @param {object} world - The world, from connectToHost.
 * @param {{name?: string}} [options] - The name in `world.data` that says whose turn it is (a role). Default 'turn'.
 * @returns {() => void} Stops following the world and removes what it drew.
 */
export function turnIndicator(container, world, { name = 'turn' } = {}) {
	const badge = document.createElement('p');
	badge.setAttribute('role', 'status');
	Object.assign(badge.style, { display: 'inline-flex', alignItems: 'center', gap: '8px', margin: '0', padding: '6px 14px', borderRadius: '30px', fontWeight: '600' });
	const dot = document.createElement('span');
	dot.setAttribute('aria-hidden', 'true');
	Object.assign(dot.style, { width: '8px', height: '8px', borderRadius: '50%' });
	const words = document.createElement('span');
	badge.append(dot, words);
	container.append(badge);

	// Called at once, then after every change of the world.
	const stop = world.subscribe((node) => {
		const { isMine, whose } = turnOf(node, name);
		let text = isMine ? 'Your turn' : `Waiting for ${whose ?? 'the others'}`;
		if (node.status === 'ended') text = 'The game is over';
		const isHighlighted = isMine && node.status !== 'ended';
		// Write only new words, to let a screen reader say them once.
		if (words.textContent !== text) words.textContent = text;
		badge.style.background = isHighlighted ? '#2A54C4' : 'rgba(127, 127, 127, 0.15)';
		badge.style.color = isHighlighted ? '#fff' : 'inherit';
		dot.style.background = isHighlighted ? '#fff' : '#9D9C9A';
	});

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

// Using it, on a fake world.

// Whose turn it is in the fake world: the person plays white.
let turn = 'white';

// A fake world to build with: after the person's move, black plays for a second and a half.
const chess = {
	name: 'Chess',
	roles: [{ name: 'white' }, { name: 'black' }],
	myRole: 'white',
	data: { turn: { every: 200, value: () => turn } },
	onMessage: () => {
		turn = 'black';
		setTimeout(() => { turn = 'white'; }, 1500);
		return 'Black is thinking…';
	},
};

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

const page = document.createElement('div');
Object.assign(page.style, { display: 'grid', gap: '12px', justifyItems: 'start' });
const title = document.createElement('h2');
title.style.margin = '0';
title.textContent = world.name;
const place = document.createElement('div');
const move = document.createElement('button');
move.type = 'button';
move.textContent = 'Make a move';
move.onclick = () => world.send('e2-e4');
page.append(title, place, move);
document.body.append(page);

turnIndicator(place, world);
world.subscribe((node) => {
	move.disabled = node.data.turn !== node.myRole;
});
Live, on a fake world
Open

Make a move gives the turn to black for a second and a half before it comes back to white.

A world can state whose turn it is in two ways (People and roles and The data of the world) and the component reads both:

  • In its data, by role. world.data.turn in the example is 'white' or 'black'. The turn belongs to the person when that string equals their role in world.myRole. The fake world of the example uses this form since a value that changes over time belongs in data.
  • Through its rules alone. It is then the turn of the person while the rules accept a message from them (world.canSend).

When world.status is 'ended' the component reads "The game is over".

Props ​

PropDefaultWhat it is
world(required)The world, from useWorld.
name'turn'The key in world.data that holds the role whose turn it is. Without a string under that key the component follows world.canSend.

Parameters ​

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

ParameterWhat it is
containerThe element to draw into.
worldThe world, from connectToHost.
options.nameThe key in world.data that holds the role whose turn it is. Default 'turn'.

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

Change it ​

  • Names instead of roles: when the world puts the name of a member in its data, compare it with the nickname of the person in world.me.nickname (WhoAmI).
  • A sound or a vibration on the turn of the person: keep the last isMine and call navigator.vibrate(200) or play a short sound when it becomes true (Play a sound for a new message).
  • The time left: put a Timer next to it.

See also ​

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