~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

Readers choose to see the console light or dark

Merged chadtmiller.com asked to merge into feature/branding from chadtmiller.com/group-host:branding/appearance
.changeset/appearance-choice.mdnew+7−0
@@ -0,0 +1,7 @@
---
default: minor
---
# Choose to see the console light or dark
The menu under your handle has Appearance: System, Light or Dark. A choice takes effect at once, is kept in a cookie of the console's own for a year, and every page is drawn in it from its first byte. System, as before, follows the device.
docs/reference/config.md+6−0
@@ -75,3 +75,9 @@ the host (`getBrand`), so they are set on the host alone.
| `PRIMARY_COLOR` | the colour of what acts on the console's pages, its buttons, links and marks, as `#rgb` or `#rrggbb`. Their hover, the soft wash behind what they mark and the line under links are mixed from it, and the text on a button is white or the page's ink, whichever reads better on it |
| `PRIMARY_COLOR_DARK` | the same in a dark colour scheme. Unset, it's `PRIMARY_COLOR` lifted to a lightness dark ink reads on |
| `THEME` | the console's set of colours for everything else, its paper, cards, lines and ink: `paper`, its own warm one, or `slate`, a cool one |
Whether a page is light or dark is the reader's, not the operator's: as
their device has it, or as they choose under Appearance in the menu under
their handle. The console keeps a choice in a cookie of its own,
`appearance` (`light` or `dark`), for a year, and draws every page in it
from its first byte.
harness/e2e/flows.spec.mjs+20−0
@@ -1000,6 +1000,26 @@ test('moving an account here to become a group', async () => {
await expect(page.getByRole('heading', { level: 1 })).toBeVisible();
});
test('choosing to see it dark, and as her system has it again', async () => {
await page.goto(url('/'));
const html = page.locator('html');
await expect(html).not.toHaveAttribute('data-theme');
// From her menu: at once, and from the page's first byte after.
await page.getByRole('banner').getByText('@alice.test', { exact: true }).click();
const appearance = page.getByRole('radiogroup', { name: 'Appearance' });
await appearance.getByText('Dark', { exact: true }).click();
await expect(html).toHaveAttribute('data-theme', 'dark');
const drawn = await page.reload();
expect(await drawn.text()).toContain('data-theme="dark"');
await expect(html).toHaveAttribute('data-theme', 'dark');
await page.getByRole('banner').getByText('@alice.test', { exact: true }).click();
await expect(appearance.getByRole('radio', { name: 'Dark' })).toBeChecked();
await appearance.getByText('System', { exact: true }).click();
await expect(html).not.toHaveAttribute('data-theme');
await page.reload();
await expect(html).not.toHaveAttribute('data-theme');
});
test('signing out', async () => {
await page.goto(url('/'));
// From her menu, under her handle: what is hers, and signing out last.
web/messages/en.json+4−0
@@ -478,6 +478,10 @@
"user_menu_move": "Move an account here",
"user_menu_sign_out_this": "Sign out of this account",
"user_menu_sign_out": "Sign out",
"appearance_label": "Appearance",
"appearance_system": "System",
"appearance_light": "Light",
"appearance_dark": "Dark",
"route_error_400": "Nothing was asked of this page",
"route_error_403": "This is not yours to see",
"route_error_404": "Nothing is here",
web/src/lib/molecules/Appearance.sveltenew+26−0
@@ -0,0 +1,26 @@
<script lang="ts">
import { m } from '#lib/paraglide/messages.js';
import { choose, type Appearance } from '#lib/appearance.ts';
/** The reader's choice of light or dark, or as their system has it (`chosen`), changed in a click, and kept. */
let { chosen }: { chosen: Appearance } = $props();
const options: { value: Appearance; text: string }[] = [
{ value: 'system', text: m.appearance_system() },
{ value: 'light', text: m.appearance_light() },
{ value: 'dark', text: m.appearance_dark() }
];
</script>
<p class="menu-label" id="appearance-label">{m.appearance_label()}</p>
<div class="appearance" role="radiogroup" aria-labelledby="appearance-label">
{#each options as o (o.value)}
<label
><input
type="radio"
name="appearance"
value={o.value}
checked={chosen === o.value}
onchange={() => choose(o.value)}
/>{o.text}</label
>
{/each}
</div>
web/src/lib/molecules/UserMenu.svelte+9−5
@@ -1,6 +1,7 @@
<script lang="ts">
import { m } from '#lib/paraglide/messages.js';
import Avatar from '#lib/atoms/Avatar.svelte';
import Appearance from '#lib/molecules/Appearance.svelte';
import { SIGN_IN } from '#lib/signIn.ts';
import { page } from '$app/state';
/**
@@ -10,11 +11,12 @@
* host's, back to this page), and adding another, by the sign-in
* dialog; their invites, how many are waiting (`invites`), as a dot on
* their picture too while any are; their groups, starting one, moving
* an account here, and, last, signing out (`/console/logout`), which
* goes on as the account used before. Without `places`, as on a page
* left only to approve or decline, the menu is the accounts alone:
* switching, adding one, and signing out. It closes on a click
* elsewhere, on Escape, and on going to another page.
* an account here; light or dark, as they choose; and, last, signing
* out (`/console/logout`), which goes on as the account used before.
* Without `places`, as on a page left only to approve or decline, the
* menu is the accounts alone: switching, adding one, and signing out.
* It closes on a click elsewhere, on Escape, and on going to another
* page.
*/
let {
handle,
@@ -86,6 +88,8 @@
<a href="/new" onclick={close}>{m.user_menu_start_group()}</a>
<a href="/move" onclick={close}>{m.user_menu_move()}</a>
<hr />
<Appearance chosen={page.data.appearance ?? 'system'} />
<hr />
{/if}
<form method="post" action="/console/logout">
<button class="quiet" type="submit"
web/src/lib/appearance.tsnew+28−0
@@ -0,0 +1,28 @@
/**
* Whether a reader sees the console light or dark: as their system is, or
* as they chose, kept in a cookie of the console's own, so the page is
* drawn so from its first byte.
*/
export type Appearance = 'system' | 'light' | 'dark';
/** The cookie a reader's choice is kept in. */
export const COOKIE = 'appearance';
/** A year: a choice is kept until changed. */
const KEEP_FOR = 365 * 24 * 60 * 60;
/** What the cookie's value says: a scheme chosen, or none, as the system has it. */
export const appearanceOf = (value?: string): Appearance =>
value === 'light' || value === 'dark' ? value : 'system';
/** The reader's choice, kept, and the page drawn in it at once: `system` forgets it. */
export function choose(appearance: Appearance) {
const root = document.documentElement;
if (appearance === 'system') {
delete root.dataset.theme;
document.cookie = `${COOKIE}=; path=/; max-age=0; samesite=lax`;
} else {
root.dataset.theme = appearance;
document.cookie = `${COOKIE}=${appearance}; path=/; max-age=${KEEP_FOR}; samesite=lax`;
}
}
web/src/routes/+layout.server.tsnew+7−0
@@ -0,0 +1,7 @@
import type { LayoutServerLoad } from './$types';
import { appearanceOf, COOKIE } from '#lib/appearance.ts';
/** Whether the reader chose light or dark, by their cookie: as their system has it, if not. */
export const load: LayoutServerLoad = ({ cookies }) => ({
appearance: appearanceOf(cookies.get(COOKIE))
});
web/src/routes/+layout.ts+4−2
@@ -13,7 +13,8 @@ import { xrpc } from '#lib/data.ts';
* makes one, as the host says, Bluesky's when it does not; and the other
* accounts this browser is signed in to, to switch to. Beside it, how
* the host shows itself, its name, logo and links: the console's own,
* from a host that does not say.
* from a host that does not say; and the reader's choice of light or
* dark, as the server read it from their cookie.
*/
export const load: LayoutLoad = async (event) => {
const [answer, brand] = await Promise.all([
@@ -33,6 +34,7 @@ export const load: LayoutLoad = async (event) => {
invitations: invited?.invites ?? [],
accounts: viewer ? (answer?.accounts ?? []) : [],
signUpAt: answer?.signUpAt ?? 'https://bsky.social',
brand
brand,
appearance: event.data.appearance
};
};
web/src/app.css+34−0
@@ -279,6 +279,40 @@ img {
.menu form {
display: block;
}
/* Light, dark, or as the system has it: three choices side by side in the
menu, the one chosen washed in the colour that acts. */
.appearance {
display: flex;
gap: var(--space-1);
padding: 0 var(--space-3) var(--space-1);
}
.appearance label {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
min-height: var(--control-sm);
border: var(--border);
border-radius: var(--radius-sm);
font-size: var(--text-sm);
cursor: pointer;
}
.appearance label:hover {
background: var(--soft);
}
.appearance input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.appearance label:has(:checked) {
background: var(--primary-soft);
border-color: var(--primary);
}
.appearance label:has(:focus-visible) {
outline: 2px solid var(--primary);
outline-offset: 2px;
}
main.wrap {
flex: 1;
padding-top: var(--space-8);
web/src/app.d.ts+3−1
web/src/app.html+1−1
web/src/hooks.server.ts+12−4