pleroma/app/soapbox/reducers/theme.js

36 lines
925 B
JavaScript
Raw Normal View History

2020-05-30 17:05:01 -07:00
import {
2020-05-30 22:21:41 -07:00
THEME_SET,
THEME_GENERATE,
2020-05-30 17:05:01 -07:00
} from '../actions/theme';
import { Map as ImmutableMap } from 'immutable';
const initialState = ImmutableMap();
const hex2rgb = c => c.substr(1).match(/../g).map(x => + `0x${x}`);
2020-05-31 13:17:10 -07:00
2020-05-31 22:49:43 -07:00
export const generateTheme = (brandColor, mode = 'light') => {
if (!brandColor) return false;
const [ r, g, b ] = hex2rgb(brandColor);
return ImmutableMap({
'brand-color-r': r,
'brand-color-g': g,
'brand-color-b': b,
});
2020-05-30 22:21:41 -07:00
};
export const setTheme = themeData => {
const { 'brand-color': brandColor } = themeData.toObject();
return ImmutableMap(generateTheme(brandColor, 'light')).merge(themeData);
2020-05-30 18:11:08 -07:00
};
2020-05-30 17:05:01 -07:00
export default function theme(state = initialState, action) {
switch(action.type) {
2020-05-30 22:21:41 -07:00
case THEME_GENERATE:
return generateTheme(action.brandColor, action.mode);
case THEME_SET:
return setTheme(ImmutableMap(action.brandColor));
2020-05-30 17:05:01 -07:00
default:
return state;
}
};