Giter Site home page Giter Site logo

coliff / dark-mode-switch Goto Github PK

View Code? Open in Web Editor NEW
559.0 11.0 114.0 241 KB

Add a dark-mode theme toggle with a Bootstrap Custom Switch

Home Page: https://coliff.github.io/dark-mode-switch/

License: MIT License

JavaScript 31.96% CSS 10.26% HTML 57.77%
dark-mode-toggle bootstrap dark-mode-switcher custom-switch

dark-mode-switch's Introduction

LICENSE GitHub Super-Linter code style: prettier Best of JS GitHub Stars NPM Downloads

🌓 Dark Mode Switch

Add a dark-mode theme toggle with a Bootstrap Custom Switch.

  • Uses local storage to save preference
  • Only 383 Bytes minified and gzipped!

Quick start

Several quick start options are available:

  • Download the latest release
  • Clone the repo git clone https://github.com/coliff/dark-mode-switch.git
  • Install with npm npm install dark-mode-switch
  • Install with yarn yarn add dark-mode-switch

Usage

  1. Add your custom switch for the Dark Mode toggle followed by the dark-mode-switch.min.js script:

Bootstrap 4.x

<div class="custom-control custom-switch">
  <input type="checkbox" class="custom-control-input" id="darkSwitch" />
  <label class="custom-control-label" for="darkSwitch">Dark Mode</label>
</div>
<script src="dark-mode-switch.min.js"></script>

Bootstrap 5.x

<div class="form-check form-switch">
  <input type="checkbox" class="form-check-input" id="darkSwitch" />
  <label class="custom-control-label" for="darkSwitch">Dark Mode</label>
</div>
<script src="dark-mode-switch.min.js"></script>
  1. Edit the dark-mode.css to suit your site - the one included here is a basic example.

How it works

Turning dark mode on will add data-theme="dark" to the body tag. You can use CSS to target the elements on the page like so:

[data-theme="dark"] {
  background-color: #111 !important;
  color: #eee;
}

Demo

Bootstrap 5 Demo page

Browser Support

Works well with all the browsers supported by Bootstrap 4 and Bootstrap 5

To Enable DarkMode in Android Webview

WebSettings.setDomStorageEnabled(true)

Credits

Created thanks to the excellent dark-theme and local storage tutorials over at codyhouse.co.

dark-mode-switch's People

Contributors

coliff avatar crazychatting avatar dependabot-preview[bot] avatar dependabot[bot] avatar fynks avatar josee9988 avatar mcnaveen avatar step-security-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dark-mode-switch's Issues

Background color of menu dropdown not changing

Hi, I just want to say thanks for writing this code, I just love it!

It seems as there is a tiny problem as the background color of a bootstrap dropdown menu doesn't change its color.

I tried to fix it myself but without any success as I'm still an absolute beginner :-)

Many thanks in advance for any help.

Delay in dark mode detection

It is more a suggestion than a real issue : when my website theme has just loaded, and dark theme was already activated, I can quickly see white theme before dark theme is activated.
I understand why I can see it considering that main theme is loaded before dark theme tag is added to body.
Do you have an idea how to not see this quick blinking on page load ?

Detect if user has prefers-dark-mode setting on

Requirements:

  • dark-mode-switch should detect the users system preference and use that unless they have already made a change to the switch (which is saved in local storage). So if someone has already set the dark mode switch on, but they have a light-mode set on their OS or browser preference it would prioritise the dark-mode-switch setting
  • I think this 'Detect if user has prefers-dark-mode setting on' feature should be enabled as standard in a v2 of this script, but open to suggestions if it should be an option.

Compatibility with cache

Hi there, I just wanna say I love this plugin, and it’s really easy to customize it with CSS!

One problem I’ve been facing though is it doesn’t work with cache plugins.
If the page is cached the page will always flash the previous colors every time I load a new page.

I’ve tried everything out there, searched the web for anything I find.

Any help would be appreciated,

Thanks!

Not Working (node.js and ejs)

Hello!

I installed the package using npm and followed the documentation (readme.md) and it doesn't work, I don't see it adding the class to the body or any other tag.

Did I did something wrong?

Using html element with dark mode

I tried to use the script and it worked with me on all class but Bootstrap is known to be having effect like :focus and :hover
so if you use

[data-theme="dark"] a.bg-light:focus, a.bg-light:hover, button.bg-light:focus, button.bg-light:hover {
    background-color: #6c757d!important;
}

And you switched between the dark mode and the normal mode, you will find that the dark mode applies these changes and cannot be changed completely until you remove !important from background-color as example and when you do, you are not able to implement these changes at all

My question is, Is there a solution to this problem?

And I thank you with all my heart for your wonderful effort

Laravell Support

Hello,
For some reason when imported to laravell with use of NPM it is not working can anybody help me ?

Not working :'(

Hi,
Im using dark-mode-switch for a meteor project.
I downloaded with npm.
But when i switch the switch, nothing appened, i have this error :
Uncaught SyntaxError: Unexpected token dark-mode-switch.min.js:1 '<' (when i load te page, not when i switch)
Maybe i forgot something ?

How to use it with sass?

This is a very good library. We have one question, how we use it with scss? Please, give me a full example. Thank you a lot.

Changes not taking effect on body element

screenshot
screenshot2

Hello, firstly, thanks for sharing the project, it is nice, lightweight, and works well. I do have one issue however. I am trying to apply dark-mode styles directly to the body tag and this doesn't seem to work. You can see from my screenshots, that I am able to make the text inside .content red, but when trying to change the gradient to white on the body, I see no changes. I could imagine that this has something to do with the fact that the data-theme attribute is set on the body and then it maybe isn't possible to use in this way. If this is the case, do you know of a workaround? Any help is greatly appreciated.

How to use it for nested CSS

How to use it for nested CSS elements like:

        .nav-tabs .nav-link {             
            border: 1px solid #fff;
            background-color: #f1f6fb;
        }

        .nav-tabs .nav-link.active{
            border: 1px solid #fff;
            background-color: #fff;
        }

Thanks. Awesome job.

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.