Refactor (if feasible) uses of the jQuery show and hide function to use vanillaJS

Created on 23 September 2021, over 3 years ago
Updated 4 June 2024, 7 months ago

Problem/Motivation

As mentioned in the parent issue #3238306: [META] Where possible, refactor existing jQuery uses to vanillaJS to reduce jQuery footprint → , we are working towards reducing our jQuery footprint. One of the ways to accomplish this is to reduce the number of jQuery features used in Drupal core. We have added eslint rules that identify specific features and fail tests when those features are in use.

There are (or will be) individual issues for each jQuery-use eslint rule. This one is specific to jquery/no-show, which targets the show function.

Since the recommended replacement is to use classes: https://browse-tutorials.com/snippet/vanilla-javascript-equivalent-jquer...
We should combine the show and hide replacements in one issue.

Steps to reproduce

Proposed resolution

Remaining tasks

  • Determine if we need to account for other display types than block since jquery show accounts for inline and inline block. https://api.jquery.com/show/
  • In core/.eslintrc.jquery.json Change "jquery/no-show": 0, to "jquery/no-show": 2, to enable eslint checks for uses of jQuery .show(). With this change, you'll be able to see uses of the undesirable jQuery feature by running yarn lint:core-js-passing from the core directory
  • Add the following lines to core/scripts/dev/commit-code-check.sh so the DrupalCI testing script can catch this jQuery usage on all files, not just those which have changed
    # @todo Remove the next chunk of lines before committing. This script only lints
    #  JavaScript files that have changed, so we add this to check all files for
    #  jQuery-specific lint errors.
    cd "$TOP_LEVEL/core"
    node ./node_modules/eslint/bin/eslint.js --quiet --config=.eslintrc.passing.json .
    
    CORRECTJQS=$?
    if [ "$CORRECTJQS" -ne "0" ]; then
      # No need to write any output the node command will do this for us.
      printf "${red}FAILURE ${reset}: unsupported jQuery usage. See errors above."
      STATUS=1
      FINAL_STATUS=1
    fi
    cd $TOP_LEVEL
    # @todo end lines to remove

    Add the block about 10 lines before the end of the file, just before if [[ "$FINAL_STATUS" == "1" ]] && [[ "$DRUPALCI" == "1" ]]; then, then remove it once all the jQuery uses have been refactored.

  • If it's determined to be feasible, refactor those uses of jQuery .show() to use Vanilla (native) JavaScript instead.

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Needs work

Version

11.0 🔥

Component
Javascript  →

Last updated about 4 hours ago

  • Maintained by
  • 🇬🇧United Kingdom @justafish
  • 🇫🇷France @nod_
Created by

