Sticky table header does not work if a table placed in a closed 'details' element

Created on 12 December 2023, about 1 year ago
Updated 23 May 2024, 8 months ago

Problem/Motivation

When a table placed in a closed 'details' element a sticky table header does not work in case user started scroll before 'details' element is opened.

This happens because sticky header is added with a width value '0' and recalculation doesn't execute after opening the 'details' element.

Steps to reproduce

Form API fields structure to reproduce issue:

...
$form['text'] = [
  '#markup' => 'Text that needed to scroll before opening the next details element...',
];

$form['wrapper'] = [
  '#type' => 'details',
  '#title' => $this->t('Table wrapper'),
  '#open' => FALSE,
];

$form['wrapper']['table'] = [
  '#type' => 'table',
  '#sticky' => TRUE,
  '#header' => [...],
  '#rows' => [...],
];
...

1. Create table in closed 'details' element accourding to example above.
2. Add any elements above the closed 'details' element with the table to be able to scroll before opening 'details'.
3. Open the page with elements, scroll to the 'details' element, open it, and scroll the table. You should see that the sticky header doesn't appear.

Proposed resolution

Recalculate sticky header during scroll.

I am sure that it is not the best solution, so, we can consider a better solution than recalculating sticky header during scroll.

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

N/A

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Javascript 

Last updated about 5 hours ago

Created by

🇺🇦Ukraine Ressinel 🇺🇦 Rivne

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

  • Issue created by @Ressinel
  • Status changed to Needs review about 1 year ago
  • 🇺🇦Ukraine Ressinel 🇺🇦 Rivne
  • Status changed to Needs work about 1 year ago
  • 🇺🇸United States smustgrave

    Thank you for reporting,

    As a bug will need test coverage to show the problem.

    Also recommended to use MRs now as patches are no longer auto ran and will be phased out.

  • last update 10 months ago
    Custom Commands Failed
  • First commit to issue fork.
  • 🇮🇳India Aadhar_Gupta

    Created MR for the patch.

  • Status changed to Needs review 9 months ago
  • Status changed to Needs work 9 months ago
  • The Needs Review Queue Bot tested this issue.

    While you are making the above changes, we recommend that you convert this patch to a merge request . Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

  • 🇺🇸United States bnjmnm Ann Arbor, MI

    Perhaps the solution can be more targeted and the sticky recalculated on the details toggle event instead of the current approach that uses the more-often-occuring scroll event.

    It's also worth pointing out that this will likely no longer be an issue once this issue lands: 📌 Make drupal.tableheader only use CSS for sticky table headers Needs review

    However, that would only benefit 11.x, and it would be nice to get this bug addressed for Drupal 10 so this could still be worth doing.

  • First commit to issue fork.
  • Pipeline finished with Failed
    8 months ago
    #174832
  • Pipeline finished with Failed
    8 months ago
    Total: 979s
    #178014
  • Pipeline finished with Failed
    8 months ago
    Total: 1110s
    #178720
  • Pipeline finished with Failed
    8 months ago
    Total: 987s
    #178874
  • Pipeline finished with Failed
    8 months ago
    Total: 1022s
    #179137
  • The current MR works only if we change table.sticky-enabled to table.position-sticky in this

    function tableHeaderInitHandler(e) {
        once('tableheader', $(e.data.context).find('table.sticky-enabled')).forEach(
          (table) => {
            TableHeader.tables.push(new TableHeader(table));
          },
        );
        forTables('onScroll');
      }

    .
    After this is changed then the recalculate function is invoked as expected.The problem with this is that the table is still overflowing out of the details element as you can see in the SS.

  • 🇺🇸United States bnjmnm Ann Arbor, MI

    Re #12
    The current MR works only if we change table.sticky-enabled to table.position-sticky in this

    Claro tableheaders are sticky even though it isn't initializing in tableheader.js, so that means that stickiness is being achieved

    Claro moved away from using tableheader.js mid-2023 in favor of CSS positioning. 📌 Use position: sticky for views sticky table header Fixed . Even if this wasn't directly obvious, the fact that the tableheaders are sticky despite not being seen by tableheader.js strongly suggests the stickiness is being implemented without tableheader.js.

    But before you go digging in that direction, it's probably worth confirming the issue as reported can even be reproduced in Claro. I can't reproduce it and I assume it works fine because it now uses CSS positioning.

    From the looks of it there's nothing needed in Claro. Even if that was reported in the issue, it's always possible something changed in an update and the reporter might be using an earlier version of Drupal.

    If you try a theme that isn't Claro, this does appear to still be an issue

    While updating tableheader might fix it, perhaps using CSS positioning in the same manner as Claro could do the trick as well. I recommend seeing if there aren't already issues-in-progress for either of those before determining what to do here.

Production build 0.71.5 2024