Add a manual light/dark mode switcher

Despite the simplicity of the commit title, this was a pretty big
change. The styling used to just go off of the system's color scheme,
but that can't be overridden. Instead, I have made a variable that
determines whether dark theme is active and made a small panel with some
buttons to change the theme. I had to change a lot of code to achieve
this and lost a lot of hair (I metaphorically pulled it out) from
writing this code.

I also changed things from legacy mode to rune mode (Svelte 4 to 5)
while I was at it, that wasn't too big.
This commit is contained in:
2025-05-07 21:17:52 -07:00
parent e6dd87427b
commit 71e7662408
12 changed files with 273 additions and 89 deletions

View File

@ -5,8 +5,18 @@
let canvas: HTMLCanvasElement;
let ctx: CanvasRenderingContext2D;
let dark_theme = false;
let time_scale = 1;
let { darkTheme = $bindable() } = $props();
let timeScale = 1; // TODO: Make entities a bit faster like they used to
$effect(() => {
darkTheme;
for (let i = 0; i < gradients.length; i++) {
let gradient = gradients[i];
gradient.color = getRandomColor();
gradient.prepareBuffer();
}
});
let particleImages: { [key: string]: HTMLImageElement } = {};
@ -49,24 +59,6 @@
return {};
});
if (
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches
) {
dark_theme = true;
}
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", (event) => {
dark_theme = event.matches;
for (let i = 0; i < gradients.length; i++) {
let gradient = gradients[i];
gradient.color = getRandomColor();
gradient.prepareBuffer();
}
});
resize();
init();
animate();
@ -99,8 +91,8 @@
}
update() {
this.x += this.speedX * time_scale;
this.y += this.speedY * time_scale;
this.x += this.speedX * timeScale;
this.y += this.speedY * timeScale;
// Reverse direction if particle hits edge
if (this.x <= 0 || this.x >= window.innerWidth) {
@ -183,10 +175,10 @@
update() {
super.update();
this.angle += this.rotationSpeed * time_scale;
this.angle += this.rotationSpeed * timeScale;
// Breathing effect: oscillate size
this.size += this.growthSpeed * time_scale;
this.size += this.growthSpeed * timeScale;
if (
this.size >= this.originalSize * 1.25 ||
this.size <= this.originalSize * 0.75
@ -199,7 +191,7 @@
ctx.save();
// The source images are black, so we are inverting them
// different amounts to get different shades of gray
ctx.filter = dark_theme ? "invert(0.15)" : "invert(0.8)";
ctx.filter = darkTheme ? "invert(0.15)" : "invert(0.8)";
// Draw center of rotation
// ctx.beginPath();
@ -214,7 +206,7 @@
}
function getRandomColor() {
if (dark_theme) {
if (darkTheme) {
let r = Math.floor(Math.random() * 255 - 100);
let b = Math.floor(Math.random() * 255 - 100);
let g = Math.floor(Math.random() * 255 - 100);
@ -273,7 +265,7 @@
this.radius,
);
gradient.addColorStop(0, this.color);
if (dark_theme) {
if (darkTheme) {
gradient.addColorStop(1, `rgba(0, 0, 0, 0)`);
} else {
gradient.addColorStop(1, `rgba(255, 255, 255, 0)`);

View File

@ -1,4 +1,6 @@
<script lang="ts">
import { getContext } from "svelte";
function toggleModalMenu() {
var pages = document.getElementById("pages") as HTMLElement;
pages.classList.toggle("hidden");
@ -9,6 +11,8 @@
pages.classList.toggle("hidden");
}
}
let darkTheme: CallableFunction = getContext("darkTheme");
</script>
<nav>
@ -33,7 +37,7 @@
>
<i class="bi bi-git"></i>
</a>
<button on:click={toggleModalMenu} class="menu-button" aria-label="menu">
<button onclick={toggleModalMenu} class="menu-button" aria-label="menu">
<i class="bi bi-list"></i>
</button>
</div>
@ -45,10 +49,10 @@ the only way to achieve a proper modal. They even do this in the
Svelte modal example, https://svelte.dev/playground/modal
-->
<!-- svelte-ignore a11y_click_events_have_key_events a11y_no_static_element_interactions -->
<span on:click={modalMenuProcessClick} id="pages" class="modalbg hidden">
<div>
<button on:click={toggleModalMenu} class="close" aria-label="Close">
<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
<span onclick={modalMenuProcessClick} id="pages" class="modalbg hidden">
<div class={darkTheme ? "dark-theme" : ""}>
<button onclick={toggleModalMenu} class="close" aria-label="Close">
<i class="bi bi-x"></i>
</button>
<ul>
@ -261,9 +265,7 @@ Svelte modal example, https://svelte.dev/playground/modal
}
}
@media (prefers-color-scheme: dark) {
span.modalbg div {
background-color: #000000bb;
}
span.modalbg div.dark-theme {
background-color: #000000bb;
}
</style>

View File

@ -0,0 +1,96 @@
<script lang="ts">
import { getContext } from "svelte";
import { themes } from "../script/theme.ts";
let lightButtonIcon = $state() as HTMLElement;
let darkButtonIcon = $state() as HTMLElement;
let autoButtonIcon = $state() as HTMLElement;
// In case easy access to the buttons themselves is useful, they are provided here.
let lightButton = $state() as HTMLButtonElement;
let darkButton = $state() as HTMLButtonElement;
let autoButton = $state() as HTMLButtonElement;
let { themeOption = $bindable() } = $props();
function setThemeOption(newThemeOption: string) {
switch (newThemeOption) {
case themes.LIGHT:
themeOption = themes.LIGHT;
lightButtonIcon.classList.replace("bi-sun", "bi-sun-fill");
darkButtonIcon.classList.replace("bi-moon-fill", "bi-moon");
autoButtonIcon.classList.replace("bi-display-fill", "bi-display");
break;
case themes.DARK:
themeOption = themes.DARK;
lightButtonIcon.classList.replace("bi-sun-fill", "bi-sun");
darkButtonIcon.classList.replace("bi-moon", "bi-moon-fill");
autoButtonIcon.classList.replace("bi-display-fill", "bi-display");
break;
case themes.AUTO:
themeOption = themes.AUTO;
lightButtonIcon.classList.replace("bi-sun-fill", "bi-sun");
darkButtonIcon.classList.replace("bi-moon-fill", "bi-moon");
autoButtonIcon.classList.replace("bi-display", "bi-display-fill");
break;
default:
console.error("setThemeOption was passed a value that is not a theme");
}
}
let darkTheme: CallableFunction = getContext("darkTheme");
</script>
<div class="panel settings {darkTheme() ? 'dark-theme' : ''}">
<button
aria-label="Dark Theme"
bind:this={darkButton}
onclick={() => {
setThemeOption(themes.DARK);
}}
>
<i bind:this={darkButtonIcon} class="bi bi-moon"></i>
</button>
<button
aria-label="Light Theme"
bind:this={lightButton}
onclick={() => {
setThemeOption(themes.LIGHT);
}}
>
<i bind:this={lightButtonIcon} class="bi bi-sun"></i>
</button>
<button
aria-label="Auto Theme"
bind:this={autoButton}
onclick={() => {
setThemeOption(themes.AUTO);
}}
>
<i bind:this={autoButtonIcon} class="bi bi-display-fill"></i>
</button>
</div>
<style lang="scss">
@use "../style/global.scss";
div.panel.settings {
padding: 6px 8px;
position: fixed;
bottom: 20px;
right: 20px;
}
div.panel.settings button {
color: global.$text-color;
font-size: 110%;
cursor: pointer;
// Reset buton style
border: none;
background-color: #00000000;
border-radius: 0;
}
</style>

View File

@ -4,16 +4,79 @@
import Navbar from "../component/navbar.svelte";
import Footer from "../component/footer.svelte";
import Bg from "../component/bg.svelte";
import Settings from "../component/settings.svelte";
import { themes } from "../script/theme";
import { onMount, setContext } from "svelte";
interface Props {
children?: import("svelte").Snippet;
}
let { children }: Props = $props();
let themeOption = $state(themes.AUTO);
let darkTheme = $state(false);
/*/
* This is necesarry for pages to read the theme,
* sucks that we have to use an anonymous function
* just to grab a variable
/*/
let darkThemeCallable = () => darkTheme;
setContext("darkTheme", darkThemeCallable);
function setAutoTheme() {
// I'm so sorry about this code (good luck reading/debugging this)
darkTheme =
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches;
document.documentElement.style.setProperty(
"--text-color",
darkTheme ? "white" : "#383c3f",
);
document.body.style.backgroundColor = darkTheme ? "black" : "white";
}
$effect(() => {
themeOption;
switch (themeOption) {
case themes.LIGHT:
darkTheme = false;
document.documentElement.style.setProperty("--text-color", "#383c3f");
document.body.style.backgroundColor = "white";
break;
case themes.DARK:
darkTheme = true;
document.documentElement.style.setProperty("--text-color", "white");
document.body.style.backgroundColor = "black";
break;
case themes.AUTO:
setAutoTheme();
}
});
onMount(() => {
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", () => {
if (themeOption === themes.AUTO) {
setAutoTheme();
}
});
});
</script>
<svelte:head>
<link rel="icon" href="/img/colormatic_logo.svg" />
</svelte:head>
<Bg />
<Bg bind:darkTheme />
<Navbar />
<slot />
{@render children?.()}
<Settings bind:themeOption />
<Footer />

View File

@ -1,3 +1,2 @@
export const prerender = true;
export const csr = true;
export const trailingSlash = "always";

View File

@ -1,5 +1,5 @@
<script lang="ts">
import { onMount } from "svelte";
import { getContext, onMount } from "svelte";
let arrow = $state() as HTMLDivElement;
@ -22,6 +22,8 @@
window.removeEventListener("scroll", checkArrow);
};
});
let darkTheme: CallableFunction = getContext("darkTheme");
</script>
<svelte:head>
@ -46,7 +48,7 @@
<meta property="og:type" content="website" />
</svelte:head>
<main>
<main class={darkTheme() ? "dark-theme" : ""}>
<div class="brand-heading">
<h1>Colormatic: A non-profit project for creation.</h1>
</div>
@ -55,7 +57,7 @@
<i class="bi bi-arrow-down-circle-fill"></i>
</div>
<div style="margin-top:25vh"></div>
<div style="margin-top:calc(100vh - 500px);"></div>
<div class="heading">Featured Colormatic Studios Projects:</div>
<div class="hero panel">

