Comments (6)
@jamesplease Glad this library is useful for you!
How about adding a try-catch around the initialization? That seems to me as good as setting an option.
One question about your use case: If the dropdown is empty, where do you want focus to go?
from focus-trap.
How about adding a try-catch around the initialization? That seems to me as good as setting an option.
I'm using the React bindings, so it's not clear to me where I would add that try-catch. Would it be best if I forked that library and added the try-catch myself?
One question about your use case: If the dropdown is empty, where do you want focus to go?
Good question! Because there are no elements that the user can activate, I am thinking that it follows that nothing be focused, and for tabbing to do nothing.
To close the dropdown, users can either hit Escape or Enter on their keyboard, or click outside of it. Then, after it is closed, I am manually returning the focus to the previously-focused item after the FocusTrap'd element is closed.
Maybe that's a little odd, though. Perhaps I'll propose a change to the UX where the dropdown is disabled when there are no items, and the explanation for the disabled state is provided in a tooltip on hover. For now, that explanation for the empty list is provided when the dropdown opens up.
from focus-trap.
Because there are no elements that the user can activate, I am thinking that it follows that nothing be focused, and for tabbing to do nothing.
I'm not sure that nothing be focused
is a good user experience to aim for. That leaves the keyboard user stranded in outer space, right? So I guess I'm still not convinced that it's a good idea to create a focus trap without a focusable element — still think that's probably an error that should be recognized as such.
from focus-trap.
I'm not sure that nothing be focused is a good user experience to aim for.
I agree that it's questionable 🙂
That leaves the keyboard user stranded in outer space, right?
In my particular situation, it doesn't. From my earlier comment:
"To close the dropdown, users can either hit Escape or Enter on their keyboard"
still think that's probably an error that should be recognized as such.
Perhaps. My bar of throwing errors is pretty high, and even higher for React components, where the developer can sometimes effectively lose all control over being able to try/catch it altogether, as in the case of the Focus Trap react wrapper.
It is interesting, because in my particular situation the dropdown represents dynamic processes, and the options available to the user may come and go depending on what is happening elsewhere (the options available are kept in sync with the server). So it is possible that you could open a dropdown with 3 options, but then, if you sit there long enough, it drops to 0.
Perhaps the UX to go with here does two things:
- Lets the user know that there are no actions to take
- Provides a button to close the dropdown, and that is what takes the focus
from focus-trap.
Perhaps the UX to go with here
Yeah, I think that this error represents an accessibility problem to be addressed rather than avoided. Every time this request has come up so far that seems to have been the case.
from focus-trap.
Whats interesting to me is that native selects behave pretty similarly to the UX I first described in this issue.
To reproduce, create a native select without any options, and open it. Click Tab and observe that it is trapped, but it behaves as if nothing is selected (although it is likely the select itself that is focused). Note: I only tested this in Safari on macOS.
Anyway, it is pretty clear to me that unmounting a React app (or a section of a React app) due to focus is never a good idea, so if you’re not open to any PRs to make adjustments I’ll go ahead and create a fork that’s more safe to use in React apps.
Thanks for the good discussion @davidtheclark !
from focus-trap.
Related Issues (20)
- Adjust types to allow `initialFocus: () => undefined` HOT 4
- Bug with selecting jumping to first tabbable from last if web component is involved. HOT 5
- Nesting focus traps? HOT 2
- Demo: in "hidden treasures" there is no way to deactivate the trap HOT 4
- Cannot tab on to or past a web-component in a container HOT 6
- Setting initialFocus=true leads to a call stack exceeded exception HOT 2
- More Flexible Mocking in Unit Tests HOT 3
- The library is not working HOT 3
- Should focusTrap only activate connected nodes? HOT 3
- Broken in Safari HOT 4
- focusIn listener causes issues with outside elements HOT 15
- Stack overflow tryFocus/checkFocusIn HOT 10
- Can't focus other textfields while focusing on autocomplete HOT 2
- Text selection within trapped element not possible if any ancestor has a `tabindex` HOT 9
- `returnFocusOnDeactivate` not working on mobile. HOT 9
- Focus does not seem to be working on firefox HOT 6
- Update type for document option to allow for shadowRoot elements. HOT 7
- "default behavior" demo broken on Firefox HOT 4
- Clicking another shadowed element inside the focus trap will deactivate the focus trap HOT 15
- focus-trap returns an error on Safari 15.3 and lower HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from focus-trap.