Skip to content

Commit

Permalink
enhance(Tabs): Add experimental prop autoScroll (#6497)
Browse files Browse the repository at this point in the history
* chore: init

* feat: autoScroll disabled

* chore: comment it
  • Loading branch information
zombieJ authored Dec 22, 2023
1 parent ff7ebbb commit dcfec98
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export type TabsProps = {
onChange?: (key: string) => void
children?: ReactNode
direction?: 'ltr' | 'rtl'
/**
* @experimental Support disabled auto scroll when Tabs header content change.
* This API name or function may change in the future.
* Please lock the version if you want to use it.
*/
autoScroll?: boolean
} & NativeProps<
| '--fixed-active-line-width'
| '--active-line-height'
Expand Down Expand Up @@ -83,7 +89,7 @@ export const Tabs: FC<TabsProps> = p => {
},
})

const [{ x, width }, api] = useSpring(() => ({
const [{ x, width }, inkApi] = useSpring(() => ({
x: 0,
width: 0,
config: {
Expand All @@ -108,12 +114,13 @@ export const Tabs: FC<TabsProps> = p => {
},
}))

function animate(immediate = false) {
function animate(immediate = false, fromMutation = false) {
const container = tabListContainerRef.current
if (!container) return

const activeIndex = keyToIndexRecord[activeKey as string]
if (activeIndex === undefined) {
api.start({
inkApi.start({
x: 0,
width: 0,
immediate: true,
Expand Down Expand Up @@ -161,7 +168,7 @@ export const Tabs: FC<TabsProps> = p => {
x = -(containerWidth - x - w)
}

api.start({
inkApi.start({
x,
width,
immediate,
Expand Down Expand Up @@ -193,11 +200,13 @@ export const Tabs: FC<TabsProps> = p => {
)
}

scrollApi.start({
scrollLeft: nextScrollLeft,
from: { scrollLeft: containerScrollLeft },
immediate,
})
if (!fromMutation || props.autoScroll !== false) {
scrollApi.start({
scrollLeft: nextScrollLeft,
from: { scrollLeft: containerScrollLeft },
immediate,
})
}
}

useIsomorphicLayoutEffect(() => {
Expand All @@ -214,7 +223,7 @@ export const Tabs: FC<TabsProps> = p => {

useMutationEffect(
() => {
animate(!x.isAnimating)
animate(!x.isAnimating, true)
},
tabListContainerRef,
{
Expand Down

0 comments on commit dcfec98

Please sign in to comment.