su-tabbar.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <view class="u-tabbar">
  3. <view
  4. class="u-tabbar__content"
  5. ref="u-tabbar__content"
  6. @touchmove.stop.prevent=""
  7. :class="[border && 'u-border-top', fixed && 'u-tabbar--fixed', { 'mid-tabbar': midTabBar }]"
  8. :style="[tabbarStyle]"
  9. >
  10. <view class="u-tabbar__content__item-wrapper">
  11. <slot></slot>
  12. </view>
  13. <view v-if="safeAreaInsetBottom" :style="[{ height: safeBottomHeight + 'px' }]"></view>
  14. </view>
  15. <view
  16. class="u-tabbar__placeholder"
  17. v-if="placeholder"
  18. :style="{
  19. height: placeholderHeight + 'px',
  20. }"
  21. ></view>
  22. </view>
  23. </template>
  24. <script>
  25. // #ifdef APP-NVUE
  26. const dom = uni.requireNativePlugin('dom');
  27. // #endif
  28. /**
  29. * Tabbar 底部导航栏
  30. * @description 此组件提供了自定义tabbar的能力。
  31. * @property {String | Number} value 当前匹配项的name
  32. * @property {Boolean} safeAreaInsetBottom 是否为iPhoneX留出底部安全距离(默认 true )
  33. * @property {Boolean} border 是否显示上方边框(默认 true )
  34. * @property {String | Number} zIndex 元素层级z-index(默认 1 )
  35. * @property {String} activeColor 选中标签的颜色(默认 '#1989fa' )
  36. * @property {String} inactiveColor 未选中标签的颜色(默认 '#7d7e80' )
  37. * @property {Boolean} fixed 是否固定在底部(默认 true )
  38. * @property {Boolean} placeholder fixed定位固定在底部时,是否生成一个等高元素防止塌陷(默认 true )
  39. * @property {Object} customStyle 定义需要用到的外部样式
  40. *
  41. */
  42. import { deepMerge, addStyle, sleep } from '@/sheep/helper';
  43. import sheep from '@/sheep';
  44. export default {
  45. name: 'su-tabbar',
  46. props: {
  47. customStyle: {
  48. type: [Object, String],
  49. default: () => ({}),
  50. },
  51. customClass: {
  52. type: String,
  53. default: '',
  54. },
  55. // 跳转的页面路径
  56. url: {
  57. type: String,
  58. default: '',
  59. },
  60. // 页面跳转的类型
  61. linkType: {
  62. type: String,
  63. default: 'navigateTo',
  64. },
  65. // 当前匹配项的name
  66. value: {
  67. type: [String, Number, null],
  68. default: '',
  69. },
  70. // 是否为iPhoneX留出底部安全距离
  71. safeAreaInsetBottom: {
  72. type: Boolean,
  73. default: true,
  74. },
  75. // 是否显示上方边框
  76. border: {
  77. type: Boolean,
  78. default: true,
  79. },
  80. // 元素层级z-index
  81. zIndex: {
  82. type: [String, Number],
  83. default: 10,
  84. },
  85. // 选中标签的颜色
  86. activeColor: {
  87. type: String,
  88. default: '#1989fa',
  89. },
  90. // 未选中标签的颜色
  91. inactiveColor: {
  92. type: String,
  93. default: '#7d7e80',
  94. },
  95. // 是否固定在底部
  96. fixed: {
  97. type: Boolean,
  98. default: true,
  99. },
  100. // fixed定位固定在底部时,是否生成一个等高元素防止塌陷
  101. placeholder: {
  102. type: Boolean,
  103. default: true,
  104. },
  105. midTabBar: {
  106. type: Boolean,
  107. default: false,
  108. },
  109. },
  110. data() {
  111. return {
  112. placeholderHeight: 0,
  113. safeBottomHeight: sheep.$platform.device.safeAreaInsets.bottom,
  114. };
  115. },
  116. computed: {
  117. tabbarStyle() {
  118. const style = {
  119. zIndex: this.zIndex,
  120. };
  121. // 合并来自父组件的customStyle样式
  122. return deepMerge(style, addStyle(this.customStyle));
  123. },
  124. // 监听多个参数的变化,通过在computed执行对应的操作
  125. updateChild() {
  126. return [this.value, this.activeColor, this.inactiveColor];
  127. },
  128. updatePlaceholder() {
  129. return [this.fixed, this.placeholder];
  130. },
  131. },
  132. watch: {
  133. updateChild() {
  134. // 如果updateChildren中的元素发生了变化,则执行子元素初始化操作
  135. this.updateChildren();
  136. },
  137. updatePlaceholder() {
  138. // 如果fixed,placeholder等参数发生变化,重新计算占位元素的高度
  139. this.setPlaceholderHeight();
  140. },
  141. },
  142. created() {
  143. this.children = [];
  144. },
  145. mounted() {
  146. this.setPlaceholderHeight();
  147. },
  148. methods: {
  149. updateChildren() {
  150. // 如果存在子元素,则执行子元素的updateFromParent进行更新数据
  151. this.children.length && this.children.map((child) => child.updateFromParent());
  152. },
  153. getRect(selector, all) {
  154. return new Promise((resolve) => {
  155. uni.createSelectorQuery()
  156. .in(this)
  157. [all ? 'selectAll' : 'select'](selector)
  158. .boundingClientRect((rect) => {
  159. if (all && Array.isArray(rect) && rect.length) {
  160. resolve(rect);
  161. }
  162. if (!all && rect) {
  163. resolve(rect);
  164. }
  165. })
  166. .exec();
  167. });
  168. },
  169. // 设置用于防止塌陷元素的高度
  170. async setPlaceholderHeight() {
  171. if (!this.fixed || !this.placeholder) return;
  172. // 延时一定时间
  173. await sleep(20);
  174. // #ifndef APP-NVUE
  175. this.getRect('.u-tabbar__content').then(({ height = 50 }) => {
  176. // 修复IOS safearea bottom 未填充高度
  177. this.placeholderHeight = height;
  178. });
  179. // #endif
  180. // #ifdef APP-NVUE
  181. dom.getComponentRect(this.$refs['u-tabbar__content'], (res) => {
  182. const { size } = res;
  183. this.placeholderHeight = size.height;
  184. });
  185. // #endif
  186. },
  187. },
  188. };
  189. </script>
  190. <style lang="scss" scoped>
  191. .u-tabbar {
  192. display: flex;
  193. flex: 1;
  194. justify-content: center;
  195. &__content {
  196. display: flex;
  197. flex-direction: column;
  198. background-color: #fff;
  199. box-shadow: 0px -2px 4px 0px rgba(51, 51, 51, 0.06);
  200. &__item-wrapper {
  201. height: 50px;
  202. display: flex;
  203. justify-content: space-around;
  204. align-items: center;
  205. }
  206. }
  207. .mid-tabbar {
  208. border-radius: 30rpx 30rpx 0 0;
  209. }
  210. &--fixed {
  211. position: fixed;
  212. bottom: -1px;
  213. left: 0;
  214. right: 0;
  215. }
  216. }
  217. </style>