Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/apps/web/lib/theme.ts
blob: c819ac86f059b411de5ca129f27fd2cbe198453d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: AGPL-3.0-or-later

/**
 * The theme a visitor chose, shared by every host of Progmasoft.
 *
 * It is a cookie of the registrable domain, as the language is, so a choice
 * made on progmasoft.com holds on account., viget., product. and console. as
 * well. Scripts of the page read and write it, so it is not HttpOnly; it
 * carries nothing but "light" or "dark". A choice stored by earlier versions
 * of the site in localStorage, which each host kept for itself, moves into
 * the cookie the first time a page sees it.
 */

export type Theme = "light" | "dark";

export const themeCookieName = "progmasoft_theme";

/** The value of the theme cookie in a Cookie header, if it holds a theme. */
export function themeFromCookies(cookieHeader: string): Theme | undefined {
  for (const part of cookieHeader.split(";")) {
    const [name, value] = part.trim().split("=", 2);
    if (name === themeCookieName && (value === "light" || value === "dark")) {
      return value;
    }
  }
  return undefined;
}

/**
 * The Set-Cookie text of a theme for `document.cookie`: a year long, for the
 * registrable domain on a Progmasoft host and for the host itself elsewhere
 * (a local development name), and Secure over HTTPS.
 */
export function themeCookie(
  theme: Theme,
  host: string,
  secure: boolean,
): string {
  const shared = host === "progmasoft.com" || host.endsWith(".progmasoft.com");
  return [
    `${themeCookieName}=${theme}`,
    "Path=/",
    `Max-Age=${60 * 60 * 24 * 365}`,
    "SameSite=Lax",
    ...(shared ? ["Domain=.progmasoft.com"] : []),
    ...(secure ? ["Secure"] : []),
  ].join("; ");
}

/**
 * The script that sets the theme before the body is painted, so no page
 * flashes the other theme: the cookie, else a choice in localStorage (which it
 * moves into the cookie), else light. It runs as an inline script with the
 * nonce of the page, so it is plain text; tests run it against the functions
 * above.
 */
export const themeBootstrap = `(() => {
  try {
    const host = location.hostname;
    const shared = host === "progmasoft.com" || host.endsWith(".progmasoft.com");
    let theme = (/(?:^|;\\s*)progmasoft_theme=(light|dark)(?:;|$)/.exec(document.cookie) || [])[1];
    if (!theme) {
      const stored = localStorage.getItem("progmasoft_theme");
      if (stored === "light" || stored === "dark") {
        theme = stored;
        document.cookie = "progmasoft_theme=" + theme + "; Path=/; Max-Age=31536000; SameSite=Lax" +
          (shared ? "; Domain=.progmasoft.com" : "") + (location.protocol === "https:" ? "; Secure" : "");
      }
    }
    theme = theme || "light";
    document.documentElement.dataset.theme = theme;
    document.documentElement.style.colorScheme = theme;
  } catch {}
})();`;