Hide inactive tab labels on mobile (#9609)

Co-authored-by: Freeplay <Freeplay@duck.com>
Reviewed-on: https://codeberg.org/calckey/calckey/pulls/9609
Co-authored-by: Free <freeplay@duck.com>
Co-committed-by: Free <freeplay@duck.com>
This commit is contained in:
Free 2023-02-12 01:11:11 +00:00 committed by Kainoa Kanter
parent 5c88cef03b
commit 474a02bfe0

View file

@ -1,5 +1,5 @@
<template> <template>
<div v-if="show" ref="el" class="fdidabkb" :class="{ slim: narrow, thin: thin_ }" :style="{ background: bg }" @click="onClick"> <div v-if="show" ref="el" class="fdidabkb" :class="{ slim: narrow, thin: thin_ }" :style="{ background: bg }" @click="onClick">
<div v-if="narrow" class="buttons left" @click="openAccountMenu"> <div v-if="narrow" class="buttons left" @click="openAccountMenu">
<MkAvatar v-if="props.displayMyAvatar && $i" class="avatar" :user="$i" :disable-preview="true"/> <MkAvatar v-if="props.displayMyAvatar && $i" class="avatar" :user="$i" :disable-preview="true"/>
</div> </div>
@ -29,30 +29,30 @@
<button v-tooltip.noDelay="action.text" class="_button button" :class="{ highlighted: action.highlighted }" @click.stop="action.handler" @touchstart="preventDrag"><i :class="action.icon"></i></button> <button v-tooltip.noDelay="action.text" class="_button button" :class="{ highlighted: action.highlighted }" @click.stop="action.handler" @touchstart="preventDrag"><i :class="action.icon"></i></button>
</template> </template>
</div> </div>
</div> </div>
</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, reactive } 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';
import { i18n } from '@/i18n'; import { i18n } from '@/i18n';
import { globalEvents } from '@/events'; import { globalEvents } from '@/events';
import { deviceKind } from '@/scripts/device-kind'; import { deviceKind } from '@/scripts/device-kind';
import { isTouchUsing } from '@/scripts/touch'; import { isTouchUsing } from '@/scripts/touch';
import { injectPageMetadata } from '@/scripts/page-metadata'; import { injectPageMetadata } from '@/scripts/page-metadata';
import { $i, openAccountMenu as openAccountMenu_ } from '@/account'; import { $i, openAccountMenu as openAccountMenu_ } from '@/account';
type Tab = { type Tab = {
key?: string | null; key?: string | null;
title: string; title: string;
icon?: string; icon?: string;
iconOnly?: boolean; iconOnly?: boolean;
onClick?: (ev: MouseEvent) => void; onClick?: (ev: MouseEvent) => void;
}; };
const props = defineProps<{ const props = defineProps<{
tabs?: Tab[]; tabs?: Tab[];
tab?: string; tab?: string;
actions?: { actions?: {
@ -62,37 +62,37 @@ const props = defineProps<{
}[]; }[];
thin?: boolean; thin?: boolean;
displayMyAvatar?: boolean; displayMyAvatar?: boolean;
}>(); }>();
const emit = defineEmits<{ const emit = defineEmits<{
(ev: 'update:tab', key: string); (ev: 'update:tab', key: string);
}>(); }>();
const metadata = injectPageMetadata(); 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 = {};
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(null);
let narrow = $ref(false); let 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 || hasActions;
}); });
const openAccountMenu = (ev: MouseEvent) => { const openAccountMenu = (ev: MouseEvent) => {
openAccountMenu_({ openAccountMenu_({
withExtraOperation: true, withExtraOperation: true,
}, ev); }, ev);
}; };
const showTabsPopup = (ev: MouseEvent) => { const showTabsPopup = (ev: MouseEvent) => {
if (!hasTabs) return; if (!hasTabs) return;
if (!narrow) return; if (!narrow) return;
ev.preventDefault(); ev.preventDefault();
@ -106,24 +106,24 @@ const showTabsPopup = (ev: MouseEvent) => {
}, },
})); }));
popupMenu(menu, ev.currentTarget ?? ev.target); popupMenu(menu, ev.currentTarget ?? ev.target);
}; };
const preventDrag = (ev: TouchEvent) => { const preventDrag = (ev: TouchEvent) => {
ev.stopPropagation(); ev.stopPropagation();
}; };
const onClick = () => { const onClick = () => {
scrollToTop(el, { behavior: 'smooth' }); scrollToTop(el, { behavior: 'smooth' });
}; };
function onTabMousedown(tab: Tab, ev: MouseEvent): void { function onTabMousedown(tab: Tab, ev: MouseEvent): void {
// mousedownonClick // mousedownonClick
if (tab.key) { if (tab.key) {
emit('update:tab', tab.key); emit('update:tab', tab.key);
} }
} }
function onTabClick(tab: Tab, ev: MouseEvent): void { function onTabClick(tab: Tab, ev: MouseEvent): void {
if (tab.onClick) { if (tab.onClick) {
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
@ -132,18 +132,18 @@ function onTabClick(tab: Tab, ev: MouseEvent): void {
if (tab.key) { if (tab.key) {
emit('update:tab', tab.key); emit('update:tab', tab.key);
} }
} }
const calcBg = () => { const calcBg = () => {
const rawBg = metadata?.bg || 'var(--bg)'; const rawBg = metadata?.bg || 'var(--bg)';
const tinyBg = tinycolor(rawBg.startsWith('var(') ? getComputedStyle(document.documentElement).getPropertyValue(rawBg.slice(4, -1)) : rawBg); const tinyBg = tinycolor(rawBg.startsWith('var(') ? getComputedStyle(document.documentElement).getPropertyValue(rawBg.slice(4, -1)) : rawBg);
tinyBg.setAlpha(0.85); tinyBg.setAlpha(0.85);
bg.value = tinyBg.toRgbString(); bg.value = tinyBg.toRgbString();
}; };
let ro: ResizeObserver | null; let ro: ResizeObserver | null;
onMounted(() => { onMounted(() => {
calcBg(); calcBg();
globalEvents.on('themeChanged', calcBg); globalEvents.on('themeChanged', calcBg);
@ -153,12 +153,14 @@ onMounted(() => {
if (tabEl && tabHighlightEl) { if (tabEl && tabHighlightEl) {
// offsetWidth offsetLeft getBoundingClientRect 使 // offsetWidth offsetLeft getBoundingClientRect 使
// https://developer.mozilla.org/ja/docs/Web/API/HTMLElement/offsetWidth#%E5%80%A4 // https://developer.mozilla.org/ja/docs/Web/API/HTMLElement/offsetWidth#%E5%80%A4
tabEl.addEventListener("transitionend", () => {
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 = rect.width + 'px'; tabHighlightEl.style.width = rect.width + 'px';
tabHighlightEl.style.left = left + 'px'; tabHighlightEl.style.left = left + 'px';
tabsEl.scrollTo({left: left - 80, behavior: "smooth"}); tabsEl?.scrollTo({left: left - 80, behavior: "smooth"});
})
} }
}); });
}, { }, {
@ -174,16 +176,16 @@ onMounted(() => {
}); });
ro.observe(el.parentElement); ro.observe(el.parentElement);
} }
}); });
onUnmounted(() => { onUnmounted(() => {
globalEvents.off('themeChanged', calcBg); globalEvents.off('themeChanged', calcBg);
if (ro) ro.disconnect(); if (ro) ro.disconnect();
}); });
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.fdidabkb { .fdidabkb {
--height: 55px; --height: 55px;
display: flex; display: flex;
width: 100%; width: 100%;
@ -229,6 +231,14 @@ onUnmounted(() => {
display: inline-block; display: inline-block;
min-width: 20%; min-width: 20%;
} }
> .tab {
&:not(.active) > .title {
font-size: 0;
opacity: 0;
margin-inline: 0;
transition: font-size .2s, opacity .1s;
}
}
} }
} }
@ -373,6 +383,9 @@ onUnmounted(() => {
> .icon + .title { > .icon + .title {
margin-left: 8px; margin-left: 8px;
} }
> .title {
transition: font-size .2s, opacity .2s .15s;
}
} }
> .highlight { > .highlight {
@ -381,9 +394,10 @@ onUnmounted(() => {
height: 3px; height: 3px;
background: var(--accent); background: var(--accent);
border-radius: 999px; border-radius: 999px;
transition: all 0.2s ease; transition: width .2s, left .2s;
transition-timing-function: cubic-bezier(0,0,0,1.2);
pointer-events: none; pointer-events: none;
} }
} }
} }
</style> </style>