Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix: make themes responsive to system theme (#55)
Peter Chinman697a186ac2ae
diff --git a/js/app/packages/app/component/Root.tsx b/js/app/packages/app/component/Root.tsxindex 805e7a5dee..c29eb800c9 100644--- a/js/app/packages/app/component/Root.tsx+++ b/js/app/packages/app/component/Root.tsx@@ -44,8 +44,8 @@ import {Show,Suspense,} from 'solid-js';-import { currentThemeId } from '../../block-theme/signals/themeSignals';-import { applyTheme } from '../../block-theme/utils/themeUtils';+import { currentThemeId, darkModeTheme, lightModeTheme, systemMode, themeShouldMatchSystem } from '../../block-theme/signals/themeSignals';+import { applyTheme, ensureMinimalThemeContrast, systemThemeEffect } from '../../block-theme/utils/themeUtils';import { useNotificationState } from '../../notification-provider/src/NotificationProvider';import { TauriRouteListener } from '../../tauri/src/TauriProvider';import { updateCookie } from '../util/updateCookie';@@ -309,7 +309,9 @@ export function Root() {const handleBeforeUnload = () => track(TrackingEvents.AUTH.TERMINATE);onMount(() => {+ systemThemeEffect();applyTheme(currentThemeId());+ ensureMinimalThemeContrast();window.addEventListener('beforeunload', handleBeforeUnload);});onCleanup(() =>diff --git a/js/app/packages/block-theme/constants.ts b/js/app/packages/block-theme/constants.tsindex 4147d49ea4..2f5b57cb7f 100644--- a/js/app/packages/block-theme/constants.ts+++ b/js/app/packages/block-theme/constants.ts@@ -1,6 +1,9 @@import type { ThemeV1 } from './types/themeTypes';-export const DEFAULT_THEMES: Array<ThemeV1> = [+export const DEFAULT_DARK_THEME: DefaultTheme = 'Macro Dark';+export const DEFAULT_LIGHT_THEME: DefaultTheme = 'Macro Light';++export const DEFAULT_THEMES = [{id: 'Macro Dark',name: 'Macro Dark',@@ -177,4 +180,6 @@ export const DEFAULT_THEMES: Array<ThemeV1> = [c4:{ l: 0.63, c: 0.192, h: 287 }}}-];+] as const satisfies ReadonlyArray<ThemeV1>;++type DefaultTheme = (typeof DEFAULT_THEMES)[number]['id'];diff --git a/js/app/packages/block-theme/signals/themeSignals.ts b/js/app/packages/block-theme/signals/themeSignals.tsindex af6a58332b..5ebfbea32b 100644--- a/js/app/packages/block-theme/signals/themeSignals.ts+++ b/js/app/packages/block-theme/signals/themeSignals.ts@@ -2,7 +2,7 @@ import { createEffect, createMemo, createSignal } from 'solid-js';import { convertThemev0v1 } from '../utils/themeMigrations';import type { ThemeV0, ThemeV1 } from '../types/themeTypes';import { makePersisted } from '@solid-primitives/storage';-import { DEFAULT_THEMES } from '../constants';+import { DEFAULT_DARK_THEME, DEFAULT_LIGHT_THEME, DEFAULT_THEMES } from '../constants';export const [isThemeSaved, setIsThemeSaved] = createSignal<boolean>(true);@@ -26,31 +26,47 @@ let convertedDefaultThemes = DEFAULT_THEMES.map((theme) => {else { return theme }});-export const [currentThemeId, setCurrentThemeId] = makePersisted(- createSignal<string>('Macro'),+export const [currentThemeId, setCurrentThemeId_] = makePersisted(+ createSignal<string>(DEFAULT_DARK_THEME),{ name: 'macro-selected-theme' });+// If theme should match system, when we set current theme, we also set the corresponding mode's theme+// This avoids the issue where a user sets a theme, and then refreshes, and gets reverted to their preferred mode's theme.+export const setCurrentThemeId = (...args: Parameters<typeof setCurrentThemeId_>) => {+ setCurrentThemeId_(...args);+ if (themeShouldMatchSystem()) {+ systemMode() === 'dark' ? setDarkModeTheme(...args) : setLightModeTheme(...args);+ }+}+export const themes = createMemo(() => [...convertedDefaultThemes,...userThemes(),]);export const [lightModeTheme, setLightModeTheme] = makePersisted(- createSignal<string>('micro'),+ createSignal<string>(DEFAULT_LIGHT_THEME),{ name: 'macro-light-mode-theme' });export const [darkModeTheme, setDarkModeTheme] = makePersisted(- createSignal<string>('macro'),+ createSignal<string>(DEFAULT_DARK_THEME),{ name: 'macro-dark-mode-theme' });export const [themeShouldMatchSystem, setThemeShouldMatchSystem] = makePersisted(- createSignal<boolean>(false),+ createSignal<boolean>(true),{name: 'macro-theme-should-match-system'});+export const [systemMode, setSystemMode] = createSignal<'dark' | 'light'>(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');++const darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)');+darkModeQuery.addEventListener('change', (e: MediaQueryListEvent) => {+ setSystemMode(e.matches ? 'dark' : 'light');+});+export const [monochromeIcons, setMonochromeIcons] = makePersisted(createSignal<boolean>(false),{ name: 'enable-monochrome-icons' }diff --git a/js/app/packages/block-theme/utils/themeUtils.ts b/js/app/packages/block-theme/utils/themeUtils.tsindex 01e0cd3df6..38ebe05ac8 100644--- a/js/app/packages/block-theme/utils/themeUtils.ts+++ b/js/app/packages/block-theme/utils/themeUtils.ts@@ -1,8 +1,10 @@-import { themes, currentThemeId, setCurrentThemeId, setIsThemeSaved, userThemes, setUserThemes } from '../signals/themeSignals';+import { themes, currentThemeId, setCurrentThemeId, setIsThemeSaved, userThemes, setUserThemes, themeShouldMatchSystem, systemMode, lightModeTheme, darkModeTheme } from '../signals/themeSignals';import type { ThemeV1, ThemeV1Tokens } from '../types/themeTypes';import { themeReactive } from '../signals/themeReactive';import { setHtmlColor } from '../signals/themeSignals';-import { batch } from 'solid-js';+import { batch, createEffect, on } from 'solid-js';+import { DEFAULT_DARK_THEME } from '../constants';+import { toast } from '@core/component/Toast/Toast';export function exportTheme(){let id = currentThemeId();@@ -10,11 +12,19 @@ export function exportTheme(){navigator.clipboard.writeText(theme);}+export function systemThemeEffect() {+ createEffect(on([themeShouldMatchSystem, systemMode, darkModeTheme, lightModeTheme], () => {+ if (themeShouldMatchSystem()) {+ applyTheme(systemMode() === 'dark' ? darkModeTheme() : lightModeTheme());+ }+ }, {defer: true}))+}+export function applyTheme(id: string): void {let theme = themes().find((t) => t.id === id);if (!theme) {console.error(`theme not found: ${id}`);- theme = themes().find((t) => t.id === 'Macro Dark')!;+ theme = themes().find((t) => t.id === DEFAULT_DARK_THEME)!;}setCurrentThemeId(theme.id);@@ -90,3 +100,16 @@ export function deleteTheme(id: string): void {setIsThemeSaved(false);setCurrentThemeId('');}++/** Checks if the theme contrast is too low, and if so, applies a readable theme. This is to prevent malicious actors sending "Theme Viruses" which make a user's theme unusable. */+export function ensureMinimalThemeContrast() {+ const spec = themes().find((t) => t.id === currentThemeId())?.tokens;+ if (!spec) return;+ // Check if the contrast is too low, so that users can't get stuck with an unreadable theme+ const lowContrastTheme =+ Math.abs((spec.c0.l) - (spec.b0.l)) < 0.2;+ if (lowContrastTheme) {+ applyTheme(DEFAULT_DARK_THEME);+ toast.alert('Tried to load a theme with low contrast, applying a readable theme.');+ }+}