Skip to content
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

enhance(Tabs): Add experimental prop autoScroll #6497

Merged
merged 3 commits into from
Dec 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
},
})

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

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 @@
x = -(containerWidth - x - w)
}

api.start({
inkApi.start({
x,
width,
immediate,
Expand Down Expand Up @@ -193,11 +200,13 @@
)
}

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

Check warning on line 204 in src/components/tabs/tabs.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/tabs/tabs.tsx#L204

Added line #L204 was not covered by tests
scrollLeft: nextScrollLeft,
from: { scrollLeft: containerScrollLeft },
immediate,
})
}
}

useIsomorphicLayoutEffect(() => {
Expand All @@ -214,7 +223,7 @@

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