u-tabbar.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <view class="u-tabbar" :id="tabbarId" :ref="tabbarId">
  3. <view
  4. class="u-tabbar__content"
  5. ref="u-tabbar__content"
  6. @touchmove.stop.prevent="noop"
  7. :class="[fixed && 'u-tabbar--fixed', shape === 'circle' && 'u-tabbar--circle']"
  8. :style="[tabbarStyle]"
  9. >
  10. <view class="u-tabbar__content__item-wrapper" :style="[{
  11. height: $u.addUnit(height)
  12. }]">
  13. <slot />
  14. </view>
  15. <u-safe-bottom v-if="safeAreaInsetBottom && shape === 'normal'"></u-safe-bottom>
  16. </view>
  17. <view
  18. class="u-tabbar__placeholder"
  19. v-if="placeholder"
  20. :style="[{
  21. height: $u.addUnit(placeholderHeight),
  22. }]"
  23. ></view>
  24. </view>
  25. </template>
  26. <script>
  27. import props from './props.js';
  28. import mixin from '../../libs/mixin/mixin'
  29. import mpMixin from '../../libs/mixin/mpMixin';
  30. // #ifdef APP-NVUE
  31. const dom = uni.requireNativePlugin('dom')
  32. // #endif
  33. /**
  34. * Tabbar 底部导航栏
  35. * @description 此组件提供了自定义tabbar的能力。
  36. * @tutorial https://uview.d3u.cn/components/tabbar.html
  37. * @property {String | Number} value 当前匹配项的name
  38. * @property {Boolean} safeAreaInsetBottom 是否为iPhoneX留出底部安全距离(默认 true )
  39. * @property {Boolean} border 是否显示上方边框(默认 true )
  40. * @property {String | Number} zIndex 元素层级z-index(默认 1 )
  41. * @property {String} activeColor 选中标签的颜色(默认 '#1989fa' )
  42. * @property {String} inactiveColor 未选中标签的颜色(默认 '#7d7e80' )
  43. * @property {Boolean} fixed 是否固定在底部(默认 true )
  44. * @property {Boolean} placeholder fixed定位固定在底部时,是否生成一个等高元素防止塌陷(默认 true )
  45. * @property {Object} customStyle 定义需要用到的外部样式
  46. *
  47. * @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>
  48. */
  49. export default {
  50. name: 'u-tabbar',
  51. mixins: [mpMixin, mixin, props],
  52. data() {
  53. return {
  54. tabbarId: 'tabbar_' + uni.$u.guid(),
  55. placeholderHeight: 0
  56. }
  57. },
  58. computed: {
  59. tabbarStyle() {
  60. const style = {
  61. zIndex: this.zIndex,
  62. backgroundColor: this.bgColor,
  63. }
  64. if(this.border && this.shape == 'normal'){
  65. style.borderTop = `0.5px solid ${this.borderColor}`
  66. }
  67. if(this.shape == 'circle'){
  68. if(this.fit){
  69. //style.width = 'fit-content';
  70. style.margin = '0 auto';
  71. }
  72. if(this.bottom){
  73. style.bottom = uni.$u.addUnit(this.bottom);
  74. }
  75. }
  76. // 合并来自父组件的customStyle样式
  77. return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle))
  78. },
  79. // 监听多个参数的变化,通过在computed执行对应的操作
  80. updateChild() {
  81. return [this.value, this.mode, this.activeColor, this.inactiveColor]
  82. },
  83. updatePlaceholder() {
  84. return [this.fixed, this.placeholder]
  85. }
  86. },
  87. watch: {
  88. updateChild() {
  89. // 如果updateChildren中的元素发生了变化,则执行子元素初始化操作
  90. this.updateChildren()
  91. },
  92. updatePlaceholder() {
  93. // 如果fixed,placeholder等参数发生变化,重新计算占位元素的高度
  94. this.setPlaceholderHeight()
  95. }
  96. },
  97. created() {
  98. this.children = []
  99. },
  100. mounted() {
  101. this.setPlaceholderHeight()
  102. },
  103. // #ifdef VUE3
  104. emits: ["change"],
  105. // #endif
  106. methods: {
  107. updateChildren() {
  108. // 如果存在子元素,则执行子元素的updateFromParent进行更新数据
  109. this.children.length && this.children.map(child => child.updateFromParent())
  110. },
  111. onChange(name) {
  112. this.$emit('change', name)
  113. },
  114. // 设置用于防止塌陷元素的高度
  115. async setPlaceholderHeight() {
  116. if (!this.fixed || !this.placeholder) return
  117. // 延时一定时间
  118. await uni.$u.sleep(20)
  119. // #ifndef APP-NVUE
  120. this.$uGetRect('.u-tabbar__content').then(({height = 50}) => {
  121. // 修复IOS safearea bottom 未填充高度
  122. this.placeholderHeight = height
  123. })
  124. // #endif
  125. // #ifdef APP-NVUE
  126. dom.getComponentRect(this.$refs['u-tabbar__content'], (res) => {
  127. const {
  128. size
  129. } = res
  130. this.placeholderHeight = size.height
  131. })
  132. // #endif
  133. },
  134. getRect(){
  135. let ref = this.tabbarId;
  136. // #ifndef APP-NVUE
  137. // $uGetRect为uView自带的节点查询简化方法,详见文档介绍:https://uview.d3u.cn/js/getRect.html
  138. // 组件内部一般用this.$uGetRect,对外的为uni.$u.getRect,二者功能一致,名称不同
  139. return new Promise(resolve => {
  140. this.$uGetRect(`#${ref}`).then(size => {
  141. resolve(size)
  142. })
  143. })
  144. // #endif
  145. // #ifdef APP-NVUE
  146. // nvue下,使用dom模块查询元素高度
  147. // 返回一个promise,让调用此方法的主体能使用then回调
  148. return new Promise(resolve => {
  149. dom.getComponentRect(this.$refs[ref], res => {
  150. resolve(res.size)
  151. })
  152. })
  153. // #endif
  154. }
  155. }
  156. }
  157. </script>
  158. <style lang="scss" scoped>
  159. @import "../../libs/css/components.scss";
  160. .u-tabbar {
  161. @include flex(column);
  162. flex: 1;
  163. justify-content: center;
  164. &__content {
  165. @include flex(column);
  166. justify-content: center;
  167. &__item-wrapper {
  168. @include flex(row);
  169. }
  170. }
  171. &--fixed {
  172. position: fixed;
  173. bottom: 0;
  174. left: 0;
  175. right: 0;
  176. }
  177. &--circle {
  178. margin: 0 16px;
  179. border-radius: 50px;
  180. overflow: hidden;
  181. box-shadow: 0 6px 30px 5px rgba(0, 0, 0, .05), 0 16px 24px 2px rgba(0, 0, 0, .04), 0 8px 10px -5px rgba(0, 0, 0, .08);
  182. }
  183. }
  184. </style>