gaoluyang
4 天以前 92230c9a97dc9ce9df3313d11d26999c04bb6b26
src/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-nav-bar.vue
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,357 @@
<template>
   <view class="uni-navbar" :class="{'uni-dark':dark, 'uni-nvue-fixed': fixed}">
      <view class="uni-navbar__content" :class="{ 'uni-navbar--fixed': fixed, 'uni-navbar--shadow': shadow, 'uni-navbar--border': border }"
         :style="{ 'background-color': themeBgColor }" >
         <status-bar v-if="statusBar" />
         <view :style="{ color: themeColor,backgroundColor: themeBgColor ,height:navbarHeight}"
            class="uni-navbar__header">
            <view @tap="onClickLeft" 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">
                     <uni-icons :color="themeColor" :type="leftIcon" size="20" />
                  </view>
                  <view :class="{ 'uni-navbar-btn-icon-left': !leftIcon.length > 0 }" class="uni-navbar-btn-text"
                     v-if="leftText.length">
                     <text :style="{ color: themeColor, fontSize: '12px' }">{{ leftText }}</text>
                  </view>
               </slot>
            </view>
            <view class="uni-navbar__header-container " @tap="onClickTitle">
               <slot>
                  <view class="uni-navbar__header-container-inner" v-if="title.length>0">
                     <text class="uni-nav-bar-text uni-ellipsis-1"
                        :style="{color: themeColor }">{{ title }}</text>
                  </view>
               </slot>
            </view>
            <view @click="onClickRight" class="uni-navbar__header-btns uni-navbar__header-btns-right"
               :style="{width:rightIconWidth}">
               <slot name="right">
                  <view v-if="rightIcon.length">
                     <uni-icons :color="themeColor" :type="rightIcon" size="22" />
                  </view>
                  <view class="uni-navbar-btn-text" v-if="rightText.length && !rightIcon.length">
                     <text class="uni-nav-bar-right-text" :style="{ color: themeColor}">{{ rightText }}</text>
                  </view>
               </slot>
            </view>
         </view>
      </view>
      <!-- #ifndef APP-NVUE -->
      <view class="uni-navbar__placeholder" v-if="fixed">
         <status-bar v-if="statusBar" />
         <view class="uni-navbar__placeholder-view" :style="{ height:navbarHeight}" />
      </view>
      <!-- #endif -->
   </view>
</template>
<script>
   import statusBar from "./uni-status-bar.vue";
   const getVal = (val) => typeof val === 'number' ? val + 'px' : val;
   /**
    *
    *
    * NavBar è‡ªå®šä¹‰å¯¼èˆªæ 
    * @description å¯¼èˆªæ ç»„件,主要用于头部导航
    * @tutorial https://ext.dcloud.net.cn/plugin?id=52
    * @property {Boolean} dark å¼€å¯é»‘暗模式
    * @property {String} title æ ‡é¢˜æ–‡å­—
    * @property {String} leftText å·¦ä¾§æŒ‰é’®æ–‡æœ¬
    * @property {String} rightText å³ä¾§æŒ‰é’®æ–‡æœ¬
    * @property {String} leftIcon å·¦ä¾§æŒ‰é’®å›¾æ ‡ï¼ˆå›¾æ ‡ç±»åž‹å‚考 [Icon å›¾æ ‡](http://ext.dcloud.net.cn/plugin?id=28) type å±žæ€§ï¼‰
    * @property {String} rightIcon å³ä¾§æŒ‰é’®å›¾æ ‡ï¼ˆå›¾æ ‡ç±»åž‹å‚考 [Icon å›¾æ ‡](http://ext.dcloud.net.cn/plugin?id=28) type å±žæ€§ï¼‰
    * @property {String} color å›¾æ ‡å’Œæ–‡å­—颜色
    * @property {String} backgroundColor å¯¼èˆªæ èƒŒæ™¯é¢œè‰²
    * @property {Boolean} fixed = [true|false] æ˜¯å¦å›ºå®šé¡¶éƒ¨
    * @property {Boolean} statusBar = [true|false] æ˜¯å¦åŒ…含状态栏
    * @property {Boolean} shadow = [true|false] å¯¼èˆªæ ä¸‹æ˜¯å¦æœ‰é˜´å½±
    * @property {Boolean} stat æ˜¯å¦å¼€å¯ç»Ÿè®¡æ ‡é¢˜ä¸ŠæŠ¥
    * @event {Function} clickLeft å·¦ä¾§æŒ‰é’®ç‚¹å‡»æ—¶è§¦å‘
    * @event {Function} clickRight å³ä¾§æŒ‰é’®ç‚¹å‡»æ—¶è§¦å‘
    * @event {Function} clickTitle ä¸­é—´æ ‡é¢˜ç‚¹å‡»æ—¶è§¦å‘
    */
   export default {
      name: "UniNavBar",
      components: {
         statusBar
      },
      emits: ['clickLeft', 'clickRight', 'clickTitle'],
      props: {
         dark: {
            type: Boolean,
            default: false
         },
         title: {
            type: String,
            default: ""
         },
         leftText: {
            type: String,
            default: ""
         },
         rightText: {
            type: String,
            default: ""
         },
         leftIcon: {
            type: String,
            default: ""
         },
         rightIcon: {
            type: String,
            default: ""
         },
         fixed: {
            type: [Boolean, String],
            default: false
         },
         color: {
            type: String,
            default: ""
         },
         backgroundColor: {
            type: String,
            default: ""
         },
         statusBar: {
            type: [Boolean, String],
            default: false
         },
         shadow: {
            type: [Boolean, String],
            default: false
         },
         border: {
            type: [Boolean, String],
            default: true
         },
         height: {
            type: [Number, String],
            default: 44
         },
         leftWidth: {
            type: [Number, String],
            default: 60
         },
         rightWidth: {
            type: [Number, String],
            default: 60
         },
         stat: {
            type: [Boolean, String],
            default: ''
         }
      },
      computed: {
         themeBgColor() {
            if (this.dark) {
               // é»˜è®¤å€¼
               if (this.backgroundColor) {
                  return this.backgroundColor
               } else {
                  return this.dark ? '#333' : '#FFF'
               }
            }
            return this.backgroundColor || '#FFF'
         },
         themeColor() {
            if (this.dark) {
               // é»˜è®¤å€¼
               if (this.color) {
                  return this.color
               } else {
                  return this.dark ? '#fff' : '#333'
               }
            }
            return this.color || '#333'
         },
         navbarHeight() {
            return getVal(this.height)
         },
         leftIconWidth() {
            return getVal(this.leftWidth)
         },
         rightIconWidth() {
            return getVal(this.rightWidth)
         }
      },
      mounted() {
         if (uni.report && this.stat && this.title !== '') {
            uni.report('title', this.title)
         }
      },
      methods: {
         onClickLeft() {
            this.$emit("clickLeft");
         },
         onClickRight() {
            this.$emit("clickRight");
         },
         onClickTitle() {
            this.$emit("clickTitle");
         }
      }
   };
