Sometimes you need to add a click listener to non-interactive elements (like <span>). Unpoly ships with some tools to prevent accessibility issues
when emulating interactivity on non-interactive elements.
Add an [up-follow] attribute on any non-interactive element to make it behave like a hyperlink.
The element will support keyboard navigation and many other behaviors for accessibility.
The link's destination URL can be set as an [up-href] attribute.
For example, the following <span> element will navigate to /details when clicked:
<span up-follow up-href="/details">Read more</span>
The [up-follow] element can be used with any hyperlink-related functionality, such as [up-instant], [up-preload] or up.follow().
In general you should prefer using regular hyperlinks:
<a> is an inline element, it may contain block elements. Hence you can use <a> to make a large area clickable.There are also some use cases for faux links:
<a>, e.g. a <tr>.You can use [up-emit] on any element to have it emit an event when clicked:
<span up-emit="user:select" up-emit-props="{ user_id: 5 }">
Alice
</span>
This can be a good solution to let widely distributed elements communicate with each other.
The [up-emit] element will support keyboard navigation and many other behaviors for accessibility.
Add an [up-clickable] attribute on any non-interactive element to make it behave like a button:
<span id="faux-button" up-clickable>Click me</span>
This will enable keyboard navigation and many other behaviors for accessibility.
It's up to you make the element appear interactive visually, e.g. by assigning a .button class from your design system.
To react to the element being activated, handle the up:click event:
let button = document.querySelector('#faux-button')
button.addEventListener('up:click', function(event) {
console.log('Click on faux button!')
})
To make elements clickable without an explicit [up-clickable] attribute, configure up.link.config.clickableSelectors.
Naively adding a click handler on a non-interactive element will cause accessibility issues.
To prevent this, Unpoly adds the following behaviors to elements with [up-follow][up-href], [up-emit] or [up-clickable]:
[role=link]
or [role=button]
attribute so screen readers announce it as an interactive element. To override Unpoly's choice of role, set a [role] attribute manually.pointer cursor when hovered over.[tabindex=0] attribute so it can be focused with the keyboard. An existing [tabindex] attribute will be preserved.up:click event when activated.[up-instant] attribute to make the element activate on mousedown instead of click ("Act on press").Note
No additional behaviors will be added to
<a>or<button>elements, as these are accessible by default.