Skip to content

Commit

Permalink
Add Cycle themes
Browse files Browse the repository at this point in the history
  • Loading branch information
dpwatrous authored and gingi committed Nov 9, 2023
1 parent 28aa74f commit 4fc66d2
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as ReactDOM from "react-dom";
import {RootPane} from "@azure/bonito-ui/lib/components/layout";
import { Subscription } from "rxjs";
import { Theme, ThemeService } from "app/services";
import { ThemeName } from "@azure/bonito-ui/lib/theme";

export const ReactWrapper: React.FC = props => {
return React.createElement(RootPane, {theme: "explorerDark"}, props.children);
Expand All @@ -37,7 +38,7 @@ export class ReactContainerComponent<P> implements OnChanges, OnDestroy, AfterVi

private _subs: Subscription[] = [];

private _themeName: string = "explorerLight";
private _themeName: ThemeName = "explorerLight";

private _themeService: ThemeService;

Expand Down
4 changes: 2 additions & 2 deletions packages/bonito-ui/src/components/layout/root-pane.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as React from "react";
import { getTheme } from "../../theme";
import { ThemeName, getTheme } from "../../theme";
import { ThemeProvider } from "@fluentui/react-theme-provider";
import { loadTheme } from "@fluentui/react/lib/Styling";

export interface RootPaneProps {
theme?: string;
theme?: ThemeName;
}

const themeProviderStyles: React.CSSProperties = {
Expand Down
29 changes: 29 additions & 0 deletions packages/bonito-ui/src/theme/azure-theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { PartialAppTheme } from "./app-theme";

export const CycleThemeLight: PartialAppTheme = {
semanticColors: {
appHeaderBackground: "#0062b3",
appHeaderText: "#ffffff",
},
};

export const CycleThemeDark: PartialAppTheme = {
semanticColors: {
appHeaderBackground: "#333842",
appHeaderText: "#ffffff",
},
};

export const CycleThemeHighContrastLight: PartialAppTheme = {
semanticColors: {
appHeaderBackground: "#333333",
appHeaderText: "#000000",
},
};

export const CycleThemeHighContrastDark: PartialAppTheme = {
semanticColors: {
appHeaderBackground: "#000000",
appHeaderText: "#ffffff",
},
};
83 changes: 71 additions & 12 deletions packages/bonito-ui/src/theme/theme-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import {
import { AppTheme } from "./app-theme";
import { useTheme } from "@fluentui/react-theme-provider";
import { getLogger } from "@azure/bonito-core";
import {
CycleThemeDark,
CycleThemeHighContrastDark,
CycleThemeHighContrastLight,
CycleThemeLight,
} from "./azure-theme";

export const BaseThemeLight = AzureThemeLight;
export const BaseThemeDark = AzureThemeDark;
Expand Down Expand Up @@ -49,7 +55,7 @@ export interface ThemeInfo extends ThemeMapEntry {
* @returns A list of objects which contain metadata on each theme
*/
export function listThemes(): ThemeInfo[] {
const sortedNames = Object.keys(_themeMap).sort();
const sortedNames = Object.keys(_themeMap).sort() as ThemeName[];
const themes: ThemeInfo[] = [];
for (const n of sortedNames) {
const entry = _themeMap[n];
Expand All @@ -62,18 +68,25 @@ export function listThemes(): ThemeInfo[] {
return themes;
}

export type ThemeName = keyof typeof _themeMap;

/**
* Gets a theme by name
*
* @returns A FluentUI theme
*/
export function getTheme(themeName: string): ThemeInfo {
let mapInfo = _themeMap[themeName];
if (!mapInfo) {
getLogger("getTheme").error(
`Unable to load theme ${themeName}: Falling back to default`
);
export function getTheme(themeName: ThemeName | "default"): ThemeInfo {
let mapInfo;
if (themeName === "default") {
mapInfo = _themeMap[defaultTheme];
} else {
mapInfo = _themeMap[themeName];
if (!mapInfo) {
getLogger("getTheme").error(
`Unable to load theme ${themeName}: Falling back to default`
);
mapInfo = _themeMap[defaultTheme];
}
}
return {
name: themeName,
Expand All @@ -87,9 +100,55 @@ interface ThemeMapEntry {
get: () => AppTheme;
}

const _themeMap: Record<string, ThemeMapEntry> = {
const _themeMap = {
cycleLight: {
label: "Cycle Light",
get: () => {
return _getThemeLazy("cycleLight", BaseThemeLight, (baseTheme) => {
return mergeThemes(baseTheme, CycleThemeLight) as AppTheme;
});
},
},
cycleDark: {
label: "Cycle Dark",
get: () => {
return _getThemeLazy("cycleDark", BaseThemeDark, (baseTheme) => {
return mergeThemes(baseTheme, CycleThemeDark) as AppTheme;
});
},
},
cycleHighContrastLight: {
label: "Cycle High Contrast (Light)",
get: () => {
return _getThemeLazy(
"cycleHighContrastLight",
BaseThemeHighContrastLight,
(baseTheme) => {
return mergeThemes(
baseTheme,
CycleThemeHighContrastLight
) as AppTheme;
}
);
},
},
cycleHighContrastDark: {
label: "Cycle High Contrast (Dark)",
get: () => {
return _getThemeLazy(
"cycleHighContrastDark",
BaseThemeHighContrastDark,
(baseTheme) => {
return mergeThemes(
baseTheme,
CycleThemeHighContrastDark
) as AppTheme;
}
);
},
},
explorerLight: {
label: "Light",
label: "Explorer Light",
get: () => {
return _getThemeLazy(
"explorerLight",
Expand All @@ -104,15 +163,15 @@ const _themeMap: Record<string, ThemeMapEntry> = {
},
},
explorerDark: {
label: "Dark",
label: "Explorer Dark",
get: () => {
return _getThemeLazy("explorerDark", BaseThemeDark, (baseTheme) => {
return mergeThemes(baseTheme, ExplorerThemeDark) as AppTheme;
});
},
},
explorerHighContrastLight: {
label: "High Contrast (Light)",
label: "Explorer High Contrast (Light)",
get: () => {
return _getThemeLazy(
"explorerHighContrastLight",
Expand All @@ -127,7 +186,7 @@ const _themeMap: Record<string, ThemeMapEntry> = {
},
},
explorerHighContrastDark: {
label: "High Contrast (Dark)",
label: "Explorer High Contrast (Dark)",
get: () => {
return _getThemeLazy(
"explorerHighContrastDark",
Expand Down
5 changes: 3 additions & 2 deletions web/src/components/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Main } from "./layout/main";
import { Stack, IStackTokens } from "@fluentui/react/lib/Stack";
import { PrimaryButton } from "@fluentui/react/lib/Button";
import { translate } from "@azure/bonito-core";
import { ThemeName } from "@azure/bonito-ui/lib/theme";

//DefaultButton
const dropdownStyles: Partial<IDropdownStyles> = {
Expand All @@ -26,7 +27,7 @@ const dropdownStyles: Partial<IDropdownStyles> = {
* Represents the entire application
*/
export const Application: React.FC = () => {
const [theme, setTheme] = React.useState(defaultTheme);
const [theme, setTheme] = React.useState<ThemeName>(defaultTheme);

const themeOptions = React.useMemo(() => {
const options: IDropdownOption[] = [];
Expand Down Expand Up @@ -74,7 +75,7 @@ export const Application: React.FC = () => {
onRenderLabel={() => <></>}
onChange={(_, option) => {
if (option) {
setTheme(String(option.key));
setTheme(option.key as ThemeName);
}
}}
/>
Expand Down
4 changes: 3 additions & 1 deletion web/src/components/layout/app-root.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as React from "react";
import { RootPane } from "@azure/bonito-ui/lib/components";
import { ThemeName } from "@azure/bonito-ui/lib/theme";

export interface RootProps {
theme?: string;
theme?: ThemeName;
}

export const AppRoot: React.FC<RootProps> = (props) => {
Expand Down

0 comments on commit 4fc66d2

Please sign in to comment.