@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
43 lines (41 loc) • 1.62 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
*/
function noopClick() {
/** noop **/
}
/**
* This helper updates the host element to prevent keyboard interaction on its subtree and sets the appropriate aria attribute for accessibility.
*
* This should be used in the `componentDidRender` lifecycle hook.
*
* **Notes**
*
* * this util is not needed for simple components whose root element or elements are an interactive component (custom element or native control). For those cases, set the `disabled` props on the root components instead.
* * technically, users can override `tabindex` and restore keyboard navigation, but this will be considered user error
*/
function updateHostInteraction(component, hostIsTabbable = false) {
if (component.disabled) {
component.el.setAttribute("tabindex", "-1");
component.el.setAttribute("aria-disabled", "true");
if (component.el.contains(document.activeElement)) {
document.activeElement.blur();
}
component.el.click = noopClick;
return;
}
component.el.click = HTMLElement.prototype.click;
if (typeof hostIsTabbable === "function") {
component.el.setAttribute("tabindex", hostIsTabbable.call(component) ? "0" : "-1");
}
else if (hostIsTabbable === true) {
component.el.setAttribute("tabindex", "0");
}
else if (hostIsTabbable === false) {
component.el.removeAttribute("tabindex");
}
else ;
component.el.removeAttribute("aria-disabled");
}
export { updateHostInteraction as u };