Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Tabs to Chakra #8673

Merged
merged 2 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/@chakra-ui/gatsby-plugin/components/Tabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { ComponentStyleConfig } from "@chakra-ui/theme"

export const Tabs: ComponentStyleConfig = {
variants: {
primary: {
tab: {
borderTopRadius: "0.3rem",
borderBottom: "1px solid",
borderBottomColor: "primary",
px: 4,
py: "0.3rem",
_selected: {
color: "background",
bg: "primary",
},
},
tabpanels: {
mt: 4,
},
tabpanel: {
p: 6,
bg: "ednBackground",
border: "1px solid",
borderColor: "lightBorder",
borderRadius: "lg",
},
},
},
defaultProps: {
variant: "primary",
},
}
2 changes: 2 additions & 0 deletions src/@chakra-ui/gatsby-plugin/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { Link } from "./Link"
import { Tag } from "./Tag"
import { Modal } from "./Modal"
import { Checkbox } from "./Checkbox"
import { Tabs } from "./Tabs"

export default {
Button,
Link,
Tag,
Modal,
Checkbox,
Tabs,
}
81 changes: 22 additions & 59 deletions src/components/Tabs.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
import React, { ReactNode, useState } from "react"
import styled from "@emotion/styled"

const TabList = styled.ul`
display: flex;
margin: 0;
list-style-type: none;
list-style-image: none;
`

const Tab = styled.li`
border-bottom: 1px solid ${(props) => props.theme.colors.primary};
margin: 0;
`

const TabButton = styled.button<{ selected: boolean }>`
display: block;
cursor: pointer;
color: ${({ theme, selected }) =>
selected ? theme.colors.background : theme.colors.text};
background-color: ${({ selected, theme }) =>
selected ? theme.colors.primary : "transparent"};
border: 0;
border-top-left-radius: 0.3rem;
border-top-right-radius: 0.3rem;
padding: 0.3rem 1rem;
`

const TabPanel = styled.div`
background: ${(props) => props.theme.colors.ednBackground};
border-radius: 0.5rem;
border: 1px solid ${(props) => props.theme.colors.lightBorder};
margin-top: 1rem;
padding: 1.5rem;
`
import React, { ReactNode } from "react"
import {
Tabs as ChakraTabs,
TabList,
Tab,
TabPanels,
TabPanel,
} from "@chakra-ui/react"

interface Tab {
title: string
Expand All @@ -44,38 +17,28 @@ export interface IProps {
onTabClick?: (tabIndex: number) => void
}

/**
* Minimal & temp Tab component until we migrate over a UI lib
*/
const Tabs: React.FC<IProps> = ({ tabs, onTabClick }) => {
const [selectedIndex, setSelectedIndex] = useState(0)
const selectedTab = tabs[selectedIndex]

const handleTabClick = (index: number) => {
setSelectedIndex(index)

if (onTabClick) {
onTabClick(index)
}
}

return (
<div>
<nav>
<TabList>
{tabs.map((tab, index) => (
<Tab key={index} onClick={() => handleTabClick(index)}>
<TabButton selected={index === selectedIndex}>
{tab.title}
</TabButton>
</Tab>
))}
</TabList>
</nav>
<main>
<TabPanel>{selectedTab.content}</TabPanel>
</main>
</div>
<ChakraTabs as="nav">
<TabList>
{tabs.map((tab, index) => (
<Tab key={index} onClick={() => handleTabClick(index)}>
{tab.title}
</Tab>
))}
</TabList>
<TabPanels as="main">
{tabs.map((tab, index) => (
<TabPanel key={index}>{tab.content}</TabPanel>
))}
</TabPanels>
</ChakraTabs>
)
}

Expand Down