GithubHelp home page GithubHelp logo

diemantra / mantra-theme-switcher Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 38 KB

Simple React library to handle switching between light/dark mode

Home Page: https://mantra-theme-switcher.vercel.app

License: MIT License

JavaScript 4.95% TypeScript 77.44% CSS 13.94% HTML 3.67%

mantra-theme-switcher's Introduction

React Theme Switcher

This is a simple theme switcher for React. It uses the Context API to provide a theme to the entire application. @example site: https://mantra-theme-switcher.vercel.app/

Installation

npm install mantra-theme-switcher

Usage main.tsx

import React from 'react';
import ReactDOM from 'react-dom/client';
import { ThemeProvider } from 'mantra-theme-switcher';
import App from './App';

// ThemeContextSettings is optional
// These are the default settings
const ThemeContextSettings = {
  initialState: "light",
  matchDevicePreference: "initialy"
  savePreference: true,
}

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
	<React.StrictMode>
		<ThemeContextProvider settings={ThemeContextSettings}>
				<App />
			</Provider>
	</React.StrictMode>
);

Usage SomeComponent.tsx

import React from 'react';
import { useTheme } from 'mantra-theme-switcher';

const SomeComponent = () => {
	const { theme, toggleTheme, isDark, isLight } = useTheme();

	return (
		<div>
			<h1>Current theme: {theme}</h1>
			<button onClick={() => toggleTheme('light')}>Set theme to light</button>
			<button onClick={() => toggleTheme('dark')}>Set theme to dark</button>
			{isDark ? <p>Help it's dark in here!</p> : <p>Actually it's light!</p>}
		</div>
	);
};

Usage in css

:root {
	--background: #fff;
	--text: #000;
}

html[data-theme='dark'] :root {
	--background: #000;
	--text: #fff;
}

Usage in Sass

$lightMode-background: #fff;
$darkMode-background: #000;

$lightMode-text: #000;
$darkMode-text: #fff;

@mixin dark {
	html[data-theme='dark'] & {
		@content;
	}
}
@mixin dark-hover {
	html[data-theme='dark'] &:hover {
		@content;
	}
}
@mixin dark-active {
	html[data-theme='dark'] &:active {
		@content;
	}
}

body {
	background: $lightMode-background;
	color: $lightMode-text;

	@include dark {
		background: $darkMode-background;
		color: $darkMode-text;
	}
}

.button {
	background: $lightMode-background;

	&:hover {
		background: lighten($lightMode-background, 10%);
	}

	@include dark {
		background: $darkMode-background;
	}

	@include dark-hover {
		background: lighten($darkMode-background, 10%);
	}
}

Settings

Name Type Default Description
initialState "light" "dark" "init" "light" The initial theme to use. (Bypassed if there is a preference in localStorage or matchDevicePreference is activated)
matchDevicePreference "initialy" "always" "never" "initialy" Match the device preference. ("initialy" = if the device theme switches during session, the theme won't be changed. "always" = the theme will change whenever the devices theme changes. "never" = the theme will never change with the device.)
savePreference boolean true Save the preference in local storage.

mantra-theme-switcher's People

Contributors

tysonjf avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.