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

44 lines
1007 B
Svelte
Raw Normal View History

2021-04-14 16:07:45 +02:00
<script>
import { getContext, onMount } from "svelte"
import Portal from "svelte-portal"
export let title
export let icon = ""
2021-04-15 14:42:39 +02:00
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()
})
2021-04-14 16:07:45 +02:00
</script>
<div
bind:this={tab}
on:click={() => ($selected = { ...$selected, title, info: tab.getBoundingClientRect() })}
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}