-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Add <Focus> component to react-router-dom #6449
Conversation
@timdorr I don't think it's necessary. Files entries are deprecated. There is not reason to add the new one. |
Uh, I didn't hit submit on those review comments. refined-github goofed 😖 |
|
||
componentDidUpdate(prevProps) { | ||
// only re-focus when the location changes | ||
if (this.props.location !== prevProps.location) { |
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.
I can't find any great documentation for how focus should be handled when clicking a hash link. This could potentially take a function prop that lets the user do a more refined comparison.
<Focus
compare={(next, prev) => {
// only re-focus when the pathname changes
return next.pathname !== prev.pathname;
}}
>...</Focus>
The best behavior would probably be to always re-focus and attach the ref
to the component with the matching id
. That requires a much more complex setup (conditionally attaching the ref
to components for location's with a hash
with a fallback to the "main" content when there is no hash
), so I'm not sure how many people would actually go that far.
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.
This is the tricky part of this implementation IMO. I've never really considered object equality a good way to compare two location
objects, so I always use locationsAreEqual
. Having said that, this is a common thing that people do, so we should probably make the location
object immutable between renders if it hasn't changed. Also, the locationsAreEqual
function compares location.state
, which I don't believe should trigger a refocus.
WRT the compare
function, it feels like we should be able to pick the right thing to do based on what browsers currently do so we won't need to provide that hook. But I'm not 100% sure.
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.
I think that a reference check is okay here because it is the object emitted by history
, but I'd be fine using locationsAreEqual
instead.
I did a quick check of a static page and if you focus an element, then click a link, the focus is moved back to document.body
*. Maybe to start, <Focus>
should re-focus for every location change. Down the road if people need more fine grained control, it would be easy enough to add the functionality.
* I just realized that I navigate with the mouse, which might have different behavior from a keyboard user. The site I was using doesn't show hash links to keyboard users (...), so I will need to find another site to verify the behavior.
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.
Thanks for the PR! This looks great, just had a few questions.
A couple other thoughts:
- Let's also work up an example (doesn't need to be in this PR, could come later) that shows people how to do this in their app
- Does it also make sense to provide a way to auto-focus the selected
<Route>
in a<Switch>
? Something like a<Switch autoFocus>
prop?
{context => <FocusWithLocation location={context.location} {...props} />} | ||
</RouterContext.Consumer> | ||
); | ||
|
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.
Should we add some propTypes
to the <Focus>
component here, like we do elsewhere?
I'm kinda on the fence about propTypes
personally these days, since so much of the community is moving away from them and they just increase our bundle size. But I think at least for 4.x we should keep using them on public API.
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.
You can build your modules with a Babel plugin that strips prop types in production mode. That's what I do for RNWeb
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.
I meant to do this. This code was adapted from TypeScript, so I stripped out all of type references and then completely forgot to add prop types.
I would not miss prop types.
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.
Thanks for the tip, @necolas :) We used to use that plugin, but we use the __DEV__
flag now instead, just so it's really clear when looking at the code which stuff is included in dev and which stuff isn't.
When I mentioned bundle size, what I meant was that I'm not sure if our prop-types
import is able to be dropped in our production builds, because it isn't conditional. We aren't actually using prop types in our production code, so in theory it should be tree-shaked out of our production bundles, but we have a brand new bundling process in 4.4 (thx to @TrySound) and I still need to verify.
|
||
componentDidUpdate(prevProps) { | ||
// only re-focus when the location changes | ||
if (this.props.location !== prevProps.location) { |
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.
This is the tricky part of this implementation IMO. I've never really considered object equality a good way to compare two location
objects, so I always use locationsAreEqual
. Having said that, this is a common thing that people do, so we should probably make the location
object immutable between renders if it hasn't changed. Also, the locationsAreEqual
function compares location.state
, which I don't believe should trigger a refocus.
WRT the compare
function, it feels like we should be able to pick the right thing to do based on what browsers currently do so we won't need to provide that hook. But I'm not 100% sure.
This is interesting! 😃 🎉 I don't quite have the time at the moment to do a full review but I wrote a big 'ol document about our experience making a React app with RR v4 accessible: https://github.com/folio-org/stripes-components/blob/master/docs/patterns/AccessibleRouting.md We tried a couple of different methods, one of them looks similar to what the |
A review of this PR from you would be invaluable, @Robdel12. If you can spare the time, I'd really appreciate it :) |
Just read through that doc, @Robdel12. I like the In your example, the HOC gives you an extra degree of control through the With that prop, you could write your HOC like this: /**
* Higher order component that, when given the prop
* `shouldFocus={true}`, will focus the component's DOM node on mount
* and on update when the prop is toggled from `false` to `true`.
*/
export default function shouldFocus(Focusable) {
return class extends Component {
static propTypes = {
shouldFocus: PropTypes.bool
};
render() {
return (
<Focus when={this.props.shouldFocus}>
{ref => (
<Focusable {...this.props} ref={ref} />
)}
</Focus>
);
}
};
} and use it like this: const CustomInput = shouldFocus(
React.forwardRef((props, ref) => (
<Wrapper {...props}>
<input ref={ref} />
</Wrapper>
))
);
<CustomInput shouldFocus={true} /> Would that work for you? |
I should have some free time later tonight / tomorrow morning! 😃 |
@mjackson yes! So much yes :) |
This should also be fairly straightforward to implement as a hook eventually. function App() {
const ref = useFocus({ preventScroll: true });
return (
<main tabIndex={-1} ref={ref}>
{/* ... */}
</main>
);
} |
Commented on |
@stowball commented where? |
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.
Much clearer, but perhaps you also need to clarify that it's for non-interactive elements in case someone focuses an a
, button
or input
?
@stowball you mean just for the <Focus>
{ref => (
<input ref={ref} />
)}
</Focus> |
@pshrmn yeah just for the |
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.
I like it 😈 I'm curious to see how folks use this out in the wild. There might be a little iteration but this is solid.
|
||
`Focus` uses a render prop to provide a `ref`. The `ref` should be passed to the element that will be focused. | ||
|
||
In order for `Focus` to work, the component type for the focused element needs to either be natively focusable (like an `<input>` or a `<button>`) or be given a `tabIndex` of `-1`. If you do not do this, then the document's `<body>` will be focused instead. |
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.
If an element isn't natively focusable, could we perhaps do a cloneElement
and add the tabIndex="-1"
automatically behind the scenes?
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.
I don't think that cloneElement
would work with this API because we don't necessarily have access to the React element. We do have a ref
to a DOM element, so the attribute could be to the element with ref.setAttribute("tabindex", "-1")
.
My personal preference would be to require the user to set the tab index. It's a little more work for the dev than RR injecting the attribute, but the ref
warning will let them know if there is an issue with their code and it lacks any "magic" (being able to focus an element that shouldn't be focusable).
End of the day, (assuming setting the attribute works like I think it would) I can make the switch if you would prefer.
it may be relevant to this discussion that there is an rfc currently being actively discussed for some focus management features to be provided by react-dom: reactjs/rfcs#104 |
This PR should make it easier to make
react-router-dom
apps more accessible. This is pretty much a copy of the same component that I use for Curi and I think that this approach works well.Focus
uses a render-invoked prop to assign a ref to the component that should be focused when the location changesThe
ref
component is only re-focused when thelocation
changes, so non-location change re-renders will not trigger this.