Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Integrate compound design tokens #11091

Merged
merged 4 commits into from
Jun 14, 2023
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"@sentry/browser": "^7.0.0",
"@sentry/tracing": "^7.0.0",
"@testing-library/react-hooks": "^8.0.1",
"@vector-im/compound-design-tokens": "^0.0.3",
"await-lock": "^2.1.0",
"blurhash": "^1.1.3",
"classnames": "^2.2.6",
Expand Down
1 change: 1 addition & 0 deletions res/css/_common.pcss
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

@import url("@vector-im/compound-design-tokens/assets/web/css/compound-design-tokens.css");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea how much this increases the bundle size by?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the amount that exists in here https://github.com/vector-im/compound-design-tokens/tree/develop/assets/web/css

So almost nothing compared to the gazillion of lines of JavaScript we send down the wire

@import "./_font-sizes.pcss";
@import "./_font-weights.pcss";
@import "./_animations.pcss";
Expand Down
18 changes: 18 additions & 0 deletions src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,24 @@ export async function setTheme(theme?: string): Promise<void> {
const styleSheet = styleElements.get(stylesheetName)!;
styleSheet.disabled = false;

/**
* Adds the Compound theme class to the top-most element in the document
* This will automatically refresh the colour scales based on the OS or user
* preferences
*
* Note: Theming through Compound is not yet established. Brand theming should
* be done in a similar manner as it used to be done.
*/
document.body.classList.remove("cpd-theme-light", "cpd-theme-dark", "cpd-theme-light-hc", "cpd-theme-dark-hc");

let compoundThemeClassName = `cpd-theme-` + (stylesheetName.includes("light") ? "light" : "dark");
// Always respect user OS preference!
if (isHighContrastTheme(theme) || window.matchMedia("(prefers-contrast: more)").matches) {
compoundThemeClassName += "-hc";
}
germain-gg marked this conversation as resolved.
Show resolved Hide resolved

document.body.classList.add(compoundThemeClassName);

return new Promise((resolve, reject) => {
const switchTheme = function (): void {
// we re-enable our theme here just in case we raced with another
Expand Down
14 changes: 14 additions & 0 deletions test/theme-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe("theme", () => {
let darkTheme: HTMLStyleElement;

let spyQuerySelectorAll: jest.MockInstance<NodeListOf<Element>, [selectors: string]>;
let spyClassList: jest.SpyInstance<void, string[], any>;

beforeEach(() => {
const styles = [
Expand All @@ -47,6 +48,7 @@ describe("theme", () => {

jest.spyOn(document.body, "style", "get").mockReturnValue([] as any);
spyQuerySelectorAll = jest.spyOn(document, "querySelectorAll").mockReturnValue(styles as any);
spyClassList = jest.spyOn(document.body.classList, "add");
});

afterEach(() => {
Expand All @@ -66,6 +68,18 @@ describe("theme", () => {
expect(spyQuerySelectorAll).toHaveBeenCalledTimes(1);
expect(lightTheme.disabled).toBe(false);
expect(darkTheme.disabled).toBe(true);
expect(spyClassList).toHaveBeenCalledWith("cpd-theme-light");
});

it("should switch to dark", async () => {
// When
await new Promise((resolve) => {
setTheme("dark").then(resolve);
darkTheme.onload!({} as Event);
});

// Then
expect(spyClassList).toHaveBeenCalledWith("cpd-theme-dark");
});

it("should reject promise on onerror call", () => {
Expand Down
Loading