Follow the theme of the webapp
Your view runs inside the webapp and should match the light or dark theme chosen there by the person. world.webapp.theme holds the current theme and subscribe runs again whenever the person switches it. With a fake world the theme follows the colour scheme of your operating system. Switch your computer to dark mode and the example turns dark at once.
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>
</>
);
}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}`;
});How it works
world.webapp.theme is 'light' or 'dark'. Keep one palette per theme and apply the right one in subscribe to follow every switch without a reload. Assigning the theme to colorScheme also tells the browser which theme is active and the controls that the browser draws itself (text fields, scroll bars, menus) then match your colours.
To see the other theme while you build, pass it to connectToHost({ fake: { world }, webapp: { theme: 'dark' } }) (Pretend the look of the webapp).
See also
FollowTheme: the same behaviour as a component.- The look of the webapp and the phone screen
- Use the language of the webapp