color-mode.astro 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. ---
  2. import Head from "@components/_head.astro"
  3. import Footer from "@components/dashboard/_footer.astro"
  4. import Topbar from "@components/dashboard/_topbar.astro"
  5. import Sidenav from "@components/dashboard/_sidenav.astro"
  6. import Scripts from "@components/_scripts.astro"
  7. import ColorMode from "@components/docs/color-mode.mdx"
  8. const title = "Color Mode | AdminLTE 4"
  9. const path = "../../../dist"
  10. const mainPage = "docs"
  11. const page = "color-mode";
  12. ---
  13. <!DOCTYPE html>
  14. <html lang="en">
  15. <!--begin::Head-->
  16. <head>
  17. <Head title={title} path={path} />
  18. </head>
  19. <!--end::Head-->
  20. <!--begin::Body-->
  21. <body class="layout-fixed sidebar-expand-lg bg-body-tertiary docs-page">
  22. <!--begin::App Wrapper-->
  23. <div class="app-wrapper">
  24. <Topbar path={path} />
  25. <Sidenav path={path} mainPage={mainPage} page={page} />
  26. <!--begin::App Main-->
  27. <main class="app-main">
  28. <!--begin::App Content Header-->
  29. <div class="app-content-header">
  30. <!--begin::Container-->
  31. <div class="container-fluid">
  32. <!--begin::Row-->
  33. <div class="row">
  34. <div class="col-sm-6">
  35. <h3 class="mb-0">Color Mode Toggle</h3>
  36. </div>
  37. <div class="col-sm-6">
  38. <ol class="breadcrumb float-sm-end">
  39. <li class="breadcrumb-item"><a href="#">Docs</a></li>
  40. <li class="breadcrumb-item active" aria-current="page">
  41. Color Mode
  42. </li>
  43. </ol>
  44. </div>
  45. </div>
  46. <!--end::Row-->
  47. </div>
  48. <!--end::Container-->
  49. </div>
  50. <!--end::App Content Header-->
  51. <!--begin::App Content-->
  52. <div class="app-content">
  53. <!--begin::Container-->
  54. <div class="container-fluid">
  55. <!--begin::Row-->
  56. <div class="row g-4">
  57. <div class="col-12">
  58. <div class="callout callout-info">
  59. <h5 class="fw-bold">Tips</h5>
  60. <p>
  61. <a href="https://getbootstrap.com/docs/5.3/customize/color-modes/" target="_blank" rel="noopener noreferrer" class="callout-link">
  62. Color modes in Bootstrap
  63. </a> allow you to switch between light and dark modes. You can do this using the data-bs-theme attribute. You can also create your own custom color modes.
  64. </p>
  65. </div>
  66. </div>
  67. <!-- Start column -->
  68. <div class="col-12">
  69. <!--begin::Header-->
  70. <nav class="navbar navbar-expand bg-body">
  71. <!--begin::Container-->
  72. <div class="container-fluid">
  73. <!--begin::Start Navbar links-->
  74. <ul class="navbar-nav">
  75. <li class="nav-item">
  76. <a
  77. class="nav-link"
  78. data-lte-toggle="sidebar"
  79. href="#"
  80. role="button"><i class="bi bi-list"></i></a
  81. >
  82. </li>
  83. <li class="nav-item d-none d-md-block">
  84. <a href="#" class="nav-link">Home</a>
  85. </li>
  86. <li class="nav-item d-none d-md-block">
  87. <a href="#" class="nav-link">Contact</a>
  88. </li>
  89. </ul>
  90. <!--end::Start Navbar links-->
  91. <!--begin::End Navbar links-->
  92. <ul class="navbar-nav ms-auto">
  93. <li class="nav-item dropdown">
  94. <button
  95. class="btn btn-link nav-link py-2 px-0 px-lg-2 dropdown-toggle d-flex align-items-center"
  96. id="bd-theme"
  97. type="button"
  98. aria-expanded="false"
  99. data-bs-toggle="dropdown"
  100. data-bs-display="static"
  101. >
  102. <span class="theme-icon-active">
  103. <i class="my-1"></i>
  104. </span>
  105. <span class="d-lg-none ms-2" id="bd-theme-text"
  106. >Toggle theme</span
  107. >
  108. </button>
  109. <ul
  110. class="dropdown-menu dropdown-menu-end"
  111. aria-labelledby="bd-theme-text"
  112. style="--bs-dropdown-min-width: 8rem;"
  113. >
  114. <li>
  115. <button
  116. type="button"
  117. class="dropdown-item d-flex align-items-center active"
  118. data-bs-theme-value="light"
  119. aria-pressed="false"
  120. >
  121. <i class="bi bi-sun-fill me-2"></i>
  122. Light
  123. <i class="bi bi-check-lg ms-auto d-none"></i>
  124. </button>
  125. </li>
  126. <li>
  127. <button
  128. type="button"
  129. class="dropdown-item d-flex align-items-center"
  130. data-bs-theme-value="dark"
  131. aria-pressed="false"
  132. >
  133. <i class="bi bi-moon-fill me-2"></i>
  134. Dark
  135. <i class="bi bi-check-lg ms-auto d-none"></i>
  136. </button>
  137. </li>
  138. <li>
  139. <button
  140. type="button"
  141. class="dropdown-item d-flex align-items-center"
  142. data-bs-theme-value="auto"
  143. aria-pressed="true"
  144. >
  145. <i class="bi bi-circle-half me-2"></i>
  146. Auto
  147. <i class="bi bi-check-lg ms-auto d-none"></i>
  148. </button>
  149. </li>
  150. </ul>
  151. </li>
  152. </ul>
  153. <!--end::End Navbar links-->
  154. </div>
  155. <!--end::Container-->
  156. </nav>
  157. <!--end::Header-->
  158. </div>
  159. <!-- Start column -->
  160. <div class="col-12">
  161. <ColorMode />
  162. </div>
  163. </div>
  164. <!--end::Row-->
  165. </div>
  166. <!--end::Container-->
  167. </div>
  168. <!--end::App Content-->
  169. </main>
  170. <!--end::App Main-->
  171. <Footer />
  172. </div>
  173. <!--end::App Wrapper-->
  174. <Scripts path={path} />
  175. <script is:inline>
  176. // Color Mode Toggler
  177. (() => {
  178. "use strict"
  179. const storedTheme = localStorage.getItem("theme")
  180. const getPreferredTheme = () => {
  181. if (storedTheme) {
  182. return storedTheme
  183. }
  184. return globalThis.matchMedia("(prefers-color-scheme: dark)").matches ?
  185. "dark" :
  186. "light"
  187. }
  188. const setTheme = function (theme) {
  189. if (
  190. theme === "auto" &&
  191. globalThis.matchMedia("(prefers-color-scheme: dark)").matches
  192. ) {
  193. document.documentElement.setAttribute("data-bs-theme", "dark")
  194. } else {
  195. document.documentElement.setAttribute("data-bs-theme", theme)
  196. }
  197. }
  198. setTheme(getPreferredTheme())
  199. const showActiveTheme = (theme, focus = false) => {
  200. const themeSwitcher = document.querySelector("#bd-theme")
  201. if (!themeSwitcher) {
  202. return
  203. }
  204. const themeSwitcherText = document.querySelector("#bd-theme-text")
  205. const activeThemeIcon = document.querySelector(
  206. ".theme-icon-active i"
  207. )
  208. const btnToActive = document.querySelector(
  209. `[data-bs-theme-value="${theme}"]`
  210. )
  211. const svgOfActiveBtn = btnToActive
  212. .querySelector("i")
  213. .getAttribute("class")
  214. for (const element of document.querySelectorAll(
  215. "[data-bs-theme-value]"
  216. )) {
  217. element.classList.remove("active")
  218. element.setAttribute("aria-pressed", "false")
  219. }
  220. btnToActive.classList.add("active")
  221. btnToActive.setAttribute("aria-pressed", "true")
  222. activeThemeIcon.setAttribute("class", svgOfActiveBtn)
  223. const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})`
  224. themeSwitcher.setAttribute("aria-label", themeSwitcherLabel)
  225. if (focus) {
  226. themeSwitcher.focus()
  227. }
  228. }
  229. globalThis
  230. .matchMedia("(prefers-color-scheme: dark)")
  231. .addEventListener("change", () => {
  232. if (storedTheme !== "light" || storedTheme !== "dark") {
  233. setTheme(getPreferredTheme())
  234. }
  235. })
  236. globalThis.addEventListener("DOMContentLoaded", () => {
  237. showActiveTheme(getPreferredTheme())
  238. for (const toggle of document.querySelectorAll(
  239. "[data-bs-theme-value]"
  240. )) {
  241. toggle.addEventListener("click", () => {
  242. const theme = toggle.getAttribute("data-bs-theme-value")
  243. localStorage.setItem("theme", theme)
  244. setTheme(theme)
  245. showActiveTheme(theme, true)
  246. })
  247. }
  248. })
  249. })()
  250. </script>
  251. <!--end::Script-->
  252. </body><!--end::Body-->
  253. </html>