-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.ts
52 lines (42 loc) · 1.31 KB
/
utils.ts
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
export type GapMode = 'padding' | 'margin';
export interface GapOffset {
left: number;
top: number;
right: number;
gap: number;
}
export const zeroGap = {
left: 0,
top: 0,
right: 0,
gap: 0,
};
const parse = (x: string | null) => parseInt(x || '', 10) || 0;
const getOffset = (gapMode: GapMode): number[] => {
const cs = window.getComputedStyle(document.body);
if (process.env.NODE_ENV !== 'production') {
if (cs.overflowY === 'hidden') {
console.error(
'react-remove-scroll-bar: cannot calculate scrollbar size because it is removed (overflow:hidden on body'
);
}
}
const left = cs[gapMode === 'padding' ? 'paddingLeft' : 'marginLeft'];
const top = cs[gapMode === 'padding' ? 'paddingTop' : 'marginTop'];
const right = cs[gapMode === 'padding' ? 'paddingRight' : 'marginRight'];
return [parse(left), parse(top), parse(right)];
};
export const getGapWidth = (gapMode: GapMode = 'margin'): GapOffset => {
if (typeof window === 'undefined') {
return zeroGap;
}
const offsets = getOffset(gapMode);
const documentWidth = document.documentElement.clientWidth;
const windowWidth = window.innerWidth;
return {
left: offsets[0],
top: offsets[1],
right: offsets[2],
gap: Math.max(0, windowWidth - documentWidth + offsets[2] - offsets[0]),
};
};