UNPKG

@sandlada/mdc

Version:

@sandlada/mdc is an open source component library that follows the Material Design 3 design specifications.

7 lines (6 loc) 1.92 kB
import"../../lit-element-Ho-VPzO1.js"; /** * @license * Copyright 2025 Kai-Orion & Sandlada * SPDX-License-Identifier: MIT */class t{get controls(){const t=this.host.getAttribute("name");return t&&this.root&&this.host.isConnected?Array.from(this.root.querySelectorAll(`[name="${t}"]`)):[this.host]}constructor(t){this.focused=!1,this.root=null,this.handleFocusIn=()=>{this.focused=!0,this.updateTabIndices()},this.handleFocusOut=()=>{this.focused=!1,this.updateTabIndices()},this.handleKeyDown=t=>{const s="ArrowDown"===t.key,e="ArrowUp"===t.key,o="ArrowLeft"===t.key,n="ArrowRight"===t.key;if(!(o||n||s||e))return;const i=this.controls;if(!i.length)return;t.preventDefault();const h="rtl"===getComputedStyle(this.host).direction?o||s:n||s,c=i.indexOf(this.host);let r=h?c+1:c-1;for(;r!==c;){r>=i.length?r=0:r<0&&(r=i.length-1);const t=i[r];if(!t.hasAttribute("disabled")){for(const s of i)s!==t&&(s.tabIndex=-1,s.blur());t.tabIndex=0,t.focus();break}h?r++:r--}},this.host=t}hostConnected(){this.root=this.host.getRootNode(),this.host.addEventListener("keydown",this.handleKeyDown),this.host.addEventListener("focusin",this.handleFocusIn),this.host.addEventListener("focusout",this.handleFocusOut),this.host.active&&this.uncheckSiblings(),this.updateTabIndices()}hostDisconnected(){this.host.removeEventListener("keydown",this.handleKeyDown),this.host.removeEventListener("focusin",this.handleFocusIn),this.host.removeEventListener("focusout",this.handleFocusOut),this.updateTabIndices(),this.root=null}handleCheckedChange(){this.host.active&&(this.uncheckSiblings(),this.updateTabIndices())}uncheckSiblings(){for(const t of this.controls)t!==this.host&&(t.active=!1)}updateTabIndices(){const t=this.controls,s=t.find(t=>t.active);if(s||this.focused){const e=s||this.host;e.tabIndex=0;for(const s of t)s!==e&&(s.tabIndex=-1);return}for(const s of t)s.tabIndex=0}}export{t as NavigationTabSingleSelectionController};