Skip to content

Commit bc346c5

Browse files
authored
fix(tabs): line indicator transition bug (#550) (#615)
1 parent deee969 commit bc346c5

File tree

1 file changed

+9
-3
lines changed
  • packages/react-vant/src/components/tabs

1 file changed

+9
-3
lines changed

packages/react-vant/src/components/tabs/Tabs.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, {
44
useEffect,
55
forwardRef,
66
useImperativeHandle,
7+
useState,
78
} from 'react'
89
import clsx from 'clsx'
910

@@ -105,13 +106,14 @@ const Tabs = forwardRef<TabsInstance, TabsProps>((props, ref) => {
105106
)
106107

107108
// 下划线偏移量
108-
const lineTranslateLeft = useMemo(() => {
109+
const [lineTranslateLeft, setLineTranslateLeft] = useState<number>(0)
110+
useUpdateEffect(() => {
109111
const hidden = isHidden(root.current)
110112
const title = titleRefs?.[index]
111113
if (!title || hidden || props.type !== 'line') {
112114
return
113115
}
114-
return title.offsetLeft + title.offsetWidth / 2
116+
setLineTranslateLeft(title.offsetLeft + title.offsetWidth / 2)
115117
}, [root.current, titleRefs, props.type, index])
116118

117119
// 下划线样式
@@ -364,7 +366,11 @@ const Tabs = forwardRef<TabsInstance, TabsProps>((props, ref) => {
364366

365367
return (
366368
<TabsContext.Provider value={{ props, currentName, scrollIntoView }}>
367-
<div ref={root} className={clsx(props.className, bem([props.type]))} style={ props.style }>
369+
<div
370+
ref={root}
371+
className={clsx(props.className, bem([props.type]))}
372+
style={props.style}
373+
>
368374
{props.sticky ? (
369375
<Sticky
370376
container={root}

0 commit comments

Comments
 (0)