Skip to content

Commit

Permalink
feat: Tabs栏 增加左右滚动按钮 (vbenjs#4161)
Browse files Browse the repository at this point in the history
  • Loading branch information
P2K0 committed Aug 15, 2024
1 parent e2d9d08 commit c9c1eac
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function scrollIntoView() {
<template>
<div :style="style" class="tabs-chrome size-full flex-1 overflow-hidden pt-1">
<VbenScrollbar
id="tabs-scrollbar"
class="tabs-chrome__scrollbar h-full"
horizontal
scroll-bar-class="z-10"
Expand Down
1 change: 1 addition & 0 deletions packages/@core/ui-kit/tabs-ui/src/components/tabs/tabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function scrollIntoView() {
<template>
<div class="h-full flex-1 overflow-hidden">
<VbenScrollbar
id="tabs-scrollbar"
class="tabs-scrollbar h-full"
horizontal
scroll-bar-class="z-10"
Expand Down
1 change: 1 addition & 0 deletions packages/effects/layouts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
}
},
"dependencies": {
"@radix-icons/vue": "^1.0.0",
"@vben-core/layout-ui": "workspace:*",
"@vben-core/menu-ui": "workspace:*",
"@vben-core/shadcn-ui": "workspace:*",
Expand Down
28 changes: 27 additions & 1 deletion packages/effects/layouts/src/basic/tabbar/tabbar.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { computed } from 'vue';
import { computed, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import { useContentMaximize, useTabs } from '@vben/hooks';
Expand All @@ -12,6 +12,9 @@ import {
TabsView,
} from '@vben-core/tabs-ui';
import { ChevronLeftIcon, ChevronRightIcon } from '@radix-icons/vue';
import { useTabViewScroll } from './use-tab-view-scroll';
import { useTabbar } from './use-tabbar';
defineOptions({
Expand Down Expand Up @@ -49,9 +52,28 @@ const menus = computed(() => {
if (!preferences.tabbar.persist) {
tabbarStore.closeOtherTabs(route);
}
const { hasScroll, scrollDirection, setScrollBarEl, setScrollViewEl } =
useTabViewScroll();
onMounted(() => {
const scrollBarEl: HTMLElement | null =
document.querySelector('#tabs-scrollbar');
const scrollViewportEl: HTMLElement | null | undefined =
scrollBarEl?.querySelector('div[data-radix-scroll-area-viewport]');
setScrollBarEl(scrollBarEl);
setScrollViewEl(scrollViewportEl);
});
</script>

<template>
{{ hasScroll }}
<ChevronLeftIcon
class="mx-2 h-full cursor-pointer"
@click="scrollDirection('left')"
/>
<TabsView
:active="currentActive"
:class="theme"
Expand All @@ -66,6 +88,10 @@ if (!preferences.tabbar.persist) {
@update:active="handleClick"
/>
<div class="flex-center h-full">
<ChevronRightIcon
class="mx-2 h-full cursor-pointer"
@click="scrollDirection('right')"
/>
<TabsToolRefresh
v-if="preferences.tabbar.showRefresh"
@refresh="refreshTab"
Expand Down
50 changes: 50 additions & 0 deletions packages/effects/layouts/src/basic/tabbar/use-tab-view-scroll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ref } from 'vue';

type El = HTMLElement | null | undefined;

export function useTabViewScroll(scrollDistance: number = 150) {
const scrollbarEl = ref<El>(null);
const scrollViewportEl = ref<El>(null);

function setScrollBarEl(el: El) {
scrollbarEl.value = el;
}

function setScrollViewEl(el: El) {
scrollViewportEl.value = el;
}

function getScrollClientWidth() {
if (!scrollbarEl.value || !scrollViewportEl.value) return {};

const scrollbarWidth = scrollbarEl.value.clientWidth;
const scrollViewWidth = scrollViewportEl.value.clientWidth;

return {
scrollbarWidth,
scrollViewWidth,
};
}

function scrollDirection(
direction: 'left' | 'right',
distance: number = scrollDistance,
) {
const { scrollbarWidth, scrollViewWidth } = getScrollClientWidth();

if (!scrollbarWidth || !scrollViewWidth) return;

if (scrollbarWidth > scrollViewWidth) return;

scrollViewportEl.value?.scrollBy({
behavior: 'smooth',
left: direction === 'left' ? -distance : +distance,
});
}

return {
scrollDirection,
setScrollBarEl,
setScrollViewEl,
};
}
23 changes: 23 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c9c1eac

Please sign in to comment.