Skip to content

Commit

Permalink
Merge pull request #584 from raunofreiberg/fix/tabs-refs
Browse files Browse the repository at this point in the history
fix(tabs): wait for TabPanel to mount before setting `hidden`
  • Loading branch information
Chance Strickland committed Jun 11, 2020
2 parents 38e30bc + 61bcce1 commit ce691e1
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/tabs/src/index.tsx
Expand Up @@ -620,6 +620,7 @@ export const TabPanel = forwardRefWithAs<TabPanelProps, "div">(
TabsContext
);
let ownRef = useRef<HTMLElement | null>(null);
let isMountedRef = useRef<boolean>(false);

let index = useDescendant(
{ element: ownRef.current! },
Expand All @@ -635,12 +636,20 @@ export const TabPanel = forwardRefWithAs<TabPanelProps, "div">(
isSelected ? selectedPanelRef : null
);

React.useEffect(() => {
isMountedRef.current = true;
}, []);

return (
<Comp
// Each element with role `tabpanel` has the property `aria-labelledby`
// referring to its associated tab element.
aria-labelledby={makeId(tabsId, "tab", index)}
hidden={!isSelected}
// During the initial render `isSelected` would be `false`
// and hide the children, which prevents focusing via refs on mount.
// As a workaround, we wait for the component to mount, and then set the `hidden` attribute.
// I guess this is hackish, but it works.
hidden={isMountedRef.current ? !isSelected : false}
// Each element that contains the content panel for a tab has role
// `tabpanel`.
// https://www.w3.org/TR/wai-aria-practices-1.2/#tabpanel
Expand Down

0 comments on commit ce691e1

Please sign in to comment.