Last active
February 4, 2021 11:57
-
-
Save cristovao-trevisan/23dcebedcb902e63838906a62db16607 to your computer and use it in GitHub Desktop.
svelte-tabs
This file contains 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 lang="ts"> | |
interface Tab { | |
title: string | |
} | |
export let tabs: Tab[] | |
export let selected = 0 | |
</script> | |
<ul class="flex w-full border-b"> | |
{#each tabs as tab, i} | |
<li | |
class="cursor-pointer -mb-px mr-1 hover:opacity-50 text-blue-500 py-2 px-4" | |
class:selected={i === selected} | |
on:click={() => { selected = i }} | |
> | |
{tab.title} | |
</li> | |
{/each} | |
</ul> | |
<slot {selected} /> | |
<style> | |
.selected { | |
@apply font-semibold rounded-t border-l border-t border-r bg-white; | |
color: var(--main-color); | |
cursor: inherit; | |
} | |
.selected:hover { | |
opacity: 1; | |
} | |
</style> |
This file contains 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 lang="ts"> | |
const tabs = [{ title: 'Tab 1' }, { title: 'Tab 2' }] | |
</script> | |
<Tabs {tabs} bind:selected> | |
{#if selected === 0} | |
Tab 1 content | |
{:else if selected === 1} | |
Tab 2 content | |
{/if} | |
</Tabs> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment