feat: refactor theme state
added hooks for is light or dark theme and cleaned up any referencespull/3083/head
parent
c20bda442e
commit
4f44a7a5fa
@ -1,34 +1,19 @@
|
||||
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
|
||||
import { ThemeStateType } from '../../themes/constants/colors';
|
||||
|
||||
export const APPLY_THEME = 'APPLY_THEME';
|
||||
// TODO Move primary color into this slice
|
||||
export const initialThemeState: ThemeStateType = 'classic-dark' as ThemeStateType;
|
||||
|
||||
export const applyTheme = (theme: ThemeStateType) => {
|
||||
return {
|
||||
type: APPLY_THEME,
|
||||
payload: theme,
|
||||
};
|
||||
};
|
||||
const themeSlice = createSlice({
|
||||
name: 'theme',
|
||||
initialState: initialThemeState,
|
||||
reducers: {
|
||||
updateTheme(_, action: PayloadAction<ThemeStateType>) {
|
||||
return action.payload;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const initialThemeState: ThemeStateType = 'classic-dark';
|
||||
|
||||
export const reducer = (
|
||||
state: any = initialThemeState,
|
||||
{
|
||||
type,
|
||||
payload,
|
||||
}: {
|
||||
type: string;
|
||||
payload: ThemeStateType;
|
||||
}
|
||||
): ThemeStateType => {
|
||||
switch (type) {
|
||||
case APPLY_THEME:
|
||||
return payload;
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export const actions = {
|
||||
applyTheme,
|
||||
};
|
||||
export const { actions, reducer } = themeSlice;
|
||||
export const { updateTheme } = actions;
|
||||
export const defaultThemeReducer = reducer;
|
||||
|
@ -1,9 +1,17 @@
|
||||
import { createSelector } from '@reduxjs/toolkit';
|
||||
import { useSelector } from 'react-redux';
|
||||
import { ThemeStateType } from '../../themes/constants/colors';
|
||||
import { StateType } from '../reducer';
|
||||
import { checkDarkTheme, checkLightTheme } from '../../util/theme';
|
||||
import { StateType } from '../reducer';
|
||||
|
||||
export const getTheme = (state: StateType): ThemeStateType => state.theme;
|
||||
|
||||
export const isDarkTheme = (state: StateType): boolean => checkDarkTheme(state.theme);
|
||||
const getIsDarkTheme = createSelector(getTheme, (state): boolean => checkDarkTheme(state));
|
||||
|
||||
const getIsLightTheme = createSelector(getTheme, (state): boolean => checkLightTheme(state));
|
||||
|
||||
export const useTheme = () => useSelector(getTheme);
|
||||
|
||||
export const useIsDarkTheme = () => useSelector(getIsDarkTheme);
|
||||
|
||||
export const isLightTheme = (state: StateType): boolean => checkLightTheme(state.theme);
|
||||
export const useIsLightTheme = () => useSelector(getIsLightTheme);
|
||||
|
Loading…
Reference in New Issue