-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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(tab): tabpanel is tabbable when content has no tabbable content #7894
fix(tab): tabpanel is tabbable when content has no tabbable content #7894
Conversation
Deploy preview for carbon-elements ready! Built with commit 3c7538d |
Deploy preview for carbon-components-react ready! Built without sensitive environment variables with commit 3c7538d https://deploy-preview-7894--carbon-components-react.netlify.app |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would we want to do this automatically for users, potentially? Like in an effect. Something like:
function TabContent(props) {
// ...
const ref = useRef(null);
const hasFocusableContent = useFocusableContent(ref);
// ...
return <div role="tabpanel" tabIndex={hasFocusableContent ? 0 : undefined}>...</div>
}
function useFocusableContent(ref) {
const [hasFocusableContent, setFocusableContent] = useState(false);
useEffect(() => {
const elements = getFocusableElements(ref.current);
if (elements.length > 0) {
setFocusableContent(true);
}
}, [ref]);
return hasFocusableContent;
}
I dig the magic there for sure. It would also work if content is passed in via My only concern would be performance of |
You know what... I forgot about |
this might be helpful for your querySelectorAll https://github.com/carbon-design-system/carbon/blob/master/packages/react/src/internal/keyboard/navigation.js#L64-L72 |
Thanks @emyarod, I hadn't seen that. I think it's I modified the PR body to reflect the new approach. Also added some tests and refactored the tests off enzyme. |
Co-authored-by: emyarod <emyarod@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Tab content is now tabbable if there's no interactive elements. Thanks for adding the comments to the code too! Super helpful! 🔥
@tay1orjones definitely get what you mean, it might be that this abstraction doesn't make see if the tabpanel also needs to be explicitly labeled so would totally understand moving forward with the prop approach if this wouldn't be a good fit to do automatically. |
@joshblack It works quite nicely. Explicitly labelling the tabpanel feels like a very unlikely use case, but we could allow it by placing the automatic tabindex above |
…78-tab-content-focus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tay1orjones this looks awesome!
I'm so so sorry for the review delay... 👀
Closes #7078
AddhasNoFocusableContent
prop to Tab to inform if the tabpanel container should be focusable. This preserves existing functionality and allows users to opt-in to the new focus behavior when their tab content is text only or has no focusable elements/content.Add
useTabbableContent
to TabContent to automatically determine if the contents contains a tabbable element.Changelog
New
useTabbableContent
hook to TabContentChanged
Testing / Reviewing