Skip to content

Commit a5f305b

Browse files
committed
feat: add color mode switch
1 parent 4baa10d commit a5f305b

File tree

7 files changed

+128
-2
lines changed

7 files changed

+128
-2
lines changed

build/postcss.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = ctx => ({
99
sourcesContent: true
1010
},
1111
plugins: {
12+
'postcss-drop-empty-css-vars': {},
1213
autoprefixer: {
1314
cascade: false
1415
},

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
"postcss": "^8.4.19",
120120
"postcss-cli": "^10.0.0",
121121
"postcss-combine-duplicated-selectors": "^10.0.3",
122+
"postcss-drop-empty-css-vars": "^0.0.0",
122123
"prettier": "2.8.0",
123124
"pug": "^3.0.2",
124125
"rimraf": "^3.0.2",

src/js/color-modes.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*!
2+
* Color mode toggler for CoreUI's docs (https://coreui.io/)
3+
* Copyright (c) 2023 creativeLabs Łukasz Holeczek
4+
* Licensed under the Creative Commons Attribution 3.0 Unported License.
5+
*/
6+
7+
(() => {
8+
'use strict'
9+
10+
const THEME = 'coreui-docs-theme'
11+
12+
const storedTheme = localStorage.getItem(THEME)
13+
14+
const getPreferredTheme = () => {
15+
if (storedTheme) {
16+
return storedTheme
17+
}
18+
19+
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
20+
}
21+
22+
const setTheme = function (theme) {
23+
if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) {
24+
document.documentElement.setAttribute('data-coreui-theme', 'dark')
25+
} else {
26+
document.documentElement.setAttribute('data-coreui-theme', theme)
27+
}
28+
}
29+
30+
setTheme(getPreferredTheme())
31+
32+
const showActiveTheme = theme => {
33+
const activeThemeIcon = document.querySelector('.theme-icon-active use')
34+
const btnToActive = document.querySelector(`[data-coreui-theme-value="${theme}"]`)
35+
const svgOfActiveBtn = btnToActive.querySelector('svg use').getAttribute('xlink:href')
36+
37+
for (const element of document.querySelectorAll('[data-coreui-theme-value]')) {
38+
element.classList.remove('active')
39+
}
40+
41+
btnToActive.classList.add('active')
42+
activeThemeIcon.setAttribute('xlink:href', svgOfActiveBtn)
43+
}
44+
45+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
46+
if (storedTheme !== 'light' || storedTheme !== 'dark') {
47+
setTheme(getPreferredTheme())
48+
}
49+
})
50+
51+
window.addEventListener('DOMContentLoaded', () => {
52+
showActiveTheme(getPreferredTheme())
53+
54+
for (const toggle of document.querySelectorAll('[data-coreui-theme-value]')) {
55+
toggle.addEventListener('click', () => {
56+
const theme = toggle.getAttribute('data-coreui-theme-value')
57+
localStorage.setItem(THEME, theme)
58+
setTheme(theme)
59+
showActiveTheme(theme)
60+
})
61+
}
62+
})
63+
})()

src/js/config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* --------------------------------------------------------------------------
3+
* CoreUI Boostrap Admin Template (v5.0.0-alpha.0): set-config.js
4+
* Licensed under MIT (https://github.com/coreui/coreui-free-bootstrap-admin-template/blob/main/LICENSE)
5+
* --------------------------------------------------------------------------
6+
*/
7+
8+
(() => {
9+
'use strict'
10+
11+
const urlParams = new URLSearchParams(window.location.href.split('?')[1])
12+
13+
if (urlParams.get('theme') && ['auto', 'dark', 'light'].includes(urlParams.get('theme'))) {
14+
localStorage.setItem('theme', urlParams.get('theme'))
15+
}
16+
})()

src/pug/_partials/head.pug

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ link(rel='stylesheet' href='css/vendors/simplebar.css')
3131
// Main styles for this application
3232
link(href='css/style.css', rel='stylesheet')
3333

34-
// We use those styles to show code examples, you should remove them in your application.
35-
link(rel='stylesheet' href='https://cdn.jsdelivr.net/npm/prismjs@1.23.0/themes/prism.css')
3634
link(href='css/examples.css', rel='stylesheet')
3735

36+
script(src='js/config.js')
37+
script(src='js/color-modes.js')
38+
3839
// Global site tag (gtag.js) - Google Analytics
3940
script(async='', src='https://www.googletagmanager.com/gtag/js?id=UA-118965717-3')
4041
script.

src/pug/_partials/header.pug

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ header.header.header-sticky.mb-4
3232
use(xlink:href="node_modules/@coreui/icons/sprites/free.svg#cil-envelope-open")
3333
|
3434
ul.header-nav.ms-3
35+
li.nav-item.py-2.py-lg-1
36+
.vr.h-100.mx-2.text-body.text-opacity-75
37+
li.nav-item.dropdown
38+
button.btn.btn-link.nav-link.py-2.px-2.d-flex.align-items-center(type="button" aria-expanded="false" data-coreui-toggle="dropdown")
39+
svg.icon.icon-lg.theme-icon-active
40+
use(xlink:href="node_modules/@coreui/icons/sprites/free.svg#cil-contrast")
41+
ul.dropdown-menu.dropdown-menu-end(aria-labelledby='bd-theme' style='--cui-dropdown-min-width: 8rem;')
42+
li
43+
button.dropdown-item.d-flex.align-items-center(type='button' data-coreui-theme-value='light')
44+
svg.icon.icon-lg.me-3
45+
use(xlink:href="node_modules/@coreui/icons/sprites/free.svg#cil-sun")
46+
| Light
47+
li
48+
button.dropdown-item.d-flex.align-items-center(type='button' data-coreui-theme-value='dark')
49+
svg.icon.icon-lg.me-3
50+
use(xlink:href="node_modules/@coreui/icons/sprites/free.svg#cil-moon")
51+
| Dark
52+
li
53+
button.dropdown-item.d-flex.align-items-center.active(type='button' data-coreui-theme-value='auto')
54+
svg.icon.icon-lg.me-3
55+
use(xlink:href="node_modules/@coreui/icons/sprites/free.svg#cil-contrast")
56+
| Auto
57+
li.nav-item.py-2.py-lg-1
58+
.vr.h-100.mx-2.text-body.text-opacity-75
3559
li.nav-item.dropdown
3660
a.nav-link.py-0(data-coreui-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false")
3761
.avatar.avatar-md

0 commit comments

Comments
 (0)