2
0
mirror of https://github.com/tenrok/bootstrap.git synced 2026-05-21 13:24:08 +03:00
Files
bootstrap/site/assets/js/application.js
T
Mark Otto b85169ef1c Add dark mode support
Heavily WIP still, but this begins the process of implementing dark mode for our docs and across the project itself.

- Color modes are toggled in the docs navbar with a custom toggler, which stores the select color mode in local storage.
- Color modes can also be set via data attribute thanks to `data-theme` (with light or dark options available currently).
- Docs are heavily WIP for demonstrating the dark mode.
- In order to best implement color modes, I've spiked out a number of new Sass and CSS variables (e.g., `--bs-secondary-bg` and `--bs-tertiary-bg`). In addition, I've added new global CSS variables like `--bs-border-color` and more. So, in addition to general color modes and theming support, we get greater real-time customization, too.

Todos and open questions:

- [ ] Do we refer to these as themes or color modes?
- [ ] Do we provide a color mode toggler JS plugin?
- [ ] Update all components to better utilize global CSS variables so they can be more easily themed (e.g., see `$dropdown-*` Sass variable changes in the diff).
2022-02-24 17:19:14 -08:00

246 lines
8.6 KiB
JavaScript

// NOTICE!! DO NOT USE ANY OF THIS JAVASCRIPT
// IT'S ALL JUST JUNK FOR OUR DOCS!
// ++++++++++++++++++++++++++++++++++++++++++
/*!
* JavaScript for Bootstrap's docs (https://getbootstrap.com/)
* Copyright 2011-2022 The Bootstrap Authors
* Copyright 2011-2022 Twitter, Inc.
* Licensed under the Creative Commons Attribution 3.0 Unported License.
* For details, see https://creativecommons.org/licenses/by/3.0/.
*/
/* global ClipboardJS: false, bootstrap: false */
(function () {
'use strict'
// Scroll the active sidebar link into view
var sidenav = document.querySelector('.bd-sidebar')
if (sidenav) {
var sidenavHeight = sidenav.clientHeight
var sidenavActiveLink = document.querySelector('.bd-links-nav .active')
var sidenavActiveLinkTop = sidenavActiveLink.offsetTop
var sidenavActiveLinkHeight = sidenavActiveLink.clientHeight
var viewportTop = sidenavActiveLinkTop
var viewportBottom = viewportTop - sidenavHeight + sidenavActiveLinkHeight
if (sidenav.scrollTop > viewportTop || sidenav.scrollTop < viewportBottom) {
sidenav.scrollTop = viewportTop - (sidenavHeight / 2) + (sidenavActiveLinkHeight / 2)
}
}
// Tooltip and popover demos
document.querySelectorAll('.tooltip-demo, .bd-navbar')
.forEach(function (tooltip) {
new bootstrap.Tooltip(tooltip, {
selector: '[data-bs-toggle="tooltip"]'
})
})
document.querySelectorAll('[data-bs-toggle="popover"]')
.forEach(function (popover) {
new bootstrap.Popover(popover)
})
var toastPlacement = document.getElementById('toastPlacement')
if (toastPlacement) {
document.getElementById('selectToastPlacement').addEventListener('change', function () {
if (!toastPlacement.dataset.originalClass) {
toastPlacement.dataset.originalClass = toastPlacement.className
}
toastPlacement.className = toastPlacement.dataset.originalClass + ' ' + this.value
})
}
document.querySelectorAll('.bd-example .toast')
.forEach(function (toastNode) {
var toast = new bootstrap.Toast(toastNode, {
autohide: false
})
toast.show()
})
var toastTrigger = document.getElementById('liveToastBtn')
var toastLiveExample = document.getElementById('liveToast')
if (toastTrigger) {
toastTrigger.addEventListener('click', function () {
var toast = new bootstrap.Toast(toastLiveExample)
toast.show()
})
}
var alertPlaceholder = document.getElementById('liveAlertPlaceholder')
var alertTrigger = document.getElementById('liveAlertBtn')
function alert(message, type) {
var wrapper = document.createElement('div')
wrapper.innerHTML = '<div class="alert alert-' + type + ' alert-dismissible" role="alert">' + message + '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>'
alertPlaceholder.append(wrapper)
}
if (alertTrigger) {
alertTrigger.addEventListener('click', function () {
alert('Nice, you triggered this alert message!', 'success')
})
}
// Demos within modals
document.querySelectorAll('.tooltip-test')
.forEach(function (tooltip) {
new bootstrap.Tooltip(tooltip)
})
document.querySelectorAll('.popover-test')
.forEach(function (popover) {
new bootstrap.Popover(popover)
})
// Indeterminate checkbox example
document.querySelectorAll('.bd-example-indeterminate [type="checkbox"]')
.forEach(function (checkbox) {
checkbox.indeterminate = true
})
// Disable empty links in docs examples
document.querySelectorAll('.bd-content [href="#"]')
.forEach(function (link) {
link.addEventListener('click', function (event) {
event.preventDefault()
})
})
// Modal relatedTarget demo
var exampleModal = document.getElementById('exampleModal')
if (exampleModal) {
exampleModal.addEventListener('show.bs.modal', function (event) {
// Button that triggered the modal
var button = event.relatedTarget
// Extract info from data-bs-* attributes
var recipient = button.getAttribute('data-bs-whatever')
// Update the modal's content.
var modalTitle = exampleModal.querySelector('.modal-title')
var modalBodyInput = exampleModal.querySelector('.modal-body input')
modalTitle.textContent = 'New message to ' + recipient
modalBodyInput.value = recipient
})
}
// Toggle color modes
var currentTheme = localStorage.getItem('theme')
var currentThemeIcon = document.querySelector('#bd-theme > .bi use')
function checkMatchMedia() {
if (window.matchMedia('(prefers-color-scheme: dark)').matches && !currentTheme) {
document.documentElement.setAttribute('data-theme', 'dark')
} else {
document.documentElement.removeAttribute('data-theme')
}
}
document.querySelectorAll('.bd-theme-toggles .dropdown-item')
.forEach(function (toggle) {
toggle.addEventListener('click', function () {
var theme = this.getAttribute('data-theme-value')
document.querySelector('.bd-theme-toggles .current').removeAttribute('class')
toggle.parentElement.setAttribute('class', 'current')
var svg = toggle.querySelector('.theme-icon use').getAttribute('href')
currentThemeIcon.setAttribute('href', svg)
console.log(theme)
// in OS darkmode, going from light to auto doesn't make it dark
if (theme === 'auto') {
document.documentElement.removeAttribute('data-theme')
localStorage.removeItem('theme')
checkMatchMedia()
} else {
document.documentElement.setAttribute('data-theme', theme)
localStorage.setItem('theme', theme)
}
})
})
if (currentTheme) {
var currentThemeButton = document.querySelector('.dropdown-item[data-theme-value="' + currentTheme + '"]')
document.documentElement.setAttribute('data-theme', currentTheme)
document.querySelector('.bd-theme-toggles .current').removeAttribute('class')
currentThemeButton.parentElement.setAttribute('class', 'current')
var svg = currentThemeButton.querySelector('.theme-icon use').getAttribute('href')
currentThemeIcon.setAttribute('href', svg)
} else {
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function () {
checkMatchMedia()
})
checkMatchMedia()
}
// Insert copy to clipboard button before .highlight
var btnTitle = 'Copy to clipboard'
var btnHtml = '<div class="bd-clipboard"><button type="button" class="btn-clipboard"><svg class="bi" width="1em" height="1em" fill="currentColor" role="img" aria-label="Copy"><use xlink:href="#clipboard"/></svg></button></div>'
document.querySelectorAll('div.highlight')
.forEach(function (element) {
element.insertAdjacentHTML('beforebegin', btnHtml)
})
document.querySelectorAll('.btn-clipboard')
.forEach(function (btn) {
var tooltipBtn = new bootstrap.Tooltip(btn, { title: btnTitle })
btn.addEventListener('mouseleave', function () {
// Explicitly hide tooltip, since after clicking it remains
// focused (as it's a button), so tooltip would otherwise
// remain visible until focus is moved away
tooltipBtn.hide()
})
})
var clipboard = new ClipboardJS('.btn-clipboard', {
target: function (trigger) {
return trigger.parentNode.nextElementSibling
}
})
clipboard.on('success', function (event) {
var iconFirstChild = event.trigger.querySelector('.bi').firstChild
var tooltipBtn = bootstrap.Tooltip.getInstance(event.trigger)
var namespace = 'http://www.w3.org/1999/xlink'
var originalXhref = iconFirstChild.getAttributeNS(namespace, 'href')
var originalTitle = event.trigger.title
tooltipBtn.setContent({ '.tooltip-inner': 'Copied!' })
event.trigger.addEventListener('hidden.bs.tooltip', function () {
tooltipBtn.setContent({ '.tooltip-inner': btnTitle })
}, { once: true })
event.clearSelection()
iconFirstChild.setAttributeNS(namespace, 'href', originalXhref.replace('clipboard', 'check2'))
setTimeout(function () {
iconFirstChild.setAttributeNS(namespace, 'href', originalXhref)
event.trigger.title = originalTitle
}, 2000)
})
clipboard.on('error', function (event) {
var modifierKey = /mac/i.test(navigator.userAgent) ? '\u2318' : 'Ctrl-'
var fallbackMsg = 'Press ' + modifierKey + 'C to copy'
var tooltipBtn = bootstrap.Tooltip.getInstance(event.trigger)
tooltipBtn.setContent({ '.tooltip-inner': fallbackMsg })
event.trigger.addEventListener('hidden.bs.tooltip', function () {
tooltipBtn.setContent({ '.tooltip-inner': btnTitle })
}, { once: true })
})
})()