-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed "document is not defined" issue when building via vite-ssg
- Loading branch information
Showing
2 changed files
with
19 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
export function getScrollbarWidth(): number { | ||
// Creating invisible container | ||
const outer = document.createElement('div') | ||
outer.style.visibility = 'hidden' | ||
outer.style.overflow = 'scroll' // forcing scrollbar to appear | ||
// outer.style.msOverflowStyle = 'scrollbar' // needed for WinJS apps | ||
document.body.appendChild(outer) | ||
try { | ||
// Creating invisible container | ||
const outer = document.createElement('div') | ||
outer.style.visibility = 'hidden' | ||
outer.style.overflow = 'scroll' // forcing scrollbar to appear | ||
// outer.style.msOverflowStyle = 'scrollbar' // needed for WinJS apps | ||
document.body.appendChild(outer) | ||
|
||
// Creating inner element and placing it in the container | ||
const inner = document.createElement('div') | ||
outer.appendChild(inner) | ||
// Creating inner element and placing it in the container | ||
const inner = document.createElement('div') | ||
outer.appendChild(inner) | ||
|
||
// Calculating difference between container's full width and the child width | ||
const scrollbarWidth = outer.offsetWidth - inner.offsetWidth | ||
// Calculating difference between container's full width and the child width | ||
const scrollbarWidth = outer.offsetWidth - inner.offsetWidth | ||
|
||
// Removing temporary elements from the DOM | ||
outer.parentNode?.removeChild(outer) | ||
// Removing temporary elements from the DOM | ||
outer.parentNode?.removeChild(outer) | ||
|
||
return scrollbarWidth | ||
return scrollbarWidth | ||
} catch (e) { | ||
return 0 | ||
} | ||
} |
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