Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Dark Mode to Your Franklin Website! #958

Open
TheCedarPrince opened this issue May 13, 2022 · 1 comment
Open

Adding Dark Mode to Your Franklin Website! #958

TheCedarPrince opened this issue May 13, 2022 · 1 comment
Labels

Comments

@TheCedarPrince
Copy link
Contributor

After some tinkering and help from @fredrikekre and @kdheepak, I am pleased to say I have found a way to bring darkmode to Franklin templates!
This details how to add darkmode.js to a Franklin website which should "just work ™️" with Franklin's templates:

  1. Visit https://darkmodejs.learn.uno/ and install the minified package lib there
  2. In your Franklin website directory, add the minified library to _libs/darkmode where you create the folder darkmode
  3. In _layout, open head.html and add the following snippet in the <head> block (I suggest adding it after the CSS stylesheets):
    <script src="/libs/darkmode/darkmode-js.min.js"></script>
    <script>
        function addDarkmodeWidget()
        {
            const options = {
                bottom: '32px', /* default: '32px' */
                right: '32px', /* default: '32px' */
                left: 'unset', /* default: 'unset' */
                time: '.0s', /* default: '0.3s' */
                mixColor: '#fff', /* default: '#fff' */
                backgroundColor: '#fff', /* default: '#fff' */
                buttonColorDark: '#100f2c', /* default: '#100f2c' */
                buttonColorLight: '#fff', /* default: '#fff' */
                saveInCookies: true, /* default: true, */
                label: '🌓', /* default: '' */
                autoMatchOsTheme: true /* default: true */
            };
            new Darkmode(options).showWidget();
        };
        window.addEventListener('DOMContentLoaded', addDarkmodeWidget);
    </script>
  1. You will need to most likely style darkmode for your site - this is what I added to my _css/franklin.css file for styling but you could do something different:
/* ==================================================================
    DARKMODE STYLING
================================================================== */

.darkmode--activated p,
.darkmode--activated h1,
.darkmode--activated h2,
.darkmode--activated h3,
.darkmode--activated h4,
.darkmode--activated h5,
.darkmode--activated span,
.darkmode--activated li,
.darkmode--activated ol {
    color: #ECEFF4;
}

.darkmode--activated blockquote {
    background-color: #2E3440;
}

.darkmode--activated ul {
    background-color: #2E3440;
}

.darkmode--activated a {
    color: #81A1C1
}

.darkmode-toggle {
    z-index: 500;
}

And once you are done, you should now have darkmode enabled for your website with a nice floating widget that you can click and reposition anywhere!
Here are some screenshots of my site:

image

image

I hope this helps with adding Darkmode to your website!

~ tcp 🌳

P.S. Also, you for the script to work on your site, you may have to set optimize(minify = false) in your .github/workflows/deploy.yml file.

@kdheepak
Copy link
Contributor

This is not necessary Franklin related, but if you use giscus you can use the following snippet in a click event handler for your toggle for light / dark mode, to switch your comment's theme to light or dark mode as well.

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

@tlienart tlienart added the docs label May 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants