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

react-theme-selector

v1.0.2

Published

A flexible and efficient React package that allows dynamic selection of multiple themes, headers, and footers using React Context. It supports asynchronous asset fetching, lazy-loaded components, and memoized values for optimal performance.

Downloads

188

Readme

react-theme-selector

A flexible and efficient React package that allows dynamic selection of multiple themes, headers, and footers using React Context. It supports asynchronous asset fetching, lazy-loaded components, and memoized values for optimal performance.

Features

  • Dynamically switch between multiple themes.
  • Select and update headers and footers on the fly.
  • Supports lazy-loaded headers, footers, and themes for improved performance.
  • Dynamic fetching of themes, headers, and footers from external sources (APIs, etc.).
  • Memoized context values to prevent unnecessary re-renders.
  • Dynamic CSS loading for theme styles.

Installation

You can install the package using npm:

npm install react-theme-selector

Usage

1. Setup ThemeProvider

Wrap your app inside the ThemeProvider. You can pass an initial theme, fetch functions for headers and footers, and a list of available themes.

import React from 'react';
import { ThemeProvider, ThemeSelector, HeaderSelector, FooterSelector, useTheme } from 'react-theme-selector';

// Example async fetch functions
const fetchHeaders = async () => [
    () => <header>Header 1</header>,
    () => <header>Header 2</header>
];

const fetchFooters = async () => [
    () => <footer>Footer 1</footer>,
    () => <footer>Footer 2</footer>
];

const AppContent = () => {
    const { header: Header, footer: Footer, theme } = useTheme();

    return (
        <div className={`app-container ${theme}`}>
            <Header />
            <div>Main Content Here</div>
            <Footer />
        </div>
    );
};

const App = () => {
    return (
        <ThemeProvider
            initialTheme="light"
            themes={['light', 'dark']}
            fetchHeaders={fetchHeaders}
            fetchFooters={fetchFooters}
        >
            <ThemeSelector />
            <HeaderSelector />
            <FooterSelector />
            <AppContent />
        </ThemeProvider>
    );
};

export default App;

2. ThemeProvider Props

| Prop | Type | Description | Default | | ------------- | ---------------- | --------------------------------------------------------------------------------------------- | --------- | | initialTheme| string | The initial theme to apply. | default | | themes | array | Array of available theme names (used for dynamically loading themes). | Required | | fetchHeaders| function | Asynchronous function to fetch an array of available headers (each a React component). | Required | | fetchFooters| function | Asynchronous function to fetch an array of available footers (each a React component). | Required |

3. ThemeSelector

This component renders a dropdown to allow users to select a theme from the available options.

import { ThemeSelector } from 'react-theme-selector';

<ThemeSelector />;

4. HeaderSelector

This component renders a dropdown to allow users to select a header from the available options.

import { HeaderSelector } from 'react-theme-selector';

<HeaderSelector />;

5. FooterSelector

This component renders a dropdown to allow users to select a footer from the available options.

import { FooterSelector } from 'react-theme-selector';

<FooterSelector />;

6. Using Theme, Header, and Footer in Your App

You can use the useTheme hook to access the current theme, header, and footer in your app components.

import React from 'react';
import { useTheme } from 'react-theme-selector';

const AppContent = () => {
    const { header: Header, footer: Footer, theme } = useTheme();

    return (
        <div className={`app-container ${theme}`}>
            <Header />
            <div>Main Content Here</div>
            <Footer />
        </div>
    );
};

7. Dynamically Loading Theme CSS

The ThemeProvider automatically loads a CSS file that matches the theme name (e.g., light.css or dark.css). Ensure that the appropriate CSS files are hosted and accessible for the themes you provide.

For example, if you pass ['light', 'dark'] as themes:

  • Ensure that you have light.css and dark.css files available at the correct path.

8. Lazy Loading Headers and Footers

For performance optimization, headers and footers are lazily loaded using React.lazy and Suspense. Here’s an example of how you can lazily load components when fetching headers and footers:

import React, { lazy, Suspense } from 'react';

const Header1 = lazy(() => import('./headers/Header1'));
const Header2 = lazy(() => import('./headers/Header2'));
const Footer1 = lazy(() => import('./footers/Footer1'));
const Footer2 = lazy(() => import('./footers/Footer2'));

const fetchHeaders = async () => [
    () => <Suspense fallback={<div>Loading...</div>}><Header1 /></Suspense>,
    () => <Suspense fallback={<div>Loading...</div>}><Header2 /></Suspense>
];

const fetchFooters = async () => [
    () => <Suspense fallback={<div>Loading...</div>}><Footer1 /></Suspense>,
    () => <Suspense fallback={<div>Loading...</div>}><Footer2 /></Suspense>
];

Example Project

Here’s a complete example of how the package can be used in your project:

import React from 'react';
import { ThemeProvider, ThemeSelector, HeaderSelector, FooterSelector, useTheme } from 'react-theme-selector';

// Example fetching functions
const fetchHeaders = async () => [
    () => <header>Header 1</header>,
    () => <header>Header 2</header>,
];

const fetchFooters = async () => [
    () => <footer>Footer 1</footer>,
    () => <footer>Footer 2</footer>,
];

const AppContent = () => {
    const { header: Header, footer: Footer, theme } = useTheme();

    return (
        <div className={`app-container ${theme}`}>
            <Header />
            <div>Main Content Here</div>
            <Footer />
        </div>
    );
};

const App = () => {
    return (
        <ThemeProvider
            initialTheme="light"
            themes={['light', 'dark']}
            fetchHeaders={fetchHeaders}
            fetchFooters={fetchFooters}
        >
            <ThemeSelector />
            <HeaderSelector />
            <FooterSelector />
            <AppContent />
        </ThemeProvider>
    );
};

export default App;

Contributing

Contributions are welcome! Feel free to submit a pull request or open an issue for any improvements or bug fixes.

License

This project is licensed under the MIT License - see the LICENSE file for details.