Events
When using web components from the Riverty Design System, you can listen for custom events to follow user interactions. This page explains how to use custom events and summarizes which events there are.
Why custom events?
Behavior and styling of web components is encapsulated using shadow DOM. This maximizes robustness and portability, and also affects event handling. As explained by Shoelace (another great component library):
You can listen for standard events such as
click
,mouseover
, etc. as you normally would. However, it’s important to note that many events emitted within a component’s shadow root will be retargeted to the host element. This may result in, for example, multipleclick
handlers executing even if the user clicks just once. Furthermore,event.target
will point to the host element, making things even more confusing.
So, in short, Riverty web components emit custom events. The even name always start with r
, such as rChange
.
Listening to events
Riverty web components emit standard Custom Events. As a result, there are numerous examples and tutorials online which all apply. Here are some examples to get you started.
Vanilla JavaScript
const element = document.querySelector('r-checkbox');
element.addEventListener('rChange', (event) => {
console.log('"rChange" event emitted, event.detail:', event.detail);
});
Angular
Within Angular environment HostListener decorator provides the way to handle custom events.
@HostListener('rChange', ['$event'])
changeHandler(event: CustomEvent) {
console.log('"rChange" event emitted, event.detail:', event.detail)
}
React
Within React environment useEffect might be used:
React.useEffect(() => {
window.addEventListener('rChange', handleChange);
return () => {
window.removeEventListener('rChange', handleChange);
};
}, []);
Vue
One of the ways to listen to custom events is to use useEventListener.
import { useEventListener } from '@vueuse/core'
useEventListener(document, 'rChange', event => {
console.log('"rChange" event emitted, event.detail:', event.detail)
})
Ionic team provides a way for event names to be converted from camel-case to lowercase for v-on:
compatibility.
import { defineCustomElements } from '@riverty/web-components/loader';
defineCustomElements({
ce: (eventname, opts) => new CustomEvent(eventname.toLowerCase(), opts)
});
Reference
Here is an overview of all custom events currently in place. The same information is always available by browsing the components docs.
Component | Event | Description | Details |
---|---|---|---|
<r-checkbox /> | rChange | Emitted when checked state changes via user interaction. |
|
rReset | Occurs when a form is reset (resets component’s checked state to initial). | ||
<r-input /> | rInput | Emitted immediately after components value change. |
|
rChange | Emitted if components value changes after element loses focus. | ||
rReset | Occurs when a form is reset (resets component’s value to initial). | ||
rClickIcon | Emitted when click on the icon within input occurs. | — | |
<r-pagination /> | rChange | Occurs when active state of pagination list elements changes. |
|
rNumberClick | Emitted when one of the pagination numbers clicked. | ||
rArrowClick | Emitted when one of the pagination arrows clicked. |
| |
<r-radio-button /> | rChange | Emitted when checked state changes via user interaction. |
|
rReset | Occurs when a form is reset (resets component’s checked state to initial). | ||
<r-select /> | rChange | Emitted when value of the select has changed. |
|
rReset | Occurs when a form is reset (resets component’s value to initial). | ||
rInputFilter | Occurs immediately after filtering input within component’s list changing value . |
| |
rChangeFilter | Emitted when filtering input within component’s list loses focus and value has changed. | ||
<r-tabs/> | rChange | Occurs when active tab changes |
|
<r-textarea /> | rInput | Emitted immediately after component’s value change. |
|
rChange | Emitted if components value changes after element loses focus. | ||
rReset | Occurs when a form is reset (resets component’s value to initial). |