cl-topbar.uvue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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: parseClass([[!backable, 'opacity-50'], 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. backable: {
  80. type: Boolean,
  81. default: true
  82. },
  83. // 返回按钮的跳转路径
  84. backPath: {
  85. type: String,
  86. default: ""
  87. },
  88. // 返回按钮的图标
  89. backIcon: {
  90. type: String,
  91. default: "back"
  92. },
  93. // 是否使用安全区域顶部边距
  94. safeAreaTop: {
  95. type: Boolean,
  96. default: false
  97. },
  98. // 是否固定在顶部
  99. fixed: {
  100. type: Boolean,
  101. default: false
  102. },
  103. // 内容高度
  104. height: {
  105. type: [Number, String] as PropType<number | string | null>,
  106. default: null
  107. }
  108. });
  109. // 定义样式透传的类型接口
  110. type PassThrough = {
  111. className?: string;
  112. title?: PassThroughProps;
  113. back?: ClIconProps;
  114. };
  115. // 解析样式透传配置,返回处理后的样式对象
  116. const pt = computed(() => parsePt<PassThrough>(props.pt));
  117. // 使用设备安全区域顶部边距
  118. const offsetTop = computed(() => {
  119. if (props.safeAreaTop) {
  120. return getSafeAreaHeight("top") + "px";
  121. }
  122. return "0px";
  123. });
  124. // 计算内容高度
  125. const height = computed(() => {
  126. if (props.height == null) {
  127. return "44px";
  128. }
  129. return parseRpx(props.height!);
  130. });
  131. // 计算背景颜色,优先级:props > 页面配置 > 全局配置
  132. const backgroundColor = computed(() => {
  133. // 如果组件属性中指定了背景色,优先使用
  134. if (props.backgroundColor != "") {
  135. return props.backgroundColor;
  136. }
  137. // 获取当前路由页面信息
  138. const { style } = router.route()!;
  139. // 如果页面配置了导航栏背景色,使用页面配置
  140. if (style != null) {
  141. if (style.navigationBarBackgroundColor != null) {
  142. return style.navigationBarBackgroundColor as string;
  143. }
  144. }
  145. // 最后使用全局配置的导航栏背景色
  146. return getConfig("navBgColor");
  147. });
  148. // 计算文字颜色,优先级:props > 页面配置 > 全局配置
  149. const color = computed(() => {
  150. // 如果组件属性中指定了文字颜色,优先使用
  151. if (props.color != "") {
  152. return props.color;
  153. }
  154. // 获取当前路由页面信息
  155. const { style } = router.route()!;
  156. // 如果页面配置了导航栏文字样式,使用页面配置
  157. if (style != null) {
  158. if (style.navigationBarTextStyle != null) {
  159. return style.navigationBarTextStyle as string;
  160. }
  161. }
  162. // 最后使用全局配置的导航栏文字样式
  163. return getConfig("navTxtStyle");
  164. });
  165. // 导航栏样式
  166. const topbarStyle = computed(() => {
  167. const style = {
  168. paddingTop: offsetTop.value
  169. };
  170. // 不与 pt 样式中的背景色冲突
  171. if (pt.value.className == null || !pt.value.className.includes("bg-")) {
  172. style["backgroundColor"] = backgroundColor.value;
  173. }
  174. return style;
  175. });
  176. // 返回按钮点击事件
  177. function back() {
  178. if (props.backable) {
  179. if (props.backPath != "") {
  180. router.to(props.backPath);
  181. } else {
  182. if (router.isFirstPage()) {
  183. router.home();
  184. } else {
  185. router.back();
  186. }
  187. }
  188. }
  189. }
  190. </script>
  191. <style lang="scss" scoped>
  192. .cl-topbar {
  193. &__inner {
  194. @apply flex flex-row items-center w-full;
  195. }
  196. &__icon {
  197. @apply flex items-center justify-center h-full;
  198. width: 30px;
  199. }
  200. &__prepend {
  201. @apply absolute left-0 top-0 z-10 h-full flex flex-row items-center;
  202. margin-left: 3px;
  203. }
  204. &__content {
  205. @apply flex flex-col items-center justify-center h-full;
  206. flex: 1;
  207. }
  208. &__append {
  209. @apply absolute right-0 top-0 z-10 h-full flex flex-row items-center;
  210. margin-right: 3px;
  211. }
  212. &--fixed {
  213. @apply fixed top-0 left-0 right-0 z-10 w-full;
  214. }
  215. }
  216. </style>