Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<meta name="theme-color" content="#0f1720" media="(prefers-color-scheme: dark)" />
<meta name="theme-color" content="#f5f3ee" media="(prefers-color-scheme: light)" />
<title>OpenCommunicate</title>
<!-- light or dark before the first paint: the choice kept under "oc-theme", else the system's (App.tsx keeps it up) -->
<script>try { document.documentElement.dataset.theme = localStorage.getItem("oc-theme") || (matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"); } catch (e) {}</script>
<link rel="icon" href="./icon.svg" />
<!-- the web app (iPhone: Share → Add to Home Screen). No start_url in the manifest: the home-screen icon
opens the page it was added from, which carries the chat repo after an invitation. -->
Expand Down
36 changes: 36 additions & 0 deletions app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,39 @@ function LangPicker() {
);
}

/* Light or dark: the system's until the header's sun/moon picks one, kept under "oc-theme" (index.html reads it
before the first paint). Picking the theme the system has goes back to following the system. */
const THEME = "oc-theme";
const systemDark = matchMedia("(prefers-color-scheme: dark)");
let pickedTheme = (() => { try { return localStorage.getItem(THEME); } catch { return null; } })();
const themeNow = () => pickedTheme === "light" || pickedTheme === "dark" ? pickedTheme : systemDark.matches ? "dark" : "light";
function applyTheme() {
document.documentElement.dataset.theme = themeNow();
const bg = getComputedStyle(document.documentElement).getPropertyValue("--bg").trim();
document.querySelectorAll<HTMLMetaElement>('meta[name="theme-color"]').forEach((m) => { m.content = bg; });
}
applyTheme();
systemDark.addEventListener("change", applyTheme);

function ThemeButton() {
const [, redraw] = useState(0);
useEffect(() => {
const again = () => redraw((n) => n + 1);
systemDark.addEventListener("change", again);
return () => systemDark.removeEventListener("change", again);
}, []);
const dark = themeNow() === "dark";
const flip = () => {
const next = dark ? "light" : "dark";
pickedTheme = next === (systemDark.matches ? "dark" : "light") ? null : next;
try { if (pickedTheme) localStorage.setItem(THEME, pickedTheme); else localStorage.removeItem(THEME); } catch { /* private mode: this page only */ }
applyTheme();
redraw((n) => n + 1);
};
const label = dark ? S.lightTheme : S.darkTheme;
return <button className="icon theme-button" title={label} aria-label={label} onClick={flip}>{dark ? <Sun /> : <Moon />}</button>;
}

/** Puts text on the clipboard; false when the browser refuses. The write starts inside the click while the
text may still be on its way: Safari only allows clipboard writes during a click, not after an await. */
function copyText(text: string | Promise<string>): Promise<boolean> {
Expand Down Expand Up @@ -1109,6 +1142,7 @@ function Chat({ session, onSession, onSignOut, chats, activity, onOpen, onAdd }:
<header className="side-head">
<span className="brand">OpenCommunicate</span>
<span className="head-buttons">
<ThemeButton />
<button className="icon" title={S.inviteSomeone} aria-label={S.inviteSomeone} onClick={() => setDialog("invite")}><PersonPlus /></button>
<button className="icon" title={S.settings} aria-label={S.settings} onClick={() => setDialog("settings")}><Gear /></button>
</span>
Expand Down Expand Up @@ -1416,6 +1450,8 @@ function Marquee({ text }: { text: string }) {

const Gear = () => <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="3" /><path d="M19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1.1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1.1 1.7 1.7 0 0 0-.3-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8V9a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z" /></svg>;
const PersonPlus = () => <svg viewBox="0 0 24 24" width="21" height="21" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><circle cx="9" cy="8" r="4" /><path d="M2 21a7 7 0 0 1 14 0M19 8v6M16 11h6" /></svg>;
const Sun = () => <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" aria-hidden="true"><circle cx="12" cy="12" r="4" /><path d="M12 2.5v2M12 19.5v2M2.5 12h2M19.5 12h2M5.3 5.3l1.4 1.4M17.3 17.3l1.4 1.4M5.3 18.7l1.4-1.4M17.3 6.7l1.4-1.4" /></svg>;
const Moon = () => <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M20 14.5A8 8 0 0 1 9.5 4a8 8 0 1 0 10.5 10.5z" /></svg>;
const Magnifier = () => <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" aria-hidden="true"><circle cx="11" cy="11" r="6.5" /><path d="m16 16 4.5 4.5" /></svg>;
const Info = () => <svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" aria-hidden="true"><circle cx="12" cy="12" r="9" /><path d="M12 11v5M12 8h.01" /></svg>;
const Clip = () => <svg viewBox="0 0 24 24" width="21" height="21" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M21 12.5 12.5 21a6 6 0 0 1-8.5-8.5L13 3.5a4 4 0 0 1 5.7 5.7L9.7 18.2a2 2 0 0 1-2.8-2.8L15 7.3" /></svg>;
Expand Down
4 changes: 4 additions & 0 deletions app/src/i18n.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ const en = {
members: (n: number) => `${n} member${n === 1 ? "" : "s"}`,
everyoneCount: (n: number) => `everyone · ${n} ${n === 1 ? "person" : "people"}`,
settings: "Settings",
lightTheme: "Light theme",
darkTheme: "Dark theme",
search: "Search",
searchChats: "Search chats or people",
searchAll: "Search chats, people or messages",
Expand Down Expand Up @@ -550,6 +552,8 @@ const tr: typeof en = {
members: (n) => `${n} üye`,
everyoneCount: (n) => `herkes · ${n} kişi`,
settings: "Ayarlar",
lightTheme: "Açık tema",
darkTheme: "Koyu tema",
search: "Ara",
searchChats: "Sohbet ya da kişi ara",
searchAll: "Sohbet, kişi ya da mesaj ara",
Expand Down
26 changes: 12 additions & 14 deletions app/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
--sat: var(--safe-area-inset-top, env(safe-area-inset-top, 0px)); --sab: var(--safe-area-inset-bottom, env(safe-area-inset-bottom, 0px));
color-scheme: light;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #0f1720; --panel: #16212c; --sunken: #1d2a37; --line: #273645;
--ink: #e8edf2; --muted: #8b98a7; --accent: #3ddc97; --accent-ink: #0f1720;
--mine: #1e4a3a; --danger: #ff7b72;
color-scheme: dark;
}
/* dark: data-theme is the system's or the person's choice (index.html and App.tsx set it); :where() keeps
each dark rule as specific as the light one it replaces */
:root[data-theme="dark"] {
--bg: #0f1720; --panel: #16212c; --sunken: #1d2a37; --line: #273645;
--ink: #e8edf2; --muted: #8b98a7; --accent: #3ddc97; --accent-ink: #0f1720;
--mine: #1e4a3a; --danger: #ff7b72;
color-scheme: dark;
}
* { box-sizing: border-box; }
html, body, #root { height: 100%; margin: 0; }
Expand Down Expand Up @@ -213,12 +213,10 @@ select { font: inherit; color: var(--ink); background: var(--sunken); border: 1p
.avatar.group { border-radius: 30%; color: hsl(var(--h) 45% 32%); background: hsl(var(--h) 45% 88%); }
.presence { position: absolute; right: -2px; bottom: -2px; width: 12px; height: 12px; border-radius: 50%; background: var(--line); border: 2px solid var(--panel); }
.presence.on { background: var(--accent); }
@media (prefers-color-scheme: dark) {
.avatar { background: hsl(var(--h) 38% 40%); }
.avatar.agent { background: hsl(var(--h) 25% 28%); }
.avatar.group { color: hsl(var(--h) 60% 80%); background: hsl(var(--h) 30% 24%); }
.who { color: hsl(var(--h) 60% 70%); }
}
:where([data-theme="dark"]) .avatar { background: hsl(var(--h) 38% 40%); }
:where([data-theme="dark"]) .avatar.agent { background: hsl(var(--h) 25% 28%); }
:where([data-theme="dark"]) .avatar.group { color: hsl(var(--h) 60% 80%); background: hsl(var(--h) 30% 24%); }
:where([data-theme="dark"]) .who { color: hsl(var(--h) 60% 70%); }
.chat-head .title { display: flex; flex-direction: row; align-items: center; gap: 10px; margin-right: auto; }
.chat-head .title-text { display: flex; flex-direction: column; min-width: 0; }
.chat-head .title-text b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
Expand Down Expand Up @@ -283,7 +281,7 @@ select { font: inherit; color: var(--ink); background: var(--sunken); border: 1p
/* read receipts: one tick sent, two delivered, two blue read */
.meta .ticks { vertical-align: -1px; margin-left: 4px; color: var(--muted); }
.meta .ticks.read, .msg-info .ticks.read { color: #1a8fd6; }
@media (prefers-color-scheme: dark) { .meta .ticks.read, .msg-info .ticks.read { color: #53bdeb; } }
:where([data-theme="dark"]) :is(.meta .ticks.read, .msg-info .ticks.read) { color: #53bdeb; }
label.toggle { justify-content: flex-start; color: var(--ink); font-size: 14px; }
label.toggle input { width: 18px; height: 18px; flex: none; margin: 0; accent-color: var(--accent); }

Expand Down
22 changes: 22 additions & 0 deletions test/app.e2e.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,28 @@ if (only === "all" || only === "search") {
await browser.close();
}

// light or dark: the system's until the header's sun/moon picks one, kept across a reload
if (only === "all" || only === "theme") {
const { browser, page } = await setup(() => 0);
const look = () => page.evaluate(() => ({ theme: document.documentElement.dataset.theme, bg: getComputedStyle(document.body).backgroundColor, kept: localStorage.getItem("oc-theme"), bar: document.querySelector('meta[name="theme-color"]').content }));
const button = page.locator(".theme-button");
await button.waitFor({ timeout: 15000 });
check("the theme follows the system: light", JSON.stringify(await look()) === JSON.stringify({ theme: "light", bg: "rgb(245, 243, 238)", kept: null, bar: "#f5f3ee" }), JSON.stringify(await look()));
await page.emulateMedia({ colorScheme: "dark" });
await sleep(200);
check("…and dark, when the system turns dark", (await look()).bg === "rgb(15, 23, 32)" && (await button.getAttribute("aria-label")) === "Light theme", JSON.stringify(await look()));
await button.click();
check("the sun picks light over the system's dark, kept", JSON.stringify(await look()) === JSON.stringify({ theme: "light", bg: "rgb(245, 243, 238)", kept: "light", bar: "#f5f3ee" }), JSON.stringify(await look()));
await page.reload();
await button.waitFor({ timeout: 15000 });
check("…and still light after a reload", (await look()).theme === "light" && (await button.getAttribute("aria-label")) === "Dark theme");
await page.screenshot({ path: path.join(process.env.SHOTS ?? "/tmp", "theme-light.png") }).catch(() => {});
await button.click();
await page.screenshot({ path: path.join(process.env.SHOTS ?? "/tmp", "theme-dark.png") }).catch(() => {});
check("the moon goes back to the system's dark, keeping nothing", JSON.stringify(await look()) === JSON.stringify({ theme: "dark", bg: "rgb(15, 23, 32)", kept: null, bar: "#0f1720" }), JSON.stringify(await look()));
await browser.close();
}

// a ```md block shows as Markdown (a table scrolls sideways), any other fenced block as code, and nothing becomes HTML
if (only === "all" || only === "markdown") {
const { bus, browser, page } = await setup(() => 0);
Expand Down
Loading