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] support run-time theme switch, and visitor preferred theme #449

Merged
merged 6 commits into from
Apr 27, 2024

Conversation

calfzhou
Copy link
Contributor

Before this change:

  • build time:
    • Inject different pieces of css code according to style.prefers_theme.
  • run time:
    • Visitor cannot switch theme among light / dark / auto.

With this change:

  • build time:
    • Use the same piece of css code, which adjust colors based on html[data-theme] and @media (prefers-color-scheme).
    • Set the default value of html[data-theme] according to style.prefers_theme.
    • The site owner can expose a button / icon to let visitor switch theme among light / dark/ auto.
  • run time:
    • When page is loading, check visitor preferred theme settings (in local storage). If there is, update html[data-theme] to his prefer dynamically.
    • If the site owner enabled visitor preferred theme, the visitor can switch theme among light / dark / auto. His choice will be save (in local storage), and will be applied to all his further visits.

把构建时候根据 style.prefers_theme 的取值直接写死到 css 中的亮色/暗色/自动样式,调整为根据 html[data-theme]@media (prefers-color-scheme) 自适应的样式。这样给运行时(访客浏览页面的时候)访客自行改变色系提供了可能性。

站点作者可以在喜欢的位置放置一个按钮,供访客自行切换色系,比如放在 leftbar 底部(在 icons.yml 中增加了 default:theme 以便直接使用,并提供 switchTheme() 函数用于切换主题):

image

提供的 switchTheme() 函数可以在 亮色 / 暗色 / 跟随系统 三者之间循环切换,并将当前的选择记录在 local storage 中。访客手动切换时,页面会弹出 toast,提示切换到了哪个状态,如:

  • image
  • image
  • image

另:如果站点开启了 giscus 评论,评论区的配色也会同步调整。

Copy link

stackblitz bot commented Apr 27, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@xaoxuu xaoxuu merged commit 09d91d0 into xaoxuu:main Apr 27, 2024
@xaoxuu
Copy link
Owner

xaoxuu commented Apr 27, 2024

谢谢大佬 🤝

@calfzhou
Copy link
Contributor Author

别别,您才是真·大佬。这个主题真的是太棒了,我完全是因为找到了这个主题,才确定使用 hexo 的。

特别感谢制作了如此优秀的项目!

panoshu pushed a commit to panoshu/hexo-theme-stellar that referenced this pull request May 5, 2024
@calfzhou calfzhou deleted the feature/switchable-theme branch May 13, 2024 15:55
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