<div id="root"></div>
:root {
--line-size: 3px;
--line-colour: #74c6e5;
--hover-colour: #07698e;
}
body {
padding: 30px;
margin: 0;
font-family: Helvetica, Arial, sans-serif;
}
ul, li {
list-style: none;
padding: 0;
margin: 0;
}
.wrapper {
max-width: 1000px;
padding: 30px;
margin: 0 auto;
}
.tab-content {
padding: 30px 0;
line-height: 1.6;
}
/* Tabs */
.tabs-list {
display: flex;
border-bottom: var(--line-size) solid #ecf0f5;
position: relative;
}
.tabs-list__item {
margin-right: 30px;
}
/* The individual tab buttons */
.tabs-list__tab {
display: block;
color: #2c3642;
padding: 18px 6px;
position: relative;
text-decoration: none;
font-weight: bold;
white-space: nowrap;
}
.tabs-list__tab:hover {
color: var(--hover-colour);
}
/* A psuedo-element to use as an active underline */
.tabs-list__tab:after {
content: '';
position: absolute;
top: 100%;
left: 0;
height: var(--line-size);
width: 100%;
}
.tabs-list__tab.active:after {
background-color: var(--line-colour);
}
const { HashRouter, Switch, Route, Redirect, Link, matchPath, withRouter } = ReactRouterDOM;
const Tabs = withRouter(({ location, items, match }) => {
const active = items.find((item) =>
matchPath(location.pathname, {
path: `/${item.route}`,
exact: true,
}),
);
const activeId = active && active.id;
return (
<div className="tabs">
<ul role="tablist" aria-orientation="horizontal" className="tabs-list">
{/* We loop through the "items" and create tabs for each */}
{items.map((item) => (
<li className="tabs-list__item" key={`tab-${item.name}`}>
<Link
to={item.route}
className={`tabs-list__tab ${activeId === item.id ? 'active' : 'inactive'}`}
>
{item.name}
</Link>
</li>
))}
</ul>
{/* Tab content - we use a Switch to only show one tab at a time */}
<Switch>
{/* We loop through the "items" and create routes to correspond to each tab */}
{items.map((item) => (
<Route
key={item.id}
path={`/${item.route}`}
render={item.render}
/>
))}
{/* We can have a Redirect route to ensure we always have an active route */}
<Route render={() => <Redirect to={items[0] ? items[0].route : '/'} />} />
</Switch>
</div>
)
});
function App() {
return (
<HashRouter>
<Tabs items={[
{
id: 'id1',
name: 'Tab #1',
route: 'id1',
render: () => (
<div className="tab-content">
Depths burying snare value law merciful value snare society eternal-return decieve aversion.
Holiest virtues pious war depths noble inexpedient against endless ultimate. Merciful
disgust convictions grandeur abstract battle gains revaluation fearful inexpedient right
holiest faithful battle. Merciful depths decrepit intentions virtues salvation war ultimate.
Sea transvaluation virtues suicide battle against victorious.
</div>
)
},
{
id: 'id2',
name: 'Tab #2',
route: 'id2',
render: () => (
<div className="tab-content">
Ideal overcome free burying grandeur aversion. Dead morality self right superiority passion
virtues hope society play of snare grandeur. Good oneself burying law good ultimate burying.
Play justice snare holiest noble sea reason marvelous right.
</div>
)
},
{
id: 'id3',
name: 'Tab #3',
route: 'id3',
render: () => (
<div className="tab-content">
Inexpedient gains prejudice aversion pious snare noble ocean ocean overcome self ubermensch
prejudice philosophy. Ocean strong sea burying reason ultimate burying spirit. Pious christianity
decieve endless abstract decrepit abstract. Ocean burying depths evil horror suicide mountains
fearful depths christianity disgust gains horror. Self marvelous passion faith against grandeur.
</div>
)
}
]} />
</HashRouter>
);
}
ReactDOM.render(<App />, document.getElementById('root'));
View Compiled
This Pen doesn't use any external CSS resources.