-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
65 lines (63 loc) · 1.69 KB
/
Copy pathindex.ts
File metadata and controls
65 lines (63 loc) · 1.69 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { createModule } from '@github1/redux-modules';
function isWindow(w: any): w is Window {
return typeof w !== 'undefined' && w.location;
}
export const resize = createModule('resize', {
initializer(props: { window: Window | 'none' }): { window: Window | 'none' } {
return {
window:
props.window === 'none'
? undefined
: props.window ||
(typeof window === 'undefined' ? undefined : window),
};
},
actionCreators: {
registerResizeListener(): { type: '@resize/register-resize-listener' } {
return {
type: '@resize/register-resize-listener',
};
},
resized(
height: number,
width: number
): { type: '@resize/resized'; height: number; width: number } {
return { type: '@resize/resized', height, width };
},
},
})
.reduce(
(
state: { height: number; width: number } = {
height: 0,
width: 0,
},
action
) => {
if (action.type === '@resize/resized') {
return {
...state,
height: action.height,
width: action.width,
};
}
return state;
}
)
.on((store) => (next) => (action) => {
if (action.type === '@resize/register-resize-listener') {
const { window } = store.props;
if (isWindow(window)) {
const onResize = () =>
next(store.actions.resized(window.innerHeight, window.innerWidth));
onResize();
window.addEventListener('resize', onResize);
} else {
store.dispatch(store.actions.resized(0, 0));
}
}
next(action);
})
.configure((store) => {
store.dispatch(store.actions.resize.registerResizeListener());
});