www: docs: feat: add dark mode (#2820)

- Added CSS vars for dark/light colors.
- Added dark/light theme toggle as Island

Fixes #2648

---------

Co-authored-by: Marvin Hagemeister <marvin@deno.com>
This commit is contained in:
David Monaghan
2025-02-04 11:58:05 +01:00
committed by GitHub
co-authored by Marvin Hagemeister
parent adf8b87a03
commit b1f2a989a5
16 changed files with 320 additions and 46 deletions
+2 -2
View File
@@ -48,8 +48,8 @@ export default function CopyArea(props: { code: string }) {
<button
aria-label="Copy to Clipboard"
disabled={!IS_BROWSER}
class={`rounded p-1.5 border border-gray-300 hover:bg-gray-700 ${
copied.value ? "text-green-500" : ""
class={`rounded p-1.5 border border-foreground-secondary/30 hover:bg-foreground-secondary/70 ${
copied.value ? "text-fresh-green/80" : ""
} relative`}
onClick={handleClick}
>
+3 -3
View File
@@ -105,7 +105,7 @@ export function TableOfContents({ headings }: TableOfContentsProps) {
<button
id="toc-outline-btn"
onClick={() => setIsOpen((v) => !v)}
class="bg-gray-100 py-2 px-4 rounded border border-gray-300 flex items-center hover:border-green-600 transition-colors text-sm"
class="bg-background-primary py-2 px-4 rounded border border-foreground-secondary/30 flex items-center hover:border-fresh-green/80 transition-colors text-sm"
>
On this page
<svg
@@ -117,7 +117,7 @@ export function TableOfContents({ headings }: TableOfContentsProps) {
</svg>
</button>
{isOpen && (
<div class="mt-2 pl-4 border-l border-gray-250 text-[13px] leading-7">
<div class="mt-2 pl-4 border-l border-foreground-primary/20 text-[13px] leading-7">
<nav aria-labelledby="toc-outline-btn">
<ul>
{headings.map((heading) => {
@@ -142,7 +142,7 @@ export function TableOfContents({ headings }: TableOfContentsProps) {
ref={refMarker}
class="marker w-[2px] bg-green-400 h-5 absolute top-0 opacity-0 transition-all"
/>
<div class="pl-4 border-l border-gray-250 text-[13px] leading-7">
<div class="pl-4 border-l border-foreground-secondary/20 text-[13px] leading-7">
<div role="heading" aria-level={2} class="font-semibold">
On this page
</div>
+62
View File
@@ -0,0 +1,62 @@
import { useEffect, useState } from "preact/hooks";
import { IS_BROWSER } from "fresh/runtime";
export default function ThemeToggle() {
const getPreferredTheme = () => {
if (!IS_BROWSER) return "light";
const storedTheme = localStorage.getItem("theme");
if (storedTheme) return storedTheme;
return window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
};
const [theme, setTheme] = useState(getPreferredTheme);
useEffect(() => {
document.documentElement.classList.remove("light", "dark");
document.documentElement.classList.add(theme);
document.documentElement.setAttribute("data-theme", theme);
localStorage.setItem("theme", theme);
}, [theme]);
const toggleTheme = () => {
setTheme((prev) => (prev === "light" ? "dark" : "light"));
};
return (
<button
type="button"
onClick={toggleTheme}
class="dark-mode-toggle button p-1 -m-1"
aria-label="Toggle Theme"
>
{theme === "light"
? (
<svg
class="fill-foreground-primary hover:fill-fresh w-6 h-6"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path d="M17.293 13.293A8 8 0 016.707 2.707a8.001 8.001 0 1010.586 10.586z">
</path>
</svg>
)
: (
<svg
class="fill-foreground-primary hover:fill-fresh w-6 h-6"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M10 2a1 1 0 011 1v1a1 1 0 11-2 0V3a1 1 0 011-1zm4 8a4 4 0 11-8 0 4 4 0 018 0zm-.464 4.95l.707.707a1 1 0 001.414-1.414l-.707-.707a1 1 0 00-1.414 1.414zm2.12-10.607a1 1 0 010 1.414l-.706.707a1 1 0 11-1.414-1.414l.707-.707a1 1 0 011.414 0zM17 11a1 1 0 100-2h-1a1 1 0 100 2h1zm-7 4a1 1 0 011 1v1a1 1 0 11-2 0v-1a1 1 0 011-1zM5.05 6.464A1 1 0 106.465 5.05l-.708-.707a1 1 0 00-1.414 1.414l.707.707zm1.414 8.486l-.707.707a1 1 0 01-1.414-1.414l.707-.707a1 1 0 011.414 1.414zM4 11a1 1
0 100-2H3a1 1 0 000 2h1z"
fill-rule="evenodd"
clip-rule="evenodd"
>
</path>
</svg>
)}
</button>
);
}
+3 -3
View File
@@ -20,7 +20,7 @@ export default function VersionSelect(
</label>
{selectedIsLatest && (
<div class="flex absolute pointer-events-none select-none w-full h-full items-center justify-end pr-8">
<div class="rounded-full px-2 py-1 text-xs tag-label bg-[#056CF025] text-blue-700">
<div class="rounded-full px-2 py-1 text-xs tag-label bg-[#056CF025] dark:bg-[#2182ff45] text-blue-700 dark:text-blue-400">
Latest
</div>
</div>
@@ -34,9 +34,9 @@ export default function VersionSelect(
)}
<select
id="version"
class={`rounded-md block border border-gray-300 appearance-none bg-white form-select-bg font-semibold ${
class={`rounded-md block border border-foreground-primary/20 appearance-none bg-background-primary form-select-bg font-semibold ${
selectedIsLatest ? "pr-22" : "pr-10"
} py-2 pl-3 w-full h-full leading-none sm:(text-sm leading-5) focus:(outline-none border-blue-300) hover:bg-gray-100`}
} py-2 pl-3 w-full h-full leading-none sm:(text-sm leading-5) focus:(outline-none border-blue-300) hover:bg-background-secondary`}
value={selectedVersion}
onChange={(e) => {
if (e.currentTarget.value !== selectedVersion) {