</script>
<style lang="scss" scoped>
   $nav-height: 44px;
   .uni-nvue-fixed {
      /* #ifdef APP-NVUE */
      position: sticky;
      /* #endif */
   }
   .uni-navbar {
      // box-sizing: border-box;
   }
   .uni-nav-bar-text {
      /* #ifdef APP-PLUS */
      font-size: 34rpx;
      /* #endif */
      /* #ifndef APP-PLUS */
      font-size: 14px;
      /* #endif */
   }
   .uni-nav-bar-right-text {
      font-size: 12px;
   }
   .uni-navbar__content {
      position: relative;
      // background-color: #fff;
      // box-sizing: border-box;
      background-color: transparent;
   }
   .uni-navbar__content_view {
      // box-sizing: border-box;
   }
   .uni-navbar-btn-text {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      flex-direction: column;
      justify-content: flex-start;
      align-items: center;
      line-height: 12px;
   }
   .uni-navbar__header {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      padding: 0 10px;
      flex-direction: row;
      height: $nav-height;
      font-size: 12px;
   }
   .uni-navbar__header-btns {
      /* #ifndef APP-NVUE */
      overflow: hidden;
      display: flex;
      /* #endif */
      flex-wrap: nowrap;
      flex-direction: row;
      width: 120rpx;
      // padding: 0 6px;
      justify-content: center;
      align-items: center;
      /* #ifdef H5 */
      cursor: pointer;
      /* #endif */
   }
   .uni-navbar__header-btns-left {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      width: 120rpx;
      justify-content: flex-start;
      align-items: center;
   }
   .uni-navbar__header-btns-right {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      flex-direction: row;
      // width: 150rpx;
      // padding-right: 30rpx;
      justify-content: flex-end;
      align-items: center;
   }
   .uni-navbar__header-container {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      flex: 1;
      padding: 0 10px;
      overflow: hidden;
   }
   .uni-navbar__header-container-inner {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      flex: 1;
      flex-direction: row;
      align-items: center;
      justify-content: center;
      font-size: 12px;
      overflow: hidden;
      // box-sizing: border-box;
   }
   .uni-navbar__placeholder-view {
      height: $nav-height;
   }
   .uni-navbar--fixed {
      position: fixed;
      z-index: 998;
      /* #ifdef H5 */
      left: var(--window-left);
      right: var(--window-right);
      /* #endif */
      /* #ifndef H5 */
      left: 0;
      right: 0;
      /* #endif */
   }
   .uni-navbar--shadow {
      box-shadow: 0 1px 6px #ccc;
   }
   .uni-navbar--border {
      border-bottom-width: 1rpx;
      border-bottom-style: solid;
      border-bottom-color: #eee;
   }
   .uni-ellipsis-1 {
      overflow: hidden;
      /* #ifndef APP-NVUE */
      white-space: nowrap;
      text-overflow: ellipsis;
      /* #endif */
      /* #ifdef APP-NVUE */
      lines: 1;
      text-overflow: ellipsis;
      /* #endif */
   }
   // æš—主题配置
   .uni-dark {}
</style>