UNPKG

@public-ui/components

Version:

Contains all web components that belong to KoliBri - The accessible HTML-Standard.

4 lines 4.96 kB
/*! * KoliBri - The accessible HTML-Standard */ import{proxyCustomElement,HTMLElement,h,Host}from"@stencil/core/internal/client";import{y as KolTreeItemTag,x as KolTreeTag}from"./component-names.js";import"./common.js";import{v as validateLabel}from"./label.js";const KolTreeWc$1=proxyCustomElement(class e extends HTMLElement{constructor(e){super(),!1!==e&&this.__registerHost(),this.state={_label:""}}validateLabel(e){validateLabel(this,e)}render(){return h(Host,{key:"6e1fc1f0d6757fec0658d28ff54fe73a58b60fa9",onSlotchange:this.handleSlotchange.bind(this)},h("nav",{key:"dfc733503c12e36fc8e6fc958273555f4e698237",class:"kol-tree","aria-label":this.state._label},h("ul",{key:"9c68ce61a9bfbe5ade976515eaa43e2d9fbf380d",class:"kol-tree__treeview-navigation",role:"tree","aria-label":this.state._label},h("slot",{key:"db660a5bc5cb5544f2c35e9e14f2a61d6e06ac69"}))))}static isTreeItem(e){return(null==e?void 0:e.tagName)===KolTreeItemTag.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 t,l,a;return null===(a=null===(l=null===(t=this.host.querySelector("slot"))||void 0===t?void 0:t.assignedNodes)||void 0===l?void 0:l.call(t))||void 0===a?void 0:a.filter(e.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 l=t.querySelectorAll(KolTreeItemTag);return[...e,t,...l]},[])}async getOpenTreeItemElements(){if(!this.treeItemElements)return;const t=async l=>!e.isTreeItem(l.parentElement)||await l.parentElement.isOpen()&&await t(l.parentElement);return(await Promise.all(this.treeItemElements.map(async e=>({value:e,include:await t(e)})))).filter(e=>e.include).map(e=>e.value)}async handleKeyDown(e){var t,l,a,i,n,o,s,r,c,d;const u=await this.getOpenTreeItemElements(),v=null===(t=document.activeElement)||void 0===t?void 0:t.closest(KolTreeItemTag),m=e.metaKey||e.altKey||e.ctrlKey||e.shiftKey;if(!u||!v)return;const h=null==u?void 0:u.findIndex(e=>e===v);switch(e.key){case"ArrowDown":await(null===(l=u[h+1])||void 0===l?void 0:l.focus()),e.preventDefault();break;case"ArrowUp":await(null===(a=u[h-1])||void 0===a?void 0:a.focus()),e.preventDefault();break;case"Right":case"ArrowRight":e.preventDefault(),await v.isOpen()?await(null===(i=u[h+1])||void 0===i?void 0:i.focus()):await v.expand();break;case"Left":case"ArrowLeft":if(e.preventDefault(),await v.isOpen())await v.collapse();else{const e=u.findIndex(e=>e===v.parentElement);-1!==e&&await(null===(n=u[e])||void 0===n?void 0:n.focus())}break;case"Home":await(null===(o=u[0])||void 0===o?void 0:o.focus()),e.preventDefault();break;case"End":await(null===(s=u[u.length-1])||void 0===s?void 0:s.focus()),e.preventDefault();break;case null===(r=e.key.match(/^[a-zA-Z0-9]$/))||void 0===r?void 0:r.input:if(!m){const t=e.key.toLowerCase(),l=u.indexOf(v)+1,a=u.concat(u),i=a.slice(l,l+u.length).findIndex(e=>{var l;return null===(l=e.getAttribute("_label"))||void 0===l?void 0:l.trim().toLowerCase().startsWith(t)});-1!==i&&(await(null===(c=a[l+i])||void 0===c?void 0:c.focus()),e.preventDefault())}break;case"*":{const e=null===(d=v.parentElement)||void 0===d?void 0:d.querySelectorAll(KolTreeItemTag);null==e||e.forEach(e=>{e.expand()});break}}}async handleFocusOut(e){e.relatedTarget&&!e.relatedTarget.closest(KolTreeTag)&&await this.ensureActiveItemVisibility()}async ensureActiveItemVisibility(){const t=l=>{e.isTreeItem(l.parentElement)&&(l.parentElement.expand(),t(l.parentElement))},l=(()=>{var t,l,a,i;const n=null!==(i=null===(a=null===(l=null===(t=this.host.querySelector("slot"))||void 0===t?void 0:t.assignedNodes)||void 0===l?void 0:l.call(t))||void 0===a?void 0:a.filter(e.isTreeItem))&&void 0!==i?i:[];for(const e of n){if(e._active)return e;const t=e.querySelector(`${KolTreeItemTag}[_active="true"]`);if(t&&t._active)return t}})();l&&t(l)}get host(){return this}static get watchers(){return{_label:["validateLabel"]}}},[260,"kol-tree-wc",{_label:[1],state:[32]},[[0,"keydown","handleKeyDown"],[0,"focusout","handleFocusOut"]],{_label:["validateLabel"]}]);function defineCustomElement$1(){if("undefined"==typeof customElements)return;["kol-tree-wc"].forEach(e=>{if("kol-tree-wc"===e)customElements.get(e)||customElements.define(e,KolTreeWc$1)})}const KolTreeWc=KolTreeWc$1,defineCustomElement=defineCustomElement$1;export{KolTreeWc,defineCustomElement};