cl-topbar.uvue 5.0 KB

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