summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib
diff options
context:
space:
mode:
authorClement Ho <clemmakesapps@gmail.com>2018-06-01 16:41:50 +0000
committerClement Ho <clemmakesapps@gmail.com>2018-06-01 16:41:50 +0000
commit30bc82f68b6de69a2e456286888824fa0221d4a7 (patch)
tree32597a02aa74ed80a3370c3f08dda12d152c5b68 /app/assets/javascripts/lib
parentabe98d44e8902a3f721c0f17cc20dc55bcb2d2f6 (diff)
downloadgitlab-ce-30bc82f68b6de69a2e456286888824fa0221d4a7.tar.gz
Revert "Merge branch '46833-sticky-polyfill' into 'master'"
This reverts merge request !19304
Diffstat (limited to 'app/assets/javascripts/lib')
-rw-r--r--app/assets/javascripts/lib/utils/sticky.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/utils/sticky.js b/app/assets/javascripts/lib/utils/sticky.js
new file mode 100644
index 00000000000..098afcfa1b4
--- /dev/null
+++ b/app/assets/javascripts/lib/utils/sticky.js
@@ -0,0 +1,39 @@
+export const createPlaceholder = () => {
+ const placeholder = document.createElement('div');
+ placeholder.classList.add('sticky-placeholder');
+
+ return placeholder;
+};
+
+export const isSticky = (el, scrollY, stickyTop, insertPlaceholder) => {
+ const top = Math.floor(el.offsetTop - scrollY);
+
+ if (top <= stickyTop && !el.classList.contains('is-stuck')) {
+ const placeholder = insertPlaceholder ? createPlaceholder() : null;
+ const heightBefore = el.offsetHeight;
+
+ el.classList.add('is-stuck');
+
+ if (insertPlaceholder) {
+ el.parentNode.insertBefore(placeholder, el.nextElementSibling);
+
+ placeholder.style.height = `${heightBefore - el.offsetHeight}px`;
+ }
+ } else if (top > stickyTop && el.classList.contains('is-stuck')) {
+ el.classList.remove('is-stuck');
+
+ if (insertPlaceholder && el.nextElementSibling && el.nextElementSibling.classList.contains('sticky-placeholder')) {
+ el.nextElementSibling.remove();
+ }
+ }
+};
+
+export default (el, stickyTop, insertPlaceholder = true) => {
+ if (!el) return;
+
+ if (typeof CSS === 'undefined' || !(CSS.supports('(position: -webkit-sticky) or (position: sticky)'))) return;
+
+ document.addEventListener('scroll', () => isSticky(el, window.scrollY, stickyTop, insertPlaceholder), {
+ passive: true,
+ });
+};