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

68 lines
1.9 KiB
Svelte
Raw Normal View History

2021-04-14 14:52:25 +02:00
<script>
import "@spectrum-css/tabs/dist/index-vars.css"
2021-04-14 16:07:45 +02:00
import { writable } from 'svelte/store'
2021-04-15 14:42:39 +02:00
import { onMount, setContext } from 'svelte'
2021-04-14 16:07:45 +02:00
export let selected;
2021-04-14 14:52:25 +02:00
export let vertical = false
2021-04-15 14:42:39 +02:00
let _id = id()
const tab = writable({title: selected, id: _id})
2021-04-14 16:07:45 +02:00
setContext('tab', tab)
2021-04-15 14:42:39 +02:00
let container;
$: selected = $tab.title
let top, left, width, height;
$: calculateIndicatorLength($tab)
$: calculateIndicatorOffset($tab)
function calculateIndicatorLength() {
if (!vertical) {
width = $tab.info?.width + 24 + 'px'
height = $tab.info?.height
} else {
height = $tab.info?.height + 4 + 'px'
width = $tab.info?.width
}
}
function calculateIndicatorOffset() {
if (!vertical) {
left = $tab.info?.left - container?.getBoundingClientRect().left - 12 + 'px'
top = $tab.info?.top
} else {
top = $tab.info?.top - container?.getBoundingClientRect().top + 'px'
left = $tab.info?.left
}
}
onMount(() => {
calculateIndicatorLength()
calculateIndicatorOffset()
})
function id() {
return (
"_" +
Math.random()
.toString(36)
.substr(2, 9)
)
}
2021-04-14 14:52:25 +02:00
</script>
2021-04-15 14:42:39 +02:00
<div bind:this={container} class="selected-border spectrum-Tabs spectrum-Tabs--{vertical ? 'vertical' : 'horizontal'}">
2021-04-14 16:07:45 +02:00
<slot />
2021-04-15 14:42:39 +02:00
<div class="spectrum-Tabs-selectionIndicator indicator-transition" style="width: {width}; height: {height}; left: {left}; top: {top};"></div>
2021-04-14 16:07:45 +02:00
</div>
2021-04-15 14:42:39 +02:00
<div class="spectrum-Tabs-content spectrum-Tabs-content-{_id}" />
2021-04-14 16:07:45 +02:00
<style>
.spectrum-Tabs-content {
margin-top: var(--spectrum-global-dimension-static-size-150);
}
2021-04-15 14:42:39 +02:00
.indicator-transition {
transition: all 200ms
}
2021-04-14 16:07:45 +02:00
</style>