I was reading this article by Chris where he talks about block links — you know, like wrapping an entire card element inside an anchor — being a bad idea. It’s bad accessibility because of how it affects screen readers. And it’s bad UX because it prevents simple user tasks, like selecting text.
But maybe there’s something else at play. Maybe it’s less an issue with the pattern than the implementation of it. That led me to believe that this is the time to write follow-up article to see if we can address some of the problems Chris pointed out.
Throughout this post, I’ll use the term “card” to describe a component using the block link pattern. Here’s what we mean by that.
Let’s see how we want our Card Components to work:
- The whole thing should be linked and clickable.
- It should be able to contain more than one link.
- Content should be semantic so assistive tech can understand it.
- The text should be selectable, like regular links.
- Things like right-click and keyboard shortcuts should work with it
- Its elements should be focusable when tabbing.
That’s a long list! And since we don’t have any standard card widget provided by the browser, we don’t have any standard guidelines to build it.
Like most things on the web, there’s more than one way to make a card component. However, I haven’t found something that checks all the requirements we just covered. In this article, we will try to hit all of them. That’s what we’re going to do now!
Method 1: Wrap everything an <a>
This is the most common and the easiest way to make a linked card. Take the HTML for the card and wrap the entire thing in an anchor tag.
<a href="/">
<!-- Card markup -->
</a>
Here’s what that gives us:
- It’s clickable.
- It works with right-click and keyboard shortcuts.
Well, not great. We still can’t:
- Put another link inside the card because the entire thing is a single link
- Use it with a screen reader — the content is not semantic, so assistive technology will announce everything inside the card, starting from the time stamp
- Select text
That’s enough 👎 that we probably shouldn’t use it. Let’s move onto the next technique.
Method 2: Just link what needs linking
This is a nice compromise that sacrifices a little UX for improved accessibility.
With this pattern we achieve most of our goals:
- We can put as many links as we want.
- Content is semantic.
- We can select the text from Card.
- Right Click and keyboard shortcuts work.
- The focus is in proper order when tabbing.
But it is missing the main feature we want in a card: the whole thing should be clickable! Looks like we need to try some other way.
Method 3: The good ol’ ::before pseudo element
In this one, we add a ::before
or ::after
element, place it above the card with absolute positioning and stretch it over the entire width and height of the card so it’s clickable.
But now:
- We still can’t add more than one link because anything else that’s linked is under the pseudo element layer. We can try to put all the text above the pseudo element, but card link itself won’t work when clicking on top of the text.
- We still can’t select the text. Again, we could swap layers, but then we’re back to the clickable link issue all over again.
Let’s try to actually check all the boxes here in our final technique.
Method 4: Sprinkle JavaScript on the second method
Let’s build off the second method. Recall that’s what where we link up everything we want to be a link:
<article class="card">
<time datetime="2020-03-20">Mar 20, 2020</time>
<h2><a href="https://css-tricks.com/a-complete-guide-to-calc-in-css/" class="main-link">A Complete Guide to calc() in CSS</a></h2>
<p>
In this guide, let’s cover just about everything there is to know about this very useful function.
</p>
<a class="author-name" href="https://css-tricks.com/author/chriscoyier/" target="_blank">Chris Coyier</a>
<div class="tags">
<a class="tag" href="https://css-tricks.com/tag/calc/" >calc</a>
</div>
</article>
So how do we make the whole card clickable? We could use JavaScript as a progressive enhancement to do that. We’ll start by adding a click
event listener to the card and trigger the click on the main link when it is triggered.
const card = document.querySelector(".card")
const mainLink = document.querySelector('.main-link')
card.addEventListener("click", handleClick)
function handleClick(event){
mainLink.click();
}
Temporarily, this introduces the problem that we can’t select the text, which we’ve been trying to fix this whole time. Here’s the trick: we’ll use the relatively less-known web API window.getSelection
. From MDN:
The
Window.getSelection()
method returns aSelection
object representing the range of text selected by the user or the current position of the caret.
Although, this method returns an Object, we can convert it to a string with toString()
.
const isTextSelected = window.getSelection().toString()
With one line and no complicated kung-fu tricks with event listeners, we know if the user has selected text. Let’s use that in our handleClick
function.
const card = document.querySelector(".card")
const mainLink = document.querySelector('.main-link')
card.addEventListener("click", handleClick)
function handleClick(event){
const isTextSelected = window.getSelection().toString();
if (!isTextSelected) {
mainLink.click();
}
}
This way, the main link can be clicked when no text selected, and all it took was a few lines of JavaScript. This satisfies our requirements:
- The whole thing is linked and clickable.
- It is able to contain more than one link.
- This content is semantic so assistive tech can understand it.
- The text should be selectable, like regular links.
- Things like right-click and keyboard shortcuts should work with it
- Its elements should be focusable when tabbing.
Here’s the final demo with all the JavaScript code we have added:
I think we’ve done it! Now you know how to make a perfect clickable card component.
What about other patterns? For example, what if the card contains the excerpt of a blog post followed by a “Read More’ link? Where should that go? Does that become the “main” link? What about image?
For those questions and more, here’s some further reading on the topic:
- Cards by Heydon Pickering
- Block Links, Cards, Clickable Regions, Rows, Etc. by Adrian Roselli
- Block Links Are a Pain (and Maybe Just a Bad Idea) by Chris Coyier
- Pitfalls of Card UIs by Dave Rupert
The post Block Links: The Search for a Perfect Solution appeared first on CSS-Tricks.
from CSS-Tricks https://ift.tt/3egVMhZ
via IFTTT
No comments:
Post a Comment