<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mauro Centurion</title>
    <description>The latest articles on Forem by Mauro Centurion (@maurocen).</description>
    <link>https://forem.com/maurocen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1199303%2Fb84897f9-4ff4-4bea-a0fb-b5f98e55924b.png</url>
      <title>Forem: Mauro Centurion</title>
      <link>https://forem.com/maurocen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/maurocen"/>
    <language>en</language>
    <item>
      <title>Fixing a table header on a horizontally scrolling table</title>
      <dc:creator>Mauro Centurion</dc:creator>
      <pubDate>Thu, 11 Jan 2024 13:17:07 +0000</pubDate>
      <link>https://forem.com/maurocen/fixing-a-table-header-on-a-horizontally-scrolling-table-2p7m</link>
      <guid>https://forem.com/maurocen/fixing-a-table-header-on-a-horizontally-scrolling-table-2p7m</guid>
      <description>&lt;p&gt;You would think this is easy. But it really isn’t.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 0: The environment
&lt;/h2&gt;

&lt;p&gt;This entire post is based on a React application, so while it’s not mandatory, it would help if you had at least basic knowledge of React and React Hooks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 1: The initial problem
&lt;/h2&gt;

&lt;p&gt;In a project we’ve been recently working on we faced a very specific issue: the client wanted a large table.&lt;/p&gt;

&lt;p&gt;What do I mean by &lt;em&gt;large&lt;/em&gt;? Well, I’ve seen tables that have 15+ columns and are much wider than the screen. Luckily this was not one of those tables, but it was wider than the available space anyways.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa7mkymzz6g75wz51cqtj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa7mkymzz6g75wz51cqtj.png" alt="Table with Pokémon info. Table overflows to the right"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In addition to this, the table needed to have buttons to be easily scrollable.&lt;/p&gt;

