iceshrimp/packages/client/src/components/global/MkPageHeader.vue

534 lines
10 KiB
Vue
Raw Normal View History

<template>
2023-04-08 09:01:42 +09:00
<div
v-if="show"
ref="el"
class="fdidabkb"
:class="{ slim: narrow, thin: thin_ }"
:style="{ background: bg }"
@click="onClick"
>
<i
@click="goBack()"
v-if="props.displayBackButton"
v-tooltip.noDelay="i18n.ts.goBack"
class="icon backButton ph-caret-left ph-bold ph-lg"
></i>
2023-04-05 14:03:24 +09:00
<div v-if="narrow" class="buttons left" @click="openAccountMenu">
2023-04-08 09:01:42 +09:00
<MkAvatar
v-if="props.displayMyAvatar && $i"
class="avatar"
:user="$i"
:disable-preview="true"
/>
2023-04-05 14:03:24 +09:00
</div>
2023-04-08 09:01:42 +09:00
<template v-if="metadata">
<div
v-if="!hideTitle"
class="titleContainer"
@click="showTabsPopup"
>
<MkAvatar
v-if="metadata.avatar"
class="avatar"
:user="metadata.avatar"
:disable-preview="true"
:show-indicator="true"
/>
<i
v-else-if="metadata.icon && !narrow"
class="icon"
:class="metadata.icon"
></i>
2023-02-12 10:14:50 +09:00
<div class="title">
2023-04-08 09:01:42 +09:00
<MkUserName
v-if="metadata.userName"
:user="metadata.userName"
:nowrap="true"
class="title"
/>
<div
v-else-if="
metadata.title &&
!(tabs != null && tabs.length > 0 && narrow)
"
class="title"
>
{{ metadata.title }}
</div>
<div v-if="!narrow && metadata.subtitle" class="subtitle">
{{ metadata.subtitle }}
</div>
</div>
</div>
<div ref="tabsEl" v-if="hasTabs" class="tabs">
2023-04-08 09:01:42 +09:00
<button
v-for="tab in tabs"
:ref="(el) => (tabRefs[tab.key] = el)"
v-tooltip.noDelay="tab.title"
class="tab _button"
:class="{
active: tab.key != null && tab.key === props.tab,
}"
@mousedown="(ev) => onTabMousedown(tab, ev)"
@click="(ev) => onTabClick(tab, ev)"
>
<i v-if="tab.icon" class="icon" :class="tab.icon"></i>
<span class="title">{{ tab.title }}</span>
</button>
<div ref="tabHighlightEl" class="highlight"></div>
</div>
</template>
<div class="buttons right">
<template v-for="action in actions">
2023-04-08 09:01:42 +09:00
<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>
</div>
</div>
</template>
2023-02-12 10:14:50 +09:00
<script lang="ts" setup>
2023-04-08 09:01:42 +09:00
import {
computed,
onMounted,
onUnmounted,
ref,
inject,
watch,
shallowReactive,
nextTick,
reactive,
} from "vue";
import tinycolor from "tinycolor2";
import { popupMenu } from "@/os";
import { scrollToTop } from "@/scripts/scroll";
import { globalEvents } from "@/events";
import { injectPageMetadata } from "@/scripts/page-metadata";
import { $i, openAccountMenu as openAccountMenu_ } from "@/account";
import { i18n } from "@/i18n";
2023-02-12 10:14:50 +09:00
type Tab = {
key?: string | null;
title: string;
icon?: string;
iconOnly?: boolean;
onClick?: (ev: MouseEvent) => void;
};
const props = defineProps<{
tabs?: Tab[];
tab?: string;
actions?: {
text: string;
icon: string;
handler: (ev: MouseEvent) => void;
}[];
thin?: boolean;
displayMyAvatar?: boolean;
2023-04-05 13:59:11 +09:00
displayBackButton?: boolean;
2023-02-12 10:14:50 +09:00
}>();
const emit = defineEmits<{
2023-04-08 09:01:42 +09:00
(ev: "update:tab", key: string);
2023-02-12 10:14:50 +09:00
}>();
const metadata = injectPageMetadata();
2023-04-08 09:01:42 +09:00
const hideTitle = inject("shouldOmitHeaderTitle", false);
const thin_ = props.thin || inject("shouldHeaderThin", false);
2023-02-12 10:14:50 +09:00
const el = $ref<HTMLElement | null>(null);
const tabRefs = {};
const tabHighlightEl = $ref<HTMLElement | null>(null);
const tabsEl = $ref<HTMLElement | null>(null);
const bg = ref(null);
let narrow = $ref(false);
const height = ref(0);
const hasTabs = $computed(() => props.tabs && props.tabs.length > 0);
const hasActions = $computed(() => props.actions && props.actions.length > 0);
const show = $computed(() => {
return !hideTitle || hasTabs || hasActions;
});
const openAccountMenu = (ev: MouseEvent) => {
2023-04-08 09:01:42 +09:00
openAccountMenu_(
{
withExtraOperation: true,
},
ev
);
2023-02-12 10:14:50 +09:00
};
const showTabsPopup = (ev: MouseEvent) => {
if (!hasTabs) return;
if (!narrow) return;
ev.preventDefault();
ev.stopPropagation();
2023-04-08 09:01:42 +09:00
const menu = props.tabs.map((tab) => ({
2023-02-12 10:14:50 +09:00
text: tab.title,
icon: tab.icon,
active: tab.key != null && tab.key === props.tab,
action: (ev) => {
onTabClick(tab, ev);
},
}));
popupMenu(menu, ev.currentTarget ?? ev.target);
};
const preventDrag = (ev: TouchEvent) => {
ev.stopPropagation();
};
const onClick = () => {
2023-04-08 09:01:42 +09:00
scrollToTop(el, { behavior: "smooth" });
2023-02-12 10:14:50 +09:00
};
function onTabMousedown(tab: Tab, ev: MouseEvent): void {
// ユーザビリティの観点からmousedown時にはonClickは呼ばない
if (tab.key) {
2023-04-08 09:01:42 +09:00
emit("update:tab", tab.key);
2023-02-12 10:14:50 +09:00
}
}
function onTabClick(tab: Tab, ev: MouseEvent): void {
if (tab.onClick) {
2022-06-22 20:47:53 +09:00
ev.preventDefault();
ev.stopPropagation();
2023-02-12 10:14:50 +09:00
tab.onClick(ev);
2022-06-22 20:47:53 +09:00
}
2023-02-12 10:14:50 +09:00
if (tab.key) {
2023-04-08 09:01:42 +09:00
emit("update:tab", tab.key);
}
2023-02-12 10:14:50 +09:00
}
2023-04-05 14:41:57 +09:00
function goBack(): void {
window.history.back();
}
2023-02-12 10:14:50 +09:00
const calcBg = () => {
2023-04-08 09:01:42 +09:00
const rawBg = metadata?.bg || "var(--bg)";
const tinyBg = tinycolor(
rawBg.startsWith("var(")
? getComputedStyle(document.documentElement).getPropertyValue(
rawBg.slice(4, -1)
)
: rawBg
);
2023-02-12 10:14:50 +09:00
tinyBg.setAlpha(0.85);
bg.value = tinyBg.toRgbString();
};
let ro: ResizeObserver | null;
onMounted(() => {
calcBg();
2023-04-08 09:01:42 +09:00
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,
}
);
2023-02-12 10:14:50 +09:00
if (el && el.parentElement) {
narrow = el.parentElement.offsetWidth < 500;
ro = new ResizeObserver((entries, observer) => {
if (el.parentElement && document.body.contains(el)) {
narrow = el.parentElement.offsetWidth < 500;
}
});
ro.observe(el.parentElement);
}
});
onUnmounted(() => {
2023-04-08 09:01:42 +09:00
globalEvents.off("themeChanged", calcBg);
2023-02-12 10:14:50 +09:00
if (ro) ro.disconnect();
});
</script>
2023-02-12 10:14:50 +09:00
<style lang="scss" scoped>
2023-02-12 10:14:50 +09:00
.fdidabkb {
--height: 55px;
display: flex;
width: 100%;
-webkit-backdrop-filter: var(--blur, blur(15px));
backdrop-filter: var(--blur, blur(15px));
border-bottom: solid 0.5px var(--divider);
height: var(--height);
&.thin {
--height: 45px;
> .buttons {
> .button {
font-size: 0.9em;
}
}
2023-02-12 10:14:50 +09:00
}
&.slim {
> .titleContainer {
flex: 1;
margin: 0 auto;
> *:first-child {
margin-left: auto;
}
2023-02-12 10:14:50 +09:00
> *:last-child {
margin-right: auto;
}
2023-02-12 10:14:50 +09:00
}
> .tabs {
padding-inline: 12px;
2023-04-08 09:01:42 +09:00
mask: linear-gradient(
to right,
transparent,
black 10px 80%,
transparent
);
-webkit-mask: linear-gradient(
to right,
transparent,
black 10px 80%,
transparent
);
2023-03-01 07:12:29 +09:00
margin-left: -10px;
padding-left: 22px;
2023-02-12 10:14:50 +09:00
scrollbar-width: none;
2023-03-01 07:12:29 +09:00
&::before {
content: unset;
}
2023-02-12 10:14:50 +09:00
&::-webkit-scrollbar {
2023-02-09 19:09:40 +09:00
display: none;
}
2023-04-08 09:01:42 +09:00
&::after {
// Force right padding
2023-02-12 10:14:50 +09:00
content: "";
display: inline-block;
min-width: 20%;
}
2023-02-09 19:09:40 +09:00
}
2023-02-12 10:14:50 +09:00
}
> .buttons {
--margin: 8px;
display: flex;
align-items: center;
height: var(--height);
margin: 0 var(--margin);
&.left {
margin-right: auto;
> .avatar {
$size: 32px;
display: inline-block;
width: $size;
height: $size;
vertical-align: bottom;
margin: 0 8px;
pointer-events: none;
}
2023-02-12 10:14:50 +09:00
}
&.right {
margin-left: auto;
}
&:empty {
display: none;
}
> .button {
display: flex;
align-items: center;
justify-content: center;
height: calc(var(--height) - (var(--margin) * 2));
width: calc(var(--height) - (var(--margin) * 2));
box-sizing: border-box;
position: relative;
border-radius: 5px;
&:hover {
background: rgba(0, 0, 0, 0.05);
}
&.highlighted {
color: var(--accent);
}
2023-02-12 10:14:50 +09:00
}
> .fullButton {
& + .fullButton {
margin-left: 12px;
}
}
}
2023-04-05 14:12:55 +09:00
> .backButton {
display: flex;
align-items: center;
justify-content: center;
margin-left: 1rem;
}
2023-02-12 10:14:50 +09:00
> .titleContainer {
display: flex;
align-items: center;
max-width: 400px;
overflow: auto;
white-space: nowrap;
text-align: left;
font-weight: bold;
flex-shrink: 0;
margin-left: 24px;
margin-right: 1rem;
> .avatar {
$size: 32px;
display: inline-block;
width: $size;
height: $size;
vertical-align: bottom;
margin: 0 8px;
pointer-events: none;
}
> .icon {
margin-right: 8px;
width: 16px;
text-align: center;
transform: translate(0em);
}
> .title {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
line-height: 1.1;
> .subtitle {
opacity: 0.6;
font-size: 0.8em;
font-weight: normal;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
2023-02-12 10:14:50 +09:00
&.activeTab {
text-align: center;
> .chevron {
display: inline-block;
margin-left: 6px;
}
}
}
}
2023-02-12 10:14:50 +09:00
}
> .tabs {
position: relative;
width: 100%;
font-size: 1em;
overflow-x: auto;
white-space: nowrap;
contain: strict;
2023-02-12 10:14:50 +09:00
2023-03-01 07:12:29 +09:00
&::before {
content: "";
display: inline-block;
height: 40%;
border-left: 1px solid var(--divider);
margin-right: 1em;
margin-left: 10px;
vertical-align: -1px;
}
2023-02-12 10:14:50 +09:00
> .tab {
display: inline-flex;
align-items: center;
position: relative;
border-inline: 10px solid transparent;
2023-02-12 10:14:50 +09:00
height: 100%;
font-weight: normal;
opacity: 0.7;
width: 38px;
--width: 38px;
overflow: hidden;
2023-04-08 09:01:42 +09:00
transition: color 0.2s, opacity 0.2s, width 0.2s;
2023-02-12 10:14:50 +09:00
&:hover {
opacity: 1;
}
2023-02-12 10:14:50 +09:00
&.active {
opacity: 1;
color: var(--accent);
font-weight: 600;
width: var(--width);
}
&:not(.active) > .title {
opacity: 0;
2023-02-12 10:14:50 +09:00
}
> .icon + .title {
margin-left: 8px;
}
2023-02-12 10:14:50 +09:00
> .title {
2023-04-08 09:01:42 +09:00
transition: opacity 0.2s;
}
2023-02-12 10:14:50 +09:00
}
> .highlight {
position: absolute;
bottom: 0;
left: 0;
2023-02-12 10:14:50 +09:00
height: 3px;
background: var(--accent);
border-radius: 999px;
2023-04-08 09:01:42 +09:00
transition: width 0.2s, transform 0.2s;
transition-timing-function: cubic-bezier(0, 0, 0, 1.2);
2023-02-12 10:14:50 +09:00
pointer-events: none;
}
}
2023-02-12 10:14:50 +09:00
}
</style>