npm create astro@latest
as per tutorialnpm run dev
in the terminal to preview your gay sitehttp://localhost:4321/
` by defaultsrc/pages/index.astro
to edit your front pagepages
called about.astro
index.astro
Stylize it tomorrow
I'm copying this guide: Setting And Persisting Color Scheme Preferences With CSS And A “Touch” Of JavaScript by Henry Bley-Vroman
I am specifically using this guide because it uses the default light/dark css themes that don't require any CSS, and allows the user to override it if they want to choose another theme.
I specifically want the ability to change background images, not just background color. I want the default mode to be “dark mode” so there's no “FART”1). And I want the ease-in-ease-out color transitions to be optional. So this is in my global.CSS
:
:root { /* dark style, dark = default */ color-scheme: var(--color-scheme, dark); --bg: url('/img/bg.jpg'); transition-duration: 200ms; transition-property: background, background-color, color; /* page preference is "dark" */ &:has(#color-scheme option[value="light"]:checked) { --color-scheme: light; /* any additional dark styles */ --bg: url('/img/bg-light.jpg'); } /* page preference is "system", and system preference is "dark" */ @media (prefers-color-scheme: light) { &:has(#color-scheme option[value="system"]:checked) { --color-scheme: light; /* any additional dark styles, again */ } } @media screen and (prefers-reduced-motion: reduce), (update: slow) { transition-duration: 0s; } }
And this is in my Toggle.astro
, which is contained in my SidebarLeft.astro
component and makes up my navigation.
/* * If a color scheme preference was previously stored, * select the corresponding option in the color scheme preference UI * unless it is already selected. */ function restoreColorSchemePreference() { const colorScheme = localStorage.getItem(colorSchemeStorageItemName); if (!colorScheme) { return; } const option = colorSchemeSelectorEl.querySelector(`[value=${colorScheme}]`); if (!option) { localStorage.removeItem(colorSchemeStorageItemName); return; } if (option.selected) { return; } option.selected = true; } /* * Store an event target's value in localStorage under colorSchemeStorageItemName */ function storeColorSchemePreference({ target }) { const colorScheme = target.querySelector(":checked").value; localStorage.setItem(colorSchemeStorageItemName, colorScheme); } const colorSchemeStorageItemName = "preferredColorScheme"; const colorSchemeSelectorEl = document.querySelector("#color-scheme"); if (colorSchemeSelectorEl) { restoreColorSchemePreference(); colorSchemeSelectorEl.addEventListener("input", storeColorSchemePreference); }
Don't ask me what any of this means LOL I just copy/pasted someone else's code in the places that make it work on my site…