fix: tablist indicator initial position

- Modified the `Tabs` component to initialize the indicator state with `undefined` for width and position.
- Updated rendering logic in `TabsList` to conditionally apply styles based on the presence of indicator values, improving robustness.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei 2025-05-20 19:38:51 +08:00
parent 5ce34edc98
commit 014c2cafc5
No known key found for this signature in database
GPG Key ID: 0F62D33977F021F7
1 changed files with 12 additions and 9 deletions

View File

@ -8,8 +8,8 @@ const TabsIdContext = React.createContext<string | null>(null)
const SetTabIndicatorContext = React.createContext<
React.Dispatch<
React.SetStateAction<{
w: number
x: number
w: number | undefined
x: number | undefined
}>
>
>(() => {})
@ -17,8 +17,8 @@ const SetTabIndicatorContext = React.createContext<
const TabVariantContext = React.createContext<"default" | "rounded" | undefined>(undefined)
const TabIndicatorContext = React.createContext<{
w: number
x: number
w: number | undefined
x: number | undefined
} | null>(null)
const Tabs: ComponentWithRef<
@ -29,9 +29,12 @@ const Tabs: ComponentWithRef<
HTMLDivElement
> = ({ ref, ...props }) => {
const { children, variant, ...rest } = props
const [indicator, setIndicator] = React.useState({
w: 0,
x: 0,
const [indicator, setIndicator] = React.useState<{
w: number | undefined
x: number | undefined
}>({
w: undefined,
x: undefined,
})
const id = React.useId()
@ -68,7 +71,7 @@ const TabsList = ({
)}
>
{props.children}
{indicator && (
{indicator?.x !== undefined && (
<span
className={cn(
"absolute left-0 duration-200 will-change-[transform,width]",
@ -78,7 +81,7 @@ const TabsList = ({
)}
style={{
width: indicator.w,
transform: `translate3d(${indicator.x}px, 0, 0)`,
transform: indicator.x ? `translate3d(${indicator.x}px, 0, 0)` : undefined,
}}
/>
)}