perf: ⚡ MkPageHeader perf
This commit is contained in:
parent
cdf9a03604
commit
4ed1b80d68
1 changed files with 50 additions and 50 deletions
|
@ -34,7 +34,7 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { computed, onMounted, onUnmounted, ref, inject, watch, shallowReactive, nextTick, reactive } from 'vue';
|
import { computed, onMounted, onUnmounted, ref, inject, watch, shallowReactive, nextTick } from 'vue';
|
||||||
import tinycolor from 'tinycolor2';
|
import tinycolor from 'tinycolor2';
|
||||||
import { popupMenu } from '@/os';
|
import { popupMenu } from '@/os';
|
||||||
import { scrollToTop } from '@/scripts/scroll';
|
import { scrollToTop } from '@/scripts/scroll';
|
||||||
|
@ -71,17 +71,17 @@ const metadata = injectPageMetadata();
|
||||||
const hideTitle = inject('shouldOmitHeaderTitle', false);
|
const hideTitle = inject('shouldOmitHeaderTitle', false);
|
||||||
const thin_ = props.thin || inject('shouldHeaderThin', false);
|
const thin_ = props.thin || inject('shouldHeaderThin', false);
|
||||||
|
|
||||||
const el = $ref<HTMLElement | null>(null);
|
const el = ref<HTMLElement | null>(null);
|
||||||
const tabRefs = {};
|
const tabRefs: Record<string, HTMLElement> = {};
|
||||||
const tabHighlightEl = $ref<HTMLElement | null>(null);
|
const tabHighlightEl = ref<HTMLElement | null>(null);
|
||||||
const tabsEl = $ref<HTMLElement | null>(null);
|
const tabsEl = ref<HTMLElement | null>(null);
|
||||||
const bg = ref(null);
|
const bg = ref<string | null>(null);
|
||||||
let narrow = $ref(false);
|
const narrow = ref(false);
|
||||||
const height = ref(0);
|
const height = ref(0);
|
||||||
const hasTabs = $computed(() => props.tabs && props.tabs.length > 0);
|
const hasTabs = computed(() => props.tabs && props.tabs.length > 0);
|
||||||
const hasActions = $computed(() => props.actions && props.actions.length > 0);
|
const hasActions = computed(() => props.actions && props.actions.length > 0);
|
||||||
const show = $computed(() => {
|
const show = computed(() => {
|
||||||
return !hideTitle || hasTabs || hasActions;
|
return !hideTitle || hasTabs.value || hasActions.value;
|
||||||
});
|
});
|
||||||
|
|
||||||
const openAccountMenu = (ev: MouseEvent) => {
|
const openAccountMenu = (ev: MouseEvent) => {
|
||||||
|
@ -91,11 +91,11 @@ const openAccountMenu = (ev: MouseEvent) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const showTabsPopup = (ev: MouseEvent) => {
|
const showTabsPopup = (ev: MouseEvent) => {
|
||||||
if (!hasTabs) return;
|
if (!hasTabs.value) return;
|
||||||
if (!narrow) return;
|
if (!narrow.value) return;
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
const menu = props.tabs.map(tab => ({
|
const menu = props.tabs!.map(tab => ({
|
||||||
text: tab.title,
|
text: tab.title,
|
||||||
icon: tab.icon,
|
icon: tab.icon,
|
||||||
active: tab.key != null && tab.key === props.tab,
|
active: tab.key != null && tab.key === props.tab,
|
||||||
|
@ -111,7 +111,7 @@ const preventDrag = (ev: TouchEvent) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const onClick = () => {
|
const onClick = () => {
|
||||||
scrollToTop(el, { behavior: 'smooth' });
|
scrollToTop(el.value!, { behavior: 'smooth' });
|
||||||
};
|
};
|
||||||
|
|
||||||
function onTabMousedown(tab: Tab, ev: MouseEvent): void {
|
function onTabMousedown(tab: Tab, ev: MouseEvent): void {
|
||||||
|
@ -142,42 +142,42 @@ const calcBg = () => {
|
||||||
let ro: ResizeObserver | null;
|
let ro: ResizeObserver | null;
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
calcBg();
|
calcBg();
|
||||||
globalEvents.on('themeChanged', calcBg);
|
globalEvents.on('themeChanged', calcBg);
|
||||||
|
|
||||||
watch(() => [props.tab, props.tabs], () => {
|
|
||||||
nextTick(() => {
|
|
||||||
const tabEl = tabRefs[props.tab];
|
|
||||||
if (tabEl && tabHighlightEl) {
|
|
||||||
// offsetWidth や offsetLeft は少数を丸めてしまうため getBoundingClientRect を使う必要がある
|
|
||||||
// https://developer.mozilla.org/ja/docs/Web/API/HTMLElement/offsetWidth#%E5%80%A4
|
|
||||||
const tabSizeX = tabEl.scrollWidth + 20; // + the tab's padding
|
|
||||||
tabEl.style = `--width: ${tabSizeX}px`;
|
|
||||||
setTimeout(() => {
|
|
||||||
const parentRect = tabsEl.getBoundingClientRect();
|
|
||||||
const rect = tabEl.getBoundingClientRect();
|
|
||||||
const left = (rect.left - parentRect.left + tabsEl?.scrollLeft);
|
|
||||||
tabHighlightEl.style.width = tabSizeX + 'px';
|
|
||||||
tabHighlightEl.style.transform = `translateX(${left}px)`;
|
|
||||||
window.requestAnimationFrame(() => {
|
|
||||||
tabsEl?.scrollTo({left: left - 60, behavior: "smooth"});
|
|
||||||
})
|
|
||||||
}, 200);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, {
|
|
||||||
immediate: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (el && el.parentElement) {
|
const updateTabHighlight = () => {
|
||||||
narrow = el.parentElement.offsetWidth < 500;
|
nextTick(() => {
|
||||||
ro = new ResizeObserver((entries, observer) => {
|
const tabEl = tabRefs[props.tab];
|
||||||
if (el.parentElement && document.body.contains(el)) {
|
if (tabEl && tabHighlightEl) {
|
||||||
narrow = el.parentElement.offsetWidth < 500;
|
const tabSizeX = tabEl.scrollWidth + 20;
|
||||||
}
|
tabEl.style.setProperty('--width', `${tabSizeX}px`);
|
||||||
});
|
|
||||||
ro.observe(el.parentElement);
|
const parentRect = tabsEl.getBoundingClientRect();
|
||||||
}
|
const rect = tabEl.getBoundingClientRect();
|
||||||
|
const left = (rect.left - parentRect.left + tabsEl.scrollLeft);
|
||||||
|
tabHighlightEl.style.width = `${tabSizeX}px`;
|
||||||
|
tabHighlightEl.style.transform = `translateX(${left}px)`;
|
||||||
|
tabsEl.scrollTo({left: left - 60, behavior: "smooth"});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateTab = () => {
|
||||||
|
emit('update:tab', props.tab);
|
||||||
|
};
|
||||||
|
|
||||||
|
watch(() => [props.tab, props.tabs], updateTabHighlight, { immediate: true });
|
||||||
|
watch(() => props.tab, updateTab);
|
||||||
|
|
||||||
|
if (el && el.parentElement) {
|
||||||
|
narrow.value = el.parentElement.offsetWidth < 500;
|
||||||
|
ro = new ResizeObserver((entries, observer) => {
|
||||||
|
if (el.parentElement && document.body.contains(el)) {
|
||||||
|
narrow.value = el.parentElement.offsetWidth < 500;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ro.observe(el.parentElement);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
onUnmounted(() => {
|
onUnmounted(() => {
|
||||||
|
|
Loading…
Reference in a new issue