mall-app-t/peach/ui/pb-navbar/pb-navbar.vue

426 lines
9.7 KiB
Vue
Raw Permalink Normal View History

2024-05-22 15:42:13 +08:00
<!-- 自定义导航栏 -->
<template>
2024-08-27 17:57:03 +08:00
<view class="uni-navbar" :class="{ 'uni-dark': dark }">
<view
:class="{
'uni-navbar--fixed': fixed,
'uni-navbar--shadow': shadow,
'uni-navbar--border': border,
}"
class="uni-navbar__content"
>
<view class="fixed-bg" :class="[opacity ? '' : opacityBgUi]"></view>
<pb-status-bar v-if="statusBar" />
<view
:style="{
color: themeColor,
height: navbarHeight,
background: backgroundColor,
}"
class="uni-navbar__header"
>
<view class="uni-navbar__header-btns uni-navbar__header-btns-left" :style="{ width: leftIconWidth }">
<slot name="left">
<view class="uni-navbar__content_view" v-if="leftIcon.length > 0">
<view class="icon-box ss-flex">
<view class="icon-button icon-button-left ss-flex ss-row-center" @tap="onClickLeft">
<text class="sicon-back" v-if="hasHistory" />
<text class="sicon-home" v-else />
2024-05-22 15:42:13 +08:00
</view>
2024-08-27 17:57:03 +08:00
<view class="line"></view>
<view class="icon-button icon-button-right ss-flex ss-row-center" @tap="showMenuTools">
<text class="sicon-more" />
2024-05-22 15:42:13 +08:00
</view>
2024-08-27 17:57:03 +08:00
</view>
2024-05-22 15:42:13 +08:00
</view>
2024-08-27 17:57:03 +08:00
<view
:class="{ 'uni-navbar-btn-icon-left': !leftIcon.length > 0 }"
class="uni-navbar-btn-text"
v-if="titleAlign === 'left' && title.length && peach.$platform.name !== 'WechatOfficialAccount'"
>
<text :style="{ color: themeColor, fontSize: '18px' }">{{ title }}</text>
</view>
</slot>
2024-05-22 15:42:13 +08:00
</view>
2024-08-27 17:57:03 +08:00
<view v-if="tools === 'search'" class="ss-flex-1">
<slot name="center">
<uni-search-bar
class="ss-flex-1 search-box"
:radius="20"
placeholder="请输入关键词"
cancelButton="none"
v-model="searchModel"
@confirm="onSearch"
/>
</slot>
2024-05-22 15:42:13 +08:00
</view>
2024-08-27 17:57:03 +08:00
<view v-else class="uni-navbar__header-container" @tap="onClickTitle">
<slot name="center">
<view
v-if="tools === 'title' && titleAlign === 'center' && title.length"
class="uni-navbar__header-container-inner"
>
<text :style="{ color: themeColor, fontSize: '36rpx' }" class="ss-line-1">{{ title }}</text>
</view>
</slot>
</view>
</view>
</view>
<view class="uni-navbar__placeholder" v-if="placeholder">
<pb-status-bar v-if="statusBar" />
<view class="uni-navbar__placeholder-view" :style="{ height: navbarHeight }" />
2024-05-22 15:42:13 +08:00
</view>
2024-08-27 17:57:03 +08:00
</view>
2024-05-22 15:42:13 +08:00
</template>
<script setup>
import peach from '@/peach'
import { onLoad } from '@dcloudio/uni-app'
import { showMenuTools } from '@/peach/hooks/useModal'
import { computed } from 'vue'
/**
* NavBar 自定义导航栏
* @description 导航栏组件主要用于头部导航
* @property {Boolean} dark 开启黑暗模式
* @property {String} title 标题文字
* @property {String} rightText 右侧按钮文本
* @property {String} leftIcon 左侧按钮图标
* @property {String} rightIcon 右侧按钮图标
* @property {String} color 图标和文字颜色
* @property {String} backgroundColor 导航栏背景颜色
* @property {Boolean} fixed = [true|false] 是否固定顶部
* @property {Boolean} statusBar = [true|false] 是否包含状态栏
* @property {Boolean} shadow = [true|false] 导航栏下是否有阴影
* @event {Function} clickLeft 左侧按钮点击时触发
* @event {Function} clickRight 右侧按钮点击时触发
* @event {Function} clickTitle 中间标题点击时触发
*/
const getVal = (val) => (typeof val === 'number' ? val + 'px' : val)
const emits = defineEmits(['clickLeft', 'clickRight', 'clickTitle', 'search'])
const props = defineProps({
2024-08-27 17:57:03 +08:00
dark: {
type: Boolean,
default: false,
},
modelValue: {
type: String,
default: '',
},
title: {
type: String,
default: '',
},
// left | center
titleAlign: {
type: String,
default: 'center',
},
rightText: {
type: String,
default: '',
},
leftIcon: {
type: String,
default: 'left',
},
rightIcon: {
type: String,
default: '',
},
fixed: {
type: [Boolean, String],
default: true,
},
placeholder: {
type: [Boolean, String],
default: true,
},
color: {
type: String,
default: '',
},
backgroundColor: {
type: String,
default: '',
},
opacity: {
type: [Boolean, String],
default: false,
},
opacityBgUi: {
type: String,
default: 'bg-white',
},
statusBar: {
type: [Boolean, String],
default: false,
},
shadow: {
type: [Boolean, String],
default: false,
},
border: {
type: [Boolean, String],
default: false,
},
height: {
type: [Number, String],
default: 44,
},
leftWidth: {
type: [Number, String],
default: 80,
},
rightWidth: {
type: [Number, String],
default: 0,
},
tools: {
type: String,
default: 'title',
},
defaultSearch: {
type: String,
default: '',
},
2024-05-22 15:42:13 +08:00
})
const searchModel = computed(() => {
2024-08-27 17:57:03 +08:00
return props.defaultSearch
2024-05-22 15:42:13 +08:00
})
const themeColor = computed(() => {
2024-08-27 17:57:03 +08:00
if (props.dark) {
return props.color ? props.color : '#fff'
}
return props.color || '#333'
2024-05-22 15:42:13 +08:00
})
const navbarHeight = computed(() => {
2024-08-27 17:57:03 +08:00
return getVal(props.height)
2024-05-22 15:42:13 +08:00
})
const leftIconWidth = computed(() => {
2024-08-27 17:57:03 +08:00
return getVal(props.leftWidth)
2024-05-22 15:42:13 +08:00
})
function onSearch(e) {
2024-08-27 17:57:03 +08:00
emits('search', e.value)
2024-05-22 15:42:13 +08:00
}
onLoad(() => {
2024-08-27 17:57:03 +08:00
if (uni.report && props.title !== '') {
uni.report('title', props.title)
}
2024-05-22 15:42:13 +08:00
})
const hasHistory = peach.$router.hasHistory()
function onClickLeft() {
2024-08-27 17:57:03 +08:00
if (hasHistory) {
peach.$router.back()
} else {
peach.$router.go('/pages/index/index')
}
emits('clickLeft')
2024-05-22 15:42:13 +08:00
}
function onClickTitle() {
2024-08-27 17:57:03 +08:00
emits('clickTitle')
2024-05-22 15:42:13 +08:00
}
</script>
<style lang="scss" scoped>
.bg-main {
2024-08-27 17:57:03 +08:00
background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient)) !important;
color: #fff !important;
2024-05-22 15:42:13 +08:00
}
.icon-box {
2024-08-27 17:57:03 +08:00
background: #ffffff;
box-shadow: 0px 0px 4rpx rgba(51, 51, 51, 0.08), 0px 4rpx 6rpx 2rpx rgba(102, 102, 102, 0.12);
border-radius: 30rpx;
width: 134rpx;
height: 56rpx;
margin-left: 8rpx;
.line {
width: 2rpx;
height: 24rpx;
background: #e5e5e7;
}
.sicon-back {
font-size: 32rpx;
color: #000;
}
.sicon-home {
font-size: 32rpx;
color: #000;
}
.sicon-more {
font-size: 32rpx;
color: #000;
}
.icon-button {
width: 67rpx;
2024-05-22 15:42:13 +08:00
height: 56rpx;
2024-08-27 17:57:03 +08:00
&-left:hover {
background: rgba(0, 0, 0, 0.16);
border-radius: 30rpx 0px 0px 30rpx;
2024-05-22 15:42:13 +08:00
}
2024-08-27 17:57:03 +08:00
&-right:hover {
background: rgba(0, 0, 0, 0.16);
border-radius: 0px 30rpx 30rpx 0px;
2024-05-22 15:42:13 +08:00
}
2024-08-27 17:57:03 +08:00
}
2024-05-22 15:42:13 +08:00
}
$nav-height: 44px;
.fixed-bg {
2024-08-27 17:57:03 +08:00
position: absolute;
width: 100%;
height: 100%;
top: 0;
z-index: 1;
pointer-events: none;
2024-05-22 15:42:13 +08:00
}
.uni-nav-bar-text {
2024-08-27 17:57:03 +08:00
/* #ifdef APP-PLUS */
font-size: 34rpx;
/* #endif */
/* #ifndef APP-PLUS */
font-size: 14px;
/* #endif */
2024-05-22 15:42:13 +08:00
}
.uni-nav-bar-right-text {
2024-08-27 17:57:03 +08:00
font-size: 12px;
2024-05-22 15:42:13 +08:00
}
.uni-navbar__content {
2024-08-27 17:57:03 +08:00
position: relative;
background-color: transparent;
2024-05-22 15:42:13 +08:00
}
.uni-navbar__content_view {
2024-08-27 17:57:03 +08:00
// box-sizing: border-box;
2024-05-22 15:42:13 +08:00
}
.uni-navbar-btn-text {
2024-08-27 17:57:03 +08:00
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: column;
justify-content: flex-start;
align-items: center;
line-height: 18px;
2024-05-22 15:42:13 +08:00
}
.uni-navbar__header {
2024-08-27 17:57:03 +08:00
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
padding: 0 10px;
flex-direction: row;
justify-content: space-between;
height: $nav-height;
font-size: 12px;
position: relative;
z-index: 2;
2024-05-22 15:42:13 +08:00
}
.uni-navbar__header-btns {
2024-08-27 17:57:03 +08:00
/* #ifndef APP-NVUE */
overflow: hidden;
display: flex;
/* #endif */
flex-wrap: nowrap;
flex-direction: row;
min-width: 40rpx;
justify-content: center;
align-items: center;
/* #ifdef H5 */
cursor: pointer;
/* #endif */
2024-05-22 15:42:13 +08:00
}
.uni-navbar__header-btns-left {
2024-08-27 17:57:03 +08:00
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
width: 120rpx;
justify-content: flex-start;
align-items: center;
2024-05-22 15:42:13 +08:00
}
.uni-navbar__header-btns-right {
2024-08-27 17:57:03 +08:00
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex-direction: row;
justify-content: flex-end;
align-items: center;
2024-05-22 15:42:13 +08:00
}
.uni-navbar__header-container {
2024-08-27 17:57:03 +08:00
position: absolute;
left: 50%;
transform: translateX(-50%) translateY(-50%);
top: 50%;
2024-05-22 15:42:13 +08:00
}
.uni-navbar__header-container-inner {
2024-08-27 17:57:03 +08:00
/* #ifndef APP-NVUE */
display: flex;
/* #endif */
flex: 1;
flex-direction: row;
align-items: center;
justify-content: center;
font-size: 12px;
overflow: hidden;
2024-05-22 15:42:13 +08:00
}
.uni-navbar__placeholder-view {
2024-08-27 17:57:03 +08:00
height: $nav-height;
2024-05-22 15:42:13 +08:00
}
.uni-navbar--fixed {
2024-08-27 17:57:03 +08:00
position: fixed;
z-index: 998;
/* #ifdef H5 */
left: var(--window-left);
right: var(--window-right);
/* #endif */
/* #ifndef H5 */
left: 0;
right: 0;
/* #endif */
2024-05-22 15:42:13 +08:00
}
.uni-navbar--shadow {
2024-08-27 17:57:03 +08:00
box-shadow: 0 1px 6px #ccc;
2024-05-22 15:42:13 +08:00
}
.uni-navbar--border {
2024-08-27 17:57:03 +08:00
border-bottom-width: 1rpx;
border-bottom-style: solid;
border-bottom-color: #eee;
2024-05-22 15:42:13 +08:00
}
.uni-ellipsis-1 {
2024-08-27 17:57:03 +08:00
overflow: hidden;
/* #ifndef APP-NVUE */
white-space: nowrap;
text-overflow: ellipsis;
/* #endif */
/* #ifdef APP-NVUE */
lines: 1;
text-overflow: ellipsis;
/* #endif */
2024-05-22 15:42:13 +08:00
}
// 暗主题配置
.uni-dark {
}
</style>