&lt;p&gt;In order to achieve this, we wrapped the table in a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; that had its &lt;code&gt;width&lt;/code&gt; locked to a specific value (something like 80% of the screen's width). We then added some buttons and a &lt;code&gt;scrolling&lt;/code&gt; function that changes the scroll position of the wrapper. The scrolling function looked something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7g2n227sk6sjexdcuol.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr7g2n227sk6sjexdcuol.png" alt="useCallback hook for horizontally scrolling table"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This resulted in a nice table with the horizontal scroll and also buttons to scroll in case you don’t have a touchpad.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv8gl5wh0t9jd29vu6lft.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv8gl5wh0t9jd29vu6lft.gif" alt="Animation of table scrolling vertically"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was good, and working as expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 2: New requirements arise
&lt;/h2&gt;

&lt;p&gt;The table in this state was approved and deployed to production. Everything was looking fine until some users started requesting that we leave the header fixed to the top when scrolling. That way they would be able to recognize what each value was without needing to scroll back and forth to check the column name.&lt;/p&gt;

&lt;p&gt;Our first instinct was what I think most people would do: we added &lt;code&gt;position: sticky&lt;/code&gt; to the &lt;code&gt;thead&lt;/code&gt; element of the table.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faamtf9qneqvao6hvaljv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faamtf9qneqvao6hvaljv.png" alt="CSS code for sticky table header"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This seems too easy to be true, right? That’s because it is, please see what happens when we do this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frgrv9u1ofpceveo2lx2x.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frgrv9u1ofpceveo2lx2x.gif" alt="Animation of table header scrolling away even when the elements have position: sticky defined"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that the table elements effectively have the position: sticky attribute, but after scrolling a bit they leave the screen.&lt;/p&gt;

&lt;p&gt;This is because, as per &lt;a href="https://medium.com/r/?url=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FCSS%2Fposition%23sticky" rel="noopener noreferrer"&gt;CSS specifications&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This value always creates a new stacking context. Note that a sticky element “sticks” to its nearest ancestor that has a “scrolling mechanism” (created when overflow is hidden, scroll, auto, or overlay), even if that ancestor isn’t the nearest actually scrolling ancestor.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So the problem here is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.layout__content&lt;/code&gt; is scrolling.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.table__wrapper&lt;/code&gt; is not scrolling.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.table&lt;/code&gt; thead is sticky relative to &lt;code&gt;.table_wrapper&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What we wanted was &lt;code&gt;.table thead&lt;/code&gt; to be sticky relative to &lt;code&gt;.layout__content&lt;/code&gt;. This is not possible because even though &lt;code&gt;.table__wrapper&lt;/code&gt; only has overflow-x specified it still has an overflow property, so the &lt;code&gt;.table thead&lt;/code&gt; sticks to it instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 3: First approach
&lt;/h2&gt;

&lt;p&gt;So, taking the specifications into account, let’s remove the &lt;code&gt;overflow-x&lt;/code&gt; from &lt;code&gt;.layout__content&lt;/code&gt; and see what happens.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqrewf5fef8zbreh2m6l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqrewf5fef8zbreh2m6l.png" alt="CSS code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What happens, then?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjtx6kuplfgktdrstcpi7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjtx6kuplfgktdrstcpi7.gif" alt="Animation of table with sticky header"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, it looks like it worked… or did it?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp18v2ba3ye20hwp6rkvf.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp18v2ba3ye20hwp6rkvf.gif" alt="Animation showing sticky header but broken horizontal scrolling"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Oh, no! Our scroll buttons stopped working. Not only that, now scrolling the table scrolls the entire page, including the title. That was not supposed to happen.&lt;/p&gt;

&lt;p&gt;But, hey! At least our headers are fixed now!&lt;/p&gt;

&lt;h2&gt;
  
  
  Chapter 4: Don’t tell me what to do!
&lt;/h2&gt;

&lt;p&gt;So after searching for solutions (and scratching our heads) for quite some time, we came up with a solution for the issue at hand: let’s use JavaScript and CSS!&lt;/p&gt;

&lt;p&gt;The basic idea is this:&lt;/p&gt;

&lt;p&gt;Let’s see where the table header is.&lt;br&gt;
Let’s see how far down the user has scrolled. a. If the user has scrolled below the header start, let’s &lt;code&gt;translate&lt;/code&gt; the &lt;code&gt;thead&lt;/code&gt; element so it keeps at the top. b. If not, remove the &lt;code&gt;translate&lt;/code&gt; property from the table header.&lt;br&gt;
Since we were using React for this app, we made use of some &lt;code&gt;refs&lt;/code&gt; to keep track of the actual DOM elements.&lt;/p&gt;

&lt;p&gt;The code turned out to look something like this (I wrote it as a hook for reusability):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbhnzzd5gqynohxduar68.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbhnzzd5gqynohxduar68.png" alt="React hook for table scrolling"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s break the code down:&lt;/p&gt;

&lt;p&gt;Firstly, we declare two &lt;code&gt;refs&lt;/code&gt;, one for the table wrapper and one for the table headers.&lt;/p&gt;

&lt;p&gt;You may ask &lt;em&gt;why&lt;/em&gt; we declare a ref for the table wrapper instead of the table itself. The reason behind this is that further down the code we use some positioning to calculate how much the user has scrolled, and the &lt;code&gt;offsetTop&lt;/code&gt; is relative to the parent, so the table always has a &lt;code&gt;offsetTop&lt;/code&gt; equal to zero relative to its parent (the wrapper). The wrapper is supposed to be a direct child of the scrolling element (in our case, the &lt;code&gt;layout__content&lt;/code&gt; element).&lt;/p&gt;

&lt;p&gt;We then write a &lt;code&gt;useEffect&lt;/code&gt; hook that is in charge of adding an event listener to the scrolling element so that when it scrolls we act accordingly.&lt;/p&gt;

&lt;p&gt;What do we mean by &lt;em&gt;acting accordingly&lt;/em&gt;? Well, we should check the position of the header relative to the scrolling element, but as we will be moving it, we should check the table position instead, and that’s where we use the table wrapper, as that element will not move around relative to the content element. From then on we can see if the parent element has scrolled past the header position, and if so, we &lt;code&gt;translate&lt;/code&gt; that element by that scroll position difference (&lt;code&gt;scroll position&lt;/code&gt; - &lt;code&gt;header position&lt;/code&gt;). If the &lt;code&gt;scroll position&lt;/code&gt; is above the header position we just remove that &lt;code&gt;translate&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;It’s also important to remember to remove that event listener, otherwise after navigating to another section of the app we could be referencing elements that are no longer there, and the app would crash. To accomplish this we return a function that does exactly that inside our &lt;code&gt;useEffect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, &lt;em&gt;did it work&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpit7pulcbvg1u0p8hye5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpit7pulcbvg1u0p8hye5.gif" alt="Animation of fully functional table: horizontal scrolling and fixed header"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yes! It’s not 100% perfect, the header may jump a little bit but that’s because of the event loop, but that’s out of the scope of this blog post.&lt;/p&gt;

&lt;p&gt;But there’s something that pops up in the dev tools:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fproydj10r0d0wjis0u0f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fproydj10r0d0wjis0u0f.png" alt="Firefox alerts of scroll-linked positioning effect"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Following that link we see that Firefox &lt;a href="https://medium.com/r/?url=https%3A%2F%2Ffirefox-source-docs.mozilla.org%2Fperformance%2Fscroll-linked_effects.html%23example-1-sticky-positioning" rel="noopener noreferrer"&gt;recommends&lt;/a&gt; using &lt;code&gt;position: sticky&lt;/code&gt; for what we are doing.&lt;/p&gt;

&lt;p&gt;Well, Firefox, we’d use &lt;code&gt;position: sticky&lt;/code&gt; if it worked like we wanted it to work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;This problem was quite entertaining to work on, but I think that it’d be nice if the CSS specification could take this into account. Maybe a property called &lt;code&gt;sticky-anchor&lt;/code&gt;&lt;code&gt;with values&lt;/code&gt;"ancestor" | "screen"`&lt;code&gt;would be nice. I think&lt;/code&gt;ancestor&lt;code&gt;would keep the current behavior (and be the default value) and&lt;/code&gt;screen` would only take into account the whole screen. If that were the case we would be able to skip all that code and just have CSS like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnx1634gdfusat8yrdc2h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnx1634gdfusat8yrdc2h.png" alt="Recommended code with position: sticky and sticky-anchor: screen"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, this method is not the best, scrolling too fast or on mid-to-low end devices causes some jittering in the header, so if you need a 100% perfect solution you should look into the event loop and animation loop for it to be even better.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