🇺🇸United States hooroomoo

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇦🇺Australia mstrelan

    I think this should be combined with the hide() function and subsequent jquery/no-hide, which I couldn't find an existing issue for. Possibly also need to consider fadeIn / fadeOut / fadeToggle / slideUp / slideDown / slideToggle at the same time.

  • Assigned to ahsannazir
  • Status changed to Needs work 12 months ago
  • Assigned to shubh511
  • Merge request !6470Refactored no-show → (Open) created by shubh511
  • Pipeline finished with Failed
    11 months ago
    Total: 496s
    #88600
  • Pipeline finished with Failed
    11 months ago
    Total: 163s
    #88770
  • Pipeline finished with Success
    11 months ago
    Total: 622s
    #89646
  • Pipeline finished with Failed
    11 months ago
    Total: 487s
    #89669
  • Pipeline finished with Running
    11 months ago
    #89695
  • Pipeline finished with Success
    11 months ago
    Total: 544s
    #90336
  • Pipeline finished with Failed
    11 months ago
    Total: 481s
    #90484
  • Pipeline finished with Failed
    11 months ago
    Total: 468s
    #90489
  • Pipeline finished with Failed
    11 months ago
    Total: 466s
    #90494
  • Pipeline finished with Failed
    11 months ago
    Total: 540s
    #90506
  • Pipeline finished with Success
    11 months ago
    Total: 468s
    #90514
  • Pipeline finished with Failed
    11 months ago
    Total: 465s
    #90537
  • Pipeline finished with Failed
    11 months ago
    Total: 169s
    #90570
  • Pipeline finished with Failed
    11 months ago
    Total: 545s
    #90572
  • Pipeline finished with Failed
    11 months ago
    Total: 535s
    #90590
  • Pipeline finished with Failed
    11 months ago
    Total: 534s
    #92837
  • Pipeline finished with Success
    11 months ago
    Total: 462s
    #92878
  • Pipeline finished with Success
    11 months ago
    Total: 502s
    #92887
  • Pipeline finished with Running
    11 months ago
    #92898
  • Pipeline finished with Success
    11 months ago
    Total: 461s
    #92909
  • Pipeline finished with Success
    11 months ago
    Total: 459s
    #92945
  • Pipeline finished with Failed
    11 months ago
    Total: 480s
    #92968
  • Pipeline finished with Success
    11 months ago
    Total: 489s
    #92981
  • Pipeline finished with Failed
    11 months ago
    Total: 477s
    #93000
  • Pipeline finished with Success
    11 months ago
    Total: 644s
    #93556
  • Pipeline finished with Failed
    11 months ago
    Total: 471s
    #93703
  • Pipeline finished with Success
    11 months ago
    Total: 615s
    #93739
  • Pipeline finished with Success
    11 months ago
    Total: 512s
    #93870
  • Pipeline finished with Success
    11 months ago
    Total: 2021s
    #93890
  • Pipeline finished with Success
    11 months ago
    Total: 480s
    #94610
  • Pipeline finished with Failed
    11 months ago
    Total: 169s
    #94642
  • Pipeline finished with Failed
    11 months ago
    Total: 178s
    #94654
  • Pipeline finished with Failed
    11 months ago
    Total: 169s
    #94661
  • Pipeline finished with Failed
    11 months ago
    Total: 162s
    #94678
  • Pipeline finished with Success
    11 months ago
    Total: 566s
    #94693
  • Pipeline finished with Failed
    11 months ago
    Total: 171s
    #94709
  • Pipeline finished with Failed
    11 months ago
    #94718
  • Pipeline finished with Failed
    11 months ago
    Total: 485s
    #94725
  • Pipeline finished with Failed
    11 months ago
    Total: 163s
    #94732
  • Pipeline finished with Failed
    11 months ago
    Total: 184s
    #94738
  • Pipeline finished with Failed
    11 months ago
    Total: 169s
    #94749
  • Pipeline finished with Success
    11 months ago
    Total: 506s
    #94752
  • Pipeline finished with Failed
    11 months ago
    Total: 174s
    #94775
  • Pipeline finished with Failed
    11 months ago
    Total: 168s
    #94793
  • Pipeline finished with Failed
    11 months ago
    Total: 161s
    #94798
  • Pipeline finished with Failed
    11 months ago
    Total: 160s
    #94801
  • Pipeline finished with Failed
    11 months ago
    Total: 182s
    #94806
  • Pipeline finished with Failed
    11 months ago
    #94817
  • Pipeline finished with Failed
    11 months ago
    Total: 169s
    #94848
  • Pipeline finished with Canceled
    11 months ago
    Total: 423s
    #94868
  • Pipeline finished with Failed
    11 months ago
    Total: 161s
    #94877
  • Pipeline finished with Failed
    11 months ago
    Total: 164s
    #94885
  • Pipeline finished with Failed
    11 months ago
    Total: 171s
    #94889
  • Pipeline finished with Failed
    11 months ago
    Total: 250s
    #94893
  • Pipeline finished with Success
    11 months ago
    #94901
  • Pipeline finished with Canceled
    11 months ago
    #94915
  • Pipeline finished with Failed
    11 months ago
    Total: 168s
    #94917
  • Pipeline finished with Failed
    11 months ago
    #94928
  • Pipeline finished with Failed
    11 months ago
    Total: 176s
    #94935
  • Pipeline finished with Canceled
    11 months ago
    Total: 256s
    #94941
  • Pipeline finished with Failed
    11 months ago
    Total: 168s
    #94949
  • Pipeline finished with Failed
    11 months ago
    #94950
  • Pipeline finished with Success
    11 months ago
    Total: 472s
    #94957
  • Pipeline finished with Success
    11 months ago
    Total: 461s
    #94964
  • Pipeline finished with Success
    11 months ago
    Total: 481s
    #96321
  • Pipeline finished with Failed
    11 months ago
    Total: 173s
    #96420
  • Pipeline finished with Failed
    11 months ago
    Total: 163s
    #96433
  • Pipeline finished with Failed
    11 months ago
    Total: 173s
    #96451
  • Pipeline finished with Failed
    11 months ago
    Total: 172s
    #96458
  • Pipeline finished with Failed
    11 months ago
    Total: 231s
    #96468
  • Pipeline finished with Failed
    11 months ago
    Total: 165s
    #96473
  • Pipeline finished with Failed
    11 months ago
    Total: 172s
    #96509
  • Pipeline finished with Failed
    11 months ago
    Total: 2082s
    #96520
  • Pipeline finished with Failed
    11 months ago
    Total: 2351s
    #98364
  • Pipeline finished with Failed
    11 months ago
    Total: 519s
    #98434
  • Pipeline finished with Canceled
    11 months ago
    Total: 67s
    #98440
  • Pipeline finished with Failed
    11 months ago
    Total: 430s
    #98444
  • Pipeline finished with Failed
    11 months ago
    #98476
  • Status changed to Needs review 11 months ago
  • 🇮🇳India shubh511

    Here, i am not able to figure out one remaining pipeline error which is Spell-check can anyone help to figureout what might be exactly the error is ?

  • 🇦🇺Australia mstrelan

    You need to push an up-to-date 11.x branch to your issue fork, I think that should fix it.

  • 🇮🇳India shubh511

    @mstrelan yes i rebased my branch with current updated 11.x still its showing the error..

  • 🇮🇳India guptahemant

    hi @shubh511

    It seems like your branch is 54 commits behind the base 11.x branch, to rebase please follow these steps https://www.drupal.org/docs/develop/git/using-gitlab-to-contribute-to-dr... →

  • Pipeline finished with Failed
    11 months ago
    #100374
  • Pipeline finished with Success
    11 months ago
    Total: 484s
    #100614
  • Pipeline finished with Failed
    11 months ago
    Total: 159s
    #100642
  • Pipeline finished with Failed
    11 months ago
    #100700
  • Pipeline finished with Failed
    11 months ago
    Total: 168s
    #100707
  • Pipeline finished with Canceled
    11 months ago
    Total: 54s
    #100711
  • Pipeline finished with Failed
    11 months ago
    Total: 172s
    #100716
  • Pipeline finished with Failed
    11 months ago
    Total: 171s
    #100720
  • Pipeline finished with Success
    11 months ago
    Total: 476s
    #101108
  • Pipeline finished with Success
    11 months ago
    Total: 506s
    #101390
  • Status changed to Needs work 11 months ago
  • 🇷🇺Russia kostyashupenko Omsk

    Added several feedbacks. However MR needs huge work still

  • Pipeline finished with Failed
    11 months ago
    Total: 171s
    #107965
  • Pipeline finished with Failed
    11 months ago
    Total: 169s
    #107998
  • Pipeline finished with Failed
    11 months ago
    Total: 517s
    #108026
  • Pipeline finished with Failed
    11 months ago
    Total: 595s
    #108082
  • Pipeline finished with Success
    11 months ago
    Total: 571s
    #108143
  • Pipeline finished with Failed
    11 months ago
    Total: 485s
    #110173
  • Status changed to Needs review 11 months ago
  • 🇮🇳India shubh511

    Acknowledged all the feedbacks on MR..

  • Pipeline finished with Success
    11 months ago
    #110312
  • 🇮🇳India shubh511

    All feedback have been addressed..

  • Status changed to Needs work 10 months ago
  • The Needs Review Queue Bot → tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide → to find step-by-step guides for working with issues.

  • Status changed to Needs review 10 months ago
  • 🇫🇷France nod_ Lille
  • Status changed to Needs work 10 months ago
  • The Needs Review Queue Bot → tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide → to find step-by-step guides for working with issues.

  • Status changed to Needs review 10 months ago
  • Status changed to Needs work 10 months ago
  • 🇺🇸United States nicxvan

    @mstrelan do you still want to combine this with other rules? I don't see your comment in #7 being addressed. I don't see a reason to combine, personally I think it makes more sense to keep this separate so scope is more limited.

    Another reason is this looks pretty close to approaching ready.

    I did a review of core and don't see any other show methods missing (the linter should catch that but I saw remaining steps mentioned changing the commit check script and I wasn't sure if that was made).

    Remaining show calls fell into three categories that I could see:
    1. asset vendor files
    2. element show methods (dialog and details)
    3. one call from context in /core/misc/machine-name.js

    Category 1 and 2 are out of scope of this issue as far as I can tell.

    I think category 3 does need to be addressed, but I am not confident. I've copied the relevant snippet below:

    if (machine !== '') {
            if (machine !== settings.replace) {
              data.$target[0].value = machine;
              data.$preview.html(
                settings.field_prefix +
                  Drupal.checkPlain(machine) +
                  settings.field_suffix,
              );
            }
            data.$suffix.show();
    }

    Suffix is defined above const $suffix = $context.find(options.suffix);
    and $context is const $context = $(context);

  • Pipeline finished with Success
    10 months ago
    Total: 555s
    #124886
  • Pipeline finished with Success
    10 months ago
    Total: 514s
    #124927
  • Status changed to Needs review 10 months ago
  • 🇮🇳India shubh511

    Hey @nicxvan your comment has been addressed around the third category which was not captured by linter..

  • Status changed to RTBC 10 months ago
  • 🇺🇸United States nicxvan

    This looks good to me and tests are still passing.

    I'm not sure a completed rule conversion should be delayed due to wanting to combine other rules.

  • 🇦🇺Australia mstrelan

    I'm not overly fussed, but we now have code that looks like this:

    // Set the machine name to the transliterated value.
    if (machine !== '') {
      // ...
      data.$suffix[0].style.display = 'block';
    } else {
      data.$suffix.hide();
      // ...
    }
    

    Where the truthy part of the if condition sets the style directly but the falsey part uses jquery hide.

    Besides, I haven't looked thoroughly, but how do we know 'block' is the right value, maybe it should be 'inline-block' or 'inline'? IIRC jquery takes care of that.

  • Status changed to Needs work 10 months ago
  • 🇺🇸United States nicxvan

    You did recall correctly:

    From the docs:
    The matched elements will be revealed immediately, with no animation. This is roughly equivalent to calling .css( "display", "block" ), except that the display property is restored to whatever it was initially. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline.

    https://api.jquery.com/show/

  • 🇺🇸United States nicxvan
  • 🇮🇳India shubh511

    so what exactly need to change here ?

  • Pipeline finished with Success
    10 months ago
    Total: 486s
    #137346
  • 🇺🇸United States nicxvan

    I think that .show() just uses a class to hide and show items so the display type of the elements is preserved.

    The current conversion will convert a flex, inline or inline-block element to a block element.

    I'm not sure of the solution, I'm not even sure this would be considered a regression for the uses in this PR, but it should be addressed.

  • 🇫🇷France nod_ Lille

    I tend to agree the display value set to block is a problem. I don't have an elegant solution here using a class or even a data attribute (maybe even the hidden attribute?) would allow to bypass the problem with display block.

    If there are tricky bits we can have followups to make it easier to get done/reviewed

  • Pipeline finished with Success
    9 months ago
    Total: 567s
    #138317
  • Status changed to Needs review 9 months ago
  • Status changed to Needs work 9 months ago
  • The Needs Review Queue Bot → tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide → to find step-by-step guides for working with issues.

  • Pipeline finished with Success
    9 months ago
    Total: 989s
    #155439
  • Status changed to Needs review 9 months ago
  • 🇺🇸United States nicxvan

    Can you confirm what else you changed?

    I'm not sure you've addressed the feedback where this functionally changes how it works I think for .show vs this solution.

  • Status changed to Needs work 9 months ago
  • 🇺🇸United States nicxvan
  • 🇺🇸United States nicxvan

    It looks like classes are the appropriate way to manage this. So instead of using display none and display block we should add and remove the hidden class:

    https://browse-tutorials.com/snippet/vanilla-javascript-equivalent-jquer...

  • 🇺🇸United States nicxvan
  • 🇺🇸United States nicxvan

    @shubh_ to be explicit since you've still got yourself assigned, I think the specific ask is to do the following:

    1. Read the linked recommendations: https://browse-tutorials.com/snippet/vanilla-javascript-equivalent-jquer...
    2. Update the show method that already exists in this MR to look for and remove the hidden class. Using this guide: https://www.drupal.org/docs/getting-started/accessibility/hide-content-p... →
    3. Find all instances of .hide() and change them to use native javascript and add the hidden class using the guide in 2.

    I think that follows best practice recommendations for how to convert from jquery to native JS in this case.

    I got the recommendation from @brianperry

Production build 0.71.5 2024