底部安全距离

This commit is contained in:
liu
2026-07-01 23:42:58 +08:00
parent 0a1c8cdff1
commit cdd91f083b
9 changed files with 31 additions and 21 deletions
+24 -4
View File
@@ -5,7 +5,6 @@ import PublishPopup from '@/components/PublishPopup'
import type { PublishOption } from '@/components/PublishPopup' import type { PublishOption } from '@/components/PublishPopup'
import './index.less' import './index.less'
/** Tab 项配置(顺序与 app.config.ts tabBar.list 一致) */
interface TabItem { interface TabItem {
key: string key: string
label: string label: string
@@ -21,13 +20,32 @@ const TAB_LIST: TabItem[] = [
] ]
interface CustomTabBarProps { interface CustomTabBarProps {
/** 当前激活的 tab key由页面传入 */ /** 当前激活的 tab key。H5 由页面传入;小程序由 Taro 自动渲染,props 为空 */
activeKey: string activeKey?: string
}
/**
* 从小程序路由推导激活 tabTaro 自动渲染 custom-tab-bar 时不传 props
*/
function deriveActiveKey(): string {
try {
const pages = Taro.getCurrentPages()
if (pages.length > 0) {
const route = pages[pages.length - 1]?.route || ''
for (const tab of TAB_LIST) {
if (route.includes(tab.key + '/index')) return tab.key
}
}
} catch { /* ignore */ }
return 'index'
} }
/** 获取底部安全距离 */ /** 获取底部安全距离 */
function getSafeBottom(): number { function getSafeBottom(): number {
try { try {
if(process.env.TARO_ENV === 'h5') {
return 0
}
const info = Taro.getSystemInfoSync() const info = Taro.getSystemInfoSync()
if (info.safeArea) { if (info.safeArea) {
return info.screenHeight - info.safeArea.bottom return info.screenHeight - info.safeArea.bottom
@@ -36,7 +54,9 @@ function getSafeBottom(): number {
return 0 return 0
} }
export default function CustomTabBar({ activeKey }: CustomTabBarProps) { export default function CustomTabBar({ activeKey: propKey }: CustomTabBarProps) {
// H5 走 props,小程序自动渲染时无 props,从路由推导
const activeKey = propKey || deriveActiveKey()
const safeBottom = getSafeBottom() const safeBottom = getSafeBottom()
const [publishVisible, setPublishVisible] = useState(false) const [publishVisible, setPublishVisible] = useState(false)
+1
View File
@@ -2,4 +2,5 @@ export default definePageConfig({
navigationBarTitleText: '首页', navigationBarTitleText: '首页',
navigationStyle: 'custom', navigationStyle: 'custom',
enablePullDownRefresh: false, enablePullDownRefresh: false,
usingComponents: {},
}) })
+1 -1
View File
@@ -165,7 +165,7 @@ export default function Index() {
<View className='bottom-safe' /> <View className='bottom-safe' />
</ScrollView> </ScrollView>
<CustomTabBar activeKey='index' /> {/*<CustomTabBar activeKey='index' />*/}
</View> </View>
) )
} }
+1 -1
View File
@@ -1,4 +1,4 @@
export default definePageConfig({ export default definePageConfig({
navigationBarTitleText: '消息', navigationBarTitleText: '消息',
navigationStyle: 'custom', usingComponents: {},
}) })
+1 -4
View File
@@ -1,7 +1,6 @@
import Taro from '@tarojs/taro' import Taro from '@tarojs/taro'
import { View, Text, Image, ScrollView } from '@tarojs/components' import { View, Text, Image, ScrollView } from '@tarojs/components'
import { Badge } from '@antmjs/vantui' import { Badge } from '@antmjs/vantui'
import CustomTabBar from '@/custom-tab-bar'
import { chatList } from '@/mock/messages' import { chatList } from '@/mock/messages'
import type { ChatItem } from '@/types/message' import type { ChatItem } from '@/types/message'
import './index.less' import './index.less'
@@ -86,7 +85,7 @@ export default function Messages() {
{/* 未读数 */} {/* 未读数 */}
{msg.unread ? ( {msg.unread ? (
<Badge content={msg.unread} max={99} /> <Badge content={msg.unread} />
) : null} ) : null}
</View> </View>
))} ))}
@@ -94,8 +93,6 @@ export default function Messages() {
<View className='bottom-safe' /> <View className='bottom-safe' />
</ScrollView> </ScrollView>
<CustomTabBar activeKey='messages' />
</View> </View>
) )
} }
+1
View File
@@ -1,4 +1,5 @@
export default definePageConfig({ export default definePageConfig({
navigationBarTitleText: '我的', navigationBarTitleText: '我的',
navigationStyle: 'custom', navigationStyle: 'custom',
usingComponents: {},
}) })
-4
View File
@@ -2,7 +2,6 @@ import { useMemo } from 'react'
import Taro from '@tarojs/taro' import Taro from '@tarojs/taro'
import { View, Text, Image, ScrollView } from '@tarojs/components' import { View, Text, Image, ScrollView } from '@tarojs/components'
import { Cell, CellGroup, Icon } from '@antmjs/vantui' import { Cell, CellGroup, Icon } from '@antmjs/vantui'
import CustomTabBar from '@/custom-tab-bar'
import './index.less' import './index.less'
/** 状态栏高度 */ /** 状态栏高度 */
@@ -155,9 +154,6 @@ export default function Profile() {
<Text className='footer-version'> v1.0.0</Text> <Text className='footer-version'> v1.0.0</Text>
</View> </View>
</ScrollView> </ScrollView>
{/* ========== TabBar ========== */}
<CustomTabBar activeKey='profile' />
</View> </View>
) )
} }
+1 -2
View File
@@ -1,5 +1,4 @@
export default definePageConfig({ export default definePageConfig({
navigationBarTitleText: '校园任务', navigationBarTitleText: '校园任务',
navigationStyle: 'custom', usingComponents: {},
"usingComponents": {}
}) })
+1 -5
View File
@@ -1,11 +1,9 @@
import { useState, useMemo } from 'react' import { useState, useMemo } from 'react'
import Taro from '@tarojs/taro' import Taro from '@tarojs/taro'
import { View, ScrollView } from '@tarojs/components' import { View, ScrollView } from '@tarojs/components'
import { Search, Tabs, Tab } from '@antmjs/vantui' import { Search } from '@antmjs/vantui'
import TaskCard from '@/components/TaskCard' import TaskCard from '@/components/TaskCard'
import CustomTabBar from '@/custom-tab-bar'
import { tasks } from '@/mock/tasks' import { tasks } from '@/mock/tasks'
import type { TaskType } from '@/types/task'
import './index.less' import './index.less'
/** 筛选标签 */ /** 筛选标签 */
@@ -100,8 +98,6 @@ export default function Tasks() {
</View> </View>
<View className='bottom-safe' /> <View className='bottom-safe' />
</ScrollView> </ScrollView>
<CustomTabBar activeKey='tasks' />
</View> </View>
) )
} }