Skip to content

Commit

Permalink
Theme switch button
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos committed Aug 13, 2024
1 parent 63b2f46 commit 138c7e2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/lib/frontend/templates/views/shared/site_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:pub_dev/frontend/request_context.dart';

import '../../../../account/models.dart' show SessionData;
import '../../../../shared/urls.dart' as urls;
import '../../../dom/dom.dart' as d;
Expand Down Expand Up @@ -109,12 +111,26 @@ d.Node siteHeaderNode({
_foldableMobileLinks('Dart', _dartLinks),
if (userSession != null && userSession.isAuthenticated)
_userBlock(userSession),
_themeSwitcher(),
],
),
],
);
}

d.Node _themeSwitcher() {
// <span id="dark-theme-button" class="material-symbols-outlined">dark_mode</span>
// <span id="light-theme-button" class="material-symbols-outlined">light_mode</span>
if (requestContext.experimentalFlags.isDarkModeEnabled) {
return d.fragment([
d.button(
classes: ['-pub-theme-toggle'], ariaLabel: 'light/dark theme toggle'),
]);
} else {
return d.fragment([]);
}
}

d.Node _userBlock(SessionData userSession) {
return d.div(
classes: ['nav-container', 'nav-profile-container', 'hoverable'],
Expand Down
14 changes: 14 additions & 0 deletions pkg/web_app/lib/script.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void main() {
window.onPageShow.listen((_) {
adjustQueryTextAfterPageShow();
});
_setupDarkThemeButton();
}

void _setupAllEvents() {
Expand All @@ -39,3 +40,16 @@ void _setupAllEvents() {
setupLikesList();
setupScreenshotCarousel();
}

void _setupDarkThemeButton() {
final button = document.querySelector('button.-pub-theme-toggle');
if (button != null) {
button.onClick.listen((_) {
final classes = document.body!.classes;
final isCurrentlyDark = classes.contains('dark-theme');
window.localStorage['colorTheme'] = isCurrentlyDark ? 'false' : 'true';
classes.toggle('dark-theme');
classes.toggle('light-theme');
});
}
}
12 changes: 12 additions & 0 deletions pkg/web_css/lib/src/_site_header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@
}
}

button.-pub-theme-toggle {
display: block;
background-color: transparent;
width: 24px;
background-position: center;
margin: 0 18px;
background-image: url("../img/light-mode-symbol-888888.svg");
background-size: 100%;
background-repeat: no-repeat;
opacity: 0.8;
}

a.logo {
display: inline-block;
height: 30px;
Expand Down
1 change: 1 addition & 0 deletions static/img/light-mode-symbol-888888.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 138c7e2

Please sign in to comment.