admin管理员组文章数量:1125082
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';
import 'react-tabs/style/react-tabs.css';
.....
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
?
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';
import 'react-tabs/style/react-tabs.css';
.....
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
Reset to default 2You can achieve this using plain CSS.
- Add a container around the
<Tabs>
component - Give fixed height to the above container
- Add
position: sticky;
to the<TabList>
component.
Here is a sample code to try.
File: App.css
.tab-container {
max-height: 400px; /* adding height to create scroll */
overflow-y: auto;
border: 1px solid #ccc;
position: relative;
}
.tab-list {
position: sticky;
top: 0;
background-color: white;
z-index: 10;
border-bottom: 1px solid #ccc;
margin: 0;
}
Update the component as below to apply the CSS classes
File: App.js
import { Tab, Tabs, TabList, TabPanel } from "react-tabs";
import "react-tabs/style/react-tabs.css";
import "./App.css";
function App() {
return (
<div className="tab-container">
<Tabs>
<TabList className="tab-list">
<Tab>Title 1</Tab>
<Tab>Title 2</Tab>
</TabList>
<TabPanel>
<h2>Any content 1</h2>
<p>
This will cause scrolling if it's long enough.
Add more text here to see the scrolling effect, if required.
</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
</TabPanel>
<TabPanel>
<h2>Any content 2</h2>
<p>
This is another content section with small content.
</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
<p>More content...</p>
</TabPanel>
</Tabs>
</div>
);
}
export default App;
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.
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!
本文标签: reactjsSticky headerswith reacttabsStack Overflow
版权声明:本文标题:reactjs - Sticky headers, with react-tabs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736652809a1946180.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论