-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add Tabs navigation #535
Open
dani-moreno
wants to merge
13
commits into
main
Choose a base branch
from
tab-navigation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Tabs navigation #535
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
271feb1
Basic
dani-moreno 1fc9992
Tab component
dani-moreno 89733c1
Move category
dani-moreno b6f1d7f
Add ring for keyboard navigation
dani-moreno e3caf67
Correct color
dani-moreno bb783c2
Styling
dani-moreno a1f4e25
package conflict
dani-moreno 1018329
Merge branch 'main' into tab-navigation
dani-moreno d8922ef
Provide whole tab data
dani-moreno 8a3aa59
Fixes
dani-moreno 5c1e2c8
secondary as boolean
dani-moreno b3853d4
styles with cva
dani-moreno 7746042
prettier
dani-moreno 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 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,60 @@ | ||
import { action } from "@storybook/addon-actions" | ||
import type { Meta, StoryObj } from "@storybook/react" | ||
import { useState } from "react" | ||
import { Tabs } from "." | ||
|
||
const meta: Meta<typeof Tabs> = { | ||
component: Tabs, | ||
tags: ["autodocs"], | ||
argTypes: { | ||
secondary: { | ||
control: "boolean", | ||
}, | ||
}, | ||
} | ||
|
||
export default meta | ||
type Story = StoryObj<typeof Tabs> | ||
|
||
const tabItems = [ | ||
{ label: "Overview", link: "/overview" }, | ||
{ label: "Courses", link: "/courses" }, | ||
{ label: "Categories", link: "/categories" }, | ||
{ label: "Catalog", link: "/catalog" }, | ||
{ label: "Requests", link: "/requests" }, | ||
] | ||
|
||
const TabsExample = ({ secondary = false }: { secondary?: boolean }) => { | ||
const [activeTab, setActiveTab] = useState("Overview") | ||
|
||
return ( | ||
<div onClick={(e) => e.preventDefault()}> | ||
<Tabs | ||
tabs={tabItems} | ||
activeTab={activeTab} | ||
onTabChange={(tab) => { | ||
setActiveTab(tab.label) | ||
action("Tab changed")(tab) | ||
}} | ||
secondary={secondary} | ||
/> | ||
<p className="mt-4 flex h-full min-h-60 items-center justify-center rounded-lg bg-f1-background-secondary/50 p-4 text-f1-foreground-secondary"> | ||
{activeTab} | ||
</p> | ||
</div> | ||
) | ||
} | ||
|
||
export const Primary: Story = { | ||
args: { | ||
secondary: false, | ||
}, | ||
render: (args) => <TabsExample {...args} />, | ||
} | ||
|
||
export const Secondary: Story = { | ||
args: { | ||
secondary: true, | ||
}, | ||
render: (args) => <TabsExample {...args} />, | ||
} |
This file contains 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,36 @@ | ||
import { TabNavigation, TabNavigationLink } from "@/ui/tab-navigation" | ||
|
||
interface TabItem { | ||
label: string | ||
link: string | ||
} | ||
|
||
interface TabsProps { | ||
tabs: TabItem[] | ||
activeTab: string | ||
onTabChange: (tab: TabItem) => void | ||
secondary?: boolean | ||
} | ||
|
||
export function Tabs({ | ||
tabs, | ||
activeTab, | ||
onTabChange, | ||
secondary = false, | ||
}: TabsProps) { | ||
return ( | ||
<TabNavigation secondary={secondary}> | ||
{tabs.map((tab) => ( | ||
<TabNavigationLink | ||
key={tab.label} | ||
active={activeTab === tab.label} | ||
onClick={() => onTabChange(tab)} | ||
href={tab.link} | ||
secondary={secondary} | ||
> | ||
{tab.label} | ||
</TabNavigationLink> | ||
))} | ||
</TabNavigation> | ||
) | ||
} |
This file contains 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,137 @@ | ||
import { cn } from "@/lib/utils" | ||
import * as NavigationMenuPrimitives from "@radix-ui/react-navigation-menu" | ||
import { cva, type VariantProps } from "class-variance-authority" | ||
import { motion } from "framer-motion" | ||
import * as React from "react" | ||
|
||
function getSubtree( | ||
options: { asChild: boolean | undefined; children: React.ReactNode }, | ||
content: React.ReactNode | ((children: React.ReactNode) => React.ReactNode) | ||
) { | ||
const { asChild, children } = options | ||
if (!asChild) | ||
return typeof content === "function" ? content(children) : content | ||
|
||
const firstChild = React.Children.only(children) as React.ReactElement | ||
return React.cloneElement(firstChild, { | ||
children: | ||
typeof content === "function" | ||
? content(firstChild.props.children) | ||
: content, | ||
}) | ||
} | ||
|
||
const tabNavigationVariants = cva( | ||
"flex items-center justify-start gap-1 overflow-x-auto whitespace-nowrap border-b border-b-f1-border-secondary px-6 py-3 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden", | ||
{ | ||
variants: { | ||
secondary: { | ||
true: "bg-f1-background-secondary/25", | ||
false: "bg-f1-background-transparent", | ||
}, | ||
}, | ||
defaultVariants: { | ||
secondary: false, | ||
}, | ||
} | ||
) | ||
|
||
interface TabNavigationProps | ||
extends React.ComponentPropsWithoutRef<typeof NavigationMenuPrimitives.Root>, | ||
VariantProps<typeof tabNavigationVariants> {} | ||
|
||
const TabNavigation = React.forwardRef< | ||
React.ElementRef<typeof NavigationMenuPrimitives.Root>, | ||
TabNavigationProps | ||
>(({ className, children, secondary, ...props }, forwardedRef) => ( | ||
<NavigationMenuPrimitives.Root ref={forwardedRef} {...props} asChild={false}> | ||
<NavigationMenuPrimitives.List | ||
className={cn(tabNavigationVariants({ secondary }), className)} | ||
> | ||
{children} | ||
</NavigationMenuPrimitives.List> | ||
</NavigationMenuPrimitives.Root> | ||
)) | ||
|
||
TabNavigation.displayName = "TabNavigation" | ||
|
||
const tabNavigationLinkVariants = cva( | ||
"flex items-center justify-center whitespace-nowrap rounded-md px-3 py-1.5 font-medium transition-all", | ||
{ | ||
variants: { | ||
secondary: { | ||
true: "bg-f1-background/60 group-hover:border-f1-border group-data-[active=true]:border-f1-border group-data-[active=true]:text-f1-foreground", | ||
false: | ||
"bg-f1-background-transparent group-hover:bg-f1-background-secondary group-hover:text-f1-foreground group-data-[active=true]:bg-f1-background-secondary group-data-[active=true]:text-f1-foreground", | ||
}, | ||
disabled: { | ||
true: "pointer-events-none text-f1-foreground-disabled", | ||
}, | ||
}, | ||
defaultVariants: { | ||
secondary: false, | ||
disabled: false, | ||
}, | ||
} | ||
) | ||
|
||
interface TabNavigationLinkProps | ||
extends Omit< | ||
React.ComponentPropsWithoutRef<typeof NavigationMenuPrimitives.Link>, | ||
"onSelect" | ||
>, | ||
VariantProps<typeof tabNavigationLinkVariants> { | ||
active?: boolean | ||
} | ||
|
||
const TabNavigationLink = React.forwardRef< | ||
React.ElementRef<typeof NavigationMenuPrimitives.Link>, | ||
TabNavigationLinkProps | ||
>( | ||
( | ||
{ asChild, disabled, active, className, children, secondary, ...props }, | ||
forwardedRef | ||
) => ( | ||
<NavigationMenuPrimitives.Item className="flex"> | ||
<NavigationMenuPrimitives.Link | ||
data-active={active ? "true" : undefined} | ||
aria-disabled={disabled || undefined} | ||
className={cn( | ||
"group relative flex shrink-0 select-none items-center justify-center rounded-md no-underline focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-f1-ring focus-visible:ring-offset-1", | ||
disabled ? "pointer-events-none" : "" | ||
)} | ||
ref={forwardedRef} | ||
onSelect={() => {}} | ||
asChild={asChild} | ||
{...props} | ||
> | ||
{getSubtree({ asChild, children }, (children) => ( | ||
<span | ||
className={cn( | ||
"border border-solid border-transparent text-f1-foreground-secondary", | ||
tabNavigationLinkVariants({ secondary, disabled }), | ||
className | ||
)} | ||
> | ||
{children} | ||
{active && !secondary && ( | ||
<motion.div | ||
layoutId="underline" | ||
className="absolute inset-x-0 -bottom-3 h-px bg-f1-background-bold" | ||
transition={{ | ||
type: "spring", | ||
bounce: 0.2, | ||
duration: 0.5, | ||
}} | ||
/> | ||
)} | ||
</span> | ||
))} | ||
</NavigationMenuPrimitives.Link> | ||
</NavigationMenuPrimitives.Item> | ||
) | ||
) | ||
|
||
TabNavigationLink.displayName = "TabNavigationLink" | ||
|
||
export { TabNavigation, TabNavigationLink } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nlopin not sure what's going on here, if I set the border this way, it is not displayed (that's why I was doing this weird thing with border-transparent etc earlier) 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are you sure
border-b
does something?