Skip to content

Fix subclassing themes #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SciReactUI Changelog

### Fixed
- Styles added to Navbar and Footer incorrectly remove built in styles.
- Themes were not inheriting all details from their parent.

### Changed
- Breadcrumbs component takes optional linkComponent prop for page routing.
Expand Down
4 changes: 2 additions & 2 deletions src/storybook/theme/Colours.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ export function ThemeColorItem({title, theme, colourSet, mode}) {
### Light Mode
<ColorPalette>
<ThemeColorItem title="Primary" theme={DiamondTheme} colourSet="primary" mode="light" />
<ThemeColorItem title="Secodary" theme={DiamondTheme} colourSet="secondary" mode="light" />
<ThemeColorItem title="Secondary" theme={DiamondTheme} colourSet="secondary" mode="light" />
</ColorPalette>

### Dark Mode
<ColorPalette>
<ThemeColorItem title="Primary" theme={DiamondTheme} colourSet="primary" mode={"dark"} />
<ThemeColorItem title="Secodary" theme={DiamondTheme} colourSet="secondary" mode={"dark"} />
<ThemeColorItem title="Secondary" theme={DiamondTheme} colourSet="secondary" mode={"dark"} />
</ColorPalette>
</div>
</div>
Expand Down
7 changes: 5 additions & 2 deletions src/storybook/theme/Logos.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Meta, ColorPalette, ColorItem } from '@storybook/blocks';
import { Meta } from '@storybook/blocks';
import {ImageColorSchemeSwitch} from "../../components/controls/ImageColorSchemeSwitch";

