Skip to content

Commit

Permalink
Update bootstrap and add dark mode toggle (#89)
Browse files Browse the repository at this point in the history
* Update Bootstrap library to 5.3 -- Also update jQuery and jquery-validate

* Add dark-mode preference toggle

* Delete CodeQL Scan workflow -- Workflow replaced with built-in GitHub CodeQL analysis
  • Loading branch information
dougwaldron authored Sep 29, 2024
1 parent 6f59365 commit f416fd3
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 65 deletions.
50 changes: 0 additions & 50 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

70 changes: 61 additions & 9 deletions PodcastRewind/Pages/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
@using PodcastRewind.Models
@{
var iconsFile = Url.Content("~/img/app-icons.svg");
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"]</title>
<link rel="stylesheet" href="~/lib/bootstrap/css/bootstrap.min.css"/>
<link rel="stylesheet" href="~/css/site.css"/>
<link rel="stylesheet" href="~/lib/bootstrap/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />

<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
Expand All @@ -18,14 +21,62 @@
<body>
<a class="skip-nav" href="#main-content">Skip to main content</a>
<header>
<nav class="navbar navbar-light bg-primary bg-opacity-25 border-bottom shadow-sm mb-3">
<nav class="navbar navbar-expand border-bottom bg-primary bg-opacity-10 shadow-sm mb-3">
<div class="container">
<a class="navbar-brand" asp-page="/Index" title="Home">
<span class="pr-logo">@Constants.Logo</span>
</a>
<div class="bg-light rounded-2">
<a class="btn btn-primary border-2" asp-page="/Search">Create a New Podcast Rewind</a>
</div>

<ul class="navbar-nav">
<li class="nav-item">
<a class="btn btn-primary border-2" asp-page="/Search">Create a New Podcast Rewind</a>
</li>
<!-- Dropdown selector for color mode -->
<li class="nav-item dropdown">
<button class="nav-link dropdown-toggle" id="bd-theme" type="button"
aria-haspopup="menu" aria-expanded="false" data-bs-toggle="dropdown" data-bs-display="static" title="Toggle theme">
<svg class="bi me-1 theme-icon-active">
<use href="@iconsFile#app-icon-circle-half"></use>
</svg>
<span class="ms-2 visually-hidden" id="bd-theme-text">Toggle theme</span>
</button>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="bd-theme">
<li>
<button type="button" class="dropdown-item d-flex align-items-center active" data-bs-theme-value="light">
<svg class="bi me-2 opacity-50">
<use href="@iconsFile#app-icon-sun-fill"></use>
</svg>
Light
<svg class="bi ms-auto theme-checkmark d-none">
<use href="@iconsFile#app-icon-check2"></use>
</svg>
</button>
</li>
<li>
<button type="button" class="dropdown-item d-flex align-items-center" data-bs-theme-value="dark">
<svg class="bi me-2 opacity-50">
<use href="@iconsFile#app-icon-moon-stars-fill"></use>
</svg>
Dark
<svg class="bi ms-auto theme-checkmark d-none">
<use href="@iconsFile#app-icon-check2"></use>
</svg>
</button>
</li>
<li>
<button type="button" class="dropdown-item d-flex align-items-center active" data-bs-theme-value="auto">
<svg class="bi me-2 opacity-50">
<use href="@iconsFile#app-icon-circle-half"></use>
</svg>
Auto
<svg class="bi ms-auto theme-checkmark d-none">
<use href="@iconsFile#app-icon-check2"></use>
</svg>
</button>
</li>
</ul>
</li>
</ul>
</div>
</nav>
</header>
Expand All @@ -41,7 +92,7 @@
&#x2000;&middot;&#x2000;
<a asp-page="/Privacy">Privacy Policy</a>
&#x2000;&middot;&#x2000;
Source code on <a href="https://github.com/dougwaldron/podcast-rewind"><img class="align-baseline" src="/img/github.svg" alt="" aria-hidden="true"/> GitHub</a>
Source code on <a href="https://github.com/dougwaldron/podcast-rewind"><img class="align-baseline" src="/img/github.svg" alt="" aria-hidden="true" /> GitHub</a>
&#x2000;&middot;&#x2000;
Version @GetType().Assembly.GetName().Version?.ToString(3)
</div>
Expand All @@ -51,6 +102,7 @@
<script src="~/lib/jquery/jquery.min.js"></script>
<script src="~/lib/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js"></script>
<script src="~/js/color-modes.js"></script>

@await RenderSectionAsync("Scripts", required: false)
</body>
Expand Down
6 changes: 3 additions & 3 deletions PodcastRewind/libman.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"libraries": [
{
"provider": "cdnjs",
"library": "jquery@3.6.1",
"library": "jquery@3.7.1",
"destination": "wwwroot/lib/jquery/",
"files": ["jquery.min.js", "jquery.min.map", "jquery.js"]
},
{
"provider": "cdnjs",
"library": "bootstrap@5.2.2",
"library": "bootstrap@5.3.3",
"destination": "wwwroot/lib/bootstrap/",
"files": [
"js/bootstrap.bundle.min.js",
Expand All @@ -23,7 +23,7 @@
},
{
"provider": "cdnjs",
"library": "jquery-validate@1.19.5",
"library": "jquery-validate@1.21.0",
"destination": "wwwroot/lib/jquery-validate/",
"files": ["jquery.validate.min.js", "jquery.validate.js"]
},
Expand Down
10 changes: 10 additions & 0 deletions PodcastRewind/wwwroot/css/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ body {
top: 0;
transition: all 0.2s ease-in-out;
}

/* Bootstrap Icons
-------------------------------------------------- */
.bi {
width: 1em;
height: 1em;
fill: currentColor;
vertical-align: -.125em;
overflow: visible !important;
}
21 changes: 21 additions & 0 deletions PodcastRewind/wwwroot/img/app-icons.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions PodcastRewind/wwwroot/img/github.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions PodcastRewind/wwwroot/js/color-modes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*!
* Color mode toggler for Bootstrap's docs (https://getbootstrap.com/)
* Copyright 2011-2023 The Bootstrap Authors
* Licensed under the Creative Commons Attribution 3.0 Unported License.
*/

(() => {
'use strict'

const getStoredTheme = () => localStorage.getItem('theme')
const setStoredTheme = theme => localStorage.setItem('theme', theme)
const getPreferredTheme = () => getStoredTheme() || 'auto'

const setTheme = theme => {
if (theme === 'auto') {
document.documentElement.setAttribute('data-bs-theme', (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'))
} else {
document.documentElement.setAttribute('data-bs-theme', theme)
}
}

setTheme(getPreferredTheme())

const showActiveTheme = (theme, focus = false) => {
const themeSwitcher = document.querySelector('#bd-theme')

if (!themeSwitcher) {
return
}

const themeSwitcherText = document.querySelector('#bd-theme-text')
const activeThemeIcon = document.querySelector('.theme-icon-active use')
const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`)
const svgOfActiveBtn = btnToActive.querySelector('svg use').getAttribute('href')

document.querySelectorAll('[data-bs-theme-value]').forEach(element => {
element.classList.remove('active')
element.setAttribute('aria-pressed', 'false')
element.querySelector('.theme-checkmark').classList.add('d-none')
})

btnToActive.classList.add('active')
btnToActive.setAttribute('aria-pressed', 'true')
btnToActive.querySelector('.theme-checkmark').classList.remove('d-none')
activeThemeIcon.setAttribute('href', svgOfActiveBtn)
const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})`
themeSwitcher.setAttribute('aria-label', themeSwitcherLabel)
themeSwitcher.setAttribute('title', themeSwitcherLabel)

if (focus) {
themeSwitcher.focus()
}
}

const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
const listener = () => {
const storedTheme = getStoredTheme();
if (storedTheme !== 'light' && storedTheme !== 'dark') {
setTheme(getPreferredTheme());
}
};

if (mediaQuery.addEventListener) {
mediaQuery.addEventListener('change', listener);
} else if (mediaQuery.addListener) {
mediaQuery.addListener(listener);
}

window.addEventListener('DOMContentLoaded', () => {
showActiveTheme(getPreferredTheme())

document.querySelectorAll('[data-bs-theme-value]')
.forEach(toggle => {
toggle.addEventListener('click', () => {
const theme = toggle.getAttribute('data-bs-theme-value')
setStoredTheme(theme)
setTheme(theme)
showActiveTheme(theme, true)
})
})
})
})()

0 comments on commit f416fd3

Please sign in to comment.