Skip to content

Segui il tema della webapp ​

La tua vista gira dentro la webapp e dovrebbe seguire il tema chiaro o scuro che la persona ha scelto lì. world.webapp.theme contiene il tema attuale e subscribe gira di nuovo ogni volta che la persona lo cambia. Con un mondo finto il tema segue lo schema di colori del tuo sistema operativo. Se passi il computer alla modalità scura, l'esempio diventa subito scuro.

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

// The colours of your view, for each theme of the webapp.
const colors = {
	light: { background: '#ffffff', text: '#1f2328', card: '#eef1f4' },
	dark: { background: '#1b1b1f', text: '#e8e8ea', card: '#2c2c33' },
};

// A fake library needs no account and no network.
const library = { name: 'Library', now: { text: 'Open until 18:00' } };

export default function App() {
	// While building: the fake world, which follows the theme of your computer.
	// To try the other one: useWorld({ fake: { world: library }, webapp: { theme: 'dark' } })
	// In the webapp: useWorld()
	const { world, error } = useWorld({ fake: { world: library } });
	const theme = world ? world.webapp.theme : null;

	// The whole page takes the theme of the webapp, and takes it again when the person switches it.
	useEffect(() => {
		if (!theme) return;
		document.body.style.background = colors[theme].background;
		document.body.style.color = colors[theme].text;
		// The browser draws its own parts (fields, scroll bars) in the same theme.
		document.documentElement.style.colorScheme = theme;
	}, [theme]);

	if (error) return <p>Could not connect ({error.code}).</p>;
	if (!world) return <p>Connecting…</p>;
	return (
		<>
			<div style={{ padding: '12px 16px', borderRadius: 8, background: colors[theme].card }}>
				<h2>{world.name}</h2>
				<p>{world.now?.text}</p>
			</div>
			<p>The webapp's theme: {theme}</p>
		</>
	);
}
js
import { connectToHost } from '@unaiverse/gui-kit';

// The colours of your view, for each theme of the webapp.
const colors = {
	light: { background: '#ffffff', text: '#1f2328', card: '#eef1f4' },
	dark: { background: '#1b1b1f', text: '#e8e8ea', card: '#2c2c33' },
};

// A fake library needs no account and no network.
const library = { name: 'Library', now: { text: 'Open until 18:00' } };

// While building: the fake world, which follows the theme of your computer.
// To try the other one: connectToHost({ fake: { world: library }, webapp: { theme: 'dark' } })
// In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: library } });

const card = document.createElement('div');
card.style.padding = '12px 16px';
card.style.borderRadius = '8px';
const title = document.createElement('h2');
const now = document.createElement('p');
const theme = document.createElement('p');
card.append(title, now);
document.body.append(card, theme);

// Called at once, then after every change, also when the person switches the theme of the webapp.
world.subscribe((node) => {
	const palette = colors[node.webapp.theme];
	document.body.style.background = palette.background;
	document.body.style.color = palette.text;
	// The browser draws its own parts (fields, scroll bars) in the same theme.
	document.documentElement.style.colorScheme = node.webapp.theme;
	card.style.background = palette.card;
	title.textContent = node.name;
	now.textContent = node.now?.text ?? '';
	theme.textContent = `The webapp's theme: ${node.webapp.theme}`;
});
Live, on a fake world
Open

Come funziona ​

world.webapp.theme vale 'light' o 'dark'. Tieni una palette per tema e applica quella giusta in subscribe per seguire ogni cambio senza ricaricare la pagina. Assegnare il tema a colorScheme informa il browser e accorda con i tuoi colori i controlli che il browser disegna da sé (campi di testo, barre di scorrimento, menu).

Per vedere l'altro tema mentre sviluppi passalo a connectToHost({ fake: { world }, webapp: { theme: 'dark' } }) (Prova un altro aspetto della webapp).

Vedi anche ​

Il GUI kit di UNaIVERSE, rilasciato con licenza Apache 2.0. Copyright 2024-2026 UNaIVERSE S.r.l.