perf: ⚡ MkPageHeader perf
This commit is contained in:
parent
cdf9a03604
commit
4ed1b80d68
|
@ -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 {
|
||||||
|
@ -145,35 +145,35 @@ 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 parentRect = tabsEl.getBoundingClientRect();
|
||||||
const rect = tabEl.getBoundingClientRect();
|
const rect = tabEl.getBoundingClientRect();
|
||||||
const left = (rect.left - parentRect.left + tabsEl?.scrollLeft);
|
const left = (rect.left - parentRect.left + tabsEl.scrollLeft);
|
||||||
tabHighlightEl.style.width = tabSizeX + 'px';
|
tabHighlightEl.style.width = `${tabSizeX}px`;
|
||||||
tabHighlightEl.style.transform = `translateX(${left}px)`;
|
tabHighlightEl.style.transform = `translateX(${left}px)`;
|
||||||
window.requestAnimationFrame(() => {
|
tabsEl.scrollTo({left: left - 60, behavior: "smooth"});
|
||||||
tabsEl?.scrollTo({left: left - 60, behavior: "smooth"});
|
|
||||||
})
|
|
||||||
}, 200);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, {
|
};
|
||||||
immediate: true,
|
|
||||||
});
|
const updateTab = () => {
|
||||||
|
emit('update:tab', props.tab);
|
||||||
|
};
|
||||||
|
|
||||||
|
watch(() => [props.tab, props.tabs], updateTabHighlight, { immediate: true });
|
||||||
|
watch(() => props.tab, updateTab);
|
||||||
|
|
||||||
if (el && el.parentElement) {
|
if (el && el.parentElement) {
|
||||||
narrow = el.parentElement.offsetWidth < 500;
|
narrow.value = el.parentElement.offsetWidth < 500;
|
||||||
ro = new ResizeObserver((entries, observer) => {
|
ro = new ResizeObserver((entries, observer) => {
|
||||||
if (el.parentElement && document.body.contains(el)) {
|
if (el.parentElement && document.body.contains(el)) {
|
||||||
narrow = el.parentElement.offsetWidth < 500;
|
narrow.value = el.parentElement.offsetWidth < 500;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ro.observe(el.parentElement);
|
ro.observe(el.parentElement);
|
||||||
|
|
Loading…
Reference in New Issue