Skip to content

Commit

Permalink
feat: dark/light mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tchapi committed Sep 3, 2023
1 parent 0404d8b commit 2c99add
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 10 deletions.
6 changes: 0 additions & 6 deletions public/css/bootstrap-light-prefers-dark.min.css

This file was deleted.

80 changes: 80 additions & 0 deletions public/js/color.mode.toggler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* Based on Bootstrap' color mode toggler */
/*!
* 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 = () => {
const storedTheme = getStoredTheme()
if (storedTheme) {
return storedTheme
}

return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
}

const setTheme = theme => {
if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-bs-theme', 'dark')
} 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')
const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"] .theme-icon`)

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

btnToActive.classList.add('active')
btnToActive.setAttribute('aria-pressed', 'true')
activeThemeIcon.innerHTML = btnToActive.innerHTML
const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})`
themeSwitcher.setAttribute('aria-label', themeSwitcherLabel)

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

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

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)
})
})
})
})()
28 changes: 25 additions & 3 deletions templates/_partials/navigation.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<nav class="navbar fixed-top navbar-expand-lg bg-body-tertiary">
<div class="container">
<a class="navbar-brand" href="#">
<a class="navbar-brand" href="{{ path('dashboard') }}">
<img src="/images/logo.png" width="30" height="30" alt=""> Davis
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="{{ "toggle.navigation"|trans }}">
Expand All @@ -18,13 +18,35 @@
{% if app.user %}
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<a class="nav-link dropdown-toggle" href="#" id="navUserMenu" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
👤 {{ app.user.username }}
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<div class="dropdown-menu" aria-labelledby="navUserMenu">
<a class="dropdown-item" href="{{ path('app_logout') }}">{{ "logout"|trans }}</a>
</div>
</li>
<li class="nav-item dropdown">
<button class="nav-link py-2 px-0 px-lg-2 dropdown-toggle d-flex align-items-center" id="bd-theme" type="button" aria-expanded="false" data-bs-toggle="dropdown" aria-label="{{ "toggle.theme"|trans }}">
<span class="theme-icon-active me-1">☀️</span> <span class="d-lg-none ms-2" id="bd-theme-text">{{ "toggle.theme"|trans }}</span>
</button>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="bd-theme-text" data-bs-popper="static">
<li>
<button type="button" class="dropdown-item d-flex align-items-center" data-bs-theme-value="light" aria-pressed="true">
<span class="theme-icon me-2">☀️</span> {{ "theme.light"|trans }}
</button>
</li>
<li>
<button type="button" class="dropdown-item d-flex align-items-center" data-bs-theme-value="dark" aria-pressed="false">
<span class="theme-icon me-2">🌙</span> {{ "theme.dark"|trans }}
</button>
</li>
<li>
<button type="button" class="dropdown-item d-flex align-items-center" data-bs-theme-value="auto" aria-pressed="false">
<span class="theme-icon me-2">🔘</span> {{ "theme.auto"|trans }}
</button>
</li>
</ul>
</li>
</ul>
{% endif %}
</div>
Expand Down
1 change: 1 addition & 0 deletions templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<div class="container">
{% block body %}{% endblock %}
</div>
<script type="text/javascript" src="/js/color.mode.toggler.js"></script>
<script type="text/javascript" src="/js/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="/js/app.js"></script>
</body>
Expand Down
3 changes: 2 additions & 1 deletion templates/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
width: 240px;
}
</style>
<script type="text/javascript" src="/js/color.mode.toggler.js"></script>
</head>
<body>
<div class="hero">
Expand All @@ -37,7 +38,7 @@
{% if webDAVEnabled %}<span class="badge bg-success rounded-pill">{{ "enabled"|trans }}</span>{% else %}<span class="badge bg-danger rounded-pill">{{ "disabled"|trans }}</span>{% endif %}
</li>
</ul>
<a href="{{ path('dashboard') }}" class="btn btn-light mt-4">{{ "admin.interface"|trans }}</a>
<a href="{{ path('dashboard') }}" class="btn btn-sm btn-outline-secondary mt-4">{{ "admin.interface"|trans }}</a>
</div>
</body>
</html>
16 changes: 16 additions & 0 deletions translations/messages+intl-icu.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@
<source>toggle.navigation</source>
<target>Toggle navigation</target>
</trans-unit>
<trans-unit id="q3rXQbK" resname="toggle.theme">
<source>toggle.theme</source>
<target>Toggle theme</target>
</trans-unit>
<trans-unit id="a.8gbaH" resname="theme.light">
<source>theme.light</source>
<target>Light</target>
</trans-unit>
<trans-unit id="PWATBYO" resname="theme.dark">
<source>theme.dark</source>
<target>Dark</target>
</trans-unit>
<trans-unit id="kIumP2R" resname="theme.auto">
<source>theme.auto</source>
<target>Auto</target>
</trans-unit>
<trans-unit id="PMgigwp" resname="title.dashboard">
<source>title.dashboard</source>
<target>Dashboard</target>
Expand Down

0 comments on commit 2c99add

Please sign in to comment.