Theme toggle
A 36px button that switches light and dark and remembers the choice, plus the head script that prevents a flash.
Native .astro · no React · MIT · free, no account
Install
npx shadcn@latest add https://www.themes.dev/astro/r/theme-toggle.jsonThe CLI needs a components.json (from npx shadcn init), which also writes the tokens these files use. No CLI? Copy the files below to the same paths.
Files
---// A 36px icon button that switches light and dark and remembers the choice (localStorage "theme"). Needs// <ThemeScript /> in <head>. Works with shadcn's tokens: they answer to the "dark" class on <html>.interface Props { label?: string; class?: string;}const { label = 'Dark mode', class: className } = Astro.props;--- <button type="button" data-theme-toggle aria-pressed="false" aria-label={label} class:list={['inline-flex size-9 items-center justify-center rounded-md text-muted-foreground outline-none transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50', className]}> <svg aria-hidden="true" viewBox="0 0 24 24" class="size-4 dark:hidden" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z" /></svg> <svg aria-hidden="true" viewBox="0 0 24 24" class="hidden size-4 dark:block" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><circle cx="12" cy="12" r="4" /><path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4" /></svg></button> <script> const root = document.documentElement; const sync = () => document.querySelectorAll('[data-theme-toggle]').forEach((b) => b.setAttribute('aria-pressed', String(root.classList.contains('dark')))); document.querySelectorAll<HTMLButtonElement>('[data-theme-toggle]').forEach((button) => button.addEventListener('click', () => { const dark = root.classList.toggle('dark'); try { localStorage.setItem('theme', dark ? 'dark' : 'light'); } catch {} sync(); }), ); sync();</script>---// Put in <head>, before your stylesheet: sets the "dark" class on <html> before the first paint, so a visitor who// chose dark never sees a light flash. Light is the default; the system setting is followed until they choose.--- <script is:inline> try { const t = localStorage.getItem('theme'); document.documentElement.classList.toggle('dark', t ? t === 'dark' : matchMedia('(prefers-color-scheme: dark)').matches); } catch (e) {}</script>More for site
- RSS feedFreeAn /rss.xml endpoint from a content collection with @astrojs/rss.rss.xml.ts
- llms.txtFreeAn /llms.txt endpoint that lists your pages and posts for AI tools, built from your collections.llms.txt.ts
- robots.txtFreeA /robots.txt endpoint with your sitemap's address and the paths to keep out of search.robots.txt.ts