Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(app): tint links with brand color, cache it for first paint

Hampusa1332554449c
diff --git a/fluxer_app/scripts/GenerateColorSystem.ts b/fluxer_app/scripts/GenerateColorSystem.ts
index bc3ce0844..6541b335a 100644
--- a/fluxer_app/scripts/GenerateColorSystem.ts
+++ b/fluxer_app/scripts/GenerateColorSystem.ts
@@ -386,7 +386,7 @@ hsl(258, calc(10% * var(--saturation-factor)), 2%) 10%
{name: '--plutonium-text', value: 'var(--text-on-brand-primary)'},
{name: '--plutonium-icon', family: 'brandIcon', lightness: 50},
{name: '--invite-verified-icon-color', value: 'var(--text-on-brand-primary)'},
- {name: '--text-link', family: 'link', lightness: 70},
+ {name: '--text-link', family: 'link', lightness: 70, instanceOverride: '--instance-brand-primary-foreground'},
{
name: '--text-on-brand-primary',
hue: 0,
@@ -541,7 +541,7 @@ hsl(245, calc(100% * var(--saturation-factor)), 80%) 40%
{name: '--control-button-active-text', family: 'neutralLight', lightness: 15},
{name: '--control-button-danger-text', hue: 359, saturation: 70, useSaturationFactor: true, lightness: 50},
{name: '--control-button-danger-hover-bg', hue: 359, saturation: 70, useSaturationFactor: true, lightness: 95},
- {name: '--text-link', family: 'link', lightness: 45},
+ {name: '--text-link', family: 'link', lightness: 45, instanceOverride: '--instance-brand-primary-foreground'},
...LIGHT_CODE_TOKENS,
{name: '--text-selection', hue: 210, saturation: 90, useSaturationFactor: true, lightness: 50, alpha: 0.2},
{name: '--markup-mention-border', family: 'link', lightness: 45, alpha: 0.4},
@@ -707,7 +707,12 @@ hsl(220, calc(13% * var(--saturation-factor)), 2%) 10%
{name: '--control-button-hover-bg', family: 'legacyDark', lightness: 22},
{name: '--control-button-active-bg', family: 'legacyDark', lightness: 24},
{name: '--status-online', family: 'legacyStatusOnline', lightness: 40},
- {name: '--text-link', family: 'legacyLink', lightness: 70},
+ {
+ name: '--text-link',
+ family: 'legacyLink',
+ lightness: 70,
+ instanceOverride: '--instance-brand-primary-foreground',
+ },
{name: '--text-selection', hue: 210, saturation: 90, useSaturationFactor: true, lightness: 70, alpha: 0.35},
{name: '--markup-mention-border', family: 'legacyLink', lightness: 70, alpha: 0.3},
...DARK_CODE_TOKENS,
diff --git a/fluxer_app/src/features/theme/hooks/useThemeCssVariables.ts b/fluxer_app/src/features/theme/hooks/useThemeCssVariables.ts
index 82620f3f3..7d6d4d968 100644
--- a/fluxer_app/src/features/theme/hooks/useThemeCssVariables.ts
+++ b/fluxer_app/src/features/theme/hooks/useThemeCssVariables.ts
@@ -1,10 +1,44 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {HdrDisplayMode} from '@app/features/accessibility/state/Accessibility';
+import {readRawStorageItem, writeRawStorageItem} from '@app/features/platform/state/PrebootMirror';
import {remFromPx} from '@app/features/theme/layout/RemFromPx';
import {getInstanceBrandVariables, INSTANCE_BRAND_VARIABLES} from '@app/features/theme/utils/InstanceBrandColors';
import {useLayoutEffect} from 'react';
+const SERVED_THEME_COLOR_STYLE_ID = 'fluxer-instance-theme-color';
+const INSTANCE_BRAND_PREBOOT_STORAGE_KEY = 'fluxer:instance-brand:preboot';
+const SATURATION_PREBOOT_STORAGE_KEY = 'fluxer:accessibility:saturation-preboot';
+
+interface SavedInstanceBrandColors {
+ c: string;
+ s: number;
+ v: Record<string, Record<string, string>>;
+}
+
+function readSavedInstanceBrandColors(): SavedInstanceBrandColors | null {
+ try {
+ const saved = JSON.parse(readRawStorageItem(INSTANCE_BRAND_PREBOOT_STORAGE_KEY) ?? 'null');
+ return saved !== null && typeof saved === 'object' && typeof saved.v === 'object' && saved.v !== null
+ ? saved
+ : null;
+ } catch {
+ return null;
+ }
+}
+
+function saveInstanceBrandColors(
+ themeColor: string,
+ saturationFactor: number,
+ theme: string,
+ variables: Record<string, string>,
+): void {
+ const saved = readSavedInstanceBrandColors();
+ const themes = saved?.c === themeColor && saved.s === saturationFactor ? saved.v : {};
+ const next: SavedInstanceBrandColors = {c: themeColor, s: saturationFactor, v: {...themes, [theme]: variables}};
+ writeRawStorageItem(INSTANCE_BRAND_PREBOOT_STORAGE_KEY, JSON.stringify(next));
+}
+
interface ThemeCssVariablesOptions {
effectiveTheme: string;
saturationFactor: number;
@@ -40,6 +74,7 @@ export function useThemeCssVariables({
}
htmlNode.classList.add(themeClass);
htmlNode.style.setProperty('--saturation-factor', saturationFactor.toString());
+ writeRawStorageItem(SATURATION_PREBOOT_STORAGE_KEY, saturationFactor === 1 ? null : saturationFactor.toString());
htmlNode.style.setProperty('--user-select', enableTextSelection ? 'auto' : 'none');
htmlNode.style.setProperty('--font-size', remFromPx(fontSize));
htmlNode.style.setProperty('--chat-horizontal-padding', remFromPx(messageGutter));
@@ -67,9 +102,18 @@ export function useThemeCssVariables({
hdrDisplayMode,
]);
useLayoutEffect(() => {
- if (instanceThemeColor === null) return;
- const variables = getInstanceBrandVariables(instanceThemeColor, effectiveTheme, saturationFactor);
- if (variables === null) return;
+ document.getElementById(SERVED_THEME_COLOR_STYLE_ID)?.remove();
+ const variables =
+ instanceThemeColor === null
+ ? null
+ : getInstanceBrandVariables(instanceThemeColor, effectiveTheme, saturationFactor);
+ if (instanceThemeColor === null || variables === null) {
+ if (readRawStorageItem(INSTANCE_BRAND_PREBOOT_STORAGE_KEY) !== null) {
+ writeRawStorageItem(INSTANCE_BRAND_PREBOOT_STORAGE_KEY, null);
+ }
+ return;
+ }
+ saveInstanceBrandColors(instanceThemeColor, saturationFactor, effectiveTheme, variables);
const htmlNode = document.documentElement;
for (const name of INSTANCE_BRAND_VARIABLES) {
htmlNode.style.setProperty(name, variables[name]);
diff --git a/fluxer_app/src/features/theme/utils/InstanceBrandColors.ts b/fluxer_app/src/features/theme/utils/InstanceBrandColors.ts
index 8c681629b..d09114a07 100644
--- a/fluxer_app/src/features/theme/utils/InstanceBrandColors.ts
+++ b/fluxer_app/src/features/theme/utils/InstanceBrandColors.ts
@@ -26,10 +26,10 @@ export const INSTANCE_BRAND_VARIABLES = [
type InstanceBrandVariable = (typeof INSTANCE_BRAND_VARIABLES)[number];
-export const MIN_TEXT_CONTRAST = 4.5;
+const MIN_TEXT_CONTRAST = 4.5;
const TARGET_CONTRAST = MIN_TEXT_CONTRAST + 0.05;
-export const FOREGROUND_SELF_TINT_RATIO = 15;
-export const FOREGROUND_BRAND_TINT_RATIO = 22;
+const FOREGROUND_SELF_TINT_RATIO = 15;
+const FOREGROUND_BRAND_TINT_RATIO = 22;
type Rgb = readonly [number, number, number];
@@ -89,29 +89,27 @@ function rgbToHsl([red, green, blue]: Rgb): Hsl {
return {hue: (hue * 60 + 360) % 360, saturation: saturation * 100, lightness: lightness * 100};
}
-export function hslToRgb(hue: number, saturation: number, lightness: number): Rgb {
+function hueSectorComponents(h: number, chroma: number, x: number): Rgb {
+ if (h < 1) return [chroma, x, 0];
+ if (h < 2) return [x, chroma, 0];
+ if (h < 3) return [0, chroma, x];
+ if (h < 4) return [0, x, chroma];
+ if (h < 5) return [x, 0, chroma];
+ return [chroma, 0, x];
+}
+
+function hslToRgb(hue: number, saturation: number, lightness: number): Rgb {
const s = Math.min(100, Math.max(0, saturation)) / 100;
const l = Math.min(100, Math.max(0, lightness)) / 100;
const chroma = (1 - Math.abs(2 * l - 1)) * s;
const h = (((hue % 360) + 360) % 360) / 60;
const x = chroma * (1 - Math.abs((h % 2) - 1));
const m = l - chroma / 2;
- const [r, g, b] =
- h < 1
- ? [chroma, x, 0]
- : h < 2
- ? [x, chroma, 0]
- : h < 3
- ? [0, chroma, x]
- : h < 4
- ? [0, x, chroma]
- : h < 5
- ? [x, 0, chroma]
- : [chroma, 0, x];
+ const [r, g, b] = hueSectorComponents(h, chroma, x);
return [(r + m) * 255, (g + m) * 255, (b + m) * 255];
}
-export function mixRgb(first: Rgb, second: Rgb, firstPercent: number): Rgb {
+function mixRgb(first: Rgb, second: Rgb, firstPercent: number): Rgb {
const ratio = firstPercent / 100;
return [
first[0] * ratio + second[0] * (1 - ratio),
@@ -163,7 +161,7 @@ export function resolveThemeSurface(theme: string, name: string, saturationFacto
return resolveTone({family: scale.family, lightness: scaleStopLightness(scale, index)}, saturationFactor);
}
-export function resolveThemeSurfaces(theme: string, saturationFactor: number): Array<Rgb> {
+function resolveThemeSurfaces(theme: string, saturationFactor: number): Array<Rgb> {
const palette = getThemePalette(theme);
const scale = COLOR_SCALES[palette.surfaceScale];
const overlay = resolveTone(palette.selectedOverlay, saturationFactor);