cl-topbar.uvue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <view
  3. v-if="fixed"
  4. class="cl-topbar-placeholder"
  5. :style="{ marginTop: offsetTop, height }"
  6. ></view>
  7. <view
  8. class="cl-topbar"
  9. :class="[{ 'cl-topbar--fixed': fixed }, pt.classNames]"
  10. :style="{
  11. paddingTop: offsetTop,
  12. backgroundColor
  13. }"
  14. >
  15. <view class="cl-topbar__inner" :style="{ height }">
  16. <view class="cl-topbar__prepend">
  17. <view v-if="showBack" class="cl-topbar__icon" @tap="back()">
  18. <cl-icon
  19. :name="backIcon"
  20. :size="pt.back?.size ?? 48"
  21. :color="pt.back?.color ?? color"
  22. ></cl-icon>
  23. </view>
  24. <slot name="prepend"></slot>
  25. </view>
  26. <view class="cl-topbar__content">
  27. <slot>
  28. <cl-text
  29. :color="color"
  30. :pt="{
  31. className: parseClass(['!text-md', pt.title?.className])
  32. }"
  33. >
  34. {{ title }}
  35. </cl-text>
  36. </slot>
  37. </view>
  38. <view class="cl-topbar__append">
  39. <slot name="append"></slot>
  40. </view>
  41. </view>
  42. </view>
  43. </template>
  44. <script setup lang="ts">
  45. import { computed, type PropType } from "vue";
  46. import { getConfig, parseClass, parsePt, parseRpx, router } from "@/cool";
  47. import type { PassThroughProps } from "../../types";
  48. import type { ClIconProps } from "../cl-icon/props";
  49. defineOptions({
  50. name: "cl-topbar"
  51. });
  52. const props = defineProps({
  53. // 样式透传对象,
  54. pt: {
  55. type: Object,
  56. default: () => ({})
  57. },
  58. // 顶部栏标题文本
  59. title: {
  60. type: String,
  61. default: ""
  62. },
  63. // 文字颜色,优先级最高
  64. color: {
  65. type: String,
  66. default: ""
  67. },
  68. // 背景颜色,优先级最高
  69. backgroundColor: {
  70. type: String,
  71. default: ""
  72. },
  73. // 是否显示返回按钮
  74. showBack: {
  75. type: Boolean,
  76. default: true
  77. },
  78. // 返回按钮的跳转路径
  79. backPath: {
  80. type: String,
  81. default: ""
  82. },
  83. // 返回按钮的图标
  84. backIcon: {
  85. type: String,
  86. default: "back"
  87. },
  88. // 是否使用安全区域顶部边距
  89. safeAreaTop: {
  90. type: Boolean,
  91. default: false
  92. },
  93. // 是否固定在顶部
  94. fixed: {
  95. type: Boolean,
  96. default: false
  97. },
  98. // 内容高度
  99. height: {
  100. type: [Number, String] as PropType<number | string | null>,
  101. default: null
  102. }
  103. });
  104. const { safeAreaInsets } = uni.getWindowInfo();
  105. // 定义样式透传的类型接口
  106. type PassThrough = {
  107. classNames?: string;
  108. title?: PassThroughProps;
  109. back?: ClIconProps;
  110. };
  111. // 解析样式透传配置,返回处理后的样式对象
  112. const pt = computed(() => parsePt<PassThrough>(props.pt));
  113. // 使用设备安全区域顶部边距
  114. const offsetTop = computed(() => {
  115. if (props.safeAreaTop) {
  116. return safeAreaInsets.top + "px";
  117. }
  118. return "0px";
  119. });
  120. // 计算内容高度
  121. const height = computed(() => {
  122. if (props.height == null) {
  123. return "44px";
  124. }
  125. return parseRpx(props.height!);
  126. });
  127. // 计算背景颜色,优先级:props > 页面配置 > 全局配置
  128. const backgroundColor = computed(() => {
  129. // 如果组件属性中指定了背景色,优先使用
  130. if (props.backgroundColor != "") {
  131. return props.backgroundColor;
  132. }
  133. // 获取当前路由页面信息
  134. const { style } = router.route()!;
  135. // 如果页面配置了导航栏背景色,使用页面配置
  136. if (style != null) {
  137. if (style.navigationBarBackgroundColor != null) {
  138. return style.navigationBarBackgroundColor as string;
  139. }
  140. }
  141. // 最后使用全局配置的导航栏背景色
  142. return getConfig("navBgColor");
  143. });
  144. // 计算文字颜色,优先级:props > 页面配置 > 全局配置
  145. const color = computed(() => {
  146. // 如果组件属性中指定了文字颜色,优先使用
  147. if (props.color != "") {
  148. return props.color;
  149. }
  150. // 获取当前路由页面信息
  151. const { style } = router.route()!;
  152. // 如果页面配置了导航栏文字样式,使用页面配置
  153. if (style != null) {
  154. if (style.navigationBarTextStyle != null) {
  155. return style.navigationBarTextStyle as string;
  156. }
  157. }
  158. // 最后使用全局配置的导航栏文字样式
  159. return getConfig("navTxtStyle");
  160. });
  161. // 返回按钮点击事件
  162. function back() {
  163. if (props.backPath != "") {
  164. router.to(props.backPath);
  165. } else {
  166. if (router.isFirstPage()) {
  167. router.home();
  168. } else {
  169. router.back();
  170. }
  171. }
  172. }
  173. </script>
  174. <style lang="scss" scoped>
  175. .cl-topbar {
  176. &__inner {
  177. @apply flex flex-row items-center w-full;
  178. }
  179. &__icon {
  180. @apply flex items-center justify-center h-full;
  181. width: 30px;
  182. }
  183. &__prepend {
  184. @apply absolute left-0 top-0 z-10 h-full flex flex-row items-center;
  185. margin-left: 3px;
  186. }
  187. &__content {
  188. @apply flex flex-col items-center justify-center h-full;
  189. flex: 1;
  190. }
  191. &__append {
  192. @apply absolute right-0 top-0 z-10 h-full flex flex-row items-center;
  193. margin-right: 3px;
  194. }
  195. &--fixed {
  196. @apply fixed top-0 left-0 right-0 z-10 w-full;
  197. }
  198. }
  199. </style>