Compound Typography pass (#11103)
* Integrate compound design tokens The icons should not be included in this repo, and should live in the compound design token repo, but for simplicity sake at this phase of the integration they will be added here * Delete unused or incorrect - sass variables * Typography pass * Deprecate _font-weights.pcss and use Compound instead * lint fix * Fix snapshot * Fix typography pass feedback * Remove unwanted e2e test cypress tests should test functionality not visual output. And we should not test visual output by inspecting CSS properties * lintfix * Migration script for baseFontSize * Updates after design review * Update font scaling panel to use min/max size * Fix custom font * Fix font slider e2e test * Update custom font * Update new baseFontSizeV2 * Disambiguate heading props * Fix appearance test * change max font size * fix e2ee test * fix tests * test baseFontSize migration code * typescript strict * Migrate baseFontSize account setting * Change assertion for font size * Fix font size controller test
This commit is contained in:
parent
ce479c5774
commit
9c7d935aae
199 changed files with 606 additions and 608 deletions
|
@ -459,6 +459,18 @@ export const SETTINGS: { [setting: string]: ISetting } = {
|
|||
"baseFontSize": {
|
||||
displayName: _td("Font size"),
|
||||
supportedLevels: LEVELS_ACCOUNT_SETTINGS,
|
||||
default: "",
|
||||
controller: new FontSizeController(),
|
||||
},
|
||||
/**
|
||||
* With the transition to Compound we are moving to a base font size
|
||||
* of 16px. We're taking the opportunity to move away from the `baseFontSize`
|
||||
* setting that had a 5px offset.
|
||||
*
|
||||
*/
|
||||
"baseFontSizeV2": {
|
||||
displayName: _td("Font size"),
|
||||
supportedLevels: [SettingLevel.DEVICE],
|
||||
default: FontWatcher.DEFAULT_SIZE,
|
||||
controller: new FontSizeController(),
|
||||
},
|
||||
|
|
|
@ -26,10 +26,18 @@ export default class FontSizeController extends SettingController {
|
|||
}
|
||||
|
||||
public onChange(level: SettingLevel, roomId: string, newValue: any): void {
|
||||
// Dispatch font size change so that everything open responds to the change.
|
||||
dis.dispatch<UpdateFontSizePayload>({
|
||||
action: Action.UpdateFontSize,
|
||||
size: newValue,
|
||||
});
|
||||
// In a distant past, `baseFontSize` was set on the account and config
|
||||
// level. This can be accessed only after the initial sync. If we end up
|
||||
// discovering that a logged in user has this kind of setting, we want to
|
||||
// trigger another migration of the base font size.
|
||||
if (level === SettingLevel.ACCOUNT || level === SettingLevel.CONFIG) {
|
||||
dis.fire(Action.MigrateBaseFontSize);
|
||||
} else if (newValue !== "") {
|
||||
// Dispatch font size change so that everything open responds to the change.
|
||||
dis.dispatch<UpdateFontSizePayload>({
|
||||
action: Action.UpdateFontSize,
|
||||
size: newValue,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright 2020 The Matrix.org Foundation C.I.C.
|
||||
Copyright 2020 - 2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
@ -22,13 +22,20 @@ import { Action } from "../../dispatcher/actions";
|
|||
import { SettingLevel } from "../SettingLevel";
|
||||
import { UpdateSystemFontPayload } from "../../dispatcher/payloads/UpdateSystemFontPayload";
|
||||
import { ActionPayload } from "../../dispatcher/payloads";
|
||||
import { clamp } from "../../utils/numbers";
|
||||
|
||||
export class FontWatcher implements IWatcher {
|
||||
public static readonly MIN_SIZE = 8;
|
||||
public static readonly DEFAULT_SIZE = 10;
|
||||
public static readonly MAX_SIZE = 15;
|
||||
// Externally we tell the user the font is size 15. Internally we use 10.
|
||||
public static readonly SIZE_DIFF = 5;
|
||||
/**
|
||||
* Value indirectly defined by Compound.
|
||||
* All `rem` calculations are made from a `16px` values in the
|
||||
* @vector-im/compound-design-tokens package
|
||||
*
|
||||
* We might want to move to using `100%` instead so we can inherit the user
|
||||
* preference set in the browser regarding font sizes.
|
||||
*/
|
||||
public static readonly DEFAULT_SIZE = 16;
|
||||
public static readonly MIN_SIZE = FontWatcher.DEFAULT_SIZE - 5;
|
||||
public static readonly MAX_SIZE = FontWatcher.DEFAULT_SIZE + 5;
|
||||
|
||||
private dispatcherRef: string | null;
|
||||
|
||||
|
@ -36,9 +43,39 @@ export class FontWatcher implements IWatcher {
|
|||
this.dispatcherRef = null;
|
||||
}
|
||||
|
||||
public start(): void {
|
||||
public async start(): Promise<void> {
|
||||
this.updateFont();
|
||||
this.dispatcherRef = dis.register(this.onAction);
|
||||
/**
|
||||
* baseFontSize is an account level setting which is loaded after the initial
|
||||
* sync. Hence why we can't do that in the `constructor`
|
||||
*/
|
||||
await this.migrateBaseFontSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrating the old `baseFontSize` for Compound.
|
||||
* Everything will becomes slightly larger, and getting rid of the `SIZE_DIFF`
|
||||
* weirdness for locally persisted values
|
||||
*/
|
||||
private async migrateBaseFontSize(): Promise<void> {
|
||||
const legacyBaseFontSize = SettingsStore.getValue("baseFontSize");
|
||||
if (legacyBaseFontSize) {
|
||||
console.log("Migrating base font size for Compound, current value", legacyBaseFontSize);
|
||||
|
||||
// For some odd reason, the persisted value in user storage has an offset
|
||||
// of 5 pixels for all values stored under `baseFontSize`
|
||||
const LEGACY_SIZE_DIFF = 5;
|
||||
// Compound uses a base font size of `16px`, whereas the old Element
|
||||
// styles based their calculations off a `15px` root font size.
|
||||
const ROOT_FONT_SIZE_INCREASE = 1;
|
||||
|
||||
const baseFontSize = legacyBaseFontSize + ROOT_FONT_SIZE_INCREASE + LEGACY_SIZE_DIFF;
|
||||
|
||||
await SettingsStore.setValue("baseFontSizeV2", null, SettingLevel.DEVICE, baseFontSize);
|
||||
await SettingsStore.setValue("baseFontSize", null, SettingLevel.DEVICE, "");
|
||||
console.log("Migration complete, deleting legacy `baseFontSize`");
|
||||
}
|
||||
}
|
||||
|
||||
public stop(): void {
|
||||
|
@ -47,7 +84,7 @@ export class FontWatcher implements IWatcher {
|
|||
}
|
||||
|
||||
private updateFont(): void {
|
||||
this.setRootFontSize(SettingsStore.getValue("baseFontSize"));
|
||||
this.setRootFontSize(SettingsStore.getValue("baseFontSizeV2"));
|
||||
this.setSystemFont({
|
||||
useSystemFont: SettingsStore.getValue("useSystemFont"),
|
||||
font: SettingsStore.getValue("systemFont"),
|
||||
|
@ -55,7 +92,9 @@ export class FontWatcher implements IWatcher {
|
|||
}
|
||||
|
||||
private onAction = (payload: ActionPayload): void => {
|
||||
if (payload.action === Action.UpdateFontSize) {
|
||||
if (payload.action === Action.MigrateBaseFontSize) {
|
||||
this.migrateBaseFontSize();
|
||||
} else if (payload.action === Action.UpdateFontSize) {
|
||||
this.setRootFontSize(payload.size);
|
||||
} else if (payload.action === Action.UpdateSystemFont) {
|
||||
this.setSystemFont(payload as UpdateSystemFontPayload);
|
||||
|
@ -72,33 +111,41 @@ export class FontWatcher implements IWatcher {
|
|||
}
|
||||
};
|
||||
|
||||
private setRootFontSize = (size: number): void => {
|
||||
const fontSize = Math.max(Math.min(FontWatcher.MAX_SIZE, size), FontWatcher.MIN_SIZE);
|
||||
private setRootFontSize = async (size: number): Promise<void> => {
|
||||
const fontSize = clamp(size, FontWatcher.MIN_SIZE, FontWatcher.MAX_SIZE);
|
||||
|
||||
if (fontSize !== size) {
|
||||
SettingsStore.setValue("baseFontSize", null, SettingLevel.DEVICE, fontSize);
|
||||
await SettingsStore.setValue("baseFontSizeV2", null, SettingLevel.DEVICE, fontSize);
|
||||
}
|
||||
document.querySelector<HTMLElement>(":root")!.style.fontSize = toPx(fontSize);
|
||||
};
|
||||
|
||||
public static readonly FONT_FAMILY_CUSTOM_PROPERTY = "--cpd-font-family-sans";
|
||||
|
||||
private setSystemFont = ({
|
||||
useSystemFont,
|
||||
font,
|
||||
}: Pick<UpdateSystemFontPayload, "useSystemFont" | "font">): void => {
|
||||
if (useSystemFont) {
|
||||
// Make sure that fonts with spaces in their names get interpreted properly
|
||||
document.body.style.fontFamily = font
|
||||
.split(",")
|
||||
.map((font) => {
|
||||
font = font.trim();
|
||||
if (!font.startsWith('"') && !font.endsWith('"')) {
|
||||
font = `"${font}"`;
|
||||
}
|
||||
return font;
|
||||
})
|
||||
.join(",");
|
||||
/**
|
||||
* Overrides the default font family from Compound
|
||||
* Make sure that fonts with spaces in their names get interpreted properly
|
||||
*/
|
||||
document.body.style.setProperty(
|
||||
FontWatcher.FONT_FAMILY_CUSTOM_PROPERTY,
|
||||
font
|
||||
.split(",")
|
||||
.map((font) => {
|
||||
font = font.trim();
|
||||
if (!font.startsWith('"') && !font.endsWith('"')) {
|
||||
font = `"${font}"`;
|
||||
}
|
||||
return font;
|
||||
})
|
||||
.join(","),
|
||||
);
|
||||
} else {
|
||||
document.body.style.fontFamily = "";
|
||||
document.body.style.removeProperty(FontWatcher.FONT_FAMILY_CUSTOM_PROPERTY);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue