
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
drkmd.js (short for darkmode.js) lets you add a dark-mode/light-mode toggle to any website. The library detects the system theme automatically and even saves the users choice in local storage or as a cookie.
The library will add the class theme-dark
/theme-light
to the body of the page and set the attribute data-theme
to dark
/light
on the html tag. See usage below how you can customize your page with this.
Add this to your HTML page:
<script src="https://cdn.jsdelivr.net/npm/drkmd-js/dist/drkmd-js.min.js"></script>
<script>
function addDarkmode() {
new Darkmode().attach();
}
window.addEventListener('load', addDarkmode);
</script>
Install drkmd.js using NPM
npm install drkmd-js
Then add the following JavaScript code:
import Darkmode from 'drkmd-js';
new Darkmode().attach();
By default drkmd.js will add the button to the bottom right corner and save the users choice in local storage, this can be configured using the options object.
You can either use the class theme-dark
/theme-light
to change your css depending on the theme, or set css variables to specify colors for each theme:
/* Light Colors */
[data-theme="light"] {
--background: #fff;
--color: #000;
}
/* Dark Colors */
[data-theme="dark"] {
--background: #000;
--color: #fff;
}
html,
body {
background: var(--background);
color: var(--color);
}
You can customize drkmd.js by passing a options object to new Darkmode()
:
const options = {
top: '20px', // default: 'unset'
bottom: 'unset', // default: '20px'
right: 'unset', // default: '20px'
left: '32px', // default: 'unset'
buttonLight: '#fff', // default: '#fff'
buttonDark: '#000', // default: '#000'
cookie: true, // default: false
localStorage: false, // default: true (will take precedence over cookie)
label: '', // default: '🌓'
autoMatchOsTheme: false // default: true
}
const darkmode = new Darkmode(options);
darkmode.attach();
If you don't want to show the button and enable/disable Darkmode programatically you can use the method toggle()
. You can also check if the Dark Mode is activated with the method isActivated()
. See them in action in the following example:
const darkmode = new Darkmode();
darkmode.toggle();
console.log(darkmode.isActivated()) // will return true
drkmd.js uses prefers-color-scheme: dark
to automatically enable the Dark Mode if the OS prefered theme is dark.
Check the current compatibility here:
prefers-color-scheme
(to activate Dark Mode automatically)css-variables
(to change specific colors depending on the theme)Issues and PRs are very welcome!
The actual source code of this library is in the drkmd.js
file in the src
folder.
Run yarn build
or npm run build
to produce a production version of drkmd.js in the dist
folder.
This library was developed by me (@betahuhn) in my free time. If you want to support me:
The library was inspired by Darkmode.js which is similar, but uses a different approach by directly changing the background color of your page, instead of letting you customize everything via css variables
.
Copyright 2020 Maximilian Schiller
This project is licensed under the MIT License - see the LICENSE.md file for details
FAQs
Simple dark-mode/light-mode toggle for any website
The npm package drkmd-js receives a total of 8 weekly downloads. As such, drkmd-js popularity was classified as not popular.
We found that drkmd-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.