From 867bc939ae35ae70e05cd130a3007a20aded9cdc Mon Sep 17 00:00:00 2001 From: Steve Kinney Date: Tue, 1 Oct 2024 18:21:25 -0500 Subject: [PATCH] Refactor the tab component --- examples/element-factory/src/tabs.svelte | 39 +++++++----------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/examples/element-factory/src/tabs.svelte b/examples/element-factory/src/tabs.svelte index 71829e4..65834e3 100644 --- a/examples/element-factory/src/tabs.svelte +++ b/examples/element-factory/src/tabs.svelte @@ -4,40 +4,24 @@ export let tabs = []; let tabbedContent = null; + let activeIndex = 0; - const selectTab = (event) => { - const tab = event.target; - const panel = tabbedContent.querySelector( - `#${tab.getAttribute('aria-controls')}`, - ); - - tabbedContent.querySelectorAll('[role="tab"]').forEach((t) => { - t.setAttribute('aria-selected', 'false'); - t.setAttribute('tabindex', '-1'); - }); - - tab.setAttribute('aria-selected', 'true'); - tab.setAttribute('tabindex', '0'); - - tabbedContent.querySelectorAll('[role="tabpanel"]').forEach((p) => { - p.hidden = true; - }); - - panel.hidden = false; - }; + const isSelected = (index) => index === activeIndex; + const getTabIndex = (index) => (isSelected(index) ? 0 : -1);
{#each tabs as tab, i} - {@const tabId = tab.id || i} + {@const id = tab.id || i} + {@const selected = i === activeIndex} @@ -45,8 +29,7 @@
{#each tabs as tab, i} - {@const tabId = tab.id || i} -