1.0.2 • Published 9 months ago

@jaiminmoslake7020/react-dark-light-theme-switcher v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
9 months ago

React Component Package

This package includes React components which can be used to add "light" and "dark" theme to your web application. CSS used in website example project is built with Tailwind seperately.

Website

https://react-dark-light-theme-switcher.netlify.app/

Installation

npm install @jaiminmoslake7020/react-dark-light-theme-switcher

How to use it

Use it with the provider, which will apply either theme="dark" or theme="light" to the applyTo selector. In the example below, a button will appear, allowing the user to change the theme, which will be applied to the body. preferColorScheme when true retrieves the color scheme from the system. And false then saves it to localStorage.

<ThemeSwitchProvider applyTo={"body"} preferColorScheme={false}>
  ...
  <ThemeSwitcherBtnProviderChild/>
</ThemeSwitchProvider>

or just use button and apply it by yourself.

<ThemeSwitcherBtn theme={theme}
                  setTheme={setTheme}
                  themeSwitcherContainerClassName={themeSwitcherContainerClassName}
                  iconWrapperClassName={iconWrapperClassName}
                  sunIconWrapperClassName={sunIconWrapperClassName}
                  moonIconWrapperClassName={moonIconWrapperClassName}/>

or hook with ThemeSwitcherBtn component,

const applyTo = 'body';
const preferColorScheme = true; // boolean
const {theme, setTheme} = useThemeSwitch(applyTo, preferColorScheme);

Component Properties

ThemeSwitcherBtn

PropertiesTypeValuesDefault
theme'light' | 'dark''light', 'dark''light'
setThemeFunction
themeSwitcherContainerClassName?string"any-styling-class"
iconWrapperClassName?string"any-styling-class"
sunIconWrapperClassName?string"any-styling-class"
moonIconWrapperClassName?string"any-styling-class"

ThemeSwitcherBtnProviderChild

PropertiesTypeValuesDefault
themeSwitcherContainerClassName?string"any-styling-class"
iconWrapperClassName?string"any-styling-class"
sunIconWrapperClassName?string"any-styling-class"
moonIconWrapperClassName?string"any-styling-class"
export type ElWithClassType = `.${string}`;
export type ElWithIdType = `#${string}`;
export type ApplyToType = 'html' | 'body' | keyof HTMLElementTagNameMap | ElWithClassType | ElWithIdType;

ThemeSwitchProvider

PropertiesTypeValuesDefault
applyToApplyToType'html' | 'body' | "any-class-or-id-selector"'body'
preferColorScheme?booleantrue, falseundefined
childrenReact.ReactNode

When preferColorScheme is undefined, it uses browser's "localStorage" API.

1.0.2

9 months ago

1.0.1

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago