about summary refs log tree commit diff
path: root/app/javascript/mastodon/utils
diff options
context:
space:
mode:
authorYamagishi Kazutoshi <ykzts@desire.sh>2019-09-29 21:30:58 +0900
committerEugen Rochko <eugen@zeonfederated.com>2019-09-29 14:30:58 +0200
commit0a49b26793d467589be09305e15ff9cc97cdd200 (patch)
treeea5b45ff99328cd67df2fae9feb62b2cc6ebfb59 /app/javascript/mastodon/utils
parent163ed91af381d86bb6c52546c983effa4c9a18c3 (diff)
Do not add margin light when opening modal on mobile (#11830)
Diffstat (limited to 'app/javascript/mastodon/utils')
-rw-r--r--app/javascript/mastodon/utils/scrollbar.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/app/javascript/mastodon/utils/scrollbar.js b/app/javascript/mastodon/utils/scrollbar.js
new file mode 100644
index 000000000..6f0ee010b
--- /dev/null
+++ b/app/javascript/mastodon/utils/scrollbar.js
@@ -0,0 +1,36 @@
+import { isMobile } from '../is_mobile';
+
+/** @type {number | null} */
+let cachedScrollbarWidth = null;
+
+/**
+ * @return {number}
+ */
+const getActualScrollbarWidth = () => {
+  const outer = document.createElement('div');
+  outer.style.visibility = 'hidden';
+  outer.style.overflow = 'scroll';
+  document.body.appendChild(outer);
+
+  const inner = document.createElement('div');
+  outer.appendChild(inner);
+
+  const scrollbarWidth = outer.offsetWidth - inner.offsetWidth;
+  outer.parentNode.removeChild(outer);
+
+  return scrollbarWidth;
+};
+
+/**
+ * @return {number}
+ */
+export const getScrollbarWidth = () => {
+  if (cachedScrollbarWidth !== null) {
+    return cachedScrollbarWidth;
+  }
+
+  const scrollbarWidth = isMobile(window.innerWidth) ? 0 : getActualScrollbarWidth();
+  cachedScrollbarWidth = scrollbarWidth;
+
+  return scrollbarWidth;
+};