Style the colour picker
This commit is contained in:
parent
47a657a36c
commit
f213a499e2
@ -34,15 +34,7 @@
|
||||
<li><a href="/links">Links</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
<color-scheme-toggle></color-scheme-toggle>
|
||||
</header>
|
||||
{{ content | safe }}
|
||||
<footer>
|
||||
<div class="wrapper-lg">Copyright © {{ today.year }} {{ metadata.author.name }}</div>
|
||||
</footer>
|
||||
|
||||
<template id="color-scheme-toggle">
|
||||
<fieldset>
|
||||
<fieldset class="color-scheme">
|
||||
<legend>Change your colour scheme</legend>
|
||||
<label>
|
||||
<input type="radio" name="color-scheme" value="dark" />
|
||||
@ -53,23 +45,17 @@
|
||||
Light
|
||||
</label>
|
||||
</fieldset>
|
||||
</template>
|
||||
</header>
|
||||
{{ content | safe }}
|
||||
<footer>
|
||||
<div class="wrapper-lg">Copyright © {{ today.year }} {{ metadata.author.name }}</div>
|
||||
</footer>
|
||||
|
||||
|
||||
<script type="text/javascript">
|
||||
window.customElements.define("color-scheme-toggle", class extends HTMLElement {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
const template = document.getElementById("color-scheme-toggle");
|
||||
const templateNode = template.content.cloneNode(true);
|
||||
|
||||
this.appendChild(templateNode);
|
||||
}
|
||||
|
||||
getColourScheme = () => {
|
||||
const colourScheme = localStorage.getItem('color-scheme');
|
||||
|
||||
if (colourScheme) {
|
||||
return colourScheme;
|
||||
const defaultColourScheme = () => {
|
||||
if (localStorage.getItem('color-scheme')) {
|
||||
return localStorage.getItem('color-scheme');
|
||||
}
|
||||
|
||||
if (window.matchMedia('(prefers-color-scheme: dark)')?.matches) {
|
||||
@ -77,31 +63,32 @@
|
||||
}
|
||||
|
||||
return "light";
|
||||
}
|
||||
};
|
||||
|
||||
onColourSchemeChange = (e) => {
|
||||
localStorage.setItem('color-scheme', e.target.value);
|
||||
this.updateComponents();
|
||||
}
|
||||
const data = new Proxy({
|
||||
'color-scheme': defaultColourScheme(),
|
||||
}, {
|
||||
set: (obj, key, value) => {
|
||||
obj[key] = value;
|
||||
localStorage.setItem(key, value);
|
||||
document.querySelectorAll(`[type="radio"][name="${key}"]`).forEach(radio => {
|
||||
radio.checked = radio.value === value;
|
||||
});
|
||||
document.body.setAttribute(`data-${key}`, value);
|
||||
|
||||
updateComponents = () => {
|
||||
this.querySelectorAll('[name="color-scheme"]').forEach(radio => {
|
||||
radio.checked = this.getColourScheme() === radio.value;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
if (localStorage.getItem('color-scheme')) {
|
||||
document.body.setAttribute('data-color-scheme', this.getColourScheme());
|
||||
}
|
||||
const onRadioChange = e => {
|
||||
data[e.target.name] = e.target.value;
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
this.querySelectorAll('[name="color-scheme"]').forEach(radio => {
|
||||
radio.addEventListener('change', this.onColourSchemeChange);
|
||||
});
|
||||
|
||||
this.updateComponents();
|
||||
}
|
||||
document.querySelectorAll('[name="color-scheme"]').forEach(radio => {
|
||||
radio.addEventListener('change', onRadioChange);
|
||||
radio.checked = data[radio.name] == radio.value;
|
||||
});
|
||||
document.body.setAttribute(`data-color-scheme`, data['color-scheme']);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
42
src/css/exceptions/color-scheme-picker.css
Normal file
42
src/css/exceptions/color-scheme-picker.css
Normal file
@ -0,0 +1,42 @@
|
||||
.color-scheme {
|
||||
border: 0;
|
||||
display: flex;
|
||||
gap: var(--space-size-xs);
|
||||
|
||||
legend {
|
||||
position:absolute;
|
||||
clip:rect(0 0 0 0);
|
||||
}
|
||||
|
||||
input[name="color-scheme"] {
|
||||
position:absolute;
|
||||
clip:rect(0 0 0 0);
|
||||
}
|
||||
|
||||
label:has(:not(:checked)) {
|
||||
color: var(--color-subtle);
|
||||
}
|
||||
|
||||
label:has(:focus-visible) {
|
||||
outline: 1px solid var(--color-primary);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
label:has([name="color-scheme"])::before {
|
||||
font-family: "font awesome 6 free";
|
||||
font-style: normal;
|
||||
font-variant: normal;
|
||||
text-rendering: auto;
|
||||
font-weight: 900;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
font-size: var(--text-size-s);
|
||||
}
|
||||
|
||||
label:has([name="color-scheme"][value="dark"])::before {
|
||||
content: "\f186";
|
||||
}
|
||||
|
||||
label:has([name="color-scheme"][value="light"])::before {
|
||||
content: "\f185";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user