index.uvue 5.27 KB
<template>
	<view class="app-navbar-root">
		<!-- fixed 时占位,避免内容顶到屏幕最上(总高 = 安全区 + 导航条) -->
		<view
			v-if="fixed && placeholder"
			class="app-navbar-placeholder"
			:style="placeholderStyle"
		></view>
		<view class="app-navbar-shell" :class="shellClass" :style="shellStyle">
			<view
				v-if="safeAreaInsetTop"
				class="app-navbar-safe-top"
				:style="safeTopStyle"
			></view>
			<view
				class="app-navbar-bar"
				:class="barBorderClass"
				:style="barRowStyle"
			>
				<view
					v-if="showBack"
					class="app-navbar-left"
					@tap="onLeftTap"
				>
					<uni-icons
						:color="leftIconColor"
						:size="leftIconSizeNum"
						:type="leftIcon"
					></uni-icons>
					<text
						v-if="leftText != ''"
						class="app-navbar-left-text"
					>{{ leftText }}</text>
				</view>
				<view v-else class="app-navbar-left-spacer"></view>
				<view class="app-navbar-center">
					<slot name="center">
						<text
							v-if="title != ''"
							class="app-navbar-title"
						>{{ title }}</text>
					</slot>
				</view>
				<view class="app-navbar-right">
					<slot name="right"></slot>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="uts">
import { computed, onMounted, ref } from "vue"
import { calcWindowTopInsetPx } from "@/utils/safe-area.uts"
import { router } from "@/utils/router.uts"

type AppNavbarProps = {
	title?: string
	/** 是否显示左侧返回区(图标 ± 文案) */
	showBack?: boolean
	/** 为 true 时点击左侧默认 router.back()(与项目路由钩子一致) */
	autoBack?: boolean
	safeAreaInsetTop?: boolean
	bgColor?: string
	border?: boolean
	fixed?: boolean
	placeholder?: boolean
	/** 导航条内容区高度(px),不含安全区 */
	barHeight?: number
	leftIcon?: string
	leftIconColor?: string
	leftIconSize?: number
	leftText?: string
	/** fixed 时壳层 z-index;不传默认 200(详情页全屏时可传 0 压低层级) */
	zIndex?: number | null
}

const props = withDefaults(defineProps<AppNavbarProps>(), {
	title: "",
	showBack: true,
	autoBack: true,
	safeAreaInsetTop: true,
	bgColor: "#ffffff",
	border: false,
	fixed: false,
	placeholder: false,
	barHeight: 48,
	leftIcon: "left",
	leftIconColor: "#000000",
	leftIconSize: 20,
	leftText: "",
	zIndex: null
})

const emit = defineEmits<{
	leftClick: []
}>()

const insetTopPx = ref(0)

function refreshInset(): void {
	insetTopPx.value = calcWindowTopInsetPx(uni.getWindowInfo())
}

onMounted(() => {
	refreshInset()
})

const barHeightPx = computed((): number => {
	const h = props.barHeight
	return h > 0 ? h : 48
})

const totalHeightPx = computed((): number => {
	const safe = props.safeAreaInsetTop ? insetTopPx.value : 0
	return safe + barHeightPx.value
})

const placeholderStyle = computed((): UTSJSONObject => {
	return {
		height: `${totalHeightPx.value}px`,
		width: "100%"
	} as UTSJSONObject
})

const shellClass = computed((): string => {
	return props.fixed ? "app-navbar-shell--fixed" : ""
})

const shellStyle = computed((): UTSJSONObject => {
	const style = {
		backgroundColor: props.bgColor,
		width: "100%"
	} as UTSJSONObject
	if (props.fixed) {
		style["position"] = "fixed"
		style["top"] = "0"
		style["left"] = "0"
		const zi = props.zIndex
		style["zIndex"] = zi != null ? zi : 200
	}
	return style
})

const safeTopStyle = computed((): UTSJSONObject => {
	return {
		height: `${insetTopPx.value}px`,
		width: "100%"
	} as UTSJSONObject
})

const barRowStyle = computed((): UTSJSONObject => {
	return {
		height: `${barHeightPx.value}px`,
		width: "100%"
	} as UTSJSONObject
})

const barBorderClass = computed((): string => {
	return props.border ? "app-navbar-bar--border" : ""
})

const leftIconSizeNum = computed((): number => {
	const s = props.leftIconSize
	return s > 0 ? s : 20
})

function onLeftTap(): void {
	emit("leftClick")
	if (props.autoBack) {
		router.back(null)
	}
}
</script>

<style>
.app-navbar-root {
	width: 100%;
	display: flex;
	flex-direction: column;
}

.app-navbar-placeholder {
	flex-shrink: 0;
}

.app-navbar-shell {
	display: flex;
	flex-direction: column;
	flex-shrink: 0;
}

.app-navbar-shell--fixed {
	width: 100%;
}

.app-navbar-safe-top {
	flex-shrink: 0;
	width: 100%;
}

.app-navbar-bar {
	display: flex;
	flex-direction: row;
	flex-shrink: 0;
	align-items: center;
	justify-content: space-between;
	box-sizing: border-box;
	padding-left: 4px;
	padding-right: 8px;
}

.app-navbar-bar--border {
	border-bottom-width: 1px;
	border-bottom-style: solid;
	border-bottom-color: #eeeeee;
}

.app-navbar-left {
	width: 48px;
	height: 100%;
	display: flex;
	flex-direction: row;
	flex-shrink: 0;
	align-items: center;
	justify-content: center;
}

.app-navbar-left-text {
	margin-left: 2px;
	font-size: 15px;
	color: #000000;
}

.app-navbar-left-spacer {
	width: 48px;
	flex-shrink: 0;
	height: 100%;
}

.app-navbar-center {
	flex: 1;
	height: 100%;
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: center;
	overflow: hidden;
	padding-left: 8px;
	padding-right: 8px;
}

.app-navbar-title {
	font-size: 17px;
	font-weight: 500;
	color: #000000;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	width: 100%;
}

.app-navbar-right {
	min-width: 48px;
	height: 100%;
	display: flex;
	flex-direction: row;
	flex-shrink: 0;
	align-items: center;
	justify-content: flex-end;
}
</style>