u-tabbar.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <view class="u-tabbar">
  3. <view class="u-tabbar__content" ref="u-tabbar__content" @touchmove.stop.prevent="noop"
  4. :class="[border && 'u-border-top', fixed && 'u-tabbar--fixed']" :style="[tabbarStyle]">
  5. <view class="u-tabbar__content__item-wrapper">
  6. <slot />
  7. </view>
  8. <u-safe-bottom v-if="safeAreaInsetBottom"></u-safe-bottom>
  9. </view>
  10. <view class="u-tabbar__placeholder" v-if="placeholder" :style="{
  11. height: placeholderHeight + 'px',
  12. }"></view>
  13. </view>
  14. </template>
  15. <script>
  16. import props from './props.js';
  17. // #ifdef APP-NVUE
  18. const dom = uni.requireNativePlugin('dom')
  19. // #endif
  20. /**
  21. * Tabbar 底部导航栏
  22. * @description 此组件提供了自定义tabbar的能力。
  23. * @tutorial https://www.uviewui.com/components/tabbar.html
  24. * @property {String | Number} value 当前匹配项的name
  25. * @property {Boolean} safeAreaInsetBottom 是否为iPhoneX留出底部安全距离(默认 true )
  26. * @property {Boolean} border 是否显示上方边框(默认 true )
  27. * @property {String | Number} zIndex 元素层级z-index(默认 1 )
  28. * @property {String} activeColor 选中标签的颜色(默认 '#1989fa' )
  29. * @property {String} inactiveColor 未选中标签的颜色(默认 '#7d7e80' )
  30. * @property {Boolean} fixed 是否固定在底部(默认 true )
  31. * @property {Boolean} placeholder fixed定位固定在底部时,是否生成一个等高元素防止塌陷(默认 true )
  32. * @property {Object} customStyle 定义需要用到的外部样式
  33. *
  34. * @example <u-tabbar :value="value2" :placeholder="false" @change="name => value2 = name" :fixed="false" :safeAreaInsetBottom="false"><u-tabbar-item text="首页" icon="home" dot ></u-tabbar-item></u-tabbar>
  35. */
  36. export default {
  37. name: 'u-tabbar',
  38. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  39. data() {
  40. return {
  41. placeholderHeight: 0
  42. }
  43. },
  44. computed: {
  45. tabbarStyle() {
  46. const style = {
  47. zIndex: 1
  48. }
  49. // 合并来自父组件的customStyle样式
  50. return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle))
  51. },
  52. // 监听多个参数的变化,通过在computed执行对应的操作
  53. updateChild() {
  54. return [this.value, this.activeColor, this.inactiveColor]
  55. },
  56. updatePlaceholder() {
  57. return [this.fixed, this.placeholder]
  58. }
  59. },
  60. watch: {
  61. updateChild() {
  62. // 如果updateChildren中的元素发生了变化,则执行子元素初始化操作
  63. this.updateChildren()
  64. },
  65. updatePlaceholder() {
  66. // 如果fixed,placeholder等参数发生变化,重新计算占位元素的高度
  67. this.setPlaceholderHeight()
  68. }
  69. },
  70. created() {
  71. this.children = []
  72. },
  73. mounted() {
  74. this.setPlaceholderHeight()
  75. },
  76. methods: {
  77. updateChildren() {
  78. // 如果存在子元素,则执行子元素的updateFromParent进行更新数据
  79. this.children.length && this.children.map(child => child.updateFromParent())
  80. },
  81. // 设置用于防止塌陷元素的高度
  82. async setPlaceholderHeight() {
  83. if (!this.fixed || !this.placeholder) return
  84. // 延时一定时间
  85. await uni.$u.sleep(20)
  86. // #ifndef APP-NVUE
  87. this.$uGetRect('.u-tabbar__content').then(({
  88. height = 50
  89. }) => {
  90. // 修复IOS safearea bottom 未填充高度
  91. this.placeholderHeight = height
  92. })
  93. // #endif
  94. // #ifdef APP-NVUE
  95. dom.getComponentRect(this.$refs['u-tabbar__content'], (res) => {
  96. const {
  97. size
  98. } = res
  99. this.placeholderHeight = size.height
  100. })
  101. // #endif
  102. }
  103. }
  104. }
  105. </script>
  106. <style lang="scss" scoped>
  107. @import "../../libs/css/components.scss";
  108. .u-tabbar {
  109. @include flex(column);
  110. flex: 1;
  111. justify-content: center;
  112. &__content {
  113. @include flex(column);
  114. background-color: #fff;
  115. &__item-wrapper {
  116. height: 50px;
  117. @include flex(row);
  118. }
  119. }
  120. &--fixed {
  121. position: fixed;
  122. bottom: 0;
  123. left: 0;
  124. right: 0;
  125. }
  126. }
  127. </style>