@public-ui/components
Version:
Contains all web components that belong to KoliBri - The accessible HTML-Standard.
4 lines • 4.36 kB
JavaScript
/*!
* KoliBri - The accessible HTML-Standard
*/
import{r as e,h as i,H as t,g as a}from"./index-DyUiyxj3.js";import{z as o,y as s}from"./component-names-B_kYyA8u.js";import"./common-BXXICkFX.js";import{v as n}from"./label-Dkj9sxQK.js";const l=class{constructor(t){e(this,t),this.state={_label:""}}validateLabel(e){n(this,e)}render(){return i(t,{key:"6e1fc1f0d6757fec0658d28ff54fe73a58b60fa9",onSlotchange:this.handleSlotchange.bind(this)},i("nav",{key:"dfc733503c12e36fc8e6fc958273555f4e698237",class:"kol-tree","aria-label":this.state._label},i("ul",{key:"9c68ce61a9bfbe5ade976515eaa43e2d9fbf380d",class:"kol-tree__treeview-navigation",role:"tree","aria-label":this.state._label},i("slot",{key:"db660a5bc5cb5544f2c35e9e14f2a61d6e06ac69"}))))}static isTreeItem(e){return(null==e?void 0:e.tagName)===o.toUpperCase()}componentWillLoad(){this.validateLabel(this._label),this.handleTreeChange(),this.observeChildListMutations()}disconnectedCallback(){var e;null===(e=this.observer)||void 0===e||e.disconnect()}observeChildListMutations(){this.observer=new MutationObserver(this.handleTreeChange.bind(this)),this.observeTopLevelItems()}handleSlotchange(){this.observeTopLevelItems(),this.handleTreeChange()}observeTopLevelItems(){var e;null===(e=this.getTopLevelTreeItems())||void 0===e||e.forEach(e=>{var t;null===(t=this.observer)||void 0===t||t.observe(e,{childList:!0,subtree:!0})})}getTopLevelTreeItems(){var e,t,a;return null===(a=null===(t=null===(e=this.host.querySelector("slot"))||void 0===e?void 0:e.assignedNodes)||void 0===t?void 0:t.call(e))||void 0===a?void 0:a.filter(l.isTreeItem)}handleTreeChange(){this.treeItemElements=this.getTreeItemElements(),this.ensureActiveItemVisibility()}getTreeItemElements(){var e;return null===(e=this.getTopLevelTreeItems())||void 0===e?void 0:e.reduce((e,t)=>{const a=t.querySelectorAll(o);return[...e,t,...a]},[])}async getOpenTreeItemElements(){if(!this.treeItemElements)return;const e=async t=>!l.isTreeItem(t.parentElement)||await t.parentElement.isOpen()&&await e(t.parentElement);return(await Promise.all(this.treeItemElements.map(async t=>({value:t,include:await e(t)})))).filter(e=>e.include).map(e=>e.value)}async handleKeyDown(e){var t,a,i,l,s,n,r,c,d,v;const u=await this.getOpenTreeItemElements(),h=null===(t=document.activeElement)||void 0===t?void 0:t.closest(o),m=e.metaKey||e.altKey||e.ctrlKey||e.shiftKey;if(!u||!h)return;const f=null==u?void 0:u.findIndex(e=>e===h);switch(e.key){case"ArrowDown":await(null===(a=u[f+1])||void 0===a?void 0:a.focus()),e.preventDefault();break;case"ArrowUp":await(null===(i=u[f-1])||void 0===i?void 0:i.focus()),e.preventDefault();break;case"Right":case"ArrowRight":e.preventDefault(),await h.isOpen()?await(null===(l=u[f+1])||void 0===l?void 0:l.focus()):await h.expand();break;case"Left":case"ArrowLeft":if(e.preventDefault(),await h.isOpen())await h.collapse();else{const e=u.findIndex(e=>e===h.parentElement);-1!==e&&await(null===(s=u[e])||void 0===s?void 0:s.focus())}break;case"Home":await(null===(n=u[0])||void 0===n?void 0:n.focus()),e.preventDefault();break;case"End":await(null===(r=u[u.length-1])||void 0===r?void 0:r.focus()),e.preventDefault();break;case null===(c=e.key.match(/^[a-zA-Z0-9]$/))||void 0===c?void 0:c.input:if(!m){const t=e.key.toLowerCase(),a=u.indexOf(h)+1,i=u.concat(u),l=i.slice(a,a+u.length).findIndex(e=>{var a;return null===(a=e.getAttribute("_label"))||void 0===a?void 0:a.trim().toLowerCase().startsWith(t)});-1!==l&&(await(null===(d=i[a+l])||void 0===d?void 0:d.focus()),e.preventDefault())}break;case"*":{const e=null===(v=h.parentElement)||void 0===v?void 0:v.querySelectorAll(o);null==e||e.forEach(e=>{e.expand()});break}}}async handleFocusOut(e){e.relatedTarget&&!e.relatedTarget.closest(s)&&await this.ensureActiveItemVisibility()}async ensureActiveItemVisibility(){const e=t=>{l.isTreeItem(t.parentElement)&&(t.parentElement.expand(),e(t.parentElement))},t=(()=>{var e,t,a,i;const s=null!==(i=null===(a=null===(t=null===(e=this.host.querySelector("slot"))||void 0===e?void 0:e.assignedNodes)||void 0===t?void 0:t.call(e))||void 0===a?void 0:a.filter(l.isTreeItem))&&void 0!==i?i:[];for(const e of s){if(e._active)return e;const t=e.querySelector(`${o}[_active="true"]`);if(t&&t._active)return t}})();t&&e(t)}get host(){return a(this)}static get watchers(){return{_label:["validateLabel"]}}};export{l as kol_tree_wc};