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

Add support for light/dark themes #10285

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions components/root/root.jsx
Expand Up @@ -264,12 +264,20 @@ export default class Root extends React.PureComponent {
BrowserStore.setLandingPageSeen(true);
}

Utils.applyTheme(this.props.theme);
if (this.props.theme.type === 'system') {
Utils.toggleColorScheme(window.matchMedia('(prefers-color-scheme: dark)'));
} else {
Utils.applyTheme(this.props.theme);
}
}

componentDidUpdate(prevProps) {
if (!deepEqual(prevProps.theme, this.props.theme)) {
Utils.applyTheme(this.props.theme);
if (this.props.theme.type === 'system') {
Utils.toggleColorScheme(window.matchMedia('(prefers-color-scheme: dark)'));
} else {
Utils.applyTheme(this.props.theme);
}
}
if (this.props.location.pathname === '/') {
if (this.props.noAccounts) {
Expand Down
Expand Up @@ -102,6 +102,7 @@ export default class ThemeSetting extends React.PureComponent {
this.props.setRequireConfirm(themeChanged);

this.setState({theme});
localStorage.setItem('mm-theme-name', theme.type);
Utils.applyTheme(theme);
};

Expand Down
28 changes: 28 additions & 0 deletions packages/mattermost-redux/src/constants/preferences.ts
Expand Up @@ -63,6 +63,34 @@ const Preferences = {

CATEGORY_THEME: 'theme',
THEMES: {
system: {
type: 'System',
sidebarBg: '#1e325c',
sidebarText: '#ffffff',
sidebarUnreadText: '#ffffff',
sidebarTextHoverBg: '#28427b',
sidebarTextActiveBorder: '#5d89ea',
sidebarTextActiveColor: '#ffffff',
sidebarHeaderBg: '#192a4d',
sidebarHeaderTextColor: '#ffffff',
sidebarTeamBarBg: '#14213e',
onlineIndicator: '#3db887',
awayIndicator: '#ffbc1f',
dndIndicator: '#d24b4e',
mentionBg: '#ffffff',
mentionBj: '#ffffff',
mentionColor: '#1e325c',
centerChannelBg: '#ffffff',
centerChannelColor: '#3f4350',
newMessageSeparator: '#cc8f00',
linkColor: '#386fe5',
buttonBg: '#1c58d9',
buttonColor: '#ffffff',
errorTextColor: '#d24b4e',
mentionHighlightBg: '#ffd470',
mentionHighlightLink: '#1b1d22',
codeTheme: 'github',
},
denim: {
type: 'Denim',
sidebarBg: '#1e325c',
Expand Down
7 changes: 7 additions & 0 deletions root.jsx
Expand Up @@ -19,9 +19,16 @@ import {AnnouncementBarTypes} from 'utils/constants';
import store from 'stores/redux_store.jsx';
import App from 'components/app';

import * as Utils from './utils/utils';

// This is for anything that needs to be done for ALL react components.
// This runs before we start to render anything.
function preRenderSetup(callwhendone) {
if (window.matchMedia) {
const prefersDarkColorScheme = window.matchMedia('(prefers-color-scheme: dark)');
prefersDarkColorScheme.addEventListener('change', Utils.toggleColorScheme);
}

window.onerror = (msg, url, line, column, stack) => {
if (msg === 'ResizeObserver loop limit exceeded') {
return;
Expand Down
11 changes: 11 additions & 0 deletions utils/utils.tsx
Expand Up @@ -1981,3 +1981,14 @@ export function numberToFixedDynamic(num: number, places: number): string {
}
return str.slice(0, indexToExclude);
}

export const toggleColorScheme = (prefersDark: MediaQueryList) => {
const name = localStorage.getItem('mm-theme-name');
if (name === 'System') {
if (prefersDark.matches) {
applyTheme(Preferences.THEMES.onyx);
} else {
applyTheme(Preferences.THEMES.denim);
}
}
};