Skip to content

Commit 0c23271

Browse files
committed
Add Dark mode checkbox
1 parent dbaa2b9 commit 0c23271

File tree

6 files changed

+106
-8
lines changed

6 files changed

+106
-8
lines changed

locales/en-US/app.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ Home--additional-content-content = You can <strong>drag and drop</strong> a prof
364364
Home--compare-recordings-info = You can also compare recordings. <a>Open the comparing interface.</a>
365365
Home--your-recent-uploaded-recordings-title = Your recent uploaded recordings
366366
367+
Home--dark-mode-title = Dark mode
368+
367369
# We replace the elements such as <perf> and <simpleperf> with links to the
368370
# documentation to use these tools.
369371
Home--load-files-from-other-tools2 =

src/components/app/Home.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@
8888
grid-column: 1 / -1; /* from start to end */
8989
}
9090

91+
.homeDarkModeBox {
92+
position: absolute;
93+
top: 4px;
94+
left: 4px;
95+
}
96+
97+
#home-dark-mode {
98+
vertical-align: middle;
99+
}
100+
91101
.homeActions > p:first-child,
92102
.homeRecentUploadedRecordingsTitle {
93103
margin-top: 0;

src/components/app/Home.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import { InnerNavigationLink } from 'firefox-profiler/components/shared/InnerNav
99
import { ListOfPublishedProfiles } from './ListOfPublishedProfiles';
1010

1111
import explicitConnect from 'firefox-profiler/utils/connect';
12+
import {
13+
isDarkMode,
14+
setLightMode,
15+
setDarkMode,
16+
} from 'firefox-profiler/utils/dark-mode';
1217
import PerfScreenshot from 'firefox-profiler-res/img/jpg/perf-screenshot-2021-05-06.jpg';
1318
import FirefoxPopupScreenshot from 'firefox-profiler-res/img/jpg/firefox-profiler-button-2021-05-06.jpg';
1419
import {
@@ -214,6 +219,7 @@ type HomeProps = ConnectedProps<
214219

215220
type HomeState = {
216221
popupInstallPhase: PopupInstallPhase;
222+
darkMode: boolean;
217223
};
218224

219225
type PopupInstallPhase =
@@ -256,6 +262,7 @@ class HomeImpl extends React.PureComponent<HomeProps, HomeState> {
256262

257263
this.state = {
258264
popupInstallPhase: popupInstallPhase as PopupInstallPhase,
265+
darkMode: isDarkMode(),
259266
};
260267
}
261268

@@ -574,8 +581,18 @@ class HomeImpl extends React.PureComponent<HomeProps, HomeState> {
574581
this.props.triggerLoadingFromUrl(url);
575582
};
576583

584+
_onDarkModeChange = (event: React.ChangeEvent<HTMLInputElement>) => {
585+
if (event.target.checked) {
586+
setDarkMode();
587+
} else {
588+
setLightMode();
589+
}
590+
this.setState({ darkMode: isDarkMode() });
591+
};
592+
577593
override render() {
578594
const { specialMessage } = this.props;
595+
const { darkMode } = this.state;
579596

580597
return (
581598
<div className="home">
@@ -686,6 +703,20 @@ class HomeImpl extends React.PureComponent<HomeProps, HomeState> {
686703
{/* End of grid container */}
687704
</section>
688705
<DragAndDropOverlay />
706+
<div className="homeDarkModeBox">
707+
<label className="photon-label" htmlFor="home-dark-mode">
708+
<input
709+
id="home-dark-mode"
710+
className="photon-checkbox photon-checkbox-default"
711+
type="checkbox"
712+
checked={darkMode}
713+
onChange={this._onDarkModeChange}
714+
/>
715+
<Localized id="Home--dark-mode-title">
716+
<span>Dark mode</span>
717+
</Localized>
718+
</label>
719+
</div>
689720
</main>
690721
</div>
691722
);

src/index.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ function initTheme() {
3131
document.documentElement.classList.add('dark-mode');
3232
}
3333
}
34-
window.useDarkMode = function () {
35-
window.localStorage.setItem('theme', 'dark');
36-
document.documentElement.classList.add('dark-mode');
37-
};
38-
window.useLightMode = function () {
39-
window.localStorage.removeItem('theme');
40-
document.documentElement.classList.remove('dark-mode');
41-
};
4234
try {
4335
initTheme();
4436
} catch (e) {

src/test/components/__snapshots__/Home.test.tsx.snap

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,23 @@ own importer
226226
Drop a saved profile here
227227
</div>
228228
</div>
229+
<div
230+
class="homeDarkModeBox"
231+
>
232+
<label
233+
class="photon-label"
234+
for="home-dark-mode"
235+
>
236+
<input
237+
class="photon-checkbox photon-checkbox-default"
238+
id="home-dark-mode"
239+
type="checkbox"
240+
/>
241+
<span>
242+
Dark mode
243+
</span>
244+
</label>
245+
</div>
229246
</main>
230247
</div>
231248
`;
@@ -511,6 +528,23 @@ own importer
511528
Drop a saved profile here
512529
</div>
513530
</div>
531+
<div
532+
class="homeDarkModeBox"
533+
>
534+
<label
535+
class="photon-label"
536+
for="home-dark-mode"
537+
>
538+
<input
539+
class="photon-checkbox photon-checkbox-default"
540+
id="home-dark-mode"
541+
type="checkbox"
542+
/>
543+
<span>
544+
Dark mode
545+
</span>
546+
</label>
547+
</div>
514548
</main>
515549
</div>
516550
`;
@@ -736,6 +770,23 @@ own importer
736770
Drop a saved profile here
737771
</div>
738772
</div>
773+
<div
774+
class="homeDarkModeBox"
775+
>
776+
<label
777+
class="photon-label"
778+
for="home-dark-mode"
779+
>
780+
<input
781+
class="photon-checkbox photon-checkbox-default"
782+
id="home-dark-mode"
783+
type="checkbox"
784+
/>
785+
<span>
786+
Dark mode
787+
</span>
788+
</label>
789+
</div>
739790
</main>
740791
</div>
741792
`;

src/utils/dark-mode.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,15 @@ export function maybeLightDark(value: string | [string, string]): string {
3737
}
3838
return lightDark(value[0], value[1]);
3939
}
40+
41+
export function setDarkMode() {
42+
_isDarkMode = true;
43+
window.localStorage.setItem('theme', 'dark');
44+
document.documentElement.classList.add('dark-mode');
45+
}
46+
47+
export function setLightMode() {
48+
_isDarkMode = false;
49+
window.localStorage.removeItem('theme');
50+
document.documentElement.classList.remove('dark-mode');
51+
}

0 commit comments

Comments
 (0)