amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

dark mode for AMP pages (it should be automatic)

Open willfs84 opened this issue 7 years ago • 14 comments

apologies if this is already being worked on or has already been ruled out but AMP provides an amazing opportunity to easily, quickly and properly make a 'dark mode' for a huge amount of web pages. More companies are looking at doing this for normal webpages (Samsung Internet Browser, Firefox on iOS and now Google Chrome, see this post for more info https://9to5google.com/2019/02/19/android-chrome-webview-web-dark-mode/ ) . Of course it would be ideal if this would happen automatically (if your OS or browser is set to dark mode, AMP should be able to see this and serve up a dark version of the AMP page).

willfs84 avatar Feb 20 '19 01:02 willfs84

We're looking to do this as well, plus give the user a choice of auto / dark / normal, and save the preference in a cookie or localstorage.

What needs to happen to make these work?

archon810 avatar Oct 04 '19 00:10 archon810

I think there also needs to be support for a user to set some persistent preference to indicate whether they want dark mode or light mode. This preference could then be applied in the page, for example, by amp-dynamic-css-classes. But maybe this can be done at the framework level.

cc @tomayac

westonruter avatar Oct 04 '19 00:10 westonruter

It would be great if the dark mode preference could persist with the user as they navigate between arbitrary AMP pages across multiple origins, though this has some privacy implications.

westonruter avatar Oct 04 '19 00:10 westonruter

The requirements you mentioned are all covered by my <dark-mode-toggle>. In an AMP context you would use it with a class that you can toggle on and off, which is called method ② in the documentation. I’m not sure what it would take to convert my custom element into an AMP element, but maybe the logic at least could be reused. Happy to help out or answer questions you may have.

tomayac avatar Oct 04 '19 06:10 tomayac

The only way to do this in AMP currently as I can tell is use amp-script to get/set the user preference for dark/light mode in localStorage. Since amp-script only allows mutations to children elements, it would not be able to change a class name on the body. In order to do this, amp-script would need to call AMP.setState() to modify the page state as a whole to set some flag which is then used to update the body class. However, I'm not clear on whether setting state in this way should be permissible on page load before a user gesture. See https://github.com/ampproject/amphtml/issues/24862#issuecomment-537659029.

westonruter avatar Oct 04 '19 17:10 westonruter

OK, this approach using amp-script will not work. Using amp-script to update state would only update bindings inside the subtree, so this could not be used to update the class on the body. See https://github.com/ampproject/amphtml/issues/24862#issuecomment-538566499.

westonruter avatar Oct 04 '19 22:10 westonruter

Do we maybe need a special component for dark mode? One that would support storing preferences for auto/light/dark?

archon810 avatar Oct 22 '19 16:10 archon810

I've implemented Prefers-color-scheme into my AMP site, this works perfectly for enabling dark mode on my site when the system is set to dark mode.

a-haan avatar Oct 27 '19 11:10 a-haan

I've implemented Prefers-color-scheme into my AMP site

Sure, getting it to work is perfectly feasible. The discussion in the lower part of the thread is around a toggle as in the footer on v8.dev.

tomayac avatar Oct 27 '19 11:10 tomayac

Here's a codepen with a simple theme switcher for dark and light mode (note that this does not adhere to prefers-color-scheme, but acts as a manual switch):

Screen Shot 2019-11-19 at 13 59 23

Screen Shot 2019-11-19 at 13 59 14

tomayac avatar Nov 19 '19 13:11 tomayac

Also, the AMP UI that appears in browsers should respect dark mode too (it doesn't, see attached screenshot). Surely this an easy and fast fix? AMPbrowserdarkUI

willfs84 avatar Dec 12 '19 22:12 willfs84

Any news on this?

ProH4Ck avatar May 02 '20 16:05 ProH4Ck

Definitely need a fix for this. While relying on prefers-color-scheme is an option, it's not ideal. Just because I prefer light scheme globally doesn't mean I don't wish to have some websites in dark mode.

asadkn avatar Jan 19 '21 21:01 asadkn

Hello 👋, I have added an initial draft PR #36958 to add support for the dark theme. I have registered a global action that can be used to toggle the dark theme mode classes on the body and store the user's preferences in local storage and by default relies on prefers-color-scheme value if the local storage is not set. As the usage of localStorage is forbidden, how can proceed to get permission for the usage of localStorage in this scenario?

dark-mode

deepaklalwani97 avatar Nov 17 '21 06:11 deepaklalwani97