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

themesx

v1.1.4

Published

<h1 align="center">ThemesX</h1>

Downloads

195

Readme

Features

  • Seamless Dark Mode Support: ThemesX provides a quick setup for dark and light modes, handling background, text colors, and system theme detection automatically.
  • System Theme Detection: Automatically syncs with the user’s device theme preference, so your app can adjust its appearance according to the system's light or dark mode setting.
  • React Ecosystem Compatibility: Works effortlessly with major frameworks like Next.js, Vite, Gatsby, Remix, Astro, Qwik, and Solid, ensuring broad compatibility for any React-based project.
  • SSR Friendly: Fully compatible with server-side rendering, making it ideal for Next.js and other SSR frameworks.
  • Dynamic Theme Switching: Includes options for system-based, user-selected dark or light themes, making it highly customizable.
  • Embedded CSS Styling: Automatically injects the necessary CSS for background and text color adjustments, saving you from adding extra styling code.
  • TypeScript Support: Fully typed with TypeScript, enabling improved development experience, code completion, and safety.

Installation

Install ThemesX using your preferred package manager:

Bun

bun add themesx

npm

npm install themesx

pnpm

pnpm add themesx

yarn

yarn add themesx

Usage

Basic Setup

In your main app file, wrap your app in ThemeProvider to enable ThemesX functionality.

import React from 'react';
import { ThemeProvider } from 'themesx';

function App({ Component, pageProps }) {
  return (
    <ThemeProvider>
      <Component {...pageProps} />
    </ThemeProvider>
  );
}

export default App;

Accessing the Theme Context

Use the useTheme hook to control and monitor theme state in your components.

import React from 'react';
import { useTheme } from 'themesx';

const MyComponent = () => {
  const { theme, setTheme } = useTheme();

  return (
    <div>
      <p>Current theme: {theme}</p>
      <button onClick={() => setTheme('dark')}>Switch to Dark Mode</button>
      <button onClick={() => setTheme('light')}>Switch to Light Mode</button>
      <button onClick={() => setTheme('system')}>Use System Theme</button>
    </div>
  );
};

export default MyComponent;

Framework-Specific Examples

Next.js

App Directory (Next.js 13+)

In the app directory, place ThemeProvider in layout.js or layout.tsx to ensure theme support for all pages.

// app/layout.js or app/layout.tsx
import React from 'react';
import { ThemeProvider } from 'themesx';

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <ThemeProvider>{children}</ThemeProvider>
      </body>
    </html>
  );
}

Pages Directory

In the pages directory, add ThemeProvider to _app.js or _app.tsx to wrap all pages with theme support.

// pages/_app.js or pages/_app.tsx
import React from 'react';
import { ThemeProvider } from 'themesx';

function MyApp({ Component, pageProps }) {
  return (
    <ThemeProvider>
      <Component {...pageProps} />
    </ThemeProvider>
  );
}

export default MyApp;

Vite

For Vite projects, simply wrap your main application component in ThemeProvider in main.js or main.tsx.

// src/main.js or src/main.tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { ThemeProvider } from 'themesx';

ReactDOM.createRoot(document.getElementById('root')).render(
  <ThemeProvider>
    <App />
  </ThemeProvider>,
);

Gatsby

To use ThemesX in a Gatsby project, add ThemeProvider in gatsby-browser.js and gatsby-ssr.js to enable SSR and client-side support.

// gatsby-browser.js and gatsby-ssr.js
import React from 'react';
import { ThemeProvider } from 'themesx';

export const wrapRootElement = ({ element }) => (
  <ThemeProvider>{element}</ThemeProvider>
);

Remix

In Remix, wrap your root component in ThemeProvider to ensure theme support throughout the app.

// root.tsx or root.js
import React from 'react';
import { ThemeProvider } from 'themesx';
import {
  Links,
  LiveReload,
  Meta,
  Outlet,
  Scripts,
  ScrollRestoration,
} from 'remix';

export default function App() {
  return (
    <html lang="en">
      <head>
        <Meta />
        <Links />
      </head>
      <body>
        <ThemeProvider>
          <Outlet />
        </ThemeProvider>
        <ScrollRestoration />
        <Scripts />
        <LiveReload />
      </body>
    </html>
  );
}

Astro

Astro with React components can use ThemeProvider by wrapping your app in the main App file.

// src/components/App.jsx
import React from 'react';
import { ThemeProvider } from 'themesx';
import MyComponent from './MyComponent';

const App = () => (
  <ThemeProvider>
    <MyComponent />
  </ThemeProvider>
);

export default App;

Qwik

In a Qwik React project, you can wrap the main component in ThemeProvider as shown below.

// src/App.jsx
import React from 'react';
import { ThemeProvider } from 'themesx';
import MyComponent from './MyComponent';

const App = () => (
  <ThemeProvider>
    <MyComponent />
  </ThemeProvider>
);

export default App;

Solid

Using ThemesX in Solid requires wrapping the main React component in ThemeProvider.

// src/App.jsx
import React from 'react';
import { ThemeProvider } from 'themesx';
import MyComponent from './MyComponent';

const App = () => (
  <ThemeProvider>
    <MyComponent />
  </ThemeProvider>
);

export default App;

API Reference

ThemeProvider

The ThemeProvider component wraps your application to provide global theme state and CSS support.

  • Props:
    • children: The components to render within the ThemeProvider.
    • enableSystemTheme: (Optional) Boolean to enable automatic system theme detection.
    • defaultTheme: (Optional) Sets the default theme ("light", "dark", or "system").

useTheme

A hook to access and control the theme state. Use useTheme to set and toggle themes in any component.

const { theme, setTheme } = useTheme();
  • theme: The current theme ("light", "dark", or "system").
  • setTheme: Function to change the theme; accepts "light", "dark", or "system" as parameters.

License

This project is licensed under the GPL-3.0-or-later License.