do-community / developer-glossary

A community-built glossary of technical terms.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`Toggle view mode` doesn't work for new users

RascalTwo opened this issue · comments

If the browser's localStorage theme is not light or dark, the button does not work.

It is initially null, therefore it fails to work for new users.

output

Expected Behavior

Pressing the Toggle view mode buttons toggles between dark/light themes.

Current Behavior

Pressing the button does nothing - no errors in console or any feedback.

Steps to Reproduce

  • Remove the theme key from localStorage.
    • Clear browser history
    • Run this JavaScript:
      localStorage.removeItem('theme')
  • Refresh the page
  • Click the Toggle view mode button

Possible Solution

A one-line JavaScript solution running in onload could look like this:

// If the theme is not light or dark, set it to dark by default
if (!['light', 'dark'].includes(localStorage.getItem('theme'))) localStorage.setItem('theme', 'dark')

The essence of the problem is not handling theme values that are not light/dark, so either ensuring that the value can never be anything but light/dark or handling whenever it's not light/dark would solve the problem.

@RascalTwo Thanks for solution.

Fixed in #39