diff options
author | Clement Ho <clemmakesapps@gmail.com> | 2018-06-06 19:15:37 +0000 |
---|---|---|
committer | Clement Ho <clemmakesapps@gmail.com> | 2018-06-06 19:15:37 +0000 |
commit | dd6aade3bf54a6d72a5b98daa34d0798b158399f (patch) | |
tree | 39d78ea0562c182faccb7672cf317fe4bf18976c | |
parent | 4f526a334be7bd2875a7037172a344140a515fe6 (diff) | |
parent | bc56f1f5c611a26a2f3f06070312b5a6705c002c (diff) | |
download | gitlab-ce-dd6aade3bf54a6d72a5b98daa34d0798b158399f.tar.gz |
Merge branch '46833-sticky-polyfill' into 'master'
Update sticky utils with polyfill
Closes #46833 and #47130
See merge request gitlab-org/gitlab-ce!19324
-rw-r--r-- | app/assets/javascripts/init_changes_dropdown.js | 2 | ||||
-rw-r--r-- | app/assets/javascripts/job.js | 11 | ||||
-rw-r--r-- | app/assets/javascripts/lib/utils/sticky.js | 23 |
3 files changed, 25 insertions, 11 deletions
diff --git a/app/assets/javascripts/init_changes_dropdown.js b/app/assets/javascripts/init_changes_dropdown.js index 09cca1dc7d9..5c5a6e01848 100644 --- a/app/assets/javascripts/init_changes_dropdown.js +++ b/app/assets/javascripts/init_changes_dropdown.js @@ -1,5 +1,5 @@ import $ from 'jquery'; -import stickyMonitor from './lib/utils/sticky'; +import { stickyMonitor } from './lib/utils/sticky'; export default (stickyTop) => { stickyMonitor(document.querySelector('.js-diff-files-changed'), stickyTop); diff --git a/app/assets/javascripts/job.js b/app/assets/javascripts/job.js index e76bae08699..fc13f467675 100644 --- a/app/assets/javascripts/job.js +++ b/app/assets/javascripts/job.js @@ -1,6 +1,6 @@ import $ from 'jquery'; import _ from 'underscore'; -import StickyFill from 'stickyfilljs'; +import { polyfillSticky } from './lib/utils/sticky'; import axios from './lib/utils/axios_utils'; import { visitUrl } from './lib/utils/url_utility'; import bp from './breakpoints'; @@ -70,14 +70,7 @@ export default class Job extends LogOutputBehaviours { } initAffixTopArea() { - /** - If the browser does not support position sticky, it returns the position as static. - If the browser does support sticky, then we allow the browser to handle it, if not - then we use a polyfill - */ - if (this.$topBar.css('position') !== 'static') return; - - StickyFill.add(this.$topBar); + polyfillSticky(this.$topBar); } scrollToBottom() { diff --git a/app/assets/javascripts/lib/utils/sticky.js b/app/assets/javascripts/lib/utils/sticky.js index 098afcfa1b4..15a4dd62012 100644 --- a/app/assets/javascripts/lib/utils/sticky.js +++ b/app/assets/javascripts/lib/utils/sticky.js @@ -1,3 +1,5 @@ +import StickyFill from 'stickyfilljs'; + export const createPlaceholder = () => { const placeholder = document.createElement('div'); placeholder.classList.add('sticky-placeholder'); @@ -28,7 +30,16 @@ export const isSticky = (el, scrollY, stickyTop, insertPlaceholder) => { } }; -export default (el, stickyTop, insertPlaceholder = true) => { +/** + * Create a listener that will toggle a 'is-stuck' class, based on the current scroll position. + * + * - If the current environment does not support `position: sticky`, do nothing. + * + * @param {HTMLElement} el The `position: sticky` element. + * @param {Number} stickyTop Used to determine when an element is stuck. + * @param {Boolean} insertPlaceholder Should a placeholder element be created when element is stuck? + */ +export const stickyMonitor = (el, stickyTop, insertPlaceholder = true) => { if (!el) return; if (typeof CSS === 'undefined' || !(CSS.supports('(position: -webkit-sticky) or (position: sticky)'))) return; @@ -37,3 +48,13 @@ export default (el, stickyTop, insertPlaceholder = true) => { passive: true, }); }; + +/** + * Polyfill the `position: sticky` behavior. + * + * - If the current environment supports `position: sticky`, do nothing. + * - Can receive an iterable element list (NodeList, jQuery collection, etc.) or single HTMLElement. + */ +export const polyfillSticky = (el) => { + StickyFill.add(el); +}; |