View File

@ -1,3 +1,9 @@
<script lang="ts">
import { getContext } from "svelte";
let darkTheme: CallableFunction = getContext("darkTheme");
</script>
<svelte:head>
<title>Colormatic - About</title>
<meta
@ -22,7 +28,7 @@
<spacer></spacer>
<main>
<main class={darkTheme() ? "dark-theme" : ""}>
<div class="hero panel">
<h1>Colormatic: A non-profit project for creation.</h1>
<p class="justify">

View File

@ -1,3 +1,9 @@
<script lang="ts">
import { getContext } from "svelte";
let darkTheme: CallableFunction = getContext("darkTheme");
</script>
<svelte:head>
<title>Colormatic Studios</title>
<meta
@ -29,7 +35,7 @@
<meta property="og:type" content="website" />
</svelte:head>
<main>
<main class={darkTheme() ? "dark-theme" : ""}>
<div class="cs-title"><h1>Colormatic Studios</h1></div>
<div class="project-grid-container">

View File

@ -1,5 +1,5 @@
<script lang="ts">
import { onMount } from "svelte";
import { getContext, onMount } from "svelte";
onMount(() => {
let channel = getParam("c");
@ -10,6 +10,8 @@
}
});
let darkTheme: CallableFunction = getContext("darkTheme");
const BASEURL = "https://files.colormatic.org/";
export function getParam(paramName: string) {
@ -105,8 +107,8 @@
<spacer></spacer>
<main>
<div class="video container">
<main class={darkTheme() ? "dark-theme" : ""}>
<div class="video panel">
<!-- Video elements are set by a script -->
<!-- svelte-ignore a11y_media_has_caption -->
<video id="videoplayer" controls></video>
@ -135,32 +137,27 @@
<style lang="scss">
@use "../../style/global.scss";
div.video.container {
div.video {
display: flex;
color: global.$text-color;
width: 90%;
margin: 16px auto 16px auto;
border: solid 1px #00000033;
border-radius: 8px;
box-shadow: 1px 1px 8px #00000033;
padding: 16px;
background-color: #ffffff22;
backdrop-filter: blur(3px);
}
div.video.container video#videoplayer {
div.video video#videoplayer {
flex-grow: 1;
border-radius: 12px;
height: auto;
max-width: 55%;
}
div.video.container div.videoobjects {
div.video div.videoobjects {
display: grid;
padding: 24px;
}
div.video.container div.videodetails h1#videotitle {
div.video div.videodetails h1#videotitle {
padding: 0 12px;
}
@ -169,7 +166,7 @@
flex-direction: column-reverse;
}
div.video.container div.download-dropdown {
div.video div.download-dropdown {
position: relative;
display: inline-block;
padding: 12px;
@ -183,11 +180,11 @@
text-align: center;
}
div.video.container div.download-dropdown:hover {
div.video div.download-dropdown:hover {
box-shadow: 1px 1px 8px #00000088;
}
div.video.container div.download-dropdown div.dropdown-content {
div.video div.download-dropdown div.dropdown-content {
display: none;
position: absolute;
font-size: 80%;
@ -198,38 +195,38 @@
text-align: center;
}
div.video.container div.download-dropdown:hover div.dropdown-content {
div.video div.download-dropdown:hover div.dropdown-content {
display: block;
}
div.video.container div.download-dropdown div.dropdown-content ul {
div.video div.download-dropdown div.dropdown-content ul {
list-style-type: none;
padding-left: 0;
}
div.video.container div.download-dropdown div.dropdown-content ul li {
div.video div.download-dropdown div.dropdown-content ul li {
padding: 4px;
cursor: pointer;
}
div.video.container div.download-dropdown div.dropdown-content ul li:hover {
div.video div.download-dropdown div.dropdown-content ul li:hover {
background-color: #dcdfdf;
}
div.video.container div.download-dropdown div.dropdown-content ul li a {
div.video div.download-dropdown div.dropdown-content ul li a {
text-decoration: none;
color: global.$text-color;
}
@media screen and (max-width: global.$mobile-width) {
div.video.container {
div.video {
display: block;
}
div.video.container video#videoplayer {
div.video video#videoplayer {
width: 100%;
max-width: none;
}
div.video.container div.download-dropdown {
div.video div.download-dropdown {
display: block;
margin-left: auto;
margin-right: auto;
@ -237,8 +234,8 @@
}
}
@media (prefers-color-scheme: dark) {
div.video.container div.download-dropdown div.dropdown-content {
main.dark-theme {
div.video div.download-dropdown div.dropdown-content {
background-color: #444444;
}
}

View File

@ -1,3 +1,9 @@
<script lang="ts">
import { getContext } from "svelte";
let darkTheme: CallableFunction = getContext("darkTheme");
</script>
<svelte:head>
<title>Colormatic - Zakarya</title>
<meta
@ -23,7 +29,7 @@
<meta property="og:type" content="website" />
</svelte:head>
<main>
<main class={darkTheme() ? "dark-theme" : ""}>
<img class="banner" src="/img/zakarya-banner.png" alt="Zakarya Banner" />
<div class="hero panel profile">
<div class="nameplate">
@ -206,9 +212,7 @@
}
}
@media (prefers-color-scheme: dark) {
main div.profile p {
border-color: #ffffff55;
}
main.dark-theme div.profile p {
border-color: #ffffff55;
}
</style>

15
src/script/theme.ts Normal file
View File

@ -0,0 +1,15 @@
/*/
* I know that having a source file this small is a bit silly,
* but I think it's probably a good way to do this. Since multiple
* components need the theme enum, why not define it in one place.
* I also think it's funny for a source file to have more comments
* than code. This will probably be changed in a future refactor.
*
* Also JavaScript enums suck (hot take)
/*/
export var themes = {
LIGHT: "light",
DARK: "dark",
AUTO: "auto",
};

View File

@ -4,21 +4,21 @@
--text-color: #383c3f;
}
@media (prefers-color-scheme: dark) {
:root {
--text-color: white;
}
body {
background-color: black;
}
}
body {
font-family: "Noto Sans", sans-serif;
margin: 0;
color: global.$text-color;
}
@media (prefers-color-scheme: dark) {
body {
background-color: black; // Don't flashbang dark theme users
}
:root {
--text-color: white;
}
}
spacer {
display: block;
margin-top: 8%;
@ -60,10 +60,12 @@ div.panel {
backdrop-filter: blur(5px);
}
@media (prefers-color-scheme: dark) {
div.panel {
border-color: #ffffff33;
}
main.dark-theme div.panel {
border-color: #ffffff33;
}
div.panel.dark-theme {
border-color: #ffffff33;
}
main div.hero {
@ -82,7 +84,7 @@ main div.hero {
}
}
main div#scroll-arrow {
main div.scroll-arrow {
text-align: center;
font-size: 200%;
width: 100%;
@ -93,7 +95,7 @@ main div#scroll-arrow {
transition: opacity 0.25s ease-in;
}
main div#scroll-arrow.scroll-arrow-hide {
main div.scroll-arrow.scroll-arrow-hide {
opacity: 0;
visibility: hidden;
transition: