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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

The Quest for the Perfect Dark Mode (Gatsby/React)

74 点作者 joshwcomeau大约 5 年前

14 条评论

joshwcomeau大约 5 年前
Hey thread, author here.<p>I don&#x27;t have the energy to debunk some of these replies, but let me quickly say:<p>1. The reason that this is a hard problem has nothing to do with React, or Gatsby. It has to do with the strategy of precompiling HTML and not having a runtime server, an approach with many many benefits (described here: <a href="https:&#x2F;&#x2F;joshwcomeau.com&#x2F;gatsby&#x2F;a-static-future&#x2F;" rel="nofollow">https:&#x2F;&#x2F;joshwcomeau.com&#x2F;gatsby&#x2F;a-static-future&#x2F;</a>). If you&#x27;re wondering why anyone would put up with all this tomfoolery for a dark mode, please read that so you can understand the benefits to this approach.<p>2. &quot;React is overkill for static sites&quot; is a debate I am not trying to have, but I can say that for my blog (the site that this post is about), React has allowed me to do lots of cool things that would have been much harder otherwise. And using Gatsby means that I have first-meaningful-paint performance that beats a PHP template with no JS, since my site doesn&#x27;t have to do any database lookups or real-time HTML generation. I am not saying that this is the right approach for every website, but it was 100% the right choice for this one.<p>4. Many of these replies are making suggestions that are discussed in the article itself. CSS variables are not an alternative to this solution, they _are_ the solution (my solution wouldn&#x27;t work without them!). Please read the post before forming an opinion about it.<p>I hope that helps clarify some things! Hope you&#x27;re all doing alright and staying home.
评论 #22926332 未加载
评论 #22925851 未加载
评论 #22926281 未加载
sneak大约 5 年前
Reading this site reminded me of this:<p><a href="https:&#x2F;&#x2F;motherfuckingwebsite.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;motherfuckingwebsite.com&#x2F;</a><p>It&#x27;s also worth mentioning that TFA&#x27;s webpage <i>is broken</i>.<p>Despite all his dozens of lines of javascript for his &quot;dark background&quot; feature, it <i>totally fails to render</i> (resulting in a blindingly white blank page, ha!) if you have cookies off (it&#x27;s a static Gatsby site, which does not need and cannot use cookies).<p>For all his hard work, his site is a blank page.<p>(Here&#x27;s why, if you care: <a href="https:&#x2F;&#x2F;sneak.berlin&#x2F;20200211&#x2F;your-website&#x2F;" rel="nofollow">https:&#x2F;&#x2F;sneak.berlin&#x2F;20200211&#x2F;your-website&#x2F;</a>)
edhelas大约 5 年前
Damn React is complicated. I added Dark mode in my app in a few lines of CSS (<a href="https:&#x2F;&#x2F;github.com&#x2F;movim&#x2F;movim&#x2F;blob&#x2F;master&#x2F;public&#x2F;theme&#x2F;css&#x2F;color.css#L13" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;movim&#x2F;movim&#x2F;blob&#x2F;master&#x2F;public&#x2F;theme&#x2F;css&#x2F;...</a>) and used the variables across the whole code. I just have to toggle a class on the body and boom, it works.<p>Guys, you don&#x27;t need all that JS, seriously. It&#x27;s slow, it&#x27;s useless most of the time. And in the end you have to spend hours for something so simple.<p>Theming the colors of your website is the role of one tech: CSS. That&#x27;s it, you don&#x27;t needs layers and layers of Javascript to change that.<p>Let&#x27;s go back to server side rendering, a bit of HTML + CSS and JS and we&#x27;re done.
评论 #22925102 未加载
评论 #22925128 未加载
评论 #22925047 未加载
评论 #22924945 未加载
评论 #22925118 未加载
pcr910303大约 5 年前
Seriously! Should every Gatsby blog post on HN have this useless &#x27;React is overkill for static sites&#x27; talk?<p><i></i><i></i><i>Gatsby is a static site generator!</i><i></i><i></i><p>Running Gatsby does NOT make the whole website run on React. It makes a bunch of HTML pages that are statically served - and some JS progressively enhances it.<p>The complexity in this blog post has nothing to do with React, it&#x27;s due to the author&#x27;s complex requirements that are very beneficial to the user.<p>Yes, the web is full of bloated pages. No, that&#x27;s not the topic here. It&#x27;s so frustrating for people to complain about React on this great post - comment about the post, people!
WiseWeasel大约 5 年前
Perfect dark mode doesn’t have a toggle; it uses the prefers-color-scheme CSS media query to key off your OS theme preference. At that point, there&#x27;s no problem with Gatsby or Next.
评论 #22924206 未加载
评论 #22925033 未加载
评论 #22924571 未加载
评论 #22924959 未加载
welcometomiami大约 5 年前
Did anyone else happen to read this and think of <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Perfect_Dark" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Perfect_Dark</a>?
评论 #22926742 未加载
memco大约 5 年前
The theme toggle button plays a sound when you click it. So not only does this require state for the theme, but also the sound. Also curious is that clicking the button to mute sound plays a sound.<p>I believe every OS defaults to silent actions for every button. Games, on the other hand, often have sounds for hover&#x2F;focus and click. I&#x27;m not sure what&#x27;s the better option, but if we&#x27;re going to default to the OS for theme choice shouldn&#x27;t we also default to the OS for UI sound choice? I don&#x27;t know if there&#x27;s any way to check that via CSS or JS. But I hope that if sites are going to start implementing this more often that browsers at least expose an API with a user preference if not the OS.
评论 #22928119 未加载
chrismorgan大约 5 年前
I like my dark theme implementation which supports the same operations, but a tad lighter, more flexible and not-Reacty: a toggle, falling back to the prefers-color-scheme value if it hasn’t been toggled <i>or if JavaScript is disabled</i> (and I don’t think Josh’s does that), and avoiding a flash of unstyled content. I wrote about it at <a href="https:&#x2F;&#x2F;chrismorgan.info&#x2F;blog&#x2F;dark-theme-implementation&#x2F;" rel="nofollow">https:&#x2F;&#x2F;chrismorgan.info&#x2F;blog&#x2F;dark-theme-implementation&#x2F;</a>.<p>The problem as a whole is definitely a tad fiddly, but I still think that trying to do things in React is a substantial part of what’s making it complicated here. I instead used regular stylesheets and the media attribute to control the toggling, and I think that’s the best solution, because it’s what makes it work perfectly with and without JavaScript. (I used external stylesheets, but you could inline them if you wanted; the media attribute works on &lt;style&gt; as well as &lt;link&gt;.)<p>Josh, I’d be interested in your opinion on it. It took me several iterations to end up where I did (definitely didn’t occur to me to alter the media attribute at first!), and I think it’d apply cleanly to such sites as yours, and be better for the toggling.
darepublic大约 5 年前
Dark mode shouldn&#x27;t be harder with SSR apps, you would just send back the dark mode CSS in the initial GET response. Complexity around this might be related to trying to work with out of the box framework settings, but comparing this problem to some advanced image recognition, while admittedly a joke.. is just over-representing the problem imo. I&#x27;m sorry gatsby didn&#x27;t give you an easy way to do this, but if you understand the principles of web applications this should not be an epic quest.
评论 #22926261 未加载
评论 #22925583 未加载
swlkr大约 5 年前
Wow this website is really creative.<p>Not sure about the dark mode implementation, since it rendered a white screen for me with no content on iOS, but on desktop it worked.<p>Also the general feel of the website is really nice, a lot of nice little touches from the sounds to the &quot;nonstop confetti party&quot; when you sign up for the newsletter.
评论 #22926944 未加载
artursapek大约 5 年前
We have had color schemes on Cryptowatch (<a href="https:&#x2F;&#x2F;cryptowat.ch" rel="nofollow">https:&#x2F;&#x2F;cryptowat.ch</a>) for years, and since a year ago we even let users create custom color schemes within the web app [1]<p>We use CSS variables for this. The stylesheet doesn&#x27;t have any hard-coded colors, it only references a set of several variables and many blends of these variables. This way, you can let a user adjust a variable using a color picker and the entire website adjusts dynamically as they do it. It also lets you render any color scheme properly in your SSR (unlike the approach in this article).<p>[1] <a href="https:&#x2F;&#x2F;guides.cryptowat.ch&#x2F;real-time-charting-interface&#x2F;custom-theming" rel="nofollow">https:&#x2F;&#x2F;guides.cryptowat.ch&#x2F;real-time-charting-interface&#x2F;cus...</a>
评论 #22927089 未加载
dgellow大约 5 年前
How does the “shining stars effect” for the sentence “the perfect dark mode” works? That’s really neat.<p>(I’m on mobile so cannot reverse)
评论 #22926400 未加载
mirthflat83大约 5 年前
Wow. What a beautiful website!
animalgonzales大约 5 年前
this is way too complicated and convoluted. just use global css selectors.
评论 #22926217 未加载