enhance(frontend): MkTabsのタブハイライト切り替えをCSS anchor positioningに対応させる

This commit is contained in:
kakkokari-gtyih 2025-10-05 17:43:03 +09:00
parent 7b1a4edf64
commit 15f8d2b557
1 changed files with 38 additions and 8 deletions

View File

@ -4,7 +4,7 @@ SPDX-License-Identifier: AGPL-3.0-only
--> -->
<template> <template>
<div :class="[$style.tabs, { [$style.centered]: props.centered }]"> <div :class="[$style.tabs, { [$style.centered]: props.centered }]" :style="{ '--tabAnchorName': tabAnchorName }">
<div :class="$style.tabsInner"> <div :class="$style.tabsInner">
<button <button
v-for="t in tabs" v-for="t in tabs"
@ -15,6 +15,7 @@ SPDX-License-Identifier: AGPL-3.0-only
[$style.active]: t.key != null && t.key === tab, [$style.active]: t.key != null && t.key === tab,
[$style.animate]: prefer.s.animation, [$style.animate]: prefer.s.animation,
}]" }]"
:style="getTabStyle(t)"
@mousedown="(ev) => onTabMousedown(t, ev)" @mousedown="(ev) => onTabMousedown(t, ev)"
@click="(ev) => onTabClick(t, ev)" @click="(ev) => onTabClick(t, ev)"
> >
@ -27,7 +28,11 @@ SPDX-License-Identifier: AGPL-3.0-only
{{ t.title }} {{ t.title }}
</div> </div>
<Transition <Transition
v-else mode="in-out" @enter="enter" @afterEnter="afterEnter" @leave="leave" v-else
mode="in-out"
@enter="enter"
@afterEnter="afterEnter"
@leave="leave"
@afterLeave="afterLeave" @afterLeave="afterLeave"
> >
<div v-show="t.key === tab" :class="[$style.tabTitle, $style.animate]">{{ t.title }}</div> <div v-show="t.key === tab" :class="[$style.tabTitle, $style.animate]">{{ t.title }}</div>
@ -55,8 +60,11 @@ export type Tab<K = string> = {
<script lang="ts" setup generic="const T extends Tab"> <script lang="ts" setup generic="const T extends Tab">
import { nextTick, onMounted, onUnmounted, useTemplateRef, watch } from 'vue'; import { nextTick, onMounted, onUnmounted, useTemplateRef, watch } from 'vue';
import { prefer } from '@/preferences.js'; import { prefer } from '@/preferences.js';
import { genId } from '@/utility/id.js';
const cssAnchorSupported = CSS.supports('position-anchor', '--anchor-name'); const cssAnchorSupported = CSS.supports('position-anchor', '--anchor-name');
const tabsComponentId = genId();
const tabAnchorName = `--${tabsComponentId}-currentTab`;
const props = withDefaults(defineProps<{ const props = withDefaults(defineProps<{
tabs?: T[]; tabs?: T[];
@ -75,6 +83,17 @@ const tab = defineModel<T['key']>('tab');
const tabHighlightEl = useTemplateRef('tabHighlightEl'); const tabHighlightEl = useTemplateRef('tabHighlightEl');
const tabRefs: Record<string, HTMLElement | null> = {}; const tabRefs: Record<string, HTMLElement | null> = {};
function getTabStyle(t: Tab): Record<string, string> {
if (!cssAnchorSupported) return {};
if (t.key === tab.value) {
return {
anchorName: tabAnchorName,
};
} else {
return {};
}
}
function onTabMousedown(selectedTab: Tab, ev: MouseEvent): void { function onTabMousedown(selectedTab: Tab, ev: MouseEvent): void {
// mousedownonClick // mousedownonClick
if (selectedTab.key) { if (selectedTab.key) {
@ -97,6 +116,8 @@ function onTabClick(t: Tab, ev: MouseEvent): void {
} }
function renderTab() { function renderTab() {
if (cssAnchorSupported) return;
const tabEl = tab.value ? tabRefs[tab.value] : undefined; const tabEl = tab.value ? tabRefs[tab.value] : undefined;
if (tabEl && tabHighlightEl.value && tabHighlightEl.value.parentElement) { if (tabEl && tabHighlightEl.value && tabHighlightEl.value.parentElement) {
// offsetWidth offsetLeft getBoundingClientRect 使 // offsetWidth offsetLeft getBoundingClientRect 使
@ -147,12 +168,14 @@ function afterLeave(el: Element) {
} }
onMounted(() => { onMounted(() => {
if (!cssAnchorSupported) {
watch([tab, () => props.tabs], () => { watch([tab, () => props.tabs], () => {
nextTick(() => { nextTick(() => {
if (entering) return; if (entering) return;
renderTab(); renderTab();
}); });
}, { immediate: true }); }, { immediate: true });
}
}); });
onUnmounted(() => { onUnmounted(() => {
@ -245,4 +268,11 @@ onUnmounted(() => {
bottom: auto; bottom: auto;
} }
} }
@supports (position-anchor: --anchor-name) {
.tabHighlight {
left: anchor(var(--tabAnchorName) start);
width: anchor-size(var(--tabAnchorName) width);
}
}
</style> </style>