|
| 1 | +<script lang="ts" setup> |
| 2 | +import { JeTransparentToolButton } from '../Button' |
| 3 | +import { TABS_INJECT_KEY } from './key' |
| 4 | +import type { JeTabPaneProps, JeTabsProps } from './types' |
| 5 | +
|
| 6 | +interface InternalPane extends JeTabPaneProps { order: number } |
| 7 | +
|
| 8 | +const props = withDefaults(defineProps<JeTabsProps>(), { |
| 9 | + closable: false, |
| 10 | + addable: false, |
| 11 | + scrollable: true, |
| 12 | +}) |
| 13 | +
|
| 14 | +const emit = defineEmits<{ |
| 15 | + (e: 'update:modelValue', v: string | number): void |
| 16 | + (e: 'tabAdd'): void |
| 17 | + (e: 'tabClose', v: string | number): void |
| 18 | +}>() |
| 19 | +
|
| 20 | +const panes = reactive<InternalPane[]>([]) |
| 21 | +let count = 0 |
| 22 | +
|
| 23 | +const activeValue = computed(() => props.modelValue) |
| 24 | +
|
| 25 | +function registerPane(pane: JeTabPaneProps) { |
| 26 | + const exist = panes.find(p => p.value === pane.value) |
| 27 | + if (exist) { |
| 28 | + exist.label = pane.label |
| 29 | + exist.disabled = pane.disabled |
| 30 | + exist.closable = pane.closable |
| 31 | + exist.icon = pane.icon |
| 32 | + exist.lazy = pane.lazy |
| 33 | + } |
| 34 | + else if (!panes.find(p => p.value === pane.value)) { |
| 35 | + panes.push({ ...pane, order: count++ }) |
| 36 | + } |
| 37 | +} |
| 38 | +function unregisterPane(value: string | number) { |
| 39 | + const idx = panes.findIndex(p => p.value === value) |
| 40 | + if (idx !== -1) |
| 41 | + panes.splice(idx, 1) |
| 42 | +} |
| 43 | +
|
| 44 | +function updateActive(v: string | number) { |
| 45 | + if (v === props.modelValue) |
| 46 | + return |
| 47 | + emit('update:modelValue', v) |
| 48 | +} |
| 49 | +
|
| 50 | +function handleClose(e: MouseEvent, pane: InternalPane) { |
| 51 | + e.stopPropagation() |
| 52 | + emit('tabClose', pane.value) |
| 53 | + const idx = panes.findIndex(p => p.value === pane.value) |
| 54 | + if (idx !== -1) |
| 55 | + panes.splice(idx, 1) |
| 56 | + if (pane.value === props.modelValue) { |
| 57 | + if (panes.length) { |
| 58 | + const newIdx = idx < panes.length ? idx : panes.length - 1 |
| 59 | + emit('update:modelValue', panes[newIdx].value) |
| 60 | + } |
| 61 | + else { |
| 62 | + emit('update:modelValue', '' as any) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | +
|
| 67 | +function handleAdd() { |
| 68 | + emit('tabAdd') |
| 69 | +} |
| 70 | +
|
| 71 | +provide(TABS_INJECT_KEY, { |
| 72 | + registerPane, |
| 73 | + unregisterPane, |
| 74 | + activeValue, |
| 75 | + parentClosable: toRef(props, 'closable'), |
| 76 | +}) |
| 77 | +
|
| 78 | +const sortedPanes = computed(() => [...panes].sort((a, b) => a.order - b.order)) |
| 79 | +</script> |
| 80 | + |
| 81 | +<template> |
| 82 | + <div class="je-tabs"> |
| 83 | + <div class="je-tabs__nav-wrapper" :class="{ 'je-tabs__nav-wrapper--scroll': scrollable }"> |
| 84 | + <div class="je-tabs__nav" :class="{ 'je-tabs__nav--scroll': scrollable }"> |
| 85 | + <div |
| 86 | + v-for="pane in sortedPanes" |
| 87 | + :key="pane.value" |
| 88 | + class="je-tabs__tab" |
| 89 | + :class="[ |
| 90 | + { 'je-tabs__tab--active': pane.value === activeValue, 'je-tabs__tab--disabled': pane.disabled }, |
| 91 | + ]" |
| 92 | + tabindex="0" |
| 93 | + role="tab" |
| 94 | + @click="!pane.disabled && updateActive(pane.value)" |
| 95 | + @keydown.enter.prevent="!pane.disabled && updateActive(pane.value)" |
| 96 | + > |
| 97 | + <i |
| 98 | + v-if="pane.icon" |
| 99 | + class="je-tabs__tab-icon" |
| 100 | + :class="pane.icon" |
| 101 | + /> |
| 102 | + <span class="je-tabs__tab-label truncate">{{ pane.label }}</span> |
| 103 | + <i |
| 104 | + v-if="(pane.closable ?? closable) && !pane.disabled" |
| 105 | + class="je-tabs__tab-close light:i-jet:close-small dark:i-jet:close-small-dark" |
| 106 | + @click="e => handleClose(e, pane)" |
| 107 | + /> |
| 108 | + </div> |
| 109 | + |
| 110 | + <JeTransparentToolButton |
| 111 | + v-if="addable" |
| 112 | + class="je-tabs__add-btn" |
| 113 | + icon="light:i-jet:add dark:i-jet:add-dark" |
| 114 | + icon-size="14px" |
| 115 | + @click="handleAdd" |
| 116 | + /> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + <div class="je-tabs__content"> |
| 120 | + <slot /> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | +</template> |
| 124 | + |
| 125 | +<style lang="scss" scoped> |
| 126 | +.je-tabs { |
| 127 | + // 基础排版 |
| 128 | + @apply flex flex-col w-full; |
| 129 | + @apply font-sans text-13px lh-20px; |
| 130 | +} |
| 131 | +
|
| 132 | +.je-tabs__nav-wrapper { |
| 133 | + @apply relative flex items-end; // 底部分隔线 |
| 134 | + @apply b-b-solid b-b-1px light:b-b-$gray-12 dark:b-b-$gray-3; |
| 135 | +
|
| 136 | + &--scroll { |
| 137 | + @apply overflow-x-auto scrollbar-none; |
| 138 | + } |
| 139 | +} |
| 140 | +
|
| 141 | +.je-tabs__nav { |
| 142 | + @apply flex items-stretch gap-2px px-2px pt-2px; // 间距 |
| 143 | +
|
| 144 | + &--scroll { |
| 145 | + @apply min-w-full; |
| 146 | + } |
| 147 | +} |
| 148 | +
|
| 149 | +.je-tabs__tab { |
| 150 | + @apply relative flex items-center gap-6px select-none cursor-pointer; |
| 151 | + @apply rounded-t-4px px-10px py-5px outline-0; |
| 152 | + @apply light:color-$gray-6 dark:color-$gray-8; // 非激活颜色 |
| 153 | + @apply light:hover:bg-$gray-13 dark:hover:bg-$gray-2; |
| 154 | + @apply transition-colors duration-130 ease-in-out; |
| 155 | +
|
| 156 | + &:focus-visible:not(.je-tabs__tab--active):not(.je-tabs__tab--disabled) { |
| 157 | + @apply outline outline-2px light:outline-$blue-4 dark:outline-$blue-6 rounded-4px; |
| 158 | + } |
| 159 | +
|
| 160 | + &--active { |
| 161 | + @apply light:bg-$gray-13 dark:bg-$gray-2; // 背景 |
| 162 | + @apply light:color-$gray-1 dark:color-$gray-12; // 文本 |
| 163 | + @apply after:absolute after:bottom-0 after:left-0 after:right-0 after:h-2px after:content-empty; |
| 164 | + @apply light:after:bg-$blue-4 dark:after:bg-$blue-6; // 下划线指示 |
| 165 | + } |
| 166 | +
|
| 167 | + &--disabled { |
| 168 | + @apply cursor-not-allowed light:color-$gray-9 dark:color-$gray-5; |
| 169 | + @apply opacity-60; |
| 170 | + } |
| 171 | +} |
| 172 | +
|
| 173 | +.je-tabs__tab-label { |
| 174 | + @apply text-default; |
| 175 | +} |
| 176 | +
|
| 177 | +.je-tabs__tab-icon { |
| 178 | + @apply text-14px; |
| 179 | +} |
| 180 | +
|
| 181 | +.je-tabs__tab-close { |
| 182 | + @apply text-14px opacity-70 hover:opacity-100 transition-opacity duration-120 cursor-pointer; |
| 183 | +} |
| 184 | +
|
| 185 | +.je-tabs__add-btn { |
| 186 | + @apply ml-4px my-auto; |
| 187 | +} |
| 188 | +
|
| 189 | +.je-tabs__content { |
| 190 | + @apply pt-8px; |
| 191 | +} |
| 192 | +</style> |
0 commit comments