<Meta title="Theme/Logos" />
Expand All @@ -24,7 +24,10 @@ import {ImageColorSchemeSwitch} from "../../components/controls/ImageColorScheme
}
```

<ImageColorSchemeSwitch image={{"src":"static/media/src/public/generic/logo-light.svg","alt":"A Logo"}}/>
<div style={{width:"200px", margin:"0 auto"}} >
<ImageColorSchemeSwitch image={{"src":"static/media/src/public/generic/logo-light.svg","alt":"A Logo",width:200}}/>
<p>Example logo</p>
</div>

## Sizes
There are two image sizes. I regular `normal` and a smaller version `short`, these are used in the Navbar and
Expand Down
6 changes: 6 additions & 0 deletions src/themes/BaseTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ const BaseThemeOptions /* : ThemeOptions */ = {
palette: {
background: { default: "#fafafa" },
},
text: {
primary: "#050505",
},
},
dark: {
palette: {
background: { default: "#050505" },
},
text: {
primary: "#fafafa",
},
},
},
components: {},
Expand Down
19 changes: 13 additions & 6 deletions src/themes/DiamondTheme.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createTheme, Theme } from "@mui/material/styles";
import type {} from "@mui/material/themeCssVarsAugmentation";
import { createTheme, Theme } from "@mui/material/styles";

import { BaseThemeOptions } from "./BaseTheme";
import { mergeThemeOptions } from "./ThemeManager";

import logoImageDark from "../public/diamond/logo-dark.svg";
import logoImageLight from "../public/diamond/logo-light.svg";
Expand All @@ -10,8 +10,7 @@
const dlsLogoBlue = "#202740";
const dlsLogoYellow = "#facf07";

const DiamondTheme: Theme = createTheme({
...BaseThemeOptions,
const DiamondThemeOptions = mergeThemeOptions({
logos: {
normal: {
src: logoImageLight,
Expand Down Expand Up @@ -41,6 +40,9 @@
dark: "#AF9004", // dark yellow
contrastText: "#000000", // black
},
text: {
secondary: "#161B2C",
},
},
},
dark: {
Expand All @@ -57,13 +59,16 @@
dark: "#AF9004", // dark yellow
contrastText: "#000000", // black
},
text: {
secondary: "#8090CA",
},
},
},
},
components: {
MuiButton: {
styleOverrides: {
root: ({ theme }) => ({
root: ({ theme }:{ theme:Theme}) => ({

Check failure on line 71 in src/themes/DiamondTheme.ts

View workflow job for this annotation

GitHub Actions / build (22.12.0)

Replace `{·theme:Theme` with `·{·theme:·Theme·`
textTransform: "none",
"&.MuiButton-contained": {},
"&.default": {
Expand Down Expand Up @@ -92,4 +97,6 @@
},
});

export { DiamondTheme };
const DiamondTheme: Theme = createTheme(DiamondThemeOptions);

export { DiamondTheme, DiamondThemeOptions };
10 changes: 6 additions & 4 deletions src/themes/GenericTheme.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type {} from "@mui/material/themeCssVarsAugmentation";
import { createTheme, Theme } from "@mui/material/styles";

import { BaseThemeOptions } from "./BaseTheme";
import { mergeThemeOptions } from "./ThemeManager";

import logoImageDark from "../public/generic/logo-dark.svg";
import logoImageLight from "../public/generic/logo-light.svg";

const GenericTheme: Theme = createTheme({
...BaseThemeOptions,
const GenericThemeOptions = mergeThemeOptions({
logos: {
normal: {
src: logoImageLight,
Expand All @@ -22,4 +22,6 @@ const GenericTheme: Theme = createTheme({
},
});

export { GenericTheme };
const GenericTheme: Theme = createTheme(GenericThemeOptions);
console.log(GenericTheme);
export { GenericTheme, GenericThemeOptions };
54 changes: 54 additions & 0 deletions src/themes/ThemeManager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import "@testing-library/jest-dom";

import { BaseThemeOptions } from "./BaseTheme";
import { mergeThemeOptions } from "./ThemeManager";

describe("Theme Manager merge", () => {
it("should merge", () => {
const a = { a: 1, b: 2 };
const b = { x: 1, y: 2 };
const new_a = mergeThemeOptions(a, b);

expect(new_a).toStrictEqual({ a: 1, b: 2, x: 1, y: 2 });
});

it("should deep merge", () => {
const a = { a: 1, b: 2, c: { d: 1, e: 2 } };
const b = { x: 1, y: { z: 3 } };
const merged = mergeThemeOptions(a, b);

expect(merged).toStrictEqual({
a: 1,
b: 2,
c: { d: 1, e: 2 },
x: 1,
y: { z: 3 },
});
});

it("should use a value over b", () => {
const a = { a: 100, b: 2 };
const b = { a: 1, c: 3 };
const merged = mergeThemeOptions(a, b);

expect(merged).toStrictEqual({ a: 100, b: 2, c: 3 });
});

it("should take the base theme and make a new one", () => {
const fontSize = 4422;
const a = { typography: { fontSize: fontSize } };
const b = BaseThemeOptions;

const merged = mergeThemeOptions(a, b);

expect(BaseThemeOptions.typography.fontSize).not.toStrictEqual(fontSize);
expect(merged.typography.fontSize).toStrictEqual(fontSize);
});

it("should effectively clone to an empty object", () => {
const a = { a: 100, b: 2 };
const cloned = mergeThemeOptions({}, a);

expect(cloned).toStrictEqual(a);
});
});
52 changes: 52 additions & 0 deletions src/themes/ThemeManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { BaseThemeOptions } from "./BaseTheme";
/* eslint-disable @typescript-eslint/no-explicit-any */

/*
Merge two options, with newThemeOptions having precedence.
If no parent is selected the BaseThemeOptions is used
Doesn't affect either options passed in.
*/
function mergeThemeOptions(
newThemeOptions: object,
parentThemeOptions: object = BaseThemeOptions,
) {
const parentThemeOptionsCopy = deepCopyObject(parentThemeOptions);
return mergeObjects(parentThemeOptionsCopy, newThemeOptions);
}

function mergeObjects(
mainThemeOptions: any,
parentThemeOptions: any,
visited = new Map<any, any>(),
) {
//This Deep Merge algorithm is based on https://www.geeksforgeeks.org/how-to-deep-merge-two-objects-in-typescript/
if (isObject(mainThemeOptions) && isObject(parentThemeOptions)) {
for (const key in parentThemeOptions) {
if (isObject(parentThemeOptions[key])) {
if (!mainThemeOptions[key]) {
mainThemeOptions[key] = {};
}
// Check if the parentThemeOptions object has already been visited
if (!visited.has(parentThemeOptions[key])) {
visited.set(parentThemeOptions[key], {});
mergeObjects(mainThemeOptions[key], parentThemeOptions[key], visited);
} else {
mainThemeOptions[key] = visited.get(parentThemeOptions[key]);
}
} else {
mainThemeOptions[key] = parentThemeOptions[key];
}
}
}
return mainThemeOptions;
}

function isObject(item: any): boolean {
return item !== null && typeof item === "object" && !Array.isArray(item);
}

function deepCopyObject(themeOptions: object) {
return mergeObjects({}, themeOptions);
}

export { mergeThemeOptions };
Loading