-
Notifications
You must be signed in to change notification settings - Fork 66
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
Improve accessibility #49
Comments
This is a very valid point. I'd wonder how far Happy to help out here with some additional help |
I have tried to come up with an easy CSS-only way to do it, but I'm afraid I don't have the answer. This CSS Tricks article lists a number of solutions, but no magic bullet. That said I do know that tabIndex is not the answer. In fact tabIndex is bad for a11y, because the browser will prioritize any element with a tabIndex greater than 0. Quoting MDN:
|
Hi! I actually handled the tab/shift+tab behavior like this: let imgRefs = [] 'images' is the name of my array of elements to be consumed <Masonry ...>
{
images.map((img, i) =>
<a href=...
key={ i }
ref={(r) => {imgRefs[`img-${i}`] = r }}
onKeyDown={ e => handleImgKeydown(e, i) }
>
<img ... />
</a>
)
}
</Masonry> This will create refs for every element inside your masonry grid and store it on that imgRefs array. Next, you can take a look at my 'handleImgKeydown' function, design only to do something when Tab is pressed const handleImgKeydown = (e, i) => {
if (e.key === 'Tab'){
// Determine the sign of the operation.
// If shift key is pressed, it goes to the previous element
let interval = 1
// Check if shift is pressed too
if(e.shiftKey){
interval = -1
}
const newIndex = i + interval
// Handle edge cases. 'Images' is the array of elements with the attributes to fill the masonry. Change it to your own.
if (newIndex >= 0 && newIndex <= images.length){
// This prevents the tab to jump to the default next element
e.preventDefault()
imgRefs[`img-${newIndex}`].focus()
}
}
} I hope that was helpful! |
@marcvangend This tells the screenreader to treat the container as So Maybe this should be added to the Readme? |
Thanks for the As for the JS to fix keyboard navigation... I appreciate the effort but personally I'm not a big fan of interfering with key presses. (What if someone changed their keyboard shortcuts because their left hand is injured?) Also this doesn't seem to solve screen reader issues. PS. You might want to know that native CSS masonry is on the horizon. |
This is great @sheparddw, thanks for the find! I'll be keeping an eye on the native CSS solution, which ideally we can roll into the plugin for supporting browsers - thanks for the link @marcvangend |
Thanks for sharing your code. I tried it - installing was easy and it works as advertised.
I do want to mention though that the resulting HTML structure has a significant impact on accessibility. Wrapping the grid items into column
<div>
s means that the tab order of the page is changed. Given a 4x3 grid, people who rely on keyboard navigation now have to step through the items in a 1-5-9-2-6-10-3-7-11-4-8-12 order. This is extremely confusing, especially for blind users.Wrapping the columns in
<div>
s also means that it is not possible to apply the masonry component to a HTML list (<ul>
or<ol>
). These tags are often used to build semantic and accessible markup.IMHO as web developers we have an obligation to build websites for everybody. If there are ways to improve the accessibility of this component, that would be awesome. If there aren't, please mention the impact on accessibility in the readme.
The text was updated successfully, but these errors were encountered: