25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

478 lines
12 KiB

  1. <template>
  2. <view
  3. class="u-tabbar-item"
  4. :style="[itemInlineStyle, addStyle(customStyle)]"
  5. :class="itemClassNames"
  6. @tap="clickHandler"
  7. >
  8. <view class="u-tabbar-item__content" :class="contentClassNames">
  9. <view class="u-tabbar-item__bubble" :class="bubbleClassNames">
  10. <view
  11. class="u-tabbar-item__icon"
  12. :class="iconClassNames"
  13. >
  14. <view v-if="isMidButton" class="u-tabbar-item__mid-button-arc">
  15. <svg viewBox="0 0 72 72" preserveAspectRatio="none">
  16. <path d="M 4 36 A 32 32 0 0 1 68 36"></path>
  17. </svg>
  18. </view>
  19. <view v-if="isMidButton" class="u-tabbar-item__mid-button-inner"></view>
  20. <up-icon
  21. v-if="resolvedIconName"
  22. :name="resolvedIconName"
  23. :color="isMidButton ? resolvedMidButtonIconColor : (isActive ? resolvedActiveColor : resolvedInactiveColor)"
  24. :size="isMidButton ? midButtonIconSize : 24"
  25. ></up-icon>
  26. <template v-else>
  27. <slot
  28. v-if="isActive"
  29. name="active-icon"
  30. />
  31. <slot
  32. v-else
  33. name="inactive-icon"
  34. />
  35. </template>
  36. <u-badge
  37. absolute
  38. :offset="[0, dot ? '34rpx' : badge > 9 ? '14rpx' : '20rpx']"
  39. :customStyle="badgeStyle"
  40. :isDot="dot"
  41. :value="badge || (dot ? 1 : null)"
  42. :show="dot || badge > 0"
  43. ></u-badge>
  44. </view>
  45. <slot name="text">
  46. <text
  47. class="u-tabbar-item__text"
  48. :class="textClassNames"
  49. :style="{
  50. color: isActive ? resolvedActiveColor : resolvedInactiveColor
  51. }"
  52. >{{ text }}</text>
  53. </slot>
  54. </view>
  55. </view>
  56. <view v-if="resolvedStyleType === 'underline'" class="u-tabbar-item__underline"></view>
  57. <view v-if="resolvedStyleType === 'dot'" class="u-tabbar-item__active-dot"></view>
  58. </view>
  59. </template>
  60. <script>
  61. import { props } from './props';
  62. import { mpMixin } from '../../libs/mixin/mpMixin';
  63. import { mixin } from '../../libs/mixin/mixin';
  64. import { addStyle, error } from '../../libs/function/index';
  65. /**
  66. * TabbarItem 底部导航栏子组件
  67. * @description 此组件提供了自定义tabbar的能力。
  68. * @tutorial https://uview-plus.jiangruyi.com/components/tabbar.html
  69. * @property {String | Number} name item标签的名称,作为与u-tabbar的value参数匹配的标识符
  70. * @property {String} icon uView内置图标或者绝对路径的图片
  71. * @property {String | Number} badge 右上角的角标提示信息
  72. * @property {Boolean} dot 是否显示圆点,将会覆盖badge参数(默认 false )
  73. * @property {String} text 描述文本
  74. * @property {Object | String} badgeStyle 控制徽标的位置,对象或者字符串形式,可以设置top和right属性(默认 'top: 6px;right:2px;' )
  75. * @property {Object} customStyle 定义需要用到的外部样式
  76. *
  77. * @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>
  78. */
  79. export default {
  80. name: 'u-tabbar-item',
  81. mixins: [mpMixin, mixin, props],
  82. data() {
  83. return {
  84. isActive: false, // 是否处于激活状态
  85. parentData: {
  86. value: null,
  87. activeColor: '',
  88. inactiveColor: '',
  89. styleType: 'default',
  90. animationType: 'none',
  91. activeBackgroundColor: '',
  92. inactiveBackgroundColor: '',
  93. itemShape: 'default',
  94. iconScale: 1.1,
  95. textMode: 'always'
  96. }
  97. }
  98. },
  99. // 微信小程序中 options 选项
  100. options: {
  101. virtualHost: true //将自定义节点设置成虚拟的,更加接近Vue组件的表现。我们不希望自定义组件的这个节点本身可以设置样式、响应 flex 布局等
  102. },
  103. computed: {
  104. // 计算是否为中间按钮
  105. isMidButton() {
  106. return this.mode === 'midButton';
  107. },
  108. resolvedActiveColor() {
  109. return !this.parentData.activeColor || this.parentData.activeColor === '#1989fa'
  110. ? this.upThemeVar('--up-primary', '#1989fa')
  111. : this.parentData.activeColor
  112. },
  113. resolvedInactiveColor() {
  114. return !this.parentData.inactiveColor || this.parentData.inactiveColor === '#7d7e80'
  115. ? this.upThemeVar('--up-content-color', '#7d7e80')
  116. : this.parentData.inactiveColor
  117. },
  118. resolvedStyleType() {
  119. return this.parentData.styleType || 'default'
  120. },
  121. resolvedAnimationType() {
  122. return this.parentData.animationType || 'none'
  123. },
  124. resolvedItemShape() {
  125. return this.parentData.itemShape || 'default'
  126. },
  127. resolvedIconName() {
  128. if (this.$slots['active-icon'] || this.$slots['inactive-icon']) return ''
  129. if (this.isActive) return this.activeIcon || this.icon
  130. return this.inactiveIcon || this.icon
  131. },
  132. resolvedMidButtonIconColor() {
  133. if (this.midButtonIconColor) return this.midButtonIconColor
  134. return this.isMidButton ? '#3c9cff' : this.resolvedActiveColor
  135. },
  136. itemClassNames() {
  137. return [
  138. this.isActive ? 'u-tabbar-item--active' : 'u-tabbar-item--inactive',
  139. this.isMidButton ? 'u-tabbar-item--mid-button' : '',
  140. `u-tabbar-item--${this.resolvedStyleType}`,
  141. this.resolvedAnimationType !== 'none' && this.isActive ? `u-tabbar-item--anim-${this.resolvedAnimationType}` : '',
  142. this.resolvedItemShape !== 'default' ? `u-tabbar-item--shape-${this.resolvedItemShape}` : '',
  143. this.isActive ? this.activeClass : this.inactiveClass
  144. ]
  145. },
  146. iconClassNames() {
  147. return [
  148. this.isMidButton ? 'u-tabbar-item__icon--mid-button' : '',
  149. `u-tabbar-item__icon--${this.resolvedStyleType}`,
  150. this.isActive && this.resolvedAnimationType !== 'none' ? `u-tabbar-item__icon--anim-${this.resolvedAnimationType}` : ''
  151. ]
  152. },
  153. contentClassNames() {
  154. return [
  155. `u-tabbar-item__content--${this.resolvedStyleType}`,
  156. this.isMidButton ? 'u-tabbar-item__content--mid-button' : ''
  157. ]
  158. },
  159. bubbleClassNames() {
  160. return [
  161. `u-tabbar-item__bubble--${this.resolvedStyleType}`,
  162. this.isActive ? 'u-tabbar-item__bubble--active' : '',
  163. this.isMidButton ? 'u-tabbar-item__bubble--mid-button' : ''
  164. ]
  165. },
  166. textClassNames() {
  167. return [
  168. `u-tabbar-item__text--${this.resolvedStyleType}`,
  169. this.parentData.textMode === 'active' && !this.isActive ? 'u-tabbar-item__text--muted' : ''
  170. ]
  171. },
  172. itemInlineStyle() {
  173. return {
  174. backgroundColor: this.isActive
  175. ? (this.parentData.activeBackgroundColor || 'transparent')
  176. : (this.parentData.inactiveBackgroundColor || 'transparent')
  177. }
  178. }
  179. },
  180. created() {
  181. this.init()
  182. },
  183. emits: ["click", "change"],
  184. methods: {
  185. addStyle,
  186. init() {
  187. // 支付宝小程序不支持provide/inject,所以使用这个方法获取整个父组件,在created定义,避免循环引用
  188. this.updateParentData()
  189. if (!this.parent) {
  190. error('up-tabbar-item必须搭配up-tabbar组件使用')
  191. }
  192. // 本子组件在u-tabbar的children数组中的索引
  193. const index = this.parent.children.indexOf(this)
  194. // 判断本组件的name(如果没有定义name,就用index索引)是否等于父组件的value参数
  195. this.isActive = (this.name || index) === this.parentData.value
  196. },
  197. updateParentData() {
  198. // 此方法在mixin中
  199. this.getParentData('u-tabbar')
  200. },
  201. // 此方法将会被父组件u-tabbar调用
  202. updateFromParent() {
  203. // 重新初始化
  204. this.init()
  205. },
  206. clickHandler() {
  207. this.$nextTick(() => {
  208. const index = this.parent.children.indexOf(this)
  209. const name = this.name || index
  210. // 点击的item为非激活的item才发出change事件
  211. if (name !== this.parent.value) {
  212. this.parent.$emit('change', name)
  213. }
  214. this.$emit('click', name)
  215. })
  216. }
  217. },
  218. }
  219. </script>
  220. <style lang="scss" scoped>
  221. .u-tabbar-item {
  222. @include flex(column);
  223. align-items: center;
  224. justify-content: center;
  225. flex: 1;
  226. position: relative;
  227. /* #ifndef APP-NVUE */
  228. width: 100%;
  229. height: 100%;
  230. /* #endif */
  231. /* #ifdef H5 */
  232. cursor: pointer;
  233. /* #endif */
  234. &__icon {
  235. @include flex;
  236. position: relative;
  237. width: 150rpx;
  238. justify-content: center;
  239. min-height: 46rpx;
  240. transition: transform 0.22s ease, opacity 0.22s ease;
  241. &--pill,
  242. &--card,
  243. &--glow,
  244. &--convex {
  245. width: 100%;
  246. }
  247. &--anim-scale {
  248. transform: scale(var(--up-tabbar-icon-scale, 1.1));
  249. }
  250. &--anim-lift {
  251. transform: translateY(-6rpx) scale(var(--up-tabbar-icon-scale, 1.08));
  252. }
  253. &--anim-swing {
  254. transform: rotate(-10deg) scale(var(--up-tabbar-icon-scale, 1.08));
  255. }
  256. &--anim-pulse {
  257. transform: scale(var(--up-tabbar-icon-scale, 1.14));
  258. }
  259. }
  260. &__content {
  261. @include flex(column);
  262. align-items: center;
  263. justify-content: center;
  264. width: 100%;
  265. height: 100%;
  266. &--mid-button {
  267. justify-content: center;
  268. }
  269. }
  270. &__bubble {
  271. @include flex(column);
  272. align-items: center;
  273. justify-content: center;
  274. min-width: 0;
  275. padding: 0;
  276. max-width: 100rpx;
  277. border-radius: 999px;
  278. transition: background-color 0.22s ease, transform 0.22s ease;
  279. &--pill.u-tabbar-item__bubble--active {
  280. background-color: transparent;
  281. }
  282. &--glow.u-tabbar-item__bubble--active {
  283. background-color: var(--up-tabbar-active-bg, rgba(125, 211, 252, 0.12));
  284. }
  285. &--mid-button {
  286. padding: 0;
  287. background-color: transparent !important;
  288. }
  289. }
  290. &__text {
  291. margin-top: 2px;
  292. font-size: 12px;
  293. color: $u-content-color;
  294. transition: transform 0.22s ease, opacity 0.22s ease;
  295. &--muted {
  296. opacity: 0.68;
  297. transform: scale(0.94);
  298. }
  299. }
  300. &__underline {
  301. position: absolute;
  302. left: 50%;
  303. bottom: 2rpx;
  304. width: 34rpx;
  305. height: 6rpx;
  306. border-radius: 999px;
  307. background-color: currentColor;
  308. transform: translateX(-50%);
  309. }
  310. &__active-dot {
  311. position: absolute;
  312. left: 50%;
  313. bottom: 8rpx;
  314. width: 10rpx;
  315. height: 10rpx;
  316. border-radius: 50%;
  317. background-color: currentColor;
  318. transform: translateX(-50%);
  319. }
  320. }
  321. .u-tabbar-item--active {
  322. color: inherit;
  323. }
  324. .u-tabbar-item--pill,
  325. .u-tabbar-item--glow,
  326. .u-tabbar-item--card {
  327. margin: 0;
  328. border-radius: 999px;
  329. }
  330. .u-tabbar-item--card {
  331. border-radius: 24rpx;
  332. }
  333. .u-tabbar-item--active.u-tabbar-item--pill,
  334. .u-tabbar-item--active.u-tabbar-item--card {
  335. box-shadow: none;
  336. }
  337. .u-tabbar-item--active.u-tabbar-item--glow {
  338. box-shadow: none;
  339. }
  340. .u-tabbar-item--lift.u-tabbar-item--active {
  341. transform: none;
  342. }
  343. .u-tabbar-item--lift.u-tabbar-item--active .u-tabbar-item__icon {
  344. transform: translateY(-6rpx) scale(1.04);
  345. }
  346. .u-tabbar-item--underline,
  347. .u-tabbar-item--dot {
  348. padding-bottom: 10rpx;
  349. }
  350. .u-tabbar-item--convex.u-tabbar-item--active:not(.u-tabbar-item--mid-button) {
  351. transform: none;
  352. }
  353. // 中间按钮样式
  354. .u-tabbar-item--mid-button {
  355. /* #ifndef APP-NVUE */
  356. transform: translateY(v-bind('`${midButtonOffsetY}px`'));
  357. /* #endif */
  358. z-index: 2;
  359. flex: 1;
  360. }
  361. .u-tabbar-item__icon--mid-button {
  362. width: 64px;
  363. height: 64px;
  364. border-radius: 999px;
  365. background: #ffffff;
  366. box-shadow: v-bind('midButtonBoxShadow || "none"');
  367. display: flex;
  368. align-items: center;
  369. justify-content: center;
  370. position: relative;
  371. overflow: visible;
  372. }
  373. .u-tabbar-item__mid-button-arc {
  374. position: absolute;
  375. left: -4px;
  376. top: -4px;
  377. width: calc(100% + 8px);
  378. height: calc(100% + 8px);
  379. pointer-events: none;
  380. z-index: 0;
  381. }
  382. .u-tabbar-item__mid-button-arc svg {
  383. width: 100%;
  384. height: 100%;
  385. }
  386. .u-tabbar-item__mid-button-arc path {
  387. fill: none;
  388. stroke: none;
  389. stroke-width: 0;
  390. stroke-linecap: round;
  391. }
  392. .u-tabbar-item__mid-button-inner {
  393. position: absolute;
  394. left: 6px;
  395. top: 6px;
  396. right: 6px;
  397. bottom: 6px;
  398. border-radius: 999px;
  399. background: v-bind('midButtonBgColor || "#ffffff"');
  400. box-shadow: v-bind('midButtonInnerBoxShadow');
  401. z-index: 1;
  402. }
  403. .u-tabbar-item__icon--mid-button :deep(.u-icon),
  404. .u-tabbar-item__icon--mid-button :deep(.u-badge) {
  405. position: relative;
  406. z-index: 2;
  407. }
  408. .u-tabbar-item--mid-button .u-tabbar-item__text {
  409. margin-top: 0;
  410. transform: translateY(-4px);
  411. }
  412. .u-tabbar-item--lift .u-tabbar-item__bubble {
  413. background-color: transparent !important;
  414. }
  415. .u-tabbar-item--lift.u-tabbar-item--active .u-tabbar-item__text {
  416. transform: translateY(-4rpx);
  417. }
  418. .u-tabbar-item--anim-pulse .u-tabbar-item__icon {
  419. animation: u-tabbar-item-pulse 1.4s ease-in-out infinite;
  420. }
  421. @keyframes u-tabbar-item-pulse {
  422. 0%, 100% {
  423. transform: scale(var(--up-tabbar-icon-scale, 1.1));
  424. }
  425. 50% {
  426. transform: scale(calc(var(--up-tabbar-icon-scale, 1.1) + 0.08));
  427. }
  428. }
  429. /* #ifdef MP */
  430. // 由于小程序都使用shadow DOM形式实现,需要给影子宿主设置flex: 1才能让其撑开
  431. :host {
  432. flex: 1;
  433. width: 100%;
  434. }
  435. /* #endif */
  436. </style>