perf: MkPageHeader perf

This commit is contained in:
ThatOneCalculator 2023-02-17 11:40:12 -08:00
parent cdf9a03604
commit 4ed1b80d68
No known key found for this signature in database
GPG Key ID: 8703CACD01000000
1 changed files with 50 additions and 50 deletions

View File

@ -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], () => { const updateTabHighlight = () => {
nextTick(() => { nextTick(() => {
const tabEl = tabRefs[props.tab]; const tabEl = tabRefs[props.tab];
if (tabEl && tabHighlightEl) { if (tabEl && tabHighlightEl) {
// offsetWidth offsetLeft getBoundingClientRect 使 const tabSizeX = tabEl.scrollWidth + 20;
// https://developer.mozilla.org/ja/docs/Web/API/HTMLElement/offsetWidth#%E5%80%A4 tabEl.style.setProperty('--width', `${tabSizeX}px`);
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 parentRect = tabsEl.getBoundingClientRect();
narrow = el.parentElement.offsetWidth < 500; const rect = tabEl.getBoundingClientRect();
ro = new ResizeObserver((entries, observer) => { const left = (rect.left - parentRect.left + tabsEl.scrollLeft);
if (el.parentElement && document.body.contains(el)) { tabHighlightEl.style.width = `${tabSizeX}px`;
narrow = el.parentElement.offsetWidth < 500; tabHighlightEl.style.transform = `translateX(${left}px)`;
} tabsEl.scrollTo({left: left - 60, behavior: "smooth"});
}); }
ro.observe(el.parentElement); });
} };
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(() => {