-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathresponsiveRedux.js
More file actions
46 lines (40 loc) · 1.38 KB
/
Copy pathresponsiveRedux.js
File metadata and controls
46 lines (40 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {makeEnum} from '@cdo/apps/utils';
const SET_RESPONSIVE_SIZE = 'responsive/SET_RESPONSIVE_SIZE';
export const setResponsiveSize = responsiveSize => ({
type: SET_RESPONSIVE_SIZE,
responsiveSize,
});
export const ResponsiveSize = makeEnum('lg', 'md', 'sm', 'xs');
// Default window widths that are the starting points for each width category.
const Breakpoints = [
{breakpoint: 992, responsiveSize: ResponsiveSize.lg},
{breakpoint: 720, responsiveSize: ResponsiveSize.md},
{breakpoint: 650, responsiveSize: ResponsiveSize.sm},
{breakpoint: 0, responsiveSize: ResponsiveSize.xs},
];
export function getResponsiveBreakpoint(width) {
const responsiveSize = Breakpoints.find(({breakpoint}) => width > breakpoint);
if (responsiveSize === undefined) {
console.error(
`No responsive size found for width ${width}, defaulting to xs`
);
return ResponsiveSize.xs;
}
return responsiveSize.responsiveSize;
}
const initialState = {
responsiveSize: getResponsiveBreakpoint(window.innerWidth),
};
/**
* Reducer for responsive sizes. Only return a new state if we've actually
* crossed into a new breakpoint width.
*/
export default function reducer(state = initialState, action) {
if (
action.type === SET_RESPONSIVE_SIZE &&
state.responsiveSize !== action.responsiveSize
) {
return {...state, responsiveSize: action.responsiveSize};
}
return state;
}