index.uvue
5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<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>