Skip to content

Commit dcc5a7a

Browse files
authored
fix(tabs): call debounce function to update scrollLeft (#19196)
* fix(tabs): call debounce function to update scrollLeft * test(tabs): add playwright test for horizontal manual scroll
1 parent ec31b96 commit dcc5a7a

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

e2e/components/Tabs/Tabs-test.avt.e2e.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,35 @@ test.describe('@avt Tabs', () => {
235235
await page.keyboard.press('Delete');
236236
await expect(page.getByRole('tab', { name: 'Tab label 4' })).toBeHidden();
237237
});
238+
239+
test('@avt-keyboard-nav - horizontal manual scroll', async ({ page }) => {
240+
await visitStory(page, {
241+
component: 'Tabs',
242+
id: 'components-tabs--default',
243+
globals: {
244+
theme: 'white',
245+
},
246+
});
247+
248+
// Set viewport size so scroll buttons appear
249+
await page.setViewportSize({
250+
width: 200,
251+
height: 480,
252+
});
253+
await page.waitForSelector('.cds--tab--overflow-nav-button--next', {
254+
state: 'visible',
255+
});
256+
257+
// Right scroll button should disappear after manually scrolling horizontally to end of tabs
258+
const tabList = page.locator('.cds--tab--list');
259+
const lastElement = page.getByText('Settings').nth(1);
260+
const nextButton = page.getByLabel('Scroll right');
261+
await tabList.hover();
262+
await lastElement.scrollIntoViewIfNeeded();
263+
await page.waitForSelector('.cds--tab--overflow-nav-button--next', {
264+
state: 'hidden',
265+
timeout: 1000,
266+
});
267+
await expect(nextButton).toBeHidden();
268+
});
238269
});

packages/react/src/components/Tabs/Tabs.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,9 +529,12 @@ function TabList({
529529

530530
const tabs = useRef<TabElement[]>([]);
531531
const debouncedOnScroll = useCallback(() => {
532-
return debounce((event) => {
533-
setScrollLeft(event.target.scrollLeft);
532+
const updateScroll = debounce(() => {
533+
if (ref.current) {
534+
setScrollLeft(ref.current.scrollLeft);
535+
}
534536
}, scrollDebounceWait);
537+
updateScroll();
535538
}, [scrollDebounceWait]);
536539

537540
function onKeyDown(event: KeyboardEvent) {

0 commit comments

Comments
 (0)