@randstad-design/orbit-multitheme
Version:
multitheme Front-end code based on Randstad Human Forward components
45 lines (36 loc) • 950 B
JavaScript
/*
* switch.js
*
* Desc: Switch widget that implements ARIA Authoring Practices
*/
;
const attributeBase = 'data-rs-switch';
export class Switch {
constructor(element) {
this.switchNode = element;
this.switchNode.addEventListener('click', () => this.toggleStatus());
this.switchNode.addEventListener('keydown', (event) =>
this.handleKeydown(event)
);
}
handleKeydown(event) {
// Only do something when space or return is pressed
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
this.toggleStatus();
}
}
// Switch state of a switch
toggleStatus() {
const currentState =
this.switchNode.getAttribute('aria-checked') === 'true';
const newState = String(!currentState);
this.switchNode.setAttribute('aria-checked', newState);
}
/**
* Get selector
*/
static getSelector() {
return `[${attributeBase}]`;
}
}