2021-04-14 14:52:25 +02:00
|
|
|
<script>
|
2021-04-23 11:48:19 +02:00
|
|
|
import "@spectrum-css/tabs/dist/index-vars.css"
|
|
|
|
import { writable } from "svelte/store"
|
|
|
|
import { onMount, setContext, createEventDispatcher } from "svelte"
|
2021-04-15 14:42:39 +02:00
|
|
|
|
2021-04-23 11:48:19 +02:00
|
|
|
export let selected
|
|
|
|
export let vertical = false
|
2021-06-22 10:14:17 +02:00
|
|
|
export let noPadding = false
|
2021-12-03 19:39:05 +01:00
|
|
|
// added as a separate option as noPadding is used for vertical padding
|
|
|
|
export let noHorizPadding = false
|
2021-08-09 16:47:30 +02:00
|
|
|
export let quiet = false
|
|
|
|
export let emphasized = false
|
2022-09-07 18:45:14 +02:00
|
|
|
export let onTop = false
|
2022-04-12 16:34:34 +02:00
|
|
|
export let size = "M"
|
2021-06-22 10:14:17 +02:00
|
|
|
|
2021-12-09 19:41:07 +01:00
|
|
|
let thisSelected = undefined
|
|
|
|
|
2021-04-23 11:48:19 +02:00
|
|
|
let _id = id()
|
2021-08-09 16:47:30 +02:00
|
|
|
const tab = writable({ title: selected, id: _id, emphasized })
|
2021-04-23 11:48:19 +02:00
|
|
|
setContext("tab", tab)
|
2021-04-15 15:39:00 +02:00
|
|
|
|
2021-04-23 11:48:19 +02:00
|
|
|
let container
|
2021-04-15 14:42:39 +02:00
|
|
|
|
2021-04-23 11:48:19 +02:00
|
|
|
const dispatch = createEventDispatcher()
|
2021-04-15 14:42:39 +02:00
|
|
|
|
2021-07-06 16:53:52 +02:00
|
|
|
$: {
|
2021-12-09 19:41:07 +01:00
|
|
|
if (thisSelected !== selected) {
|
|
|
|
thisSelected = selected
|
|
|
|
dispatch("select", thisSelected)
|
|
|
|
} else if ($tab.title !== thisSelected) {
|
|
|
|
thisSelected = $tab.title
|
2021-12-13 12:24:13 +01:00
|
|
|
selected = $tab.title
|
2021-12-09 19:41:07 +01:00
|
|
|
dispatch("select", thisSelected)
|
|
|
|
}
|
|
|
|
if ($tab.title !== thisSelected) {
|
|
|
|
tab.update(state => {
|
|
|
|
state.title = thisSelected
|
|
|
|
return state
|
|
|
|
})
|
2021-07-06 16:53:52 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-23 11:48:19 +02:00
|
|
|
|
|
|
|
let top, left, width, height
|
|
|
|
$: calculateIndicatorLength($tab)
|
|
|
|
$: calculateIndicatorOffset($tab)
|
2021-04-15 14:42:39 +02:00
|
|
|
|
2021-04-23 11:48:19 +02:00
|
|
|
function calculateIndicatorLength() {
|
|
|
|
if (!vertical) {
|
|
|
|
width = $tab.info?.width + "px"
|
|
|
|
height = $tab.info?.height
|
|
|
|
} else {
|
|
|
|
height = $tab.info?.height + 4 + "px"
|
|
|
|
width = $tab.info?.width
|
2021-04-15 14:42:39 +02:00
|
|
|
}
|
2021-04-23 11:48:19 +02:00
|
|
|
}
|
2021-04-15 14:42:39 +02:00
|
|
|
|
2021-04-23 11:48:19 +02:00
|
|
|
function calculateIndicatorOffset() {
|
|
|
|
if (!vertical) {
|
|
|
|
left = $tab.info?.left - container?.getBoundingClientRect().left + "px"
|
|
|
|
} else {
|
|
|
|
top = $tab.info?.top - container?.getBoundingClientRect().top + "px"
|
2021-04-15 14:42:39 +02:00
|
|
|
}
|
2021-04-23 11:48:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
calculateIndicatorLength()
|
|
|
|
calculateIndicatorOffset()
|
|
|
|
})
|
|
|
|
|
|
|
|
function id() {
|
2022-02-20 15:28:39 +01:00
|
|
|
return "_" + Math.random().toString(36).slice(2, 9)
|
2021-04-23 11:48:19 +02:00
|
|
|
}
|
2021-04-14 14:52:25 +02:00
|
|
|
</script>
|
2021-04-23 11:48:19 +02:00
|
|
|
|
|
|
|
<div
|
|
|
|
bind:this={container}
|
2022-04-12 16:34:34 +02:00
|
|
|
class:spectrum-Tabs--quiet={quiet}
|
2021-12-03 19:39:05 +01:00
|
|
|
class:noHorizPadding
|
2022-09-07 18:45:14 +02:00
|
|
|
class:onTop
|
2022-04-12 16:34:34 +02:00
|
|
|
class:spectrum-Tabs--vertical={vertical}
|
|
|
|
class:spectrum-Tabs--horizontal={!vertical}
|
|
|
|
class="spectrum-Tabs spectrum-Tabs--size{size}"
|
2021-05-04 12:04:42 +02:00
|
|
|
>
|
2021-04-23 11:48:19 +02:00
|
|
|
<slot />
|
|
|
|
{#if $tab.info}
|
|
|
|
<div
|
2022-04-12 16:34:34 +02:00
|
|
|
class="spectrum-Tabs-selectionIndicator"
|
|
|
|
class:emphasized
|
|
|
|
style="width: {width}; height: {height}; left: {left}; top: {top};"
|
2021-05-04 12:04:42 +02:00
|
|
|
/>
|
2021-04-23 11:48:19 +02:00
|
|
|
{/if}
|
2021-04-14 16:07:45 +02:00
|
|
|
</div>
|
|
|
|
|
2021-06-22 10:14:17 +02:00
|
|
|
<div
|
|
|
|
class="spectrum-Tabs-content spectrum-Tabs-content-{_id}"
|
|
|
|
class:noPadding
|
|
|
|
/>
|
2021-04-14 16:07:45 +02:00
|
|
|
|
|
|
|
<style>
|
2022-04-12 16:34:34 +02:00
|
|
|
.spectrum-Tabs--quiet {
|
2021-08-09 16:47:30 +02:00
|
|
|
border-bottom: none !important;
|
|
|
|
}
|
2021-04-23 11:48:19 +02:00
|
|
|
.spectrum-Tabs {
|
|
|
|
padding-left: var(--spacing-xl);
|
|
|
|
padding-right: var(--spacing-xl);
|
2021-04-23 12:30:17 +02:00
|
|
|
position: relative;
|
2022-04-12 16:34:34 +02:00
|
|
|
border-bottom-color: var(--spectrum-global-color-gray-200);
|
2021-04-23 11:48:19 +02:00
|
|
|
}
|
|
|
|
.spectrum-Tabs-content {
|
|
|
|
margin-top: var(--spectrum-global-dimension-static-size-150);
|
|
|
|
}
|
2022-04-12 16:34:34 +02:00
|
|
|
.spectrum-Tabs-selectionIndicator {
|
2021-04-23 11:48:19 +02:00
|
|
|
transition: all 200ms;
|
2022-04-12 16:34:34 +02:00
|
|
|
background-color: var(--spectrum-global-color-gray-900);
|
|
|
|
}
|
|
|
|
.spectrum-Tabs-selectionIndicator.emphasized {
|
|
|
|
background-color: var(--spectrum-global-color-blue-400);
|
2021-04-23 11:48:19 +02:00
|
|
|
}
|
2021-04-23 12:30:17 +02:00
|
|
|
.spectrum-Tabs--horizontal .spectrum-Tabs-selectionIndicator {
|
|
|
|
}
|
2021-12-03 19:39:05 +01:00
|
|
|
.noHorizPadding {
|
|
|
|
padding: 0;
|
|
|
|
}
|
2021-06-22 10:14:17 +02:00
|
|
|
.noPadding {
|
|
|
|
margin: 0;
|
|
|
|
}
|
2022-09-07 18:45:14 +02:00
|
|
|
.onTop {
|
|
|
|
z-index: 100;
|
|
|
|
}
|
2021-04-23 11:48:19 +02:00
|
|
|
</style>
|