[go: up one dir, main page]

Skip to content

Refactor lib/utils/common_utils.js to make things more explicit

The following discussion from !72278 (merged) should be addressed:

  • @dmishunov started a discussion: (+2 comments)

    suggestion: @iamphill what do you think about moving these assignments to the top of the fn's definition to have something more explicit than options? Something like

    Index: app/assets/javascripts/lib/utils/common_utils.js
    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    ===================================================================
    diff --git a/app/assets/javascripts/lib/utils/common_utils.js b/app/assets/javascripts/lib/utils/common_utils.js
    --- a/app/assets/javascripts/lib/utils/common_utils.js	(revision faa5927e4f6bea1d6bb62aa8577219b0c874499d)
    +++ b/app/assets/javascripts/lib/utils/common_utils.js	(date 1634851834346)
    @@ -207,7 +207,10 @@
       }, 0);
     };
     
    -export const scrollToElement = (element, options = {}) => {
    +export const scrollToElement = (
    +  element,
    +  { duration = 200, offset = 0, behavior = duration ? 'smooth' : 'auto' } = {},
    +) => {
       let el = element;
       if (element instanceof $) {
         // eslint-disable-next-line prefer-destructuring
    @@ -220,7 +223,6 @@
         // In the previous implementation, jQuery naturally deferred this scrolling.
         // Unfortunately, we're quite coupled to this implementation detail now.
         defer(() => {
    -      const { duration = 200, offset = 0, behavior = duration ? 'smooth' : 'auto' } = options;
           const y = el.getBoundingClientRect().top + window.pageYOffset + offset - contentTop();
           window.scrollTo({ top: y, behavior });
         });
    

    Or do we expect any other options to be passed in here? 🤔

    I understand that we might not be willing to get into refactoring here so feel free to resolve in a follow-up if you see it as a more appropriate resolution.