Skip to content

Commit

Permalink
moving the apply of theme to header
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrrm committed Apr 11, 2024
1 parent c998562 commit 1a5b26d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
21 changes: 2 additions & 19 deletions src/NuGetGallery/Scripts/gallery/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,34 +536,17 @@

initializeJQueryValidator();

// Check the browser preferred color scheme
const prefersDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches;
const defaultTheme = prefersDarkMode ? "dark" : "light";
const preferredTheme = localStorage.getItem("theme")

// Check if the localStorage item is set, if not set it to the system theme
if (!preferredTheme || !(preferredTheme === "dark" || preferredTheme === "light")) {
localStorage.setItem("theme", "system");
}

if (preferredTheme === "light" || preferredTheme === "dark") {
document.documentElement.setAttribute('data-theme', preferredTheme);
}
else {
document.documentElement.setAttribute('data-theme', defaultTheme);
}

// Add listener to the theme selector
var themeSelector = document.getElementById("select-option-theme");
themeSelector.addEventListener("change", () => {
if (themeSelector.value === "system") {
localStorage.setItem("theme", "system");
document.documentElement.setAttribute('data-theme', defaultTheme);
document.body.setAttribute('data-theme', defaultTheme);
document.getElementById("user-prefered-theme").textContent = "System";
}
else {
localStorage.setItem("theme", themeSelector.value);
document.documentElement.setAttribute('data-theme', themeSelector.value);
document.body.setAttribute('data-theme', themeSelector.value);
document.getElementById("user-prefered-theme").textContent = themeSelector.value == "light" ? "Light" : "Dark";
}
})
Expand Down
19 changes: 19 additions & 0 deletions src/NuGetGallery/Views/Shared/Gallery/Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@

@Styles.Render("~/Content/gallery/css/site.min.css")

<script>
// Check the browser preferred color scheme
const prefersDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches;
const defaultTheme = prefersDarkMode ? "dark" : "light";
const preferredTheme = localStorage.getItem("theme")
// Check if the localStorage item is set, if not set it to the system theme
if (!preferredTheme || !(preferredTheme === "dark" || preferredTheme === "light")) {
localStorage.setItem("theme", "system");
}
if (preferredTheme === "light" || preferredTheme === "dark") {
document.documentElement.setAttribute('data-theme', preferredTheme);
}
else {
document.documentElement.setAttribute('data-theme', defaultTheme);
}
</script>

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
Expand Down
2 changes: 1 addition & 1 deletion src/NuGetGallery/Views/Shared/ListPackages.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
<div class="sortby col-md-3">
<div class="row">
<label for="sortby">Sort by</label>
<select name="sortby" id="sortby" form="search-form" aria-label="sort package search results by">
<select name="sortby" id="sortby" class="select-brand" form="search-form" aria-label="sort package search results by">
<option value="relevance" aria-label="Sort By: Relevance" @(Model.SortBy == "relevance" ? "selected" : "")>Relevance</option>
<option value="totalDownloads-desc" aria-label="Sort By: Downloads" @(Model.SortBy == "totalDownloads-desc" ? "selected" : "")>Downloads</option>
<option value="created-desc" aria-label="Sort By: Recently updated" @(Model.SortBy == "created-desc" ? "selected" : "")>Recently updated</option>
Expand Down

0 comments on commit 1a5b26d

Please sign in to comment.