Your dark mode toggle is broken

栏目: IT技术 · 发布时间: 5年前

内容简介:With more and more sites gaining support for dark mode and adding very pretty toggles to their design, it’s important to implement them correctly. Unfortunately most sites do not and as a result their toggle is broken.Most websites that have a dark and lig

With more and more sites gaining support for dark mode and adding very pretty toggles to their design, it’s important to implement them correctly. Unfortunately most sites do not and as a result their toggle is broken.

Most websites that have a dark and light mode have very pretty toggles switching between some sort of sun icon, and some sort of moon icon. It’s definitely the type of element to spend (too much) time on to make it a pretty animation but it’s also important to think about what functionality you’re designing.

Do we need a toggle?

Before explaining why your dark mode toggle is broken let’s ask: do we really need a toggle to support dark mode?

The answer is not really. We can support dark mode by using the prefers-color-scheme option built into browsers, that gives you one of three values:

  • light
  • dark
  • no-preference

With this we can implement our dark and light mode (and, for no-preference , choose one or the other as our default). But the user might prefer your dark mode but their operating systems light mode and in that case, they’re out of luck. They can’t choose dark mode for just your site. They might not even realize your site has a dark mode.

So you’ll probably want to tell visitors your have a dark mode and let them choose which one they prefer. And a good way to do that is of course with a fancily animated toggle. (No judgement, I love a good animated toggle).

The issue

The issue is that most toggles are two states: you either have light mode or dark mode and the choice is forever stored in a cookie or localStorage. And that makes your dark mode toggle broken.

Just like prefers-color-scheme toggles between three states, your dark mode toggle should also toggle between three states:

  • light
  • dark
  • system

Dark and light seem pretty self-explanatory, but what do we mean with system? System is whatever the prefers-color-scheme css media query gives you: light, dark or no-preference.

When the system gives you no-preference I guess you can do what you want. But what should happen if the system tells you the user prefers light but your localStorage cookie says dark?

Most implementations I’ve seen, you’re out of luck. Once that cookie is set, you’re getting that version, and there’s no way to say “please unset the cookie and let the operating system decide”. It’s good to realize users make an explicit choice when changing their OS theme:

  • Maybe I prefer my OS to be light but to switch to dark mode at sunset.
  • Maybe I prefer dark mode in general, but sometimes the sun shines on my screen and then I switch to light mode to make sure I can still read what’s on my screen.

In both cases I can configure my OS to do this automatically for me, but your website will still be way too bright or unreadable dark.

How to decide?

Now the crux is: how do you decide which is more important: the explicit choice a user makes for their OS theme, or the one they made on your site?

Of course there’s no right answer there. Some people will prefer your site to always be in one theme while others want you to respond to their OS theme change. And the only way to support that is by making “let my operating system decide” an explicit option for your site.

By adding this third “system” option to your toggle, you can support not only the people that explicitly choose a color scheme for your site , but also the ones that initially want to have a look at your different versions, but otherwise prefer you to defer to the operating system .

Your dark mode toggle is broken

A good example to follow is Stack Overflow’s implementation, which has these three options of light, dark and system. They wrote about here: Building dark mode on Stack Overflow .

Unfortunately they don’t go into why they chose a three-state implementation but they’re a good example to follow: make sure your dark mode toggle can switch to the system value.

Your dark mode toggle is broken Hi, I'm Kilian. I make Polypane , the browser for responsive web development and design . If you're reading this site, that's probably interesting to you. Try it out!

Related articles

Fromscratch 1.4.0

Apps , 23 July 2018

I just released FromScratch 1.4.0 with a number of often requested new features: a light theme, portable mode support, installers for all major platforms, full screen support and a handy overview of all the smart shortcuts FromScratch has. Fromscratch is a simple but smart notetaking app. It gets out of your way with no “note management”…

Your chatbot should have a game loop

Chatbots , 26 April 2017

Many of the “smart” chatbots with Natural Language Processing and Artificial Intelligence still have a hard time keeping a conversation going. They keep forgetting whatever it was you were talking about even if you can see it plainly in the message right above! …

The box model is not layout

Design , 8 October 2019

One term that keeps coming up in the design community as a stand-in for layout is “box model”, for example in “Why don’t design tools have the box model?” The CSS Box model is a well-defined term though, and it does not do layout. If we keep referring to our imaginary perfect layout system in design…


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机算法

计算机算法

霍罗威茨 / 机械工业 / 2006-1 / 55.00元

本书是计算机算法在设计与分析方面的一本经典著作。书中介绍了算法和算法性能的基本知识,基本的数据结构知识,重点讨论了不同的算法设计策略,研究了下界理论等,提供了计算机算法的设计技术和有效的算法分析,以及大量的详细实例和实际应用。同时,对NP难和NP完全问题能否有效求解进行了分析。本书还汇聚了各种随机算法与并行算法的充分比较。   本书为读者提供了当前流行的对象设计语言C++的实现版本,适合作为......一起来看看 《计算机算法》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具