2021-04-14 16:07:45 +02:00
|
|
|
<script>
|
2021-04-23 12:30:17 +02:00
|
|
|
import { getContext, onMount, createEventDispatcher } from "svelte"
|
2021-04-23 11:48:19 +02:00
|
|
|
import Portal from "svelte-portal"
|
|
|
|
export let title
|
|
|
|
export let icon = ""
|
2021-04-15 14:42:39 +02:00
|
|
|
|
2021-04-23 12:30:17 +02:00
|
|
|
const dispatch = createEventDispatcher()
|
2021-12-01 18:55:57 +01:00
|
|
|
let selected = getContext("tab")
|
2021-04-23 11:48:19 +02:00
|
|
|
let tab
|
|
|
|
let tabInfo
|
2021-10-11 12:34:19 +02:00
|
|
|
|
2021-04-23 11:48:19 +02:00
|
|
|
const setTabInfo = () => {
|
2021-10-11 12:34:19 +02:00
|
|
|
// If the tabs are being rendered inside a component which uses
|
|
|
|
// a svelte transition to enter, then this initial getBoundingClientRect
|
|
|
|
// will return an incorrect position.
|
|
|
|
// We just need to get this off the main thread to fix this, by using
|
|
|
|
// a 0ms timeout.
|
|
|
|
setTimeout(() => {
|
2021-12-01 18:55:57 +01:00
|
|
|
tabInfo = tab?.getBoundingClientRect()
|
|
|
|
if (tabInfo && $selected.title === title) {
|
2021-10-11 12:34:19 +02:00
|
|
|
$selected.info = tabInfo
|
|
|
|
}
|
|
|
|
}, 0)
|
2021-04-23 11:48:19 +02:00
|
|
|
}
|
2021-04-15 14:42:39 +02:00
|
|
|
|
2021-04-23 11:48:19 +02:00
|
|
|
onMount(() => {
|
|
|
|
setTabInfo()
|
|
|
|
})
|
2021-04-23 12:30:17 +02:00
|
|
|
|
|
|
|
const onClick = () => {
|
|
|
|
$selected = { ...$selected, title, info: tab.getBoundingClientRect() }
|
|
|
|
dispatch("click")
|
|
|
|
}
|
2021-04-14 16:07:45 +02:00
|
|
|
</script>
|
|
|
|
|
2021-04-23 11:48:19 +02:00
|
|
|
<div
|
|
|
|
bind:this={tab}
|
2021-04-23 12:30:17 +02:00
|
|
|
on:click={onClick}
|
2021-04-23 11:48:19 +02:00
|
|
|
class:is-selected={$selected.title === title}
|
|
|
|
class="spectrum-Tabs-item"
|
2021-08-09 16:47:30 +02:00
|
|
|
class:emphasized={$selected.title === title && $selected.emphasized}
|
2021-05-04 12:04:42 +02:00
|
|
|
tabindex="0"
|
|
|
|
>
|
2021-04-23 11:48:19 +02:00
|
|
|
{#if icon}
|
|
|
|
<svg
|
|
|
|
class="spectrum-Icon spectrum-Icon--sizeM"
|
|
|
|
focusable="false"
|
|
|
|
aria-hidden="true"
|
2021-05-04 12:04:42 +02:00
|
|
|
aria-label="Folder"
|
|
|
|
>
|
2021-04-23 11:48:19 +02:00
|
|
|
<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}
|
2021-04-23 11:48:19 +02:00
|
|
|
<Portal target=".spectrum-Tabs-content-{$selected.id}">
|
|
|
|
<slot />
|
|
|
|
</Portal>
|
|
|
|
{/if}
|
2021-08-09 16:47:30 +02:00
|
|
|
|
|
|
|
<style>
|
|
|
|
.emphasized {
|
|
|
|
color: var(--spectrum-global-color-blue-600);
|
|
|
|
}
|
|
|
|
</style>
|