Skip to content

FollowTheme ​

Your view sits in an iframe inside the webapp under its top bar. In the dark theme of the webapp a white view stands out as a bright block in a dark page. FollowTheme gives its content the background and text colours of the current theme of the webapp (light or dark) and switches them as soon as the person switches theme. Wrap your whole view in it.

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

// The component: copy this part.

/** The colours for each theme of the webapp: its own background and text. */
const THEME_COLORS = {
	light: { background: '#F1F0EF', text: '#141312' },
	dark: { background: '#0D0D0D', text: '#F7F6F5' },
};

/**
 * FollowTheme: draws its content in the theme of the webapp, light or dark, and
 * switches when the person switches the theme of the webapp. The fields, buttons
 * and scrollbars that the browser draws inside it follow too.
 * @param {object} props
 * @param {object} props.world - The world, from useWorld.
 * @param {{light: {background: string, text: string}, dark: {background: string, text: string}}} [props.colors] - Your colours for each theme.
 * @param {object} [props.style] - More style for the box, for example `{ minHeight: '100vh' }` to fill the view.
 * @param {React.ReactNode} props.children - What to draw in the theme.
 */
export function FollowTheme({ world, colors = THEME_COLORS, style, children }) {
	// 'light' or 'dark'. The webapp says which, and says it again when it changes.
	const theme = world.webapp.theme;
	const look = colors[theme] ?? colors.light;
	return (
		<div style={{ colorScheme: theme, background: look.background, color: look.text, ...style }}>
			{children}
		</div>
	);
}

// Using it, on a fake world.

// A fake world to build with: no account, no network.
const studio = { name: 'Studio' };

export default function App() {
	// While building: the fake world, in a webapp that pretends to be dark. In the webapp: useWorld()
	// (Leave out `webapp`, and the fake webapp follows the colour scheme of your page.)
	const { world, error } = useWorld({ fake: { world: studio }, webapp: { theme: 'dark' } });

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;
	return (
		<FollowTheme world={world} style={{ padding: 16, borderRadius: 14 }}>
			<h2 style={{ marginTop: 0 }}>{world.name}</h2>
			<p>The webapp's theme: {world.webapp.theme}</p>
			<div style={{ display: 'flex', gap: 8 }}>
				<input placeholder="A field of the browser" />
				<button type="button">A button</button>
			</div>
		</FollowTheme>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// The component: copy this part.

/** The colours for each theme of the webapp: its own background and text. */
const THEME_COLORS = {
	light: { background: '#F1F0EF', text: '#141312' },
	dark: { background: '#0D0D0D', text: '#F7F6F5' },
};

/**
 * Draws `container` in the theme of the webapp, light or dark, and switches when
 * the person switches the theme of the webapp. The fields, buttons and scrollbars
 * that the browser draws inside it follow too. Pass `document.body` for the whole view.
 * @param {HTMLElement} container - The element to colour.
 * @param {object} world - The world, from connectToHost.
 * @param {{colors?: object}} [options] - Your colours for each theme: `{ light: { background, text }, dark: {...} }`.
 * @returns {() => void} Stops following the world and gives `container` its colours back.
 */
export function followTheme(container, world, { colors = THEME_COLORS } = {}) {
	const before = { colorScheme: container.style.colorScheme, background: container.style.background, color: container.style.color };

	// Called at once, then after every change of the world (the theme is part of it).
	const stop = world.subscribe((node) => {
		// 'light' or 'dark'. The webapp says which, and says it again when it changes.
		const theme = node.webapp.theme;
		const look = colors[theme] ?? colors.light;
		container.style.colorScheme = theme;
		container.style.background = look.background;
		container.style.color = look.text;
	});

	return () => {
		stop();
		Object.assign(container.style, before);
	};
}

// Using it, on a fake world.

// A fake world to build with: no account, no network.
const studio = { name: 'Studio' };

// While building: the fake world, in a webapp that pretends to be dark. In the webapp: const world = await connectToHost();
// (Leave out `webapp`, and the fake webapp follows the colour scheme of your page.)
const world = await connectToHost({ fake: { world: studio }, webapp: { theme: 'dark' } });

const box = document.createElement('div');
box.style.padding = '16px';
box.style.borderRadius = '14px';
const title = document.createElement('h2');
title.style.marginTop = '0';
const words = document.createElement('p');
const row = document.createElement('div');
Object.assign(row.style, { display: 'flex', gap: '8px' });
const field = document.createElement('input');
field.placeholder = 'A field of the browser';
const button = document.createElement('button');
button.type = 'button';
button.textContent = 'A button';
row.append(field, button);
box.append(title, words, row);
document.body.append(box);

followTheme(box, world);
world.subscribe((node) => {
	title.textContent = node.name;
	words.textContent = `The webapp's theme: ${node.webapp.theme}`;
});
Live, on a fake world
Open

The example passes webapp: { theme: 'dark' } to connectToHost (or useWorld) together with the fake option. The box is therefore dark whatever the colour scheme of your page. Without webapp the fake world uses the colour scheme of your page. Inside the real webapp the theme is the one chosen by the person.

The theme is world.webapp.theme ('light' or 'dark'). A change of theme counts as a change of the world. Your subscribe callback runs again and the component applies the new colours in it. The component also writes the CSS color-scheme of the container to make the browser render its own inputs, buttons and scrollbars in the same theme (The look of the webapp and the phone screen).

Props ​

PropDefaultWhat it is
world(required)The world returned by useWorld.
colorsthose of the webappYour colours for each theme as { light: { background, text }, dark: { background, text } }.
stylenoneExtra styles for the box (for example { minHeight: '100vh' } to fill the view).
childrenThe content to render in the theme (usually your whole view).

Parameters ​

followTheme(container, world, options) colours container and colours it again whenever the theme of the webapp changes. Pass document.body to colour the whole view.

ParameterWhat it is
containerThe element to colour.
worldThe world that connectToHost resolves with.
options.colorsYour colours for each theme in the same shape as the prop.

It returns a function that unsubscribes from the world and restores the inline color-scheme, background and color that container had before.

Change it ​

  • Colour the whole view. In React add minHeight: '100vh' to style and remove the margin of the page (body { margin: 0 }). The plain JavaScript version takes document.body as container.
  • Use the colours of your brand through colors with a high contrast between text and background in both themes.
  • Add more colours such as accent, border or muted to each theme in colors and expose them as CSS variables ('--accent': look.accent in the style) to the rest of your view.
  • Follow the language of the webapp too by reading world.webapp.language (Use the language of the webapp).

See also ​

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