Skip to content

Commit 605c566

Browse files
authored
Merge pull request #8673 from amit-ksh/chakra/tabs
Migrate Tabs to Chakra
2 parents 55486e7 + 34ee4de commit 605c566

File tree

3 files changed

+56
-59
lines changed

3 files changed

+56
-59
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { ComponentStyleConfig } from "@chakra-ui/theme"
2+
3+
export const Tabs: ComponentStyleConfig = {
4+
variants: {
5+
primary: {
6+
tab: {
7+
borderTopRadius: "0.3rem",
8+
borderBottom: "1px solid",
9+
borderBottomColor: "primary",
10+
px: 4,
11+
py: "0.3rem",
12+
_selected: {
13+
color: "background",
14+
bg: "primary",
15+
},
16+
},
17+
tabpanels: {
18+
mt: 4,
19+
},
20+
tabpanel: {
21+
p: 6,
22+
bg: "ednBackground",
23+
border: "1px solid",
24+
borderColor: "lightBorder",
25+
borderRadius: "lg",
26+
},
27+
},
28+
},
29+
defaultProps: {
30+
variant: "primary",
31+
},
32+
}

src/@chakra-ui/gatsby-plugin/components/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { Link } from "./Link"
33
import { Tag } from "./Tag"
44
import { Modal } from "./Modal"
55
import { Checkbox } from "./Checkbox"
6+
import { Tabs } from "./Tabs"
67

78
export default {
89
Button,
910
Link,
1011
Tag,
1112
Modal,
1213
Checkbox,
14+
Tabs,
1315
}

src/components/Tabs.tsx

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,11 @@
1-
import React, { ReactNode, useState } from "react"
2-
import styled from "@emotion/styled"
3-
4-
const TabList = styled.ul`
5-
display: flex;
6-
margin: 0;
7-
list-style-type: none;
8-
list-style-image: none;
9-
`
10-
11-
const Tab = styled.li`
12-
border-bottom: 1px solid ${(props) => props.theme.colors.primary};
13-
margin: 0;
14-
`
15-
16-
const TabButton = styled.button<{ selected: boolean }>`
17-
display: block;
18-
cursor: pointer;
19-
color: ${({ theme, selected }) =>
20-
selected ? theme.colors.background : theme.colors.text};
21-
background-color: ${({ selected, theme }) =>
22-
selected ? theme.colors.primary : "transparent"};
23-
border: 0;
24-
border-top-left-radius: 0.3rem;
25-
border-top-right-radius: 0.3rem;
26-
padding: 0.3rem 1rem;
27-
`
28-
29-
const TabPanel = styled.div`
30-
background: ${(props) => props.theme.colors.ednBackground};
31-
border-radius: 0.5rem;
32-
border: 1px solid ${(props) => props.theme.colors.lightBorder};
33-
margin-top: 1rem;
34-
padding: 1.5rem;
35-
`
1+
import React, { ReactNode } from "react"
2+
import {
3+
Tabs as ChakraTabs,
4+
TabList,
5+
Tab,
6+
TabPanels,
7+
TabPanel,
8+
} from "@chakra-ui/react"
369

3710
interface Tab {
3811
title: string
@@ -44,38 +17,28 @@ export interface IProps {
4417
onTabClick?: (tabIndex: number) => void
4518
}
4619

47-
/**
48-
* Minimal & temp Tab component until we migrate over a UI lib
49-
*/
5020
const Tabs: React.FC<IProps> = ({ tabs, onTabClick }) => {
51-
const [selectedIndex, setSelectedIndex] = useState(0)
52-
const selectedTab = tabs[selectedIndex]
53-
5421
const handleTabClick = (index: number) => {
55-
setSelectedIndex(index)
56-
5722
if (onTabClick) {
5823
onTabClick(index)
5924
}
6025
}
6126

6227
return (
63-
<div>
64-
<nav>
65-
<TabList>
66-
{tabs.map((tab, index) => (
67-
<Tab key={index} onClick={() => handleTabClick(index)}>
68-
<TabButton selected={index === selectedIndex}>
69-
{tab.title}
70-
</TabButton>
71-
</Tab>
72-
))}
73-
</TabList>
74-
</nav>
75-
<main>
76-
<TabPanel>{selectedTab.content}</TabPanel>
77-
</main>
78-
</div>
28+
<ChakraTabs as="nav">
29+
<TabList>
30+
{tabs.map((tab, index) => (
31+
<Tab key={index} onClick={() => handleTabClick(index)}>
32+
{tab.title}
33+
</Tab>
34+
))}
35+
</TabList>
36+
<TabPanels as="main">
37+
{tabs.map((tab, index) => (
38+
<TabPanel key={index}>{tab.content}</TabPanel>
39+
))}
40+
</TabPanels>
41+
</ChakraTabs>
7942
)
8043
}
8144

0 commit comments

Comments
 (0)