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

feat: add automatic dark and light mode detect #26

Merged
merged 1 commit into from
Apr 28, 2023

Conversation

kusyka911
Copy link
Contributor

@kusyka911 kusyka911 commented Apr 27, 2023

What problem does this PR solve?

Automatically detect user's theme preferences from OS settings.

Is this PR adding a new feature?

site configuration:

[params]
defaultColor = 'auto'

You may also need to clear your current settings in browser's local storage or use incognito mode.
image

When there is no preferred theme color set by site owner, and by site user, current theme will be automatically detected based on user's OS settings.
If site owner set preferred color scheme to dark or light it will be used by default.
If site user changed preferred color scheme at least one, it will be saved in local storage and this value will be used until it is cleared manually.

In case of ruining site in JS disabled environment default color scheme will be detected automatically by CSS media query and can-not be set by the Site owner. Theme switcher button will not work for user because JS is required for it.

Some web browsers like brave may enforce color scheme to light for privacy reasons even when OS color preferences are set to dark.

Is this PR related to any issue or discussion?

PR Checklist

  • I have verified that the code works as described/as intended.
  • This change adds a social icon which has a permissive license to use it.
  • This change does not include any external library/resources.
  • This change does not include any unrelated scripts (e.g. bash and python scripts).
  • I have enabled maintainer edits for this PR.

@netlify
Copy link

netlify bot commented Apr 27, 2023

Deploy Preview for hugo-blog-awesome ready!

Name Link
🔨 Latest commit 78e5a9b
🔍 Latest deploy log https://app.netlify.com/sites/hugo-blog-awesome/deploys/644ac7cf7af7980008e73cb6
😎 Deploy Preview https://deploy-preview-26--hugo-blog-awesome.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Copy link
Owner

@hugo-sid hugo-sid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kusyka911, for adding automatic light/dark mode detection feature to this theme.

I appreciate your desire to improve this theme.

Have a good day. Happy blogging :)

@@ -2,7 +2,7 @@
<html lang="{{ .Site.LanguageCode }}">
{{- partial "head.html" . -}}

<body data-theme="{{ .Site.Params.defaultColor }}" class="notransition">
<body data-theme="{{ .Site.Params.defaultColor | default "auto" }}" class="notransition">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kusyka911, I like what you have done here: setting the default color mode to auto.

body[data-theme="dark"] {
color: $dark-text-base-color;
background-color: $dark-black;
@mixin dark-mode {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using mixin is a great idea !

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 😄

if (lsState) return lsState;

let state;
switch (config) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel, using switch/case is a nice idea for this particular use case (color theme change).

@hugo-sid hugo-sid merged commit cfa6ca6 into hugo-sid:main Apr 28, 2023
@hugo-sid
Copy link
Owner

I have added documentation regarding this here:

sitename = "Awesome hugo blog"
defaultColor = "dark" # set color mode: dark, light, auto
# Setting it to 'auto' applies the color scheme based on the visitor's device color preference.If you don't specify anything, ignore this parameter, or leave it blank,
# the default value is set to 'auto'.
# You can take a look at layouts/index.html for more information.
description = "Minimal Hugo blog theme with light and dark mode support"
mainSections = ['posts']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants