-
-
Notifications
You must be signed in to change notification settings - Fork 4.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 Key handling to modal example #3799
Conversation
This is a basic accessibility requirement for key access
closeButton.focus() | ||
}) | ||
|
||
</cript> |
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.
Looks like a typo here with a missing s at the start of "</script>"
</cript> | |
</script> |
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.
yikes! you are correct. Thanks
Thank you! I wanted to better understand focus trapping, and disappeared down a very deep rabbit hole, and I am sorry to report that The Platform is still flaming hot garbage. Specifically, I figured it'd be better to continue to allow tabbing, but restrict it to the contents of the modal. You'd think, given that this is a W3C recommended best practice, that there'd be a sensible declarative way to achieve this — Eventually settled on this: const handle_keydown = e => {
if (e.key === 'Escape') {
close();
return;
}
if (e.key === 'Tab') {
// trap focus
const nodes = modal.querySelectorAll('*');
const tabbable = Array.from(nodes).filter(n => n.tabIndex >= 0);
let index = tabbable.indexOf(document.activeElement);
if (index === -1 && e.shiftKey) index = 0;
index += tabbable.length + (e.shiftKey ? -1 : 1);
index %= tabbable.length;
tabbable[index].focus();
e.preventDefault();
}
}; It's not perfect — it doesn't respect non-zero tabindexes (which you should avoid anyway, apparently), and it focuses Also added |
hey @Rich-Harris that is fantastic and a good implementation too, even restoring focus; plus ARIA! Thanks for being so conscientious! It must have been conciderable work. I must admit I kept it really simple as not sure how much you would accept here. So I decided to just do enough to highlight there’s a need to handle key as well as pointer/touch. a) examples generally show platform features and are not necessarily feature complete best practices (the details of which can obscure what is being demonstrated) I’m so pleased you took the basic accessibility requirement as something Svelte should promote with decent implementation! \0/
that can actually happen anywhere when tabbing gets stuck, not just in modals
Yeah! The need to roll your own modal (and combo box) are a massive source of bad implementations and accessibility bugs. Basically, devs have to reproduce missing browser behaviour and need to test all the hard discovered edge cases. We REALLY need the dialog element to be properly supported by browsers ASAP!
Exactly right! I fudged it in the PR :-/
I think it’s only required in a modal DIALOG, so again we need the element to be supported
Can’t argue with that. We should not need to do it at all, but rather just let the browser take the strain!
That’s really nice modern ‘functional’ style. The kind that I try to write these days . I hadn’t realised the tab index property is computed, that saves a tonne of pain.
weird. And then, Safari disables tab by default !!!??
Again thanks for including this in the Modal example that other svelete devs will likely copy! |
@LJWatson @hidde @yatil @stevefaulkner Would any of you mind reviewing @Rich-Harris's code above for the official Sveltejs modal example? It's a great opportunity to have an example that includes the very best practice. I submitted a woeful PR to improve the key access / a11y and Rich rolled with it and came up with a solution that looks really good to me. It's based on the WAI ARIA example pattern for modal dialogs Thanks (assuming you got notifications) |
Woa, great work @Rich-Harris, I haven't seen an implementation this concise. Will look closer when time allows later in the week. Re: a One thing I noticed and perhaps you have too… I am not sure if
is going to return the right set of elements. For example, elements with |
:) Thanks @hidde!
Oh, great spot! So should we also check the computed styles for non visible? |
wow,, that's a lot of code!!! bring on the dialog element and we can have zero code.!!! |
Yes, looking forward to some of the standards work around this to come to browsers. See also the |
@Rich-Harris I noticed the PR has not deployed yet - just in case you have CD in place and it glitched |
@Rich-Harris Just copying you code and I noticed you switched to the Why oh why can we just use the declarative version :( |
This is a basic accessibility requirement for key access
I'm fast 'n' playing loose with the check list as its an update to one of the examples
Before submitting the PR, please make sure you do the following
npm run lint
!)Tests
npm test
oryarn test
)