mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-19 07:21:19 +00:00
Docs auto dark mode (#20188)
* Implement auto dark mode * Rename class * Fix borders cutout * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
d8d86e07c3
commit
70a8e0d65c
@@ -135,12 +135,13 @@ doc.body_toc_groupsection = """
|
||||
doc.body_toc_group = """
|
||||
<div class="row">
|
||||
<div class="three columns">
|
||||
<div class="theme-switch-wrapper">
|
||||
<label class="theme-switch" for="checkbox">
|
||||
<input type="checkbox" id="checkbox" />
|
||||
<div class="slider round"></div>
|
||||
</label>
|
||||
<em>Dark Mode</em>
|
||||
<div class="theme-select-wrapper">
|
||||
<label for="theme-select">Theme: </label>
|
||||
<select id="theme-select" onchange="setTheme(this.value)">
|
||||
<option value="auto">🌗 Match OS</option>
|
||||
<option value="dark">🌑 Dark</option>
|
||||
<option value="light">🌕 Light</option>
|
||||
</select>
|
||||
</div>
|
||||
<div id="global-links">
|
||||
<ul class="simple-boot">
|
||||
@@ -174,12 +175,13 @@ doc.body_toc_group = """
|
||||
doc.body_toc_group = """
|
||||
<div class="row">
|
||||
<div class="three columns">
|
||||
<div class="theme-switch-wrapper">
|
||||
<label class="theme-switch" for="checkbox">
|
||||
<input type="checkbox" id="checkbox" />
|
||||
<div class="slider round"></div>
|
||||
</label>
|
||||
<em>Dark Mode</em>
|
||||
<div class="theme-select-wrapper">
|
||||
<label for="theme-select">Theme: </label>
|
||||
<select id="theme-select" onchange="setTheme(this.value)">
|
||||
<option value="auto">🌗 Match OS</option>
|
||||
<option value="dark">🌑 Dark</option>
|
||||
<option value="light">🌕 Light</option>
|
||||
</select>
|
||||
</div>
|
||||
<div id="global-links">
|
||||
<ul class="simple">
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -27,12 +27,13 @@
|
||||
<h1 class="title">Not a Nim Manual</h1>
|
||||
<div class="row">
|
||||
<div class="three columns">
|
||||
<div class="theme-switch-wrapper">
|
||||
<label class="theme-switch" for="checkbox">
|
||||
<input type="checkbox" id="checkbox" />
|
||||
<div class="slider round"></div>
|
||||
</label>
|
||||
<em>Dark Mode</em>
|
||||
<div class="theme-select-wrapper">
|
||||
<label for="theme-select">Theme: </label>
|
||||
<select id="theme-select" onchange="setTheme(this.value)">
|
||||
<option value="auto">🌗 Match OS</option>
|
||||
<option value="dark">🌑 Dark</option>
|
||||
<option value="light">🌕 Light</option>
|
||||
</select>
|
||||
</div>
|
||||
<div id="global-links">
|
||||
<ul class="simple">
|
||||
|
||||
@@ -27,12 +27,13 @@
|
||||
<h1 class="title">nimdoc/test_out_index_dot_html/foo</h1>
|
||||
<div class="row">
|
||||
<div class="three columns">
|
||||
<div class="theme-switch-wrapper">
|
||||
<label class="theme-switch" for="checkbox">
|
||||
<input type="checkbox" id="checkbox" />
|
||||
<div class="slider round"></div>
|
||||
</label>
|
||||
<em>Dark Mode</em>
|
||||
<div class="theme-select-wrapper">
|
||||
<label for="theme-select">Theme: </label>
|
||||
<select id="theme-select" onchange="setTheme(this.value)">
|
||||
<option value="auto">🌗 Match OS</option>
|
||||
<option value="dark">🌑 Dark</option>
|
||||
<option value="light">🌕 Light</option>
|
||||
</select>
|
||||
</div>
|
||||
<div id="global-links">
|
||||
<ul class="simple">
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -27,12 +27,13 @@
|
||||
<h1 class="title">subdir/subdir_b/utils</h1>
|
||||
<div class="row">
|
||||
<div class="three columns">
|
||||
<div class="theme-switch-wrapper">
|
||||
<label class="theme-switch" for="checkbox">
|
||||
<input type="checkbox" id="checkbox" />
|
||||
<div class="slider round"></div>
|
||||
</label>
|
||||
<em>Dark Mode</em>
|
||||
<div class="theme-select-wrapper">
|
||||
<label for="theme-select">Theme: </label>
|
||||
<select id="theme-select" onchange="setTheme(this.value)">
|
||||
<option value="auto">🌗 Match OS</option>
|
||||
<option value="dark">🌑 Dark</option>
|
||||
<option value="light">🌕 Light</option>
|
||||
</select>
|
||||
</div>
|
||||
<div id="global-links">
|
||||
<ul class="simple">
|
||||
|
||||
@@ -27,12 +27,13 @@
|
||||
<h1 class="title">testproject</h1>
|
||||
<div class="row">
|
||||
<div class="three columns">
|
||||
<div class="theme-switch-wrapper">
|
||||
<label class="theme-switch" for="checkbox">
|
||||
<input type="checkbox" id="checkbox" />
|
||||
<div class="slider round"></div>
|
||||
</label>
|
||||
<em>Dark Mode</em>
|
||||
<div class="theme-select-wrapper">
|
||||
<label for="theme-select">Theme: </label>
|
||||
<select id="theme-select" onchange="setTheme(this.value)">
|
||||
<option value="auto">🌗 Match OS</option>
|
||||
<option value="dark">🌑 Dark</option>
|
||||
<option value="light">🌕 Light</option>
|
||||
</select>
|
||||
</div>
|
||||
<div id="global-links">
|
||||
<ul class="simple">
|
||||
|
||||
@@ -2,16 +2,19 @@ import dom
|
||||
import fuzzysearch
|
||||
|
||||
|
||||
proc switchTheme(event: Event) =
|
||||
if event.target.checked:
|
||||
document.documentElement.setAttribute("data-theme", "dark")
|
||||
window.localStorage.setItem("theme", "dark")
|
||||
else:
|
||||
document.documentElement.setAttribute("data-theme", "light")
|
||||
window.localStorage.setItem("theme", "light")
|
||||
proc setTheme(theme: cstring) {.exportc.} =
|
||||
document.documentElement.setAttribute("data-theme", theme)
|
||||
window.localStorage.setItem("theme", theme)
|
||||
|
||||
# set `data-theme` attribute early to prevent white flash
|
||||
setTheme:
|
||||
let t = window.localStorage.getItem("theme")
|
||||
if t.isNil: cstring"auto" else: t
|
||||
|
||||
proc onDOMLoaded(e: Event) {.exportc.} =
|
||||
# set theme select value
|
||||
document.getElementById("theme-select").value = window.localStorage.getItem("theme")
|
||||
|
||||
proc nimThemeSwitch(event: Event) {.exportC.} =
|
||||
var pragmaDots = document.getElementsByClassName("pragmadots")
|
||||
for i in 0..<pragmaDots.len:
|
||||
pragmaDots[i].onclick = proc (event: Event) =
|
||||
@@ -20,25 +23,10 @@ proc nimThemeSwitch(event: Event) {.exportC.} =
|
||||
# Show actual
|
||||
event.target.parentNode.nextSibling.style.display = "inline"
|
||||
|
||||
let toggleSwitch = document.querySelector(".theme-switch input[type=\"checkbox\"]")
|
||||
|
||||
if toggleSwitch != nil:
|
||||
toggleSwitch.addEventListener("change", switchTheme, false)
|
||||
proc textContent(e: Element): cstring {.importcpp: "#.textContent", nodecl.}
|
||||
|
||||
var currentTheme = window.localStorage.getItem("theme")
|
||||
if currentTheme.len == 0 and window.matchMedia("(prefers-color-scheme: dark)").matches:
|
||||
currentTheme = "dark"
|
||||
if currentTheme.len > 0:
|
||||
document.documentElement.setAttribute("data-theme", currentTheme);
|
||||
|
||||
if currentTheme == "dark" and toggleSwitch != nil:
|
||||
toggleSwitch.checked = true
|
||||
|
||||
proc textContent(e: Element): cstring {.
|
||||
importcpp: "#.textContent", nodecl.}
|
||||
|
||||
proc textContent(e: Node): cstring {.
|
||||
importcpp: "#.textContent", nodecl.}
|
||||
proc textContent(e: Node): cstring {.importcpp: "#.textContent", nodecl.}
|
||||
|
||||
proc tree(tag: string; kids: varargs[Element]): Element =
|
||||
result = document.createElement tag
|
||||
@@ -431,4 +419,4 @@ proc copyToClipboard*() {.exportc.} =
|
||||
.}
|
||||
|
||||
copyToClipboard()
|
||||
window.addEventListener("DOMContentLoaded", nimThemeSwitch)
|
||||
window.addEventListener("DOMContentLoaded", onDOMLoaded)
|
||||
|
||||
Reference in New Issue
Block a user