-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
59 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
<title>Example</title> | ||
<style> | ||
body { | ||
margin: 0; | ||
/*margin: 10px;*/ | ||
} | ||
</style> | ||
</head> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,42 @@ | ||
export type GapMode = 'padding' | 'margin'; | ||
|
||
const getOffset = (gapMode: GapMode) => { | ||
export interface GapOffset { | ||
left: number; | ||
top: number; | ||
right: number; | ||
gap: number; | ||
} | ||
|
||
export const zeroGap = { | ||
left: 0, | ||
top: 0, | ||
right: 0, | ||
gap: 0, | ||
}; | ||
|
||
const getOffset = (gapMode: GapMode): number[] => { | ||
const cs = window.getComputedStyle(document.body); | ||
const value = cs[gapMode === 'padding' ? 'paddingRight' : 'marginRight']; | ||
return parseInt(value || '', 10) || 0; | ||
const left = cs[gapMode === 'padding' ? 'paddingLeft' : 'marginLeft']; | ||
const top = cs[gapMode === 'padding' ? 'paddingTop' : 'marginTop']; | ||
const right = cs[gapMode === 'padding' ? 'paddingRight' : 'marginRight']; | ||
return [ | ||
parseInt(left || '', 10) || 0, | ||
parseInt(top || '', 10) || 0, | ||
parseInt(right || '', 10) || 0 | ||
]; | ||
}; | ||
|
||
export const getGapWidth = (gapMode: GapMode = 'margin') => { | ||
export const getGapWidth = (gapMode: GapMode = 'margin'): GapOffset => { | ||
if (typeof window === 'undefined') { | ||
return 0; | ||
return zeroGap; | ||
} | ||
const currentOffset = getOffset(gapMode); | ||
const offsets = getOffset(gapMode); | ||
const documentWidth = document.documentElement.clientWidth; | ||
const windowWidth = window.innerWidth; | ||
return Math.max(0, windowWidth - documentWidth + currentOffset); | ||
return { | ||
left: offsets[0], | ||
top: offsets[1], | ||
right: offsets[2], | ||
gap: Math.max(0, windowWidth - documentWidth + offsets[2] - offsets[0]), | ||
} | ||
}; |