su-navbar.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <template>
  2. <view class="uni-navbar" :class="{ 'uni-dark': dark }">
  3. <view
  4. :class="{
  5. 'uni-navbar--fixed': fixed,
  6. 'uni-navbar--shadow': shadow,
  7. 'uni-navbar--border': border,
  8. }"
  9. class="uni-navbar__content"
  10. >
  11. <view class="fixed-bg" :class="[opacity ? '' : opacityBgUi]"></view>
  12. <su-status-bar v-if="statusBar" />
  13. <view
  14. :style="{
  15. color: themeColor,
  16. height: navbarHeight,
  17. background: backgroundColor,
  18. }"
  19. class="uni-navbar__header"
  20. >
  21. <view
  22. class="uni-navbar__header-btns uni-navbar__header-btns-left"
  23. :style="{ width: leftIconWidth }"
  24. >
  25. <slot name="left">
  26. <view class="uni-navbar__content_view" v-if="leftIcon.length > 0">
  27. <view class="icon-box ss-flex">
  28. <view class="icon-button icon-button-left ss-flex ss-row-center" @tap="onClickLeft">
  29. <text class="sicon-back" v-if="hasHistory" />
  30. <text class="sicon-home" v-else />
  31. </view>
  32. <view class="line"></view>
  33. <view
  34. class="icon-button icon-button-right ss-flex ss-row-center"
  35. @tap="showMenuTools"
  36. >
  37. <text class="sicon-more" />
  38. </view>
  39. </view>
  40. </view>
  41. <view
  42. :class="{ 'uni-navbar-btn-icon-left': !leftIcon.length > 0 }"
  43. class="uni-navbar-btn-text"
  44. v-if="
  45. titleAlign === 'left' &&
  46. title.length &&
  47. sheep.$platform.name !== 'WechatOfficialAccount'
  48. "
  49. >
  50. <text :style="{ color: themeColor, fontSize: '18px' }">{{ title }}</text>
  51. </view>
  52. </slot>
  53. </view>
  54. <view v-if="tools === 'search'" class="ss-flex-1">
  55. <slot name="center">
  56. <uni-search-bar
  57. class="ss-flex-1 search-box"
  58. :radius="20"
  59. placeholder="请输入关键词"
  60. cancelButton="none"
  61. v-model="defaultSearch"
  62. @confirm="onSearch"
  63. />
  64. </slot>
  65. </view>
  66. <view v-else class="uni-navbar__header-container" @tap="onClickTitle">
  67. <slot name="center">
  68. <view
  69. v-if="tools === 'title' && titleAlign === 'center' && title.length"
  70. class="uni-navbar__header-container-inner"
  71. >
  72. <text :style="{ color: themeColor, fontSize: '36rpx' }" class="ss-line-1">{{
  73. title
  74. }}</text>
  75. </view>
  76. </slot>
  77. </view>
  78. </view>
  79. </view>
  80. <view class="uni-navbar__placeholder" v-if="placeholder">
  81. <su-status-bar v-if="statusBar" />
  82. <view class="uni-navbar__placeholder-view" :style="{ height: navbarHeight }" />
  83. </view>
  84. <!-- 头部问题 -->
  85. <!-- #ifdef MP -->
  86. <!-- <view :style="[capsuleStyle]"></view> -->
  87. <!-- #endif -->
  88. </view>
  89. </template>
  90. <script setup>
  91. import sheep from '@/sheep';
  92. import { onLoad } from '@dcloudio/uni-app';
  93. import { showMenuTools, closeMenuTools } from '@/sheep/hooks/useModal';
  94. import { computed, ref } from 'vue';
  95. /**
  96. * NavBar 自定义导航栏
  97. * @description 导航栏组件,主要用于头部导航
  98. * @property {Boolean} dark 开启黑暗模式
  99. * @property {String} title 标题文字
  100. * @property {String} rightText 右侧按钮文本
  101. * @property {String} leftIcon 左侧按钮图标
  102. * @property {String} rightIcon 右侧按钮图标
  103. * @property {String} color 图标和文字颜色
  104. * @property {String} backgroundColor 导航栏背景颜色
  105. * @property {Boolean} fixed = [true|false] 是否固定顶部
  106. * @property {Boolean} statusBar = [true|false] 是否包含状态栏
  107. * @property {Boolean} shadow = [true|false] 导航栏下是否有阴影
  108. * @event {Function} clickLeft 左侧按钮点击时触发
  109. * @event {Function} clickRight 右侧按钮点击时触发
  110. * @event {Function} clickTitle 中间标题点击时触发
  111. */
  112. const getVal = (val) => (typeof val === 'number' ? val + 'px' : val);
  113. const emits = defineEmits(['clickLeft', 'clickRight', 'clickTitle', 'search']);
  114. const props = defineProps({
  115. dark: {
  116. type: Boolean,
  117. default: false,
  118. },
  119. modelValue: {
  120. type: String,
  121. default: '',
  122. },
  123. title: {
  124. type: String,
  125. default: '',
  126. },
  127. titleAlign: {
  128. type: String,
  129. default: 'center', // left | center
  130. },
  131. rightText: {
  132. type: String,
  133. default: '',
  134. },
  135. leftIcon: {
  136. type: String,
  137. default: 'left',
  138. },
  139. rightIcon: {
  140. type: String,
  141. default: '',
  142. },
  143. fixed: {
  144. type: [Boolean, String],
  145. default: true,
  146. },
  147. placeholder: {
  148. type: [Boolean, String],
  149. default: true,
  150. },
  151. color: {
  152. type: String,
  153. default: '',
  154. },
  155. backgroundColor: {
  156. type: String,
  157. default: '',
  158. },
  159. opacity: {
  160. type: [Boolean, String],
  161. default: false,
  162. },
  163. opacityBgUi: {
  164. type: String,
  165. default: 'bg-white',
  166. },
  167. statusBar: {
  168. type: [Boolean, String],
  169. default: false,
  170. },
  171. shadow: {
  172. type: [Boolean, String],
  173. default: false,
  174. },
  175. border: {
  176. type: [Boolean, String],
  177. default: false,
  178. },
  179. height: {
  180. type: [Number, String],
  181. default: 44,
  182. },
  183. leftWidth: {
  184. type: [Number, String],
  185. default: 80,
  186. },
  187. rightWidth: {
  188. type: [Number, String],
  189. default: 0,
  190. },
  191. tools: {
  192. type: String,
  193. default: 'title',
  194. },
  195. defaultSearch: {
  196. type: String,
  197. default: '',
  198. },
  199. });
  200. const capsuleStyle = computed(() => {
  201. return {
  202. width: sheep.$platform.capsule.width + 'px',
  203. height: sheep.$platform.capsule.height + 'px',
  204. margin: '0 ' + (sheep.$platform.device.windowWidth - sheep.$platform.capsule.right) + 'px',
  205. };
  206. });
  207. const themeBgColor = computed(() => {
  208. if (props.dark) {
  209. // 默认值
  210. if (props.backgroundColor) {
  211. return props.backgroundColor;
  212. } else {
  213. return props.dark ? '#333' : '#FFF';
  214. }
  215. }
  216. return props.backgroundColor || '#FFF';
  217. });
  218. const themeColor = computed(() => {
  219. if (props.dark) {
  220. // 默认值
  221. if (props.color) {
  222. return props.color;
  223. } else {
  224. return props.dark ? '#fff' : '#333';
  225. }
  226. }
  227. return props.color || '#333';
  228. });
  229. const navbarHeight = computed(() => {
  230. return getVal(props.height);
  231. });
  232. const leftIconWidth = computed(() => {
  233. return getVal(props.leftWidth);
  234. });
  235. const rightIconWidth = computed(() => {
  236. return getVal(props.rightWidth);
  237. });
  238. function onSearch(e) {
  239. emits('search', e.value);
  240. }
  241. onLoad(() => {
  242. if (uni.report && props.title !== '') {
  243. uni.report('title', props.title);
  244. }
  245. });
  246. const hasHistory = sheep.$router.hasHistory();
  247. function onClickLeft() {
  248. if (hasHistory) {
  249. sheep.$router.back();
  250. } else {
  251. sheep.$router.go('/pages/index/index');
  252. }
  253. emits('clickLeft');
  254. }
  255. function onClickRight() {
  256. showMenuTools();
  257. }
  258. function onClickTitle() {
  259. emits('clickTitle');
  260. }
  261. </script>
  262. <style lang="scss" scoped>
  263. .bg-main {
  264. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient)) !important;
  265. color: #fff !important;
  266. }
  267. .icon-box {
  268. background: #ffffff;
  269. box-shadow: 0px 0px 4rpx rgba(51, 51, 51, 0.08), 0px 4rpx 6rpx 2rpx rgba(102, 102, 102, 0.12);
  270. border-radius: 30rpx;
  271. width: 134rpx;
  272. height: 56rpx;
  273. margin-left: 8rpx;
  274. .line {
  275. width: 2rpx;
  276. height: 24rpx;
  277. background: #e5e5e7;
  278. }
  279. .sicon-back {
  280. font-size: 32rpx;
  281. color: #000;
  282. }
  283. .sicon-home {
  284. font-size: 32rpx;
  285. color: #000;
  286. }
  287. .sicon-more {
  288. font-size: 32rpx;
  289. color: #000;
  290. }
  291. .icon-button {
  292. width: 67rpx;
  293. height: 56rpx;
  294. &-left:hover {
  295. background: rgba(0, 0, 0, 0.16);
  296. border-radius: 30rpx 0px 0px 30rpx;
  297. }
  298. &-right:hover {
  299. background: rgba(0, 0, 0, 0.16);
  300. border-radius: 0px 30rpx 30rpx 0px;
  301. }
  302. }
  303. }
  304. $nav-height: 44px;
  305. .fixed-bg {
  306. position: absolute;
  307. width: 100%;
  308. height: 100%;
  309. top: 0;
  310. z-index: 1;
  311. pointer-events: none;
  312. }
  313. .uni-nav-bar-text {
  314. /* #ifdef APP-PLUS */
  315. font-size: 34rpx;
  316. /* #endif */
  317. /* #ifndef APP-PLUS */
  318. font-size: 14px;
  319. /* #endif */
  320. }
  321. .uni-nav-bar-right-text {
  322. font-size: 12px;
  323. }
  324. .uni-navbar__content {
  325. position: relative;
  326. // background-color: #fff;
  327. // box-sizing: border-box;
  328. background-color: transparent;
  329. }
  330. .uni-navbar__content_view {
  331. // box-sizing: border-box;
  332. }
  333. .uni-navbar-btn-text {
  334. /* #ifndef APP-NVUE */
  335. display: flex;
  336. /* #endif */
  337. flex-direction: column;
  338. justify-content: flex-start;
  339. align-items: center;
  340. line-height: 18px;
  341. }
  342. .uni-navbar__header {
  343. /* #ifndef APP-NVUE */
  344. display: flex;
  345. /* #endif */
  346. padding: 0 10px;
  347. flex-direction: row;
  348. justify-content: space-between;
  349. height: $nav-height;
  350. font-size: 12px;
  351. position: relative;
  352. z-index: 2;
  353. }
  354. .uni-navbar__header-btns {
  355. /* #ifndef APP-NVUE */
  356. overflow: hidden;
  357. display: flex;
  358. /* #endif */
  359. flex-wrap: nowrap;
  360. flex-direction: row;
  361. // min-width: 120rpx;
  362. min-width: 40rpx;
  363. // padding: 0 6px;
  364. justify-content: center;
  365. align-items: center;
  366. /* #ifdef H5 */
  367. cursor: pointer;
  368. /* #endif */
  369. }
  370. .uni-navbar__header-btns-left {
  371. /* #ifndef APP-NVUE */
  372. display: flex;
  373. /* #endif */
  374. width: 120rpx;
  375. justify-content: flex-start;
  376. align-items: center;
  377. }
  378. .uni-navbar__header-btns-right {
  379. /* #ifndef APP-NVUE */
  380. display: flex;
  381. /* #endif */
  382. flex-direction: row;
  383. // width: 150rpx;
  384. // padding-right: 30rpx;
  385. justify-content: flex-end;
  386. align-items: center;
  387. }
  388. .uni-navbar__header-container {
  389. /* #ifndef APP-NVUE */
  390. // display: flex;
  391. /* #endif */
  392. // flex: 1;
  393. // padding: 0 10px;
  394. // overflow: hidden;
  395. position: absolute;
  396. left: 50%;
  397. transform: translateX(-50%) translateY(-50%);
  398. top: 50%;
  399. }
  400. .uni-navbar__header-container-inner {
  401. /* #ifndef APP-NVUE */
  402. display: flex;
  403. /* #endif */
  404. flex: 1;
  405. flex-direction: row;
  406. align-items: center;
  407. justify-content: center;
  408. font-size: 12px;
  409. overflow: hidden;
  410. // box-sizing: border-box;
  411. }
  412. .uni-navbar__placeholder-view {
  413. height: $nav-height;
  414. }
  415. .uni-navbar--fixed {
  416. position: fixed;
  417. z-index: 998;
  418. /* #ifdef H5 */
  419. left: var(--window-left);
  420. right: var(--window-right);
  421. /* #endif */
  422. /* #ifndef H5 */
  423. left: 0;
  424. right: 0;
  425. /* #endif */
  426. }
  427. .uni-navbar--shadow {
  428. box-shadow: 0 1px 6px #ccc;
  429. }
  430. .uni-navbar--border {
  431. border-bottom-width: 1rpx;
  432. border-bottom-style: solid;
  433. border-bottom-color: #eee;
  434. }
  435. .uni-ellipsis-1 {
  436. overflow: hidden;
  437. /* #ifndef APP-NVUE */
  438. white-space: nowrap;
  439. text-overflow: ellipsis;
  440. /* #endif */
  441. /* #ifdef APP-NVUE */
  442. lines: 1;
  443. text-overflow: ellipsis;
  444. /* #endif */
  445. }
  446. // 暗主题配置
  447. .uni-dark {
  448. }
  449. </style>