sandoche / Darkmode.js

🌓 Add a dark-mode / night-mode to your website in a few seconds
https://darkmodejs.learn.uno
MIT License
2.71k stars 175 forks source link

Allow running callbacks when `button` is clicked #88

Open kdheepak opened 2 years ago

kdheepak commented 2 years ago

Thanks for maintaining darkmode.js.

This is a feature request. It would be nice to run some additional custom javascript code on light / dark mode toggle. For example, a user might want to toggle the theme of their comments using this:

const iframe = document.querySelector("iframe.giscus-frame");
iframe.contentWindow.postMessage({giscus: {setConfig: {theme: "dark"}}}, "https://giscus.app/");

It would be great if the constructor accepted a callback that was called in the on click handler for the button for toggling the light / dark mode.

sandoche commented 2 years ago

Hey that's a nice idea, if you have some time feel free to implement it! I'll happily review, merge and deploy the feature.

eugenekolo commented 1 year ago

This works:

var darkmode = new Darkmode();
darkmode.button.addEventListener('click', () => {
        console.log("hello cb");
});