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

fix(tabs): wait for TabPanel to mount before setting hidden #584

Merged
merged 1 commit into from Jun 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/tabs/src/index.tsx
Expand Up @@ -608,6 +608,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 @@ -623,12 +624,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