Skip to content

Instantly share code, notes, and snippets.

@dimfeld
Last active November 19, 2020 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimfeld/5e8b91d3da6558ca472e2ad827acd672 to your computer and use it in GitHub Desktop.
Save dimfeld/5e8b91d3da6558ca472e2ad827acd672 to your computer and use it in GitHub Desktop.
An abbreviated version of my tabs components
<Tabs>
<Tab id="settings" name="Settings">Content</Tab>
<Tab id="sharing" name="Sharing">Content2</Tab>
</Tabs>
<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}
<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