-
Notifications
You must be signed in to change notification settings - Fork 10
Tab component v1 #63
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
Merged
Merged
Tab component v1 #63
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f5e845a
Tab component v1
saurabhsutar192 aa63a93
Tab Component Changes
saurabhsutar192 90280c0
Merge branch 'main' into tab-component
saurabhsutar192 f2bb0c5
document fix
saurabhsutar192 b8ae76a
minor fix
saurabhsutar192 b9a8f4d
mdx changes
saurabhsutar192 10429f2
mdx changes
saurabhsutar192 5b9e071
minor mdx changes
saurabhsutar192 3b4e2c1
type changes
saurabhsutar192 682d29e
changes for build
saurabhsutar192 cdaeef1
docs removed
saurabhsutar192 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { assignInlineVars } from "@vanilla-extract/dynamic"; | ||
| import { | ||
| forwardRef, | ||
| ForwardRefRenderFunction, | ||
| MouseEvent, | ||
| useEffect, | ||
| useState, | ||
| } from "react"; | ||
| import { Badge } from "../Badge"; | ||
| import { Flex } from "../Flex"; | ||
| import { | ||
| badgeStyles, | ||
| contentStyles, | ||
| iconStyles, | ||
| tabHeaderContainerStyles, | ||
| tabRecipe, | ||
| tabVars, | ||
| } from "./tab.css"; | ||
| import { TabsObjectProps, TabProps } from "./tab.types"; | ||
|
|
||
| const TabComponent: ForwardRefRenderFunction<HTMLDivElement, TabProps> = ( | ||
| { | ||
| defaultValue, | ||
| value, | ||
| color = "#2F80ED", | ||
| background = "#d7e9ff", | ||
| onChange = () => {}, | ||
| grow = false, | ||
| height = "40px", | ||
| tabData, | ||
| style, | ||
| className, | ||
| children, | ||
| ...nativeProps | ||
| }, | ||
| ref | ||
| ) => { | ||
| const [selectedTab, setSelectedTab] = useState<TabsObjectProps | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| const activeValue = value || defaultValue; | ||
| const tab = tabData.find((tabItem) => tabItem.value === activeValue); | ||
| tab && setSelectedTab(tab); | ||
| }, [defaultValue, tabData, value]); | ||
|
|
||
| const internalOnClickHandler = ( | ||
| event: MouseEvent<HTMLDivElement>, | ||
| tabItem: TabsObjectProps | ||
| ) => { | ||
| onChange(tabItem, event); | ||
| !value && setSelectedTab(tabItem); | ||
| }; | ||
|
|
||
| return ( | ||
| <div ref={ref} {...nativeProps} className={className} style={style}> | ||
| <Flex | ||
| style={assignInlineVars({ | ||
| [tabVars.height]: height, | ||
| })} | ||
| className={`${tabHeaderContainerStyles}`} | ||
| > | ||
| {tabData.map((tabItem, ind) => { | ||
| const tabClass = tabRecipe({ | ||
| active: tabItem.value === selectedTab?.value, | ||
| disabled: tabItem.disabled, | ||
| }); | ||
| return ( | ||
| <Flex | ||
| alignItems="center" | ||
| justifyContent="center" | ||
| flexGrow={grow ? "1" : "0"} | ||
| style={assignInlineVars({ | ||
| [tabVars.color]: color, | ||
| [tabVars.background]: background, | ||
| })} | ||
| className={tabClass} | ||
| onClick={(event) => internalOnClickHandler(event, tabItem)} | ||
| key={ind} | ||
| > | ||
| {tabItem.icon && ( | ||
| <span className={`${iconStyles}`}>{tabItem.icon}</span> | ||
| )} | ||
| {tabItem.label && <span>{tabItem.label}</span>} | ||
| {tabItem.badge && ( | ||
| <Badge | ||
| label={tabItem.badge} | ||
| className={`${badgeStyles}`} | ||
| shape={"rounded"} | ||
| color={"#DA2C2C"} | ||
| /> | ||
| )} | ||
| </Flex> | ||
| ); | ||
| })} | ||
| </Flex> | ||
| {selectedTab && ( | ||
| <div className={`${contentStyles}`}>{children(selectedTab)}</div> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const Tab = forwardRef(TabComponent); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from "./Tab"; | ||
| export * from "./tab.css"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { createTheme, style } from "@vanilla-extract/css"; | ||
| import { recipe } from "@vanilla-extract/recipes"; | ||
| import { TabTheme } from "./tab.types"; | ||
|
|
||
| export const [tabTheme, tabVars]: TabTheme = createTheme({ | ||
| color: "#2F80ED", | ||
| background: "#BDDBFF60", | ||
| height: "50px", | ||
| }); | ||
|
|
||
| export const tabHeaderContainerStyles = style({ | ||
| height: tabVars.height, | ||
| position: "relative", | ||
| borderBottom: "2px solid #EBECF0", | ||
| }); | ||
|
|
||
| export const tabRecipe = recipe({ | ||
| base: { | ||
| cursor: "pointer", | ||
| height: "100%", | ||
| position: "relative", | ||
| zIndex: 1, | ||
| padding: "8px 10px", | ||
| borderRadius: "4px 4px 0px 0px", | ||
| ":hover": { | ||
| background: tabVars.background, | ||
| color: tabVars.color, | ||
| }, | ||
| }, | ||
| variants: { | ||
| active: { | ||
| true: { | ||
| color: tabVars.color, | ||
| ":after": { | ||
| content: "", | ||
| background: tabVars.color, | ||
| position: "absolute", | ||
| bottom: "-2px", | ||
| left: 0, | ||
| borderRadius: "20px", | ||
| height: "2px", | ||
| width: "100%", | ||
| zIndex: 0, | ||
| }, | ||
| }, | ||
| }, | ||
| disabled: { | ||
| true: { | ||
| color: "#42526E", | ||
| pointerEvents: "none", | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| export const contentStyles = style({ | ||
| padding: "8px 0", | ||
| }); | ||
|
|
||
| export const iconStyles = style({ | ||
| marginRight: "6px", | ||
| }); | ||
|
|
||
| export const badgeStyles = style({ | ||
| marginLeft: "6px", | ||
| }); | ||
|
saurabhsutar192 marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| export type TabProps = JSX.IntrinsicElements["div"] & { | ||
| children: (selectedTab: TabsObjectProps) => JSX.Element; | ||
| color?: string; | ||
| background?: string; | ||
| defaultValue?: string | number; | ||
| value?: string | number; | ||
| height?: string; | ||
| onChange?: ( | ||
| value: TabsObjectProps, | ||
| event: React.MouseEvent<HTMLDivElement> | ||
| ) => void; | ||
| tabData: TabsObjectProps[]; | ||
| grow?: boolean; | ||
| }; | ||
|
|
||
| export type TabsObjectProps = { | ||
| label: string; | ||
| value: string | number; | ||
| icon?: JSX.Element; | ||
| disabled?: boolean; | ||
| badge?: string | JSX.Element; | ||
| }; | ||
|
|
||
| export type TabTheme = [ | ||
| string, | ||
| { | ||
| color: string; | ||
| background: string; | ||
| height: string; | ||
| } | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.