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(react): fix issue where ref property was getting clobberd on <Tab> #728

Merged
merged 3 commits into from
Aug 10, 2022

Conversation

scurker
Copy link
Member

@scurker scurker commented Aug 9, 2022

This fixes an issue where <Tab ref={tabRef}> was always null because it was getting clobbered within cloneElement.

@scurker scurker requested a review from a team as a code owner August 9, 2022 20:26
@github-actions
Copy link
Contributor

github-actions bot commented Aug 9, 2022

Preview branch generated at https://tab-ref.d1gko6en628vir.amplifyapp.com

@@ -122,7 +122,6 @@ const Tabs = ({
tabIndex: index === activeIndex ? 0 : -1,
['aria-controls']: target.current?.id,
['aria-selected']: selected,
ref: index === activeIndex ? focusedTabRef : null,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was clobbering the ref, in reality we don't really need it when we can utilize [aria-selected="true"]

Copy link
Collaborator

@thuey thuey Aug 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe what you have implemented here should work. However, I have found relying on query selectors in React components to be somewhat finicky. For example, sometimes the DOM isn't in the state you expect it to be in. Or, the structure changes and your query selectors are now incorrect.

fwiw, it should be possible to get this working closer to the way it was originally implemented. See: facebook/react#8873 (comment) and facebook/react#8873 (comment).

Suggested change
ref: index === activeIndex ? focusedTabRef : null,
ref: index === activeIndex ? (node) => {
focusedTabRef.current = node;
if (typeof child.ref === 'function') {
child.ref(node)
} else if (child.ref !== null) {
child.ref.current = node
}
} : child.ref

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query selector shouldn't need to change unless something were to change with the WCAG spec respective of how tabs are implemented. I feel that it's much safer to rely on query here rather than trying to patch into whatever React uses for refs.

@scurker scurker marked this pull request as draft August 9, 2022 20:36
@scurker scurker marked this pull request as ready for review August 9, 2022 20:42
thuey
thuey previously requested changes Aug 10, 2022
Copy link
Collaborator

@thuey thuey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused variable should be removed. The other comment is just a suggestion

packages/react/src/components/Tabs/Tabs.tsx Outdated Show resolved Hide resolved
@@ -122,7 +122,6 @@ const Tabs = ({
tabIndex: index === activeIndex ? 0 : -1,
['aria-controls']: target.current?.id,
['aria-selected']: selected,
ref: index === activeIndex ? focusedTabRef : null,
Copy link
Collaborator

@thuey thuey Aug 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe what you have implemented here should work. However, I have found relying on query selectors in React components to be somewhat finicky. For example, sometimes the DOM isn't in the state you expect it to be in. Or, the structure changes and your query selectors are now incorrect.

fwiw, it should be possible to get this working closer to the way it was originally implemented. See: facebook/react#8873 (comment) and facebook/react#8873 (comment).

Suggested change
ref: index === activeIndex ? focusedTabRef : null,
ref: index === activeIndex ? (node) => {
focusedTabRef.current = node;
if (typeof child.ref === 'function') {
child.ref(node)
} else if (child.ref !== null) {
child.ref.current = node
}
} : child.ref

@scurker scurker enabled auto-merge (squash) August 10, 2022 14:52
@scurker scurker merged commit d45d734 into develop Aug 10, 2022
@scurker scurker deleted the tab-ref branch August 10, 2022 15:03
@github-actions
Copy link
Contributor

Preview branch generated at https://tab-ref.d1gko6en628vir.amplifyapp.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants