Skip to content

Commit

Permalink
feat(project): add dom util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
RCVZ committed May 5, 2021
1 parent 235c99c commit c28a288
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/utils/domHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export const canUseDOM = !!(
typeof window !== 'undefined' &&
window.document &&
window.document.createElement
);

let size: number;

export default function scrollbarSize(recalc?: boolean) {
if ((!size && size !== 0) || recalc) {
if (canUseDOM) {
const scrollDiv = document.createElement('div');

scrollDiv.style.position = 'absolute';
scrollDiv.style.top = '-9999px';
scrollDiv.style.width = '50px';
scrollDiv.style.height = '50px';
scrollDiv.style.overflow = 'scroll';

document.body.appendChild(scrollDiv);
size = scrollDiv.offsetWidth - scrollDiv.clientWidth;
document.body.removeChild(scrollDiv);
}
}

return size;
}

0 comments on commit c28a288

Please sign in to comment.