su-navbar.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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="searchModel"
  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 searchModel = computed(() => {
  208. return props.defaultSearch
  209. })
  210. const themeBgColor = computed(() => {
  211. if (props.dark) {
  212. // 默认值
  213. if (props.backgroundColor) {
  214. return props.backgroundColor;
  215. } else {
  216. return props.dark ? '#333' : '#FFF';
  217. }
  218. }
  219. return props.backgroundColor || '#FFF';
  220. });
  221. const themeColor = computed(() => {
  222. if (props.dark) {
  223. // 默认值
  224. if (props.color) {
  225. return props.color;
  226. } else {
  227. return props.dark ? '#fff' : '#333';
  228. }
  229. }
  230. return props.color || '#333';
  231. });
  232. const navbarHeight = computed(() => {
  233. return getVal(props.height);
  234. });
  235. const leftIconWidth = computed(() => {
  236. return getVal(props.leftWidth);
  237. });
  238. const rightIconWidth = computed(() => {
  239. return getVal(props.rightWidth);
  240. });
  241. function onSearch(e) {
  242. emits('search', e.value);
  243. }
  244. onLoad(() => {
  245. if (uni.report && props.title !== '') {
  246. uni.report('title', props.title);
  247. }
  248. });
  249. const hasHistory = sheep.$router.hasHistory();
  250. function onClickLeft() {
  251. if (hasHistory) {
  252. sheep.$router.back();
  253. } else {
  254. sheep.$router.go('/pages/index/index');
  255. }
  256. emits('clickLeft');
  257. }
  258. function onClickRight() {
  259. showMenuTools();
  260. }
  261. function onClickTitle() {
  262. emits('clickTitle');
  263. }
  264. </script>
  265. <style lang="scss" scoped>
  266. .bg-main {
  267. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient)) !important;
  268. color: #fff !important;
  269. }
  270. .icon-box {
  271. background: #ffffff;
  272. box-shadow: 0px 0px 4rpx rgba(51, 51, 51, 0.08), 0px 4rpx 6rpx 2rpx rgba(102, 102, 102, 0.12);
  273. border-radius: 30rpx;
  274. width: 134rpx;
  275. height: 56rpx;
  276. margin-left: 8rpx;
  277. .line {
  278. width: 2rpx;
  279. height: 24rpx;
  280. background: #e5e5e7;
  281. }
  282. .sicon-back {
  283. font-size: 32rpx;
  284. color: #000;
  285. }
  286. .sicon-home {
  287. font-size: 32rpx;
  288. color: #000;
  289. }
  290. .sicon-more {
  291. font-size: 32rpx;
  292. color: #000;
  293. }
  294. .icon-button {
  295. width: 67rpx;
  296. height: 56rpx;
  297. &-left:hover {
  298. background: rgba(0, 0, 0, 0.16);
  299. border-radius: 30rpx 0px 0px 30rpx;
  300. }
  301. &-right:hover {
  302. background: rgba(0, 0, 0, 0.16);
  303. border-radius: 0px 30rpx 30rpx 0px;
  304. }
  305. }
  306. }
  307. $nav-height: 44px;
  308. .fixed-bg {
  309. position: absolute;
  310. width: 100%;
  311. height: 100%;
  312. top: 0;
  313. z-index: 1;
  314. pointer-events: none;
  315. }
  316. .uni-nav-bar-text {
  317. /* #ifdef APP-PLUS */
  318. font-size: 34rpx;
  319. /* #endif */
  320. /* #ifndef APP-PLUS */
  321. font-size: 14px;
  322. /* #endif */
  323. }
  324. .uni-nav-bar-right-text {
  325. font-size: 12px;
  326. }
  327. .uni-navbar__content {
  328. position: relative;
  329. // background-color: #fff;
  330. // box-sizing: border-box;
  331. background-color: transparent;
  332. }
  333. .uni-navbar__content_view {
  334. // box-sizing: border-box;
  335. }
  336. .uni-navbar-btn-text {
  337. /* #ifndef APP-NVUE */
  338. display: flex;
  339. /* #endif */
  340. flex-direction: column;
  341. justify-content: flex-start;
  342. align-items: center;
  343. line-height: 18px;
  344. }
  345. .uni-navbar__header {
  346. /* #ifndef APP-NVUE */
  347. display: flex;
  348. /* #endif */
  349. padding: 0 10px;
  350. flex-direction: row;
  351. justify-content: space-between;
  352. height: $nav-height;
  353. font-size: 12px;
  354. position: relative;
  355. z-index: 2;
  356. }
  357. .uni-navbar__header-btns {
  358. /* #ifndef APP-NVUE */
  359. overflow: hidden;
  360. display: flex;
  361. /* #endif */
  362. flex-wrap: nowrap;
  363. flex-direction: row;
  364. // min-width: 120rpx;
  365. min-width: 40rpx;
  366. // padding: 0 6px;
  367. justify-content: center;
  368. align-items: center;
  369. /* #ifdef H5 */
  370. cursor: pointer;
  371. /* #endif */
  372. }
  373. .uni-navbar__header-btns-left {
  374. /* #ifndef APP-NVUE */
  375. display: flex;
  376. /* #endif */
  377. width: 120rpx;
  378. justify-content: flex-start;
  379. align-items: center;
  380. }
  381. .uni-navbar__header-btns-right {
  382. /* #ifndef APP-NVUE */
  383. display: flex;
  384. /* #endif */
  385. flex-direction: row;
  386. // width: 150rpx;
  387. // padding-right: 30rpx;
  388. justify-content: flex-end;
  389. align-items: center;
  390. }
  391. .uni-navbar__header-container {
  392. /* #ifndef APP-NVUE */
  393. // display: flex;
  394. /* #endif */
  395. // flex: 1;
  396. // padding: 0 10px;
  397. // overflow: hidden;
  398. position: absolute;
  399. left: 50%;
  400. transform: translateX(-50%) translateY(-50%);
  401. top: 50%;
  402. }
  403. .uni-navbar__header-container-inner {
  404. /* #ifndef APP-NVUE */
  405. display: flex;
  406. /* #endif */
  407. flex: 1;
  408. flex-direction: row;
  409. align-items: center;
  410. justify-content: center;
  411. font-size: 12px;
  412. overflow: hidden;
  413. // box-sizing: border-box;
  414. }
  415. .uni-navbar__placeholder-view {
  416. height: $nav-height;
  417. }
  418. .uni-navbar--fixed {
  419. position: fixed;
  420. z-index: 998;
  421. /* #ifdef H5 */
  422. left: var(--window-left);
  423. right: var(--window-right);
  424. /* #endif */
  425. /* #ifndef H5 */
  426. left: 0;
  427. right: 0;
  428. /* #endif */
  429. }
  430. .uni-navbar--shadow {
  431. box-shadow: 0 1px 6px #ccc;
  432. }
  433. .uni-navbar--border {
  434. border-bottom-width: 1rpx;
  435. border-bottom-style: solid;
  436. border-bottom-color: #eee;
  437. }
  438. .uni-ellipsis-1 {
  439. overflow: hidden;
  440. /* #ifndef APP-NVUE */
  441. white-space: nowrap;
  442. text-overflow: ellipsis;
  443. /* #endif */
  444. /* #ifdef APP-NVUE */
  445. lines: 1;
  446. text-overflow: ellipsis;
  447. /* #endif */
  448. }
  449. // 暗主题配置
  450. .uni-dark {
  451. }
  452. </style>