In a web app (NextJS/React) I am using react-tabs to handle tabs.
I wonder how to keep the tab headers sticky. That is preventing them from scrolling up and disapearing when I scroll up the contents of the page.
After searching the net, I tried to use react-sticky, but it does not seem to work.
I even get the impression that react-tabs and react-sticky are not supposed to work together. Tell me if I am wrong.
Here is the relevant current code:
import {Tabs,TabList,Tab,TabPanel} from 'react-tabs';
.....
return (
<Tabs defaultIndex={tabInit()}>
<TabList>
<Tab>Title-One</Tab>
<Tab>Title-Two</Tab>
<Tab>Title-Three</Tab>
<Tab>Title-Four</Tab>
<Tab>Title-Five</Tab>
<Tab>Title-Six</Tab>
</TabList>
<TabPanel><Contents-One/></TabPanel>
<TabPanel><Contents-Two/></TabPanel>
<TabPanel><Contents-Three/></TabPanel>
<TabPanel><Contents-Four/></TabPanel>
<TabPanel><Contents-Five/></TabPanel>
<TabPanel><Contents-Six/></TabPanel>
</Tabs>
)
How can I get the effect I need while using react-tabs ?
2
Answers
I would rather look at navs that have a tab-like behavior rather than forcing stickiness upon a tab component, which is meant to be used within content and hence should scroll with the page.
You could take a look at React Bootstrap’s Navs and tabs or other libraries for navigation components.
Good luck!
You can achieve this using plain CSS.
<Tabs>
componentposition: sticky;
to the<TabList>
component.Here is a sample code to try.
File:
App.css
Update the component as below to apply the CSS classes
File:
App.js
Here is the code in a playground: https://playcode.io/2216306
Let me know if you can see the required effect after these changes or if you face any error.