-
Notifications
You must be signed in to change notification settings - Fork 13
Feature/theme toggle #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * | ||
| * ThemeToggle Component | ||
| * | ||
| */ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { connect } from 'react-redux'; | ||
| import { createStructuredSelector } from 'reselect'; | ||
| import styled from 'styled-components'; | ||
| import { themeActionCreators } from '@app/containers/Theme/reducer'; | ||
| import { selectTheme } from '@app/containers/Theme/selectors'; | ||
|
|
||
| const ToggleButton = styled.button` | ||
| background: var(--color-accent-muted); | ||
| border: 1px solid var(--color-border); | ||
| border-radius: 50%; | ||
| width: 40px; | ||
| height: 40px; | ||
| padding: 0; | ||
| color: var(--color-text); | ||
| cursor: pointer; | ||
| font-size: 20px; | ||
| line-height: 1; | ||
| font-family: inherit; | ||
| transition: all var(--transition-fast); | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| flex-shrink: 0; | ||
|
|
||
| &:hover { | ||
| background: var(--color-accent); | ||
| color: #fff; | ||
| border-color: var(--color-accent); | ||
| } | ||
|
|
||
| &:active { | ||
| transform: scale(0.98); | ||
| } | ||
| `; | ||
|
|
||
| export function ThemeToggle({ theme, onToggle }) { | ||
| return ( | ||
| <ToggleButton | ||
| onClick={onToggle} | ||
| aria-label={theme === 'dark' ? 'Switch to light mode' : 'Switch to dark mode'} | ||
| data-testid="theme-toggle" | ||
| > | ||
| {theme === 'dark' ? '☀️' : '🌙'} | ||
| </ToggleButton> | ||
| ); | ||
| } | ||
|
|
||
| ThemeToggle.propTypes = { | ||
| theme: PropTypes.string.isRequired, | ||
| onToggle: PropTypes.func.isRequired | ||
| }; | ||
|
|
||
| const mapStateToProps = createStructuredSelector({ | ||
| theme: selectTheme() | ||
| }); | ||
|
|
||
| const mapDispatchToProps = (dispatch) => ({ | ||
| onToggle: () => dispatch(themeActionCreators.toggleTheme()) | ||
| }); | ||
|
|
||
| export default connect(mapStateToProps, mapDispatchToProps)(ThemeToggle); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,48 @@ | ||||||||||||||||||
| /* | ||||||||||||||||||
| * | ||||||||||||||||||
| * Theme reducer | ||||||||||||||||||
| * | ||||||||||||||||||
| */ | ||||||||||||||||||
| import { createActions } from 'reduxsauce'; | ||||||||||||||||||
| import produce from 'immer'; | ||||||||||||||||||
|
|
||||||||||||||||||
| export const THEME_PAYLOAD = { | ||||||||||||||||||
| THEME: 'theme' | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| export const initialState = { | ||||||||||||||||||
| theme: 'light' // 'light' or 'dark' | ||||||||||||||||||
| }; | ||||||||||||||||||
|
|
||||||||||||||||||
| export const { Types: themeActionTypes, Creators: themeActionCreators } = createActions({ | ||||||||||||||||||
| setTheme: [THEME_PAYLOAD.THEME], | ||||||||||||||||||
| toggleTheme: null | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| function persistTheme(theme) { | ||||||||||||||||||
| if (typeof window !== 'undefined') { | ||||||||||||||||||
| localStorage.setItem('theme', theme); | ||||||||||||||||||
| document.documentElement.setAttribute('data-theme', theme); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| export function themeReducer(state = initialState, action) { | ||||||||||||||||||
| return produce(state, (draft) => { | ||||||||||||||||||
| switch (action.type) { | ||||||||||||||||||
| case themeActionTypes.SET_THEME: | ||||||||||||||||||
| draft.theme = action.theme; | ||||||||||||||||||
| persistTheme(action.theme); | ||||||||||||||||||
|
Comment on lines
+32
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If 🛡️ Proposed fix — guard against invalid values case themeActionTypes.SET_THEME:
+ if (action.theme !== 'light' && action.theme !== 'dark') break;
draft.theme = action.theme;
persistTheme(action.theme);
break;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
| break; | ||||||||||||||||||
| case themeActionTypes.TOGGLE_THEME: { | ||||||||||||||||||
| const newTheme = draft.theme === 'light' ? 'dark' : 'light'; | ||||||||||||||||||
| draft.theme = newTheme; | ||||||||||||||||||
| persistTheme(newTheme); | ||||||||||||||||||
| break; | ||||||||||||||||||
| } | ||||||||||||||||||
| default: | ||||||||||||||||||
| break; | ||||||||||||||||||
| } | ||||||||||||||||||
| }); | ||||||||||||||||||
|
Comment on lines
+22
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side effects (
The persistence and DOM update should be moved to a middleware, a saga, or a ♻️ Suggested approach — move side effects out of the reducerKeep the reducer pure: -function persistTheme(theme) {
- if (typeof window !== 'undefined') {
- localStorage.setItem('theme', theme);
- document.documentElement.setAttribute('data-theme', theme);
- }
-}
export function themeReducer(state = initialState, action) {
return produce(state, (draft) => {
switch (action.type) {
case themeActionTypes.SET_THEME:
draft.theme = action.theme;
- persistTheme(action.theme);
break;
case themeActionTypes.TOGGLE_THEME: {
const newTheme = draft.theme === 'light' ? 'dark' : 'light';
draft.theme = newTheme;
- persistTheme(newTheme);
break;
}
...
}
});
}Then in a layout component or // In a useEffect watching the theme selector:
useEffect(() => {
localStorage.setItem('theme', theme);
document.documentElement.setAttribute('data-theme', theme);
}, [theme]);🤖 Prompt for AI Agents |
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| export default themeReducer; | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { createSelector } from 'reselect'; | ||
| import { initialState } from './reducer'; | ||
|
|
||
| const selectThemeDomain = (state) => state.theme || initialState; | ||
|
|
||
| export const selectTheme = () => createSelector(selectThemeDomain, (substate) => substate.theme); | ||
|
|
||
| export const selectIsDarkMode = () => createSelector(selectThemeDomain, (substate) => substate.theme === 'dark'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initialStatehardcodes'light'— causes a flash of wrong theme on page load.When a user with a saved
'dark'preference revisits, the Redux store initializes to'light'. Until an action is dispatched to restore the saved theme (if that even happens), CSS variables will render in the wrong mode, causing a visible flash. A common fix is to seedinitialStatefromlocalStoragein the browser, or apply thedata-themeattribute in a blocking<script>in_document.jsbefore React hydrates.🤖 Prompt for AI Agents