forked from unimatrix/web
9 lines
1.8 KiB
JavaScript
9 lines
1.8 KiB
JavaScript
let sequence=0;
|
|
export function organisationTabs(groups,{label='Workspace sections',initial}={}){
|
|
const root=document.createElement('div');root.className='organisation-tabbed';
|
|
if(!document.querySelector('link[data-organisation-tabs]')){const css=document.createElement('link');css.rel='stylesheet';css.href=new URL('./codex-tabs.css',import.meta.url).href;css.dataset.organisationTabs='';document.head.append(css);}
|
|
const nav=document.createElement('div');nav.className='organisation-tabs';nav.setAttribute('role','tablist');nav.setAttribute('aria-label',label);root.append(nav);const entries=[];const prefix='organisation-tabs-'+(++sequence);
|
|
groups.forEach(({id,title,content},i)=>{const button=document.createElement('button'),panel=document.createElement('section');button.type='button';button.textContent=title;button.id=prefix+'-'+id;button.setAttribute('role','tab');button.setAttribute('aria-controls',button.id+'-panel');panel.id=button.id+'-panel';panel.setAttribute('role','tabpanel');panel.setAttribute('aria-labelledby',button.id);panel.tabIndex=0;panel.append(content);nav.append(button);root.append(panel);entries.push({id,button,panel});button.onclick=()=>select(id);button.onkeydown=e=>{let index;if(e.key==='ArrowRight')index=(i+1)%entries.length;if(e.key==='ArrowLeft')index=(i+entries.length-1)%entries.length;if(e.key==='Home')index=0;if(e.key==='End')index=entries.length-1;if(index!==undefined){e.preventDefault();select(entries[index].id);entries[index].button.focus();}};});
|
|
function select(id){for(const entry of entries){const active=entry.id===id;entry.button.setAttribute('aria-selected',String(active));entry.button.tabIndex=active?0:-1;entry.panel.hidden=!active;}root.dispatchEvent(new CustomEvent('organisation-tab-change',{detail:{id}}));}
|
|
select(entries.some(e=>e.id===initial)?initial:entries[0]?.id);return root;
|
|
}
|

Staff