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

Commit 4243847

Browse files
author
Germain
authored
Integrate compound design tokens (#11091)
* 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 * lintfix * Use correct SpyInstance import * Using npm build of design tokens
1 parent b9b9326 commit 4243847

File tree

5 files changed

+222
-3
lines changed

5 files changed

+222
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"@sentry/browser": "^7.0.0",
6767
"@sentry/tracing": "^7.0.0",
6868
"@testing-library/react-hooks": "^8.0.1",
69+
"@vector-im/compound-design-tokens": "^0.0.3",
6970
"await-lock": "^2.1.0",
7071
"blurhash": "^1.1.3",
7172
"classnames": "^2.2.6",

res/css/_common.pcss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ See the License for the specific language governing permissions and
1717
limitations under the License.
1818
*/
1919

20+
@import url("@vector-im/compound-design-tokens/assets/web/css/compound-design-tokens.css");
2021
@import "./_font-sizes.pcss";
2122
@import "./_font-weights.pcss";
2223
@import "./_animations.pcss";

src/theme.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,24 @@ export async function setTheme(theme?: string): Promise<void> {
269269
const styleSheet = styleElements.get(stylesheetName)!;
270270
styleSheet.disabled = false;
271271

272+
/**
273+
* Adds the Compound theme class to the top-most element in the document
274+
* This will automatically refresh the colour scales based on the OS or user
275+
* preferences
276+
*
277+
* Note: Theming through Compound is not yet established. Brand theming should
278+
* be done in a similar manner as it used to be done.
279+
*/
280+
document.body.classList.remove("cpd-theme-light", "cpd-theme-dark", "cpd-theme-light-hc", "cpd-theme-dark-hc");
281+
282+
let compoundThemeClassName = `cpd-theme-` + (stylesheetName.includes("light") ? "light" : "dark");
283+
// Always respect user OS preference!
284+
if (isHighContrastTheme(theme) || window.matchMedia("(prefers-contrast: more)").matches) {
285+
compoundThemeClassName += "-hc";
286+
}
287+
288+
document.body.classList.add(compoundThemeClassName);
289+
272290
return new Promise((resolve, reject) => {
273291
const switchTheme = function (): void {
274292
// we re-enable our theme here just in case we raced with another

test/theme-test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe("theme", () => {
2222
let darkTheme: HTMLStyleElement;
2323

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

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

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

5254
afterEach(() => {
@@ -66,6 +68,18 @@ describe("theme", () => {
6668
expect(spyQuerySelectorAll).toHaveBeenCalledTimes(1);
6769
expect(lightTheme.disabled).toBe(false);
6870
expect(darkTheme.disabled).toBe(true);
71+
expect(spyClassList).toHaveBeenCalledWith("cpd-theme-light");
72+
});
73+
74+
it("should switch to dark", async () => {
75+
// When
76+
await new Promise((resolve) => {
77+
setTheme("dark").then(resolve);
78+
darkTheme.onload!({} as Event);
79+
});
80+
81+
// Then
82+
expect(spyClassList).toHaveBeenCalledWith("cpd-theme-dark");
6983
});
7084

7185
it("should reject promise on onerror call", () => {

0 commit comments

Comments
 (0)