Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<title>{title} - Rust cheat sheet</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" id="theme" href="theme-light.css">
<script src="script.js"></script>
<body>
{content}
<footer>
<ul>
<li><a href="?dark">Dark</a>
<li><a href="?single">Single</a>
<li><a id="theme-btn" href="#">Dark</a>
<li class="single-link"><a href="?single">Single</a>
<li><a href="?large">Large</a>
<li><a href="https://github.com/upsuper/rust-cheatsheet">GitHub</a>
</ul>
</footer>
<script src="script.js"></script>
</body>
37 changes: 35 additions & 2 deletions static/script.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const themeKey = 'theme';
let currentTheme = localStorage.getItem(themeKey);

const root = document.documentElement;
const args = location.search.slice(1).split(',').filter(arg => !!arg);
for (const arg of args) {
switch (arg) {
case 'dark':
document.getElementById('theme').href = 'theme-dark.css';
currentTheme = 'dark';
break;
case 'large':
case 'single':
Expand All @@ -14,13 +17,43 @@ for (const arg of args) {
}
}

if (!currentTheme) {
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
currentTheme = 'dark';
} else {
currentTheme = 'light';
}
localStorage.setItem(themeKey, currentTheme);
}

const themeClass = 'on';

const themeButton = document.getElementById('theme-btn');
const themeStylesheet = document.getElementById('theme');
const applyThemeClass = () => {
if (currentTheme === 'dark') {
themeButton.parentNode.classList.add(themeClass);
themeStylesheet.href = 'theme-dark.css';
} else {
themeButton.parentNode.classList.remove(themeClass);
themeStylesheet.href = 'theme-light.css';
}
}

applyThemeClass();
themeButton.addEventListener('click', () => {
currentTheme = (currentTheme === 'dark') ? 'light' : 'dark';
localStorage.setItem(themeKey, currentTheme);
applyThemeClass();
});

window.addEventListener("DOMContentLoaded", () => {
const footer = document.querySelector('footer');
const modeSwitches = footer.querySelectorAll('li > a[href^="?"]');
for (const a of modeSwitches) {
const mode = a.getAttribute('href').slice(1);
if (args.includes(mode)) {
a.parentNode.classList.add('on');
a.parentNode.classList.add(themeClass);
a.href = '?' + args.filter(arg => arg !== mode).join(',');
} else {
a.href = '?' + [...args, mode].join(',');
Expand Down
13 changes: 13 additions & 0 deletions static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ main {
width: auto;
display: flex;
white-space: pre;
flex-direction: column;
transform-origin: 0 0;
transform: scale(0.5);
}
Expand Down Expand Up @@ -142,3 +143,15 @@ footer li:last-child {
footer a:hover {
text-decoration: none;
}
.single-link {
display: none;
}

@media (min-width: 768px) {
main {
flex-direction: row;
}
.single-link {
display: block;
}
}