When conditionally hiding some field from the webform, it seems that the hiding is done purely with JS. This leads to the effect that hidden elements flash as visible momentarily before JS kicks in and hides them.

This leads to a bit bad user experience especially with large forms when a lot of unnecessary fields can be first visible and then hidden.

Generally I would think the hidden elements could have a default CSS style of display: none which would then be then toggled off with JS.

I tested doing this with custom classes / css styles but these are not added to the correct DIV in the DOM so to they don't affect the div revealed when the JS kicks in.

Is there any other solution for the problem?

Comments

HeikkiY created an issue. See original summary.

jrockowitz’s picture

Status: Active » Needs review
StatusFileSize
new1.55 KB

The issue with hiding elements using 'display:none' is the element's content won't be indexed by any search engines. At the same time, I feel UX trumps SEO.

Here is my first attempt at adding a .js-webform-hidden class to conditionally hidden elements when they are being rendered on the page.

Status: Needs review » Needs work

The last submitted patch, 2: 3042289-2.patch, failed testing. View results

jrockowitz’s picture

Status: Needs work » Needs review
StatusFileSize
new4.61 KB

Status: Needs review » Needs work

The last submitted patch, 4: 3042289-4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jrockowitz’s picture

Status: Needs work » Needs review
StatusFileSize
new7.7 KB

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    
jrockowitz’s picture

StatusFileSize
new21.38 KB

The attached patch includes test coverage but still requires some manual review.

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

Status: Needs review » Needs work

The last submitted patch, 8: 3042289-8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed b86c474 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    

  • jrockowitz committed 1ae757b on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes
    
jrockowitz’s picture

Status: Needs work » Needs review
StatusFileSize
new21.42 KB
jrockowitz’s picture

Priority: Normal » Major
heikkiy’s picture

A note about SEO. According to several posts (for example https://www.seroundtable.com/google-content-tabs-hidden-change-22950.html), Google has changed their view on hidden content because of mobile first approach. So this change is fine both from the SEO and UX point of view.

jrockowitz’s picture

@HeikkiY Can you please thoroughly test this patch.

heikkiy’s picture

@jrockowitz

Of course. I installed 5.2 rc3 + this patch.

I tested this with one form which had a dropdown reference to taxonomy term and it shows markup fields based on the value. That worked fine. Also tested a checkbox condition which also worked.

I also went through a very large multistep form with a lot of conditions. No problems there either.

The UX was a lot better in both cases.

We also have automated tests in place in the project. I will see if we can run this version on the project and report if we bump into any issues. But so far, so good.

jrockowitz’s picture

The main regression I want to prevent is unexpectedly hiding an element which is then no longer conditionally displayed via #states.

heikkiy’s picture

@jrockowitz

Yes, we can definitely monitor that.

We did notice one thing. At the moment we have a conditionally hidden webform-flexbox. I think the JS is now triggering a display: block style for the conditionally shown elements? This will break the flexbox container layout. It should instead trigger display: flex for those elements.

Example markup:
<div class="js-webform-states-hidden webform-flexbox js-webform-flexbox js-form-wrapper form-wrapper" data-drupal-selector="edit-flexbox-31" data-drupal-states="{&quot;invisible&quot;:{&quot;.webform-submission-test-node-31-add-form :input[name=\u0022test1\u0022]&quot;:{&quot;checked&quot;:true}}}" id="edit-flexbox-31" style="display: block;">

Should be instead:
<div class="js-webform-states-hidden webform-flexbox js-webform-flexbox js-form-wrapper form-wrapper" data-drupal-selector="edit-flexbox-31" data-drupal-states="{&quot;invisible&quot;:{&quot;.webform-submission-test-node-31-add-form :input[name=\u0022test1\u0022]&quot;:{&quot;checked&quot;:true}}}" id="edit-flexbox-31" style="display: flex;">

  • jrockowitz committed 950e310 on 3042289
    Issue #3042289 by jrockowitz: Conditionally hidden field flashes. Fix...
jrockowitz’s picture

StatusFileSize
new21.91 KB
new1.64 KB
heikkiy’s picture

@jrockowitz

I noticed that patch #65 has changes regarding flexbox. Not 100% sure if it was supposed to tackle the issue I mentioned above with the webform-flexbox, but the issue mentioned above is still valid after the patch.

I can find the following styles from /webform/css/webform.element.flexbox.css

display: -webkit-box;
display: -ms-flexbox;
display: flex;
margin: 1em -.5em;

And this gets overridden with display: block when triggering the visibility.

jrockowitz’s picture

Is the below YAML an accurate representation of the issue?

  trigger_checkbox:
    '#type': checkbox
    '#title': trigger_checkbox
  dependent_flexbox:
    '#type': webform_flexbox
    '#states':
      visible:
        ':input[name="trigger_checkbox"]':
          checked: true
    dependent_flexbox_textfield:
      '#type': textfield
      '#title': dependent_flexbox_textfield
      '#states':
        visible:
          ':input[name="trigger_checkbox"]':
            checked: true
    flexbox_textfield:
      '#type': textfield
      '#title': flexbox_textfield

Are conditional flexboxes working without this patch?

What browser are you using?

jrockowitz’s picture

heikkiy’s picture

@jrockowitz

I debugged this a bit more. It seems it might actually be a regression with 5.2 version of Webform. We are running 5.1 on our staging server and 5.2 in our development server. Staging server shows the flexbox layout correctly. The issue was introduced when I upgraded to 5.2 and applied this patch. Removing the patch does not help.

The following YAML shows elements where the problem occurs:

elements: |
  applicants_information:
    '#type': webform_wizard_page
    '#title': 'Applicant''s information'
    personal_information:
      '#type': fieldset
      '#title': 'Personal information'
      '#format_attributes':
        class:
          - preview-grid
      flexbox_16:
        '#type': webform_flexbox
        first_name:
          '#type': textfield
          '#title': 'First name'
          '#required': true
        last_name:
          '#type': textfield
          '#title': 'Last name'
          '#required': true
  co_applicants_information:
    '#type': webform_wizard_page
    '#title': 'Co-applicant’s information'
    applying_with_co_applicant:
      '#type': fieldset
      '#title': 'Are you applying for an apartment with your spouse or co-applicant?'
      '#format_attributes':
        class:
          - preview-hidden
      applying_with_other_applicant:
        '#type': radios
        '#title': 'Are you applying for an apartment with your spouse or co-applicant'
        '#title_display': invisible
        '#options':
          'no': 'No'
          'yes': 'Yes'
        '#required': true
        '#wrapper_attributes':
          class:
            - hidden-legend
        '#format_attributes':
          class:
            - preview-hidden
    co_applicants_personal_info:
      '#type': fieldset
      '#title': 'Co-applicant’s personal information'
      '#states':
        visible:
          ':input[name="applying_with_other_applicant"]':
            value: 'yes'
      '#format_attributes':
        class:
          - preview-grid
      flexbox_29:
        '#type': webform_flexbox
        co_applicants_first_name:
          '#type': textfield
          '#title': 'First name'
          '#required': true
        co_applicants_last_name:
          '#type': textfield
          '#title': 'Last name'
          '#required': true
    co_applicants_contact_information:
      '#type': fieldset
      '#title': 'Co-applicant’s contact information'
      '#states':
        visible:
          ':input[name="applying_with_other_applicant"]':
            value: 'yes'
      '#format_attributes':
        class:
          - preview-grid
      same_address:
        '#type': checkbox
        '#title': 'Same as the applicant’s'
      flexbox_31:
        '#type': webform_flexbox
        '#states':
          invisible:
            ':input[name="same_address"]':
              checked: true
        co_applicants_street_address:
          '#type': textfield
          '#title': 'Street address'
          '#required': true
          '#states':
            required:
              ':input[name="applying_with_other_applicant"]':
                value: 'yes'
              ':input[name="same_address"]':
                unchecked: true
        co_applicants_postal_code:
          '#type': textfield
          '#title': 'Postal code'
          '#required': true
          '#states':
            required:
              ':input[name="applying_with_other_applicant"]':
                value: 'yes'
              ':input[name="same_address"]':
                unchecked: true
        co_applicants_city:
          '#type': textfield
          '#title': City/town
          '#required': true
          '#states':
            required:
              ':input[name="applying_with_other_applicant"]':
                value: 'yes'
              ':input[name="same_address"]':
                unchecked: true
      co_applicants_phone_number:
        '#type': tel
        '#title': 'Telephone number'
        '#required': true
      co_applicants_email:
        '#type': webform_email_confirm
        '#title': 'Email address'
        '#wrapper_attributes':
          class:
            - email-confirm
        '#confirm__title': 'Confirm email address'
        '#required': true
  actions:
    '#type': webform_actions
    '#title': 'Submit button(s)'

The problematic part is the element flexbox_31 in step 2 which looses the flexbox styling which as said happens with 5.2 and not with 5.1.

I tried to test this with your example elements, and actually that works fine with 5.2 also. So the problem is somewhere with the example above.

Also related to another ticket https://www.drupal.org/project/webform/issues/3031383, I noticed the previous page and clientside validation issue is still relevant with the example above. If you can also verify this, I could open a new ticket about that.

heikkiy’s picture

Yes, I think https://stackoverflow.com/questions/38491653/jquery-show-a-flex-box/4121... is exactly the same issue I was thinking about in #63.

jrockowitz’s picture

I can't replicate any flexbox issue using the example from #69.

Are you using the Bartik theme?

What browser are you using?

heikkiy’s picture

StatusFileSize
new65.09 KB
new56.62 KB

@jrockowitz

Tested with Bartik and Chrome 73.0.3683.86.

I tested with 5.1 and 5.2. Attached are screenshots of the problem. In version 5.1 the fields are positioned side by side with flexbox. In 5.2 they are triggered to display block and full width.

The problem also comes back just with composer installing between 5.1 / 5.2 and nothing else changed in the environment.

jrockowitz’s picture

I am not able to replicate this issue. If this is a regression, please create a new ticket with simplified webform (not multistep) that can be used to replicate the issue.

heikkiy’s picture

@jrockowich

I think I figured it out. The problem occurs when we are using the condition in reverse.

We have the following.

flexbox_31:
        '#type': webform_flexbox
        '#states':
          invisible:
            ':input[name="same_address"]':
              checked: true

This seems to break the flexbox.

On the other hand, this does not break it:

flexbox_31:
        '#type': webform_flexbox
        '#states':
          visible:
            ':input[name="same_address"]':
              unchecked: true

Do you still want me to report a separate issue for this?

jrockowitz’s picture

I still am not able to replicate the problem If the issue is occuring without this issue's patch, please create a new ticket.

heikkiy’s picture

I tested once more with 5.2 and 5.2 + patch. It seems it is related to this patch because without this patch it does seem to work ok with a simpler form. I will try to create a simplytest.me with this.

heikkiy’s picture

@jrockowitz

Installed simplytest.me with 5.2 rc3 + patch.

Broken flexbox: https://d0k5j.ply.st/form/checked-visible
Working flexbox: https://d0k5j.ply.st/form/unchecked-visible

  • jrockowitz committed d781f70 on 3042289
    Issue #3042289 by jrockowitz, HeikkiY: Conditionally hidden field...
jrockowitz’s picture

StatusFileSize
new2.05 KB
new22.77 KB

I was finally able to replicate the issue and updated the test webform.

heikkiy’s picture

@jrockowitz

Patch #78 seems to resolve the issue.

jrockowitz’s picture

@HeikkiY Thanks for reviewing this patch.

I am going to hold off committing this patch with hopes that someone else can do a manual review before I commit it.

  • jrockowitz committed 3cf7d04 on 3042289
    Issue #3042289: Conditionally hidden field flashes
    

  • jrockowitz committed 969bfd6 on 3042289
    Issue #3042289: Conditionally hidden field flashes. Write tests.
    
jrockowitz’s picture

I think this issue can also help address #3045662: Field Visible on 2nd Page of Wizard When Should be Hidden which is completely removing hidden element via #access and breaking the expected behavior.

The attached uses this issue's new .js-webform-states-hidden class to hide elements instead of #access: FALSE property.

jrockowitz’s picture

StatusFileSize
new5.33 KB
new27.04 KB
cleo7186’s picture

StatusFileSize
new1.2 KB

I am trying to add visibility using multiple conditions and i am getting this error: "Argument 2 passed to Drupal\webform\WebformSubmissionConditionsValidator::validateCondition() must be of the type array, null given, called in /var/www/wvufs/docroot/modules/contrib/webform/src/WebformSubmissionConditionsValidator.php on line 539"

Here is the file I am trying to save- I believe it looks correct.

jrockowitz’s picture

I can't replicate #86 using the latest dev release with this patch.

What values are you entering? Are you using the test tab?

cleo7186’s picture

Ah, I have no idea what happened, but it seems to be working now. Excuse my ignorance, what is the "test tab" exactly?

I have tested all of my forms and I do not see any issues with them currently.

And by test I mean I just fiddled with the values to check if the proper fields show up, and that they allow me to submit them without any improper validation errors.

I am not familiar with writing unit tests.

jrockowitz’s picture

StatusFileSize
new145.12 KB

The 'Test' tab just populates your form with dummy data.

cleo7186’s picture

Oh, okay, thanks.

So far all of my forms seem to be working now with this patch.

jrockowitz’s picture

Status: Needs review » Needs work

The next steps for this issue are for me to manually review it (again( and create a change record.

  • jrockowitz committed bbd1e18 on 8.x-5.x
    Issue #3042289 by jrockowitz, HeikkiY, cleo7186: Conditionally hidden...
jrockowitz’s picture

I committed the patch and wrote a change record.

jrockowitz’s picture

Status: Needs work » Fixed
Thank you for helping make the Webform module and our community a little better and stronger.
jrockowitz’s picture

If you enjoy and value Drupal and the Webform module, get involved, consider joining the Drupal Association, and backing the Webform module's Open Collective.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

gargsuchi’s picture

Hi @jrockowitz - I have a form where the Submit button is hidden on initial form load, and shows up only when one of the fields is filled.
And the button is flickering, as the fixStatesWrapper function does not add the class 'js-webform-states-hidden' to the submit button.
Could you guide me where I should be adding it - and I can try to create a patch for the same?

jrockowitz’s picture

Please create a new ticket and provide an example webform.