Skip to content

Commit

Permalink
Only use lightswitch js if lightswitch is present (r-lib#2594)
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored and SebKrantz committed Jun 1, 2024
1 parent 98c2a2c commit bdb2857
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 85 deletions.
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}}
<script src="{{#site}}{{root}}{{/site}}lightswitch.js"></script>
{{/lightswitch}}

{{{headdeps}}}

<!-- pkgdown -->
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/_snaps/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
init_site(pkg)
Message
-- Initialising site -----------------------------------------------------------
Copying <pkgdown>/BS5/assets/lightswitch.js to lightswitch.js
Copying <pkgdown>/BS5/assets/link.svg to link.svg
Copying <pkgdown>/BS5/assets/pkgdown.js to pkgdown.js
Copying pkgdown/extra.css to extra.css
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/_snaps/render.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
footer:
left: <p>Developed by Hadley Wickham, RStudio.</p>
right: <p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> {version}.</p>
lightswitch: no


# check_opengraph validates inputs
Expand Down

0 comments on commit bdb2857

Please sign in to comment.