Skip to content

Commit

Permalink
Rewrite effect
Browse files Browse the repository at this point in the history
  • Loading branch information
stephl3 committed May 23, 2024
1 parent cca8f2d commit 2d63657
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions packages/tabs/src/TabTitle/TabTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,21 @@ const TabTitle: ExtendableBox<BaseTabTitleProps, 'button'> = ({
const theme = darkMode ? Theme.Dark : Theme.Light;
const selected = index === selectedIndex;

// Checks to see if the current activeElement is a part of the same tab set
// as the current TabTitle. If so, and the current TabTitle is not disabled
// and is selected, we manually move focus to that TabTitle.
useEffect(() => {
// if tab is disabled or not selected, return early
// otherwise, focus may need to be manually moved
if (disabled || !selected || !titleRef.current) return;

// if focus is not on tab descendants, return early
// otherwise, focus needs to be manually moved
const activeEl = document.activeElement;
const tabList = tabDescendants.map(
descendant => descendant.element.parentElement,
);
const activeEl = document.activeElement;
const isFocusOnTabDescendants = activeEl instanceof HTMLElement && tabList.indexOf(activeEl) !== -1;
if (!isFocusOnTabDescendants) return;

if (
activeEl &&
activeEl instanceof HTMLElement &&
tabList.indexOf(activeEl) !== -1 &&
!disabled &&
selected &&
titleRef.current
) {
titleRef.current.focus();
}
titleRef.current.focus();
}, [disabled, selected, tabDescendants, titleRef]);

const relatedTabPanel = useMemo(() => {
Expand Down

0 comments on commit 2d63657

Please sign in to comment.