cl-topbar.uvue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.className]"
  10. :style="topbarStyle"
  11. >
  12. <view class="cl-topbar__inner" :style="{ height }">
  13. <view class="cl-topbar__prepend">
  14. <view v-if="showBack" class="cl-topbar__icon" @tap="back()">
  15. <cl-icon
  16. :pt="{
  17. className: pt.back?.className
  18. }"
  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, getSafeAreaHeight, 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. // 定义样式透传的类型接口
  105. type PassThrough = {
  106. className?: string;
  107. title?: PassThroughProps;
  108. back?: ClIconProps;
  109. };
  110. // 解析样式透传配置,返回处理后的样式对象
  111. const pt = computed(() => parsePt<PassThrough>(props.pt));
  112. // 使用设备安全区域顶部边距
  113. const offsetTop = computed(() => {
  114. if (props.safeAreaTop) {
  115. return getSafeAreaHeight("top") + "px";
  116. }
  117. return "0px";
  118. });
  119. // 计算内容高度
  120. const height = computed(() => {
  121. if (props.height == null) {
  122. return "44px";
  123. }
  124. return parseRpx(props.height!);
  125. });
  126. // 计算背景颜色,优先级:props > 页面配置 > 全局配置
  127. const backgroundColor = computed(() => {
  128. // 如果组件属性中指定了背景色,优先使用
  129. if (props.backgroundColor != "") {
  130. return props.backgroundColor;
  131. }
  132. // 获取当前路由页面信息
  133. const { style } = router.route()!;
  134. // 如果页面配置了导航栏背景色,使用页面配置
  135. if (style != null) {
  136. if (style.navigationBarBackgroundColor != null) {
  137. return style.navigationBarBackgroundColor as string;
  138. }
  139. }
  140. // 最后使用全局配置的导航栏背景色
  141. return getConfig("navBgColor");
  142. });
  143. // 计算文字颜色,优先级:props > 页面配置 > 全局配置
  144. const color = computed(() => {
  145. // 如果组件属性中指定了文字颜色,优先使用
  146. if (props.color != "") {
  147. return props.color;
  148. }
  149. // 获取当前路由页面信息
  150. const { style } = router.route()!;
  151. // 如果页面配置了导航栏文字样式,使用页面配置
  152. if (style != null) {
  153. if (style.navigationBarTextStyle != null) {
  154. return style.navigationBarTextStyle as string;
  155. }
  156. }
  157. // 最后使用全局配置的导航栏文字样式
  158. return getConfig("navTxtStyle");
  159. });
  160. // 导航栏样式
  161. const topbarStyle = computed(() => {
  162. const style = {
  163. paddingTop: offsetTop.value
  164. };
  165. // 不与 pt 样式中的背景色冲突
  166. if (pt.value.className == null || !pt.value.className.includes("bg-")) {
  167. style["backgroundColor"] = backgroundColor.value;
  168. }
  169. return style;
  170. });
  171. // 返回按钮点击事件
  172. function back() {
  173. if (props.backPath != "") {
  174. router.to(props.backPath);
  175. } else {
  176. if (router.isFirstPage()) {
  177. router.home();
  178. } else {
  179. router.back();
  180. }
  181. }
  182. }
  183. </script>
  184. <style lang="scss" scoped>
  185. .cl-topbar {
  186. &__inner {
  187. @apply flex flex-row items-center w-full;
  188. }
  189. &__icon {
  190. @apply flex items-center justify-center h-full;
  191. width: 30px;
  192. }
  193. &__prepend {
  194. @apply absolute left-0 top-0 z-10 h-full flex flex-row items-center;
  195. margin-left: 3px;
  196. }
  197. &__content {
  198. @apply flex flex-col items-center justify-center h-full;
  199. flex: 1;
  200. }
  201. &__append {
  202. @apply absolute right-0 top-0 z-10 h-full flex flex-row items-center;
  203. margin-right: 3px;
  204. }
  205. &--fixed {
  206. @apply fixed top-0 left-0 right-0 z-10 w-full;
  207. }
  208. }
  209. </style>