npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

astro-themes

v0.2.5

Published

Easy dark mode for Astro websites

Downloads

723

Readme

🚀 astro-themes GitHub Workflow Status (branch) npm

Easy dark mode for Astro websites. Add themes in 2 lines of code! Zero dependencies, supports SSR, and works with any framework or UI.

Install

npm install --save-dev astro-themes

Use

Add the <Themes /> component in your app. Works best in a centralized layout component, so it's shared everywhere.

code sample adding Themes component in document head

Usage

astro-themes gives you the ability to get and set the theme, with builtin browser standards. No polluting the global scope or any other hacks! These methods will work without a framework, or from within your React/Svelte/Vue/etc components.

Set the theme:

document.dispatchEvent(new CustomEvent('set-theme', { detail: 'dark' })) // or pass `null` to clear the saved setting

Get the current theme:

document.documentElement.attributes.getNamedItem('data-theme')?.value // 'light' | 'dark'

Set a default theme (only applied if the browser doesn't specify a preference for dark mode):

<Themes defaultTheme="dark" />

Compatibility with Tailwind

Tailwind expects class="dark" on the html element rather than data-theme. To ensure that dark: classes work correctly, you can configure your dark mode selector as described here.

/** @type {import('tailwindcss').Config} */
module.exports = {
  darkMode: ['class', '[data-theme="dark"]'],
  // ...
}

What does it do?

  • Provides ability to get and set the theme from any framework, or none at all
    • Set theme by dispatching a custom browser event: document.dispatchEvent(new CustomEvent('set-theme', { detail: 'dark' }))
    • Get current theme: document.documentElement.attributes.getNamedItem('data-theme')?.value
  • Sets data-theme attribute on the html element, so you can implement dark mode in CSS
  • Sets color-scheme CSS attribute on the html element, so the browser renders built in elements appropriately
  • Reads user theme preference on page load
    • From localStorage, if they've set it in the past
    • From prefers-color-scheme: dark, if supported
    • Falls back to the defaultTheme option, or light if not specified
  • Synchronizes setting across tabs
  • Responds to changes in OS preference (for example, the OS automatically switching to dark mode in the evening)

Run example locally

gh repo clone alex-grover/astro-themes
cd astro-themes
npm install
npm link
npm link astro-themes
npm run example

Feedback

Ideas, bug reports, or questions are welcomed! Please don't hesitate to open an issue.

This component was inspired by the excellent next-themes.