From 5729286a71429be0400236435c48cdd64b045a76 Mon Sep 17 00:00:00 2001 From: kolirt Date: Wed, 20 Dec 2023 13:22:20 +0200 Subject: [PATCH] fixed "document is not defined" issue when building via vite-ssg --- lib/utils/getScrollbarWidth.ts | 32 ++++++++++++++++++-------------- package.json | 2 +- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/utils/getScrollbarWidth.ts b/lib/utils/getScrollbarWidth.ts index e3447e2..f386884 100644 --- a/lib/utils/getScrollbarWidth.ts +++ b/lib/utils/getScrollbarWidth.ts @@ -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 + } } diff --git a/package.json b/package.json index e6355cd..036bcc0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kolirt/vue-modal", - "version": "0.0.15", + "version": "0.0.16", "description": "Simple Vue3 modal package", "author": "kolirt", "license": "MIT",