|
1 | 1 | <script setup> |
2 | | -import { ref, computed, useSlots } from 'vue' |
| 2 | +import { ref, computed, useSlots, onMounted, watch, nextTick } from 'vue' |
3 | 3 |
|
4 | 4 | const slots = useSlots() |
5 | 5 | const tabKeys = Object.keys(slots) |
6 | 6 | const activeTab = ref(tabKeys[0] ?? '') |
7 | 7 | const currentTab = computed(() => activeTab.value) |
| 8 | +const wrapperRef = ref(null) |
| 9 | +
|
| 10 | +function formatKey(key) { |
| 11 | + return key.replace(/[__]/g, ' ') |
| 12 | +} |
| 13 | +
|
| 14 | +defineProps({ |
| 15 | + label: { |
| 16 | + type: String, |
| 17 | + default: '' |
| 18 | + } |
| 19 | +}) |
| 20 | +
|
| 21 | +onMounted(() => { |
| 22 | + const hash = decodeURIComponent(window.location.hash.slice(1)) |
| 23 | + if (tabKeys.includes(hash)) { |
| 24 | + activeTab.value = hash |
| 25 | + nextTick(() => { |
| 26 | + const el = wrapperRef.value |
| 27 | + if (el) { |
| 28 | + const offset = 80 |
| 29 | + const top = el.getBoundingClientRect().top + window.scrollY - offset |
| 30 | + window.scrollTo({ top, behavior: 'smooth' }) |
| 31 | + } |
| 32 | + }) |
| 33 | + } |
| 34 | +}) |
| 35 | +
|
| 36 | +watch(activeTab, (newVal) => { |
| 37 | + if (newVal) { |
| 38 | + history.replaceState(null, '', `#${encodeURIComponent(newVal)}`) |
| 39 | + } |
| 40 | +}) |
8 | 41 | </script> |
9 | 42 |
|
10 | 43 | <template> |
11 | | - <div class="table-tabs mt-8 border rounded-lg overflow-hidden text-sm"> |
12 | | - <div class="bg-gray-50 flex justify-between items-center border-b"> |
13 | | -<!-- <span class="font-medium text-gray-700">Optional Text:span> --> |
14 | | - <select |
15 | | - v-model="activeTab" |
16 | | - class="bg-white text-gray-800 text-sm rounded border-gray-300 focus:outline-none focus:ring-1 focus:ring-blue-500" |
17 | | - > |
| 44 | + <div ref="wrapperRef" class="table-tabs" :id="activeTab"> |
| 45 | + <div class="tab-header"> |
| 46 | + <span v-if="label" class="label-text"> |
| 47 | + {{ label }} |
| 48 | + </span> |
| 49 | + <select v-model="activeTab" class="tab-select"> |
18 | 50 | <option v-for="key in tabKeys" :key="key" :value="key"> |
19 | | - {{ key }} |
| 51 | + {{ formatKey(key) }} |
20 | 52 | </option> |
21 | 53 | </select> |
22 | 54 | </div> |
23 | 55 |
|
24 | | - <div class="tab-content prose prose-sm max-w-none"> |
| 56 | + <div class="tab-content"> |
25 | 57 | <slot :name="currentTab" /> |
26 | 58 | </div> |
| 59 | +
|
| 60 | + <div class="bottom-line" /> |
27 | 61 | </div> |
28 | 62 | </template> |
29 | 63 |
|
30 | 64 |
|
| 65 | +<style scoped> |
| 66 | +.table-tabs { |
| 67 | + background: #fff; |
| 68 | + scroll-margin-top: 4rem; |
| 69 | +} |
| 70 | +
|
| 71 | +.tab-header { |
| 72 | + border-top: 1px solid #d1d5db; |
| 73 | + padding: 1rem 0; |
| 74 | +} |
| 75 | +
|
| 76 | +.label-text { |
| 77 | + color: #314659; |
| 78 | +} |
| 79 | +
|
| 80 | +.tab-select { |
| 81 | + background-color: #fff; |
| 82 | + color: #314659; |
| 83 | + font-size: 0.9rem; |
| 84 | + padding: 0.25rem 0.25rem; |
| 85 | + border: 1px solid #d1d5db; |
| 86 | + border-radius: 0.5rem; |
| 87 | + outline: none; |
| 88 | +} |
| 89 | +
|
| 90 | +.tab-select:focus { |
| 91 | + border-color: #5897fb; |
| 92 | + box-shadow: 0 0 0 1px #5897fb; |
| 93 | +} |
| 94 | +
|
| 95 | +.bottom-line { |
| 96 | + border-top: 1px solid #d1d5db; |
| 97 | + margin-top: 1rem; |
| 98 | +} |
| 99 | +</style> |
| 100 | +
|
0 commit comments