budibase/packages/bbui/src/Tabs/Tab.svelte

52 lines
1.1 KiB
Svelte
Raw Normal View History

2021-04-14 16:07:45 +02:00
<script>
import { getContext, onMount, createEventDispatcher } from "svelte"
import Portal from "svelte-portal"
export let title
export let icon = ""
2021-04-15 14:42:39 +02:00
const dispatch = createEventDispatcher()
const selected = getContext("tab")
let tab
let tabInfo
const setTabInfo = () => {
tabInfo = tab.getBoundingClientRect()
if ($selected.title === title) {
$selected.info = tabInfo
}
}
2021-04-15 14:42:39 +02:00
onMount(() => {
setTabInfo()
})
const onClick = () => {
$selected = { ...$selected, title, info: tab.getBoundingClientRect() }
dispatch("click")
}
2021-04-14 16:07:45 +02:00
</script>
<div
bind:this={tab}
on:click={onClick}
class:is-selected={$selected.title === title}
class="spectrum-Tabs-item"
tabindex="0"
>
{#if icon}
<svg
class="spectrum-Icon spectrum-Icon--sizeM"
focusable="false"
aria-hidden="true"
aria-label="Folder"
>
<use xlink:href="#spectrum-icon-18-{icon}" />
</svg>
{/if}
<span class="spectrum-Tabs-itemLabel">{title}</span>
2021-04-14 16:07:45 +02:00
</div>
2021-04-15 14:42:39 +02:00
{#if $selected.title === title}
<Portal target=".spectrum-Tabs-content-{$selected.id}">
<slot />
</Portal>
{/if}