seanmturley / natureddit

A simple Reddit client, featuring a landing page dedicated to nature with posts pulled from a curated list of subreddits.
0 stars 1 forks source link

Add functionality to dark/light mode toggle button #140

Closed seanmturley closed 9 months ago

seanmturley commented 9 months ago

Description

The user will be able to switch between dark and light colour themes (dark is the default).

Implementation

See this CSS Tricks article for an example of the above approach.

seanmturley commented 9 months ago

The lightTheme is now controlled by App state, and can be toggled by the ToggleSwitch in the DropdownMenu

To avoid an extra layer of prop drilling and reduce overall complexity, the NavBar component was removed. Instead, the PageHeadings, SearchBar, and DropdownMenu are now directly rendered inside the MainLayout.

There may be room for further refinement by lifting components all the way up to the App level, but this is somewhat limited by the need for useNavigation inside the MainLayout (useNavigation has to be called within a Router).

seanmturley commented 9 months ago

There may be room for further refinement by lifting components all the way up to the App level...

Upon further investigation, this turns out not to be practical as useLocation and useNavigate are used in PageHeadings and SearchBar respectively, both of which must be called within the context of a Router.

seanmturley commented 9 months ago

Installed useLocalStorage custom React Hook (repo, npm).

It's very easy to use and appears to work right out the box.

The user's theme selection is now persisted in local storage.