Profile picture of Michael GroßklausMichael Großklaus

RSS
Color scheme

Collecting some frontend related notes, thoughts, bookmarks and tips.

  1. There is a storage event that gets fired when the local storage for a site is updated:

    // Listen for local storage changes on our theme key. This lets
    // one tab to be notified if the theme is changed in another,
    // and update itself accordingly.
    window.addEventListener("storage", (e) => {
        if (e.key == LOCAL_STORAGE_KEY_THEME) {
            setThemeFromLocalStorageOrMediaPreference()
        }
    })
  2. Implementing a light and dark theme based on the user's OS settings has become quite popular. But often users just want their OS to be dark, while they prefer reading a website with a light background, for example. It is therefore considered good practice to offer a theme toggle, that allows the user to change the theme of the website regardless of their OS theme. […]

  3. There is a change event that can be used to listen to media query changes:

    const mediaQuery = window.matchMedia("…");
    
    mediaQuery.addEventListener("change", (e) => {});