Last active
November 19, 2020 22:16
-
-
Save dimfeld/5e8b91d3da6558ca472e2ad827acd672 to your computer and use it in GitHub Desktop.
An abbreviated version of my tabs components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Tabs> | |
<Tab id="settings" name="Settings">Content</Tab> | |
<Tab id="sharing" name="Sharing">Content2</Tab> | |
</Tabs> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
export let id: string; | |
export let name: string; | |
export let icon: IconDefinition = undefined; | |
let { activeTabId, manager, alwaysRender } = getContext<TabManager>( | |
'tab-manager' | |
); | |
$: manager.update({ id, name, icon }); | |
</script> | |
{#if $activeTabId === id} | |
<slot /> | |
{/if} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
export let activeTab = undefined; | |
let activeTabId = writable(activeTab); | |
$: activeTabId.set(activeTab); | |
let tabs = []; | |
setContext('tab-manager', { | |
activeTabId, | |
manager: { | |
update(newTabData) { | |
let tabIndex = tabs.findIndex((t) => t.id === newTabData.id); | |
if (tabIndex >= 0) { | |
tabs[tabIndex] = newTabData; | |
} else { | |
tabs.push(newTabData); | |
} | |
tabs = tabs; | |
if (!activeTab) { | |
activeTab = newTabData.id; | |
} | |
}, | |
}, | |
}); | |
</script> | |
<TabsHeader {tabs} /> | |
<div> | |
<slot /> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment