primevue-mirror/components/lib/tablist/TabList.vue

166 lines
6.0 KiB
Vue
Raw Normal View History

2024-04-19 13:51:57 +00:00
<template>
<div ref="list" :class="cx('root')" v-bind="ptmi('root')">
2024-04-24 23:37:23 +00:00
<button
2024-04-19 13:51:57 +00:00
v-if="showNavigators && isPrevButtonEnabled"
ref="prevButton"
2024-04-24 23:37:23 +00:00
v-ripple
2024-05-27 08:48:42 +00:00
:class="cx('prevButton')"
2024-04-19 13:51:57 +00:00
:aria-label="prevButtonAriaLabel"
:tabindex="$pcTabs.tabindex"
@click="onPrevButtonClick"
2024-05-27 08:48:42 +00:00
v-bind="ptm('prevButton')"
2024-04-19 13:51:57 +00:00
data-pc-group-section="navigator"
>
2024-05-27 08:48:42 +00:00
<component :is="templates.previcon || 'ChevronLeftIcon'" aria-hidden="true" v-bind="ptm('prevIcon')" />
2024-04-24 23:37:23 +00:00
</button>
2024-04-26 09:28:47 +00:00
<div ref="content" :class="cx('content')" @scroll="onScroll" v-bind="ptm('content')">
<div ref="tabs" :class="cx('tabs')" role="tablist" :aria-orientation="$pcTabs.orientation || 'horizontal'" v-bind="ptm('tabs')">
2024-04-24 23:37:23 +00:00
<slot></slot>
<span ref="inkbar" :class="cx('inkbar')" role="presentation" aria-hidden="true" v-bind="ptm('inkbar')"></span>
</div>
2024-04-19 13:51:57 +00:00
</div>
2024-04-24 23:37:23 +00:00
<button
2024-04-19 13:51:57 +00:00
v-if="showNavigators && isNextButtonEnabled"
ref="nextButton"
2024-04-24 23:37:23 +00:00
v-ripple
2024-04-19 13:51:57 +00:00
:class="cx('nextButton')"
:aria-label="nextButtonAriaLabel"
:tabindex="$pcTabs.tabindex"
@click="onNextButtonClick"
2024-04-24 23:37:23 +00:00
v-bind="ptm('nextButton')"
2024-04-19 13:51:57 +00:00
data-pc-group-section="navigator"
>
2024-04-24 23:37:23 +00:00
<component :is="templates.nexticon || 'ChevronRightIcon'" aria-hidden="true" v-bind="ptm('nextIcon')" />
</button>
2024-04-19 13:51:57 +00:00
</div>
</template>
<script>
import ChevronLeftIcon from 'primevue/icons/chevronleft';
import ChevronRightIcon from 'primevue/icons/chevronright';
import { DomHandler } from 'primevue/utils';
import BaseTabList from './BaseTabList.vue';
export default {
name: 'TabList',
extends: BaseTabList,
inheritAttrs: false,
inject: ['$pcTabs'],
data() {
return {
isPrevButtonEnabled: false,
isNextButtonEnabled: true
};
},
resizeObserver: undefined,
watch: {
showNavigators(newValue) {
newValue ? this.bindResizeObserver() : this.unbindResizeObserver();
},
activeValue: {
flush: 'post',
handler() {
this.updateInkBar();
}
}
},
mounted() {
2024-04-19 14:04:19 +00:00
this.$nextTick(() => {
this.updateInkBar();
});
2024-04-19 13:51:57 +00:00
if (this.showNavigators) {
this.updateButtonState();
this.bindResizeObserver();
}
},
updated() {
this.showNavigators && this.updateButtonState();
},
beforeUnmount() {
this.unbindResizeObserver();
},
methods: {
onScroll(event) {
this.showNavigators && this.updateButtonState();
event.preventDefault();
},
onPrevButtonClick() {
const content = this.$refs.content;
const width = DomHandler.getWidth(content);
const pos = content.scrollLeft - width;
content.scrollLeft = pos <= 0 ? 0 : pos;
},
onNextButtonClick() {
const content = this.$refs.content;
const width = DomHandler.getWidth(content) - this.getVisibleButtonWidths();
const pos = content.scrollLeft + width;
const lastPos = content.scrollWidth - width;
content.scrollLeft = pos >= lastPos ? lastPos : pos;
},
bindResizeObserver() {
this.resizeObserver = new ResizeObserver(() => this.updateButtonState());
this.resizeObserver.observe(this.$refs.list);
},
unbindResizeObserver() {
this.resizeObserver?.unobserve(this.$refs.list);
this.resizeObserver = undefined;
},
updateInkBar() {
2024-04-24 23:37:23 +00:00
const { content, inkbar, tabs } = this.$refs;
2024-04-19 13:51:57 +00:00
const activeTab = DomHandler.findSingle(content, '[data-pc-name="tab"][data-p-active="true"]');
2024-04-25 00:44:13 +00:00
if (this.$pcTabs.isVertical()) {
inkbar.style.height = DomHandler.getOuterHeight(activeTab) + 'px';
inkbar.style.top = DomHandler.getOffset(activeTab).top - DomHandler.getOffset(tabs).top + 'px';
} else {
inkbar.style.width = DomHandler.getOuterWidth(activeTab) + 'px';
inkbar.style.left = DomHandler.getOffset(activeTab).left - DomHandler.getOffset(tabs).left + 'px';
}
2024-04-19 13:51:57 +00:00
},
updateButtonState() {
const { list, content } = this.$refs;
2024-04-25 00:44:13 +00:00
const { scrollLeft, scrollTop, scrollWidth, scrollHeight, offsetWidth, offsetHeight } = content;
const [width, height] = [DomHandler.getWidth(content), DomHandler.getHeight(content)];
2024-04-19 13:51:57 +00:00
2024-04-25 00:44:13 +00:00
if (this.$pcTabs.isVertical()) {
this.isPrevButtonEnabled = scrollTop !== 0;
this.isNextButtonEnabled = list.offsetHeight >= offsetHeight && parseInt(scrollTop) !== scrollHeight - height;
} else {
this.isPrevButtonEnabled = scrollLeft !== 0;
this.isNextButtonEnabled = list.offsetWidth >= offsetWidth && parseInt(scrollLeft) !== scrollWidth - width;
}
2024-04-19 13:51:57 +00:00
},
getVisibleButtonWidths() {
const { prevBtn, nextBtn } = this.$refs;
return [prevBtn, nextBtn].reduce((acc, el) => (el ? acc + DomHandler.getWidth(el) : acc), 0);
}
},
computed: {
templates() {
return this.$pcTabs.$slots;
},
activeValue() {
return this.$pcTabs.d_value;
},
showNavigators() {
return this.$pcTabs.scrollable && this.$pcTabs.showNavigators;
},
prevButtonAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.previous : undefined;
},
nextButtonAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.next : undefined;
}
},
components: {
ChevronLeftIcon,
ChevronRightIcon
}
};
</script>