Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use lightswitch js if lightswitch is present #2594

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ data_template <- function(pkg = ".", depth = 0L) {

out$navbar <- data_navbar(pkg, depth = depth)
out$footer <- data_footer(pkg)
out$lightswitch <- uses_lightswitch(pkg)

print_yaml(out)
}
Expand Down
85 changes: 85 additions & 0 deletions inst/BS5/assets/lightswitch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

/*!
* 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.
* Updates for {pkgdown} by the {bslib} authors, also licensed under CC-BY-3.0.
*/

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') {
document.documentElement.setAttribute('data-bs-theme', (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'))
} else {
document.documentElement.setAttribute('data-bs-theme', theme)
}
}

function bsSetupThemeToggle () {
'use strict'

const showActiveTheme = (theme, focus = false) => {
var activeLabel, activeIcon;

document.querySelectorAll('[data-bs-theme-value]').forEach(element => {
const buttonTheme = element.getAttribute('data-bs-theme-value')
const isActive = buttonTheme == theme

element.classList.toggle('active', isActive)
element.setAttribute('aria-pressed', isActive)

if (isActive) {
activeLabel = element.textContent;
activeIcon = element.querySelector('span').classList.value;
}
})

const themeSwitcher = document.querySelector('#dropdown-lightswitch')
if (!themeSwitcher) {
return
}

themeSwitcher.setAttribute('aria-label', activeLabel)
themeSwitcher.querySelector('span').classList.value = activeIcon;

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')
setTheme(theme)
setStoredTheme(theme)
showActiveTheme(theme, true)
})
})
})
}

setTheme(getPreferredTheme());
bsSetupThemeToggle();
85 changes: 0 additions & 85 deletions inst/BS5/assets/pkgdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,88 +152,3 @@ async function searchFuse(query, callback) {
});
});
})(window.jQuery || window.$)

/*!
* 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.
* Updates for {pkgdown} by the {bslib} authors, also licensed under CC-BY-3.0.
*/

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') {
document.documentElement.setAttribute('data-bs-theme', (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'))
} else {
document.documentElement.setAttribute('data-bs-theme', theme)
}
}

function bsSetupThemeToggle () {
'use strict'

const showActiveTheme = (theme, focus = false) => {
var activeLabel, activeIcon;

document.querySelectorAll('[data-bs-theme-value]').forEach(element => {
const buttonTheme = element.getAttribute('data-bs-theme-value')
const isActive = buttonTheme == theme

element.classList.toggle('active', isActive)
element.setAttribute('aria-pressed', isActive)

if (isActive) {
activeLabel = element.textContent;
activeIcon = element.querySelector('span').classList.value;
}
})

const themeSwitcher = document.querySelector('#dropdown-lightswitch')
if (!themeSwitcher) {
return
}

themeSwitcher.setAttribute('aria-label', activeLabel)
themeSwitcher.querySelector('span').classList.value = activeIcon;

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')
setTheme(theme)
setStoredTheme(theme)
showActiveTheme(theme, true)
})
})
})
}

setTheme(getPreferredTheme());
bsSetupThemeToggle();
4 changes: 4 additions & 0 deletions inst/BS5/templates/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<link rel="apple-touch-icon" type="image/png" sizes="60x60" href="{{#site}}{{root}}{{/site}}apple-touch-icon-60x60.png" />
{{/has_favicons}}

{{#lightswitch}}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has the added benefit of making this the first js loaded, meaning folks are less likely to get a flash of light content before the dark page loads.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consistent with a comment @gadenbuie made early in #2338.

<script src="{{#site}}{{root}}{{/site}}lightswitch.js"></script>
{{/lightswitch}}

{{{headdeps}}}

<!-- pkgdown -->
Expand Down
Loading