🎨
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
:class="$style.tabTitle">{{ t.title }}</div>
|
||||
<Transition v-else @enter="enter" @after-enter="afterEnter" @leave="leave" @after-leave="afterLeave"
|
||||
mode="in-out">
|
||||
<div v-if="t.key === tab" :class="$style.tabTitle">{{ t.title }}</div>
|
||||
<div v-show="t.key === tab" :class="[$style.tabTitle, $style.animate]">{{ t.title }}</div>
|
||||
</Transition>
|
||||
</div>
|
||||
</button>
|
||||
@@ -34,7 +34,7 @@ export type Tab = {
|
||||
</script>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { onMounted, onUnmounted, watch, nextTick } from 'vue';
|
||||
import { onMounted, onUnmounted, watch, nextTick, Transition, shallowRef } from 'vue';
|
||||
import { defaultStore } from '@/store';
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
@@ -50,9 +50,9 @@ const emit = defineEmits<{
|
||||
(ev: 'tabClick', key: string);
|
||||
}>();
|
||||
|
||||
let el = $shallowRef<HTMLElement | null>(null);
|
||||
const el = shallowRef<HTMLElement | null>(null);
|
||||
const tabRefs: Record<string, HTMLElement | null> = {};
|
||||
let tabHighlightEl = $shallowRef<HTMLElement | null>(null);
|
||||
const tabHighlightEl = shallowRef<HTMLElement | null>(null);
|
||||
|
||||
function onTabMousedown(tab: Tab, ev: MouseEvent): void {
|
||||
// ユーザビリティの観点からmousedown時にはonClickは呼ばない
|
||||
@@ -77,13 +77,13 @@ function onTabClick(t: Tab, ev: MouseEvent): void {
|
||||
|
||||
function renderTab() {
|
||||
const tabEl = props.tab ? tabRefs[props.tab] : undefined;
|
||||
if (tabEl && tabHighlightEl && tabHighlightEl.parentElement) {
|
||||
if (tabEl && tabHighlightEl.value && tabHighlightEl.value.parentElement) {
|
||||
// offsetWidth や offsetLeft は少数を丸めてしまうため getBoundingClientRect を使う必要がある
|
||||
// https://developer.mozilla.org/ja/docs/Web/API/HTMLElement/offsetWidth#%E5%80%A4
|
||||
const parentRect = tabHighlightEl.parentElement.getBoundingClientRect();
|
||||
const parentRect = tabHighlightEl.value.parentElement.getBoundingClientRect();
|
||||
const rect = tabEl.getBoundingClientRect();
|
||||
tabHighlightEl.style.width = rect.width + 'px';
|
||||
tabHighlightEl.style.left = (rect.left - parentRect.left + tabHighlightEl.parentElement.scrollLeft) + 'px';
|
||||
tabHighlightEl.value.style.width = rect.width + 'px';
|
||||
tabHighlightEl.value.style.left = (rect.left - parentRect.left + tabHighlightEl.value.parentElement.scrollLeft) + 'px';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,22 +99,32 @@ function onTabWheel(ev: WheelEvent) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function enter(el: HTMLElement) {
|
||||
let entering = false;
|
||||
|
||||
async function enter(el: HTMLElement) {
|
||||
entering = true;
|
||||
const elementWidth = el.getBoundingClientRect().width;
|
||||
el.style.width = '0';
|
||||
el.offsetWidth; // reflow
|
||||
el.style.paddingLeft = '0';
|
||||
el.offsetWidth; // force reflow
|
||||
el.style.width = elementWidth + 'px';
|
||||
setTimeout(renderTab, 70);
|
||||
el.style.paddingLeft = '';
|
||||
nextTick(() => {
|
||||
entering = false;
|
||||
});
|
||||
|
||||
setTimeout(renderTab, 170);
|
||||
}
|
||||
function afterEnter(el: HTMLElement) {
|
||||
el.style.width = '';
|
||||
nextTick(renderTab);
|
||||
//el.style.width = '';
|
||||
}
|
||||
function leave(el: HTMLElement) {
|
||||
async function leave(el: HTMLElement) {
|
||||
const elementWidth = el.getBoundingClientRect().width;
|
||||
el.style.width = elementWidth + 'px';
|
||||
el.offsetWidth; // reflow
|
||||
el.style.paddingLeft = '';
|
||||
el.offsetWidth; // force reflow
|
||||
el.style.width = '0';
|
||||
el.style.paddingLeft = '0';
|
||||
}
|
||||
function afterLeave(el: HTMLElement) {
|
||||
el.style.width = '';
|
||||
@@ -124,14 +134,17 @@ let ro2: ResizeObserver | null;
|
||||
|
||||
onMounted(() => {
|
||||
watch([() => props.tab, () => props.tabs], () => {
|
||||
nextTick(() => renderTab());
|
||||
nextTick(() => {
|
||||
if (entering) return;
|
||||
renderTab();
|
||||
});
|
||||
}, {
|
||||
immediate: true,
|
||||
});
|
||||
|
||||
if (props.rootEl) {
|
||||
ro2 = new ResizeObserver((entries, observer) => {
|
||||
if (document.body.contains(el as HTMLElement)) {
|
||||
if (document.body.contains(el.value as HTMLElement)) {
|
||||
nextTick(() => renderTab());
|
||||
}
|
||||
});
|
||||
@@ -194,12 +207,15 @@ onUnmounted(() => {
|
||||
}
|
||||
|
||||
.tabIcon+.tabTitle {
|
||||
margin-left: 8px;
|
||||
padding-left: 8px;
|
||||
}
|
||||
|
||||
.tabTitle {
|
||||
overflow: hidden;
|
||||
transition: width 0.15s ease-in-out;
|
||||
|
||||
&.animate {
|
||||
transition: width .15s linear, padding-left .15s linear;
|
||||
}
|
||||
}
|
||||
|
||||
.tabHighlight {
|
||||
|
Reference in New Issue
Block a user