Skip to content

Commit 7e947a1

Browse files
committed
Integrate automatic theme in tabbed options
1 parent 04136fe commit 7e947a1

File tree

4 files changed

+36
-49
lines changed

4 files changed

+36
-49
lines changed

src/components/common/tabbed-options.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ interface TabClickEvent extends React.MouseEvent {
1515
}
1616

1717
export const TabsContainer = styled((p: {
18-
onClick: (tabValue: any) => void;
19-
isSelected: (value: any) => boolean;
20-
children: Array<React.ReactElement<any, typeof Tab>>;
21-
disabled?: boolean;
18+
onClick: (tabValue: any) => void,
19+
isSelected: (value: any) => boolean,
20+
children: Array<React.ReactElement<any, typeof Tab>>
2221
}) => <nav
2322
{...omit(p, 'isSelected')}
2423
onClick={(event: TabClickEvent) => {
@@ -41,11 +40,6 @@ export const TabsContainer = styled((p: {
4140
flex-direction: column;
4241
justify-content: center;
4342
align-items: flex-start;
44-
45-
${p => p.disabled && css`
46-
opacity: 0.5;
47-
pointer-events: none;
48-
`}
4943
`;
5044

5145
export const Tab = styled((p: {

src/components/settings/settings-page.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,11 @@ const AccountContactFooter = styled.div`
8686
}
8787
`;
8888

89-
const ThemeAutoButton = styled(SettingsButton)`
90-
margin: 10px 0;
91-
`;
92-
9389
const ThemeColors = styled.div`
9490
display: grid;
9591
grid-template-columns: 1fr 1fr;
9692
border: 3px solid #999;
97-
margin: 0 20px;
93+
margin: auto 20px;
9894
`;
9995

10096
const ThemeColorBlock = styled.div<{ themeColor: keyof Theme }>`
@@ -107,6 +103,7 @@ const EditorContainer = styled.div`
107103
border: 3px solid #999;
108104
height: 300px;
109105
flex-grow: 1;
106+
margin: auto 0;
110107
`;
111108

112109
const AccountUpdateSpinner = styled(Icon).attrs(() => ({
@@ -290,21 +287,23 @@ class SettingsPage extends React.Component<SettingsPageProps> {
290287
Themes
291288
</CollapsibleCardHeading>
292289
</header>
293-
<ThemeAutoButton onClick={uiStore.toggleAutoTheme}>
294-
{uiStore.autoTheme ? 'Use specific theme' : 'Use automatic theme'}
295-
</ThemeAutoButton>
296290
<TabbedOptionsContainer>
297291
<TabsContainer
298-
disabled={uiStore.autoTheme}
299-
onClick={(value: ThemeName | Theme) => uiStore.setTheme(value)}
300-
isSelected={(value: ThemeName | Theme) => {
292+
onClick={(value: ThemeName | 'automatic' | Theme) => uiStore.setTheme(value)}
293+
isSelected={(value: ThemeName | 'automatic' | Theme) => {
301294
if (typeof value === 'string') {
302-
return uiStore.themeName === value
295+
return uiStore.themeName === value;
303296
} else {
304297
return _.isEqual(value, uiStore.theme);
305298
}
306299
}}
307300
>
301+
<Tab
302+
icon={['fas', 'magic']}
303+
value='automatic'
304+
>
305+
Automatic
306+
</Tab>
308307
<Tab
309308
icon={['fas', 'sun']}
310309
value='light'

src/icons.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { faExclamationTriangle } from '@fortawesome/free-solid-svg-icons/faExcla
3737
import { faLightbulb } from '@fortawesome/free-solid-svg-icons/faLightbulb';
3838
import { faCog } from '@fortawesome/free-solid-svg-icons/faCog';
3939
import { faStar } from '@fortawesome/free-regular-svg-icons/faStar';
40+
import { faMagic } from '@fortawesome/free-solid-svg-icons/faMagic';
4041
import { faSun } from '@fortawesome/free-solid-svg-icons/faSun';
4142
import { faMoon } from '@fortawesome/free-solid-svg-icons/faMoon';
4243
import { faAdjust } from '@fortawesome/free-solid-svg-icons/faAdjust';
@@ -114,6 +115,7 @@ library.add(
114115
faLightbulb,
115116
faCog,
116117
faStar,
118+
faMagic,
117119
faSun,
118120
faMoon,
119121
faAdjust,

src/model/ui/ui-store.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ export class UiStore {
112112
// closed), but don't get reset when the app starts with stale account data.
113113
observe(this.accountStore, 'accountDataLastUpdated', () => {
114114
if (!this.accountStore.isPaidUser) {
115-
this.autoTheme = false;
116115
this.setTheme('light');
117116
}
118117
});
@@ -123,18 +122,16 @@ export class UiStore {
123122
});
124123

125124
const darkThemeMq = window.matchMedia("(prefers-color-scheme: dark)");
126-
this._prefersDarkTheme = darkThemeMq.matches;
127-
this._updateAutoTheme();
125+
this._setPrefersDarkTheme(darkThemeMq.matches);
128126
darkThemeMq.addEventListener('change', e => {
129-
this._prefersDarkTheme = e.matches;
130-
this._updateAutoTheme();
127+
this._setPrefersDarkTheme(e.matches);
131128
});
132129

133130
console.log('UI store initialized');
134131
});
135132

136133
@action.bound
137-
setTheme(themeNameOrObject: Theme | ThemeName) {
134+
setTheme(themeNameOrObject: Theme | ThemeName | 'automatic') {
138135
if (typeof themeNameOrObject === 'string') {
139136
this._themeName = themeNameOrObject;
140137
this.customTheme = undefined;
@@ -145,42 +142,37 @@ export class UiStore {
145142
}
146143

147144
@persist @observable
148-
private _themeName: ThemeName | 'custom' = 'light';
145+
private _themeName: ThemeName | 'automatic' | 'custom' = 'light';
149146

150147
get themeName() {
151148
return this._themeName;
152149
}
153150

154-
@persist('object') @observable
155-
private customTheme: Theme | undefined = undefined;
156-
157-
@computed
158-
get theme(): Theme {
159-
if (this.themeName === 'custom') {
160-
return this.customTheme!;
161-
} else {
162-
return Themes[this.themeName];
163-
}
164-
}
165-
166151
/**
167152
* Stores if user prefers a dark color theme (for example when set in system settings).
168153
* Used if automatic theme is enabled.
169154
*/
155+
@observable
170156
private _prefersDarkTheme: boolean = false;
171157

172-
@persist @observable
173-
autoTheme: boolean = false;
174-
175158
@action.bound
176-
toggleAutoTheme() {
177-
this.autoTheme = !this.autoTheme
178-
this._updateAutoTheme();
159+
private _setPrefersDarkTheme(value: boolean) {
160+
this._prefersDarkTheme = value;
179161
}
180162

181-
private _updateAutoTheme() {
182-
if (!this.autoTheme) return;
183-
this.setTheme(this._prefersDarkTheme ? 'dark' : 'light');
163+
@persist('object') @observable
164+
private customTheme: Theme | undefined = undefined;
165+
166+
@computed
167+
get theme(): Theme {
168+
switch(this.themeName) {
169+
case 'automatic':
170+
return {...Themes[this._prefersDarkTheme ? 'dark' : 'light']}
171+
case 'custom':
172+
return this.customTheme!;
173+
default:
174+
return Themes[this.themeName];
175+
}
184176
}
185177

186178
// Set briefly at the moment any card expansion is toggled, to trigger animation for the expansion as it's

0 commit comments

Comments
 (0)