TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Elegant three-state 'Dark mode' toggle, how best to implement?

1 点作者 evolve2k大约 2 个月前
Anyone implemented a nice three-state dark mode toggle? Love to hear your tips, ideas and even links to examples&#x2F;code are welcome.<p>This feature is more involved that first look as this HN post unpacks.. https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=23197966<p>My current wishlist:<p>- Three states (system&#x2F;light&#x2F;dark) - Preference stored for next time - Two icons light&#x2F;dark (click selected icon a second time to &#x27;turn it off&#x27; and return to system) - Vanilla JS&#x2F;CSS&#x2F;HTML - Leans into CSS<p>This implementation comes pretty close. https:&#x2F;&#x2F;www.bram.us&#x2F;2020&#x2F;04&#x2F;26&#x2F;the-quest-for-the-perfect-dark-mode-using-vanilla-javascript<p>This post points to a bunch of great three-state dark mode articles in its references. https:&#x2F;&#x2F;www.brycewray.com&#x2F;posts&#x2F;2024&#x2F;01&#x2F;its-tri-state-switch-time<p>Sidenote: I found some discussion towards a w3c web preferences API to make this easier but this is far from ready or resolved.<p>https:&#x2F;&#x2F;github.com&#x2F;WICG&#x2F;web-preferences-api https:&#x2F;&#x2F;front-end.social&#x2F;@kizu&#x2F;113160248251763608

4 条评论

evolve2k大约 2 个月前
I started looking at this as I&#x27;d like to add one to a few of my sites, but soon started down a rabbit hole:<p>1. Many code examples just handled light&#x2F;dark and once clicked you cant easily get back to the system setting.<p>2. Seems folks going about this many different ways.<p>3. The Flash of Unstyled Content issue.<p>4. Im a big tailwind fan, but I think a clean implementation should exist just with the most vanilla CSS&#x2F;HTML and very light use of JS as necessary.<p>5. Storing the preference once set<p>6. Discussions on adding this &quot;simple&quot; (seeming) feature bought out a wall of comments when someone suggested adding it right here on HN, and as of today, HN itself still doesnt have this &quot;simple&quot; feature.
评论 #43371061 未加载
stop50大约 2 个月前
I did once a simple implementation: light, dark and system. Data was stored in localstorage. The default was system and used quite of bit scss to implement. Effectivly it consisted of 3 parts: and theming mixing that accepted the background and foreground color, an theme mixin that generated the surrounding &quot;body.$schema&quot; and &quot;@media (prefers-color-scheme: $schema)&quot; and at last an fallback block that only activated when it doesn&#x27;t support prefers-color-scheme.<p>The javascript was pretty short not even 50 lines and no dependency.
评论 #43371145 未加载
Terr_大约 2 个月前
&gt; Anyone implemented a nice three-state dark mode toggle?<p>Anecdotally, I basically never adjust light&#x2F;dark mode, I pick what I want once and leave it alone forever. (Is this common? Does anyone have telemetry about it?)<p>So the only reason for it to take up prominent real-estate is to ensure it is found at the same initial moment that I want to find it. Something like that could also be done with some kind of quickstart or initial-settings wizard.<p>So my only-half-joking crotchety old-school response would be for a settings screen, with three mutually exclusive option-bullets.
evolve2k大约 2 个月前
Credit to Ben Schwarz for providing me with this excellent implementation.<p>Supports: Light&#x2F;Dark&#x2F;System as well as storing the user preference. Implements via a simple dropdown select box.<p><a href="https:&#x2F;&#x2F;codepen.io&#x2F;benschwarz&#x2F;pen&#x2F;bGPyboB" rel="nofollow">https:&#x2F;&#x2F;codepen.io&#x2F;benschwarz&#x2F;pen&#x2F;bGPyboB</a>