TopBar.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <template>
  2. <view class="sticky top-0 z-40 bg-white border-b border-gray-100">
  3. <view class="status-bar" :style="{ height: `${statusBarHeight}px` }"></view>
  4. <view class="top-bar px-4 flex items-center justify-between">
  5. <view class="flex items-center flex-1 min-w-0">
  6. <view
  7. v-if="showBack"
  8. class="mr-2 p-2 -ml-2 clickable rounded-lg"
  9. @click="goBack"
  10. >
  11. <text class="text-gray-600 text-lg">←</text>
  12. </view>
  13. <slot name="left" />
  14. <text class="text-base font-semibold text-gray-800 truncate">{{ title }}</text>
  15. </view>
  16. <CapsuleSafeArea>
  17. <view class="flex items-center">
  18. <slot name="right" />
  19. </view>
  20. </CapsuleSafeArea>
  21. </view>
  22. </view>
  23. </template>
  24. <script setup lang="ts">
  25. import { onMounted, ref } from 'vue'
  26. import CapsuleSafeArea from './CapsuleSafeArea.vue'
  27. const props = withDefaults(
  28. defineProps<{
  29. title: string
  30. showBack?: boolean
  31. }>(),
  32. {
  33. showBack: false
  34. }
  35. )
  36. const statusBarHeight = ref(0)
  37. onMounted(() => {
  38. try {
  39. const info = uni.getSystemInfoSync()
  40. statusBarHeight.value = info.statusBarHeight || 0
  41. } catch (e) {
  42. statusBarHeight.value = 0
  43. }
  44. })
  45. function goBack() {
  46. if (props.showBack) {
  47. uni.navigateBack({ delta: 1 })
  48. }
  49. }
  50. </script>
  51. <style scoped>
  52. .top-bar {
  53. height: 44px;
  54. }
  55. </style>