AppTabbar.vue 2.05 KB
<template>
  <view class="app-tabbar">
    <nut-tabbar
      :model-value="activeTab"
      bottom
      placeholder
      safe-area-inset-bottom
      :active-color="activeColor"
      :unactive-color="inactiveColor"
      @tab-switch="handleTabSwitch"
    >
      <nut-tabbar-item
        v-for="item in tabItems"
        :key="item.name"
        :name="item.name"
        :tab-title="item.title"
      >
        <template #icon="{ active }">
          <component
            :is="item.icon"
            size="18"
            :color="active ? activeColor : inactiveColor"
          />
        </template>
      </nut-tabbar-item>
    </nut-tabbar>
  </view>
</template>

<script setup>
import { computed } from 'vue'
import Taro from '@tarojs/taro'
import { Home, Message, My } from '@nutui/icons-vue-taro'

const props = defineProps({
  current: {
    type: String,
    required: true,
  },
})

const activeColor = '#a67939'
const inactiveColor = '#8b95a7'

const tabItems = [
  {
    name: 'home',
    title: '首页',
    icon: Home,
    url: '/pages/index/index',
  },
  {
    name: 'message',
    title: '消息',
    icon: Message,
    url: '/pages/message/index',
  },
  {
    name: 'mine',
    title: '我的',
    icon: My,
    url: '/pages/mine/index',
  },
]

const activeTab = computed(() => props.current)

const handleTabSwitch = (...args) => {
  const nextTab = args[1]
  const target = tabItems.find((item) => item.name === nextTab)

  if (!target || target.name === props.current) {
    return
  }

  Taro.redirectTo({
    url: target.url,
  })
}
</script>

<style lang="less">
.app-tabbar {
  :deep(.nut-tabbar) {
    left: 24rpx;
    right: 24rpx;
    bottom: 24rpx;
    width: auto;
    border: 2rpx solid rgba(166, 121, 57, 0.12);
    border-radius: 999rpx;
    background: rgba(255, 255, 255, 0.96);
    box-shadow: 0 24rpx 60rpx rgba(15, 23, 42, 0.12);
    overflow: hidden;
  }

  :deep(.nut-tabbar-item_icon-box) {
    padding-top: 8rpx;
  }

  :deep(.nut-tabbar-item_icon-box_nav-word) {
    margin-top: 8rpx;
    font-size: 22rpx;
    font-weight: 600;
  }
}
</style>