Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

230 рядки
5.9 KiB

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