cl-button.uvue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. <template>
  2. <view
  3. class="cl-button"
  4. :class="[
  5. `cl-button--${size}`,
  6. `cl-button--${type} `,
  7. {
  8. 'cl-button--loading': loading,
  9. 'cl-button--disabled': disabled,
  10. 'cl-button--text': text,
  11. 'cl-button--border': border,
  12. 'cl-button--rounded': rounded,
  13. 'cl-button--icon': isIcon,
  14. 'cl-button--hover': isHover,
  15. 'is-dark': isDark
  16. },
  17. isHover ? hoverClass : '',
  18. pt.className
  19. ]"
  20. :key="cache.key"
  21. :style="buttonStyle"
  22. @tap.stop="onTap"
  23. >
  24. <button
  25. class="cl-button__clicker"
  26. :disabled="isDisabled"
  27. :hover-class="hoverClass"
  28. :hover-stop-propagation="hoverStopPropagation"
  29. :hover-start-time="hoverStartTime"
  30. :hover-stay-time="hoverStayTime"
  31. :form-type="formType"
  32. :open-type="openType"
  33. :lang="lang"
  34. :session-from="sessionFrom"
  35. :send-message-title="sendMessageTitle"
  36. :send-message-path="sendMessagePath"
  37. :send-message-img="sendMessageImg"
  38. :show-message-card="showMessageCard"
  39. :app-parameter="appParameter"
  40. :group-id="groupId"
  41. :guild-id="guildId"
  42. :public-id="publicId"
  43. :phone-number-no-quota-toast="phoneNumberNoQuotaToast"
  44. :createliveactivity="createliveactivity"
  45. @getuserinfo="onGetUserInfo"
  46. @contact="onContact"
  47. @getphonenumber="onGetPhoneNumber"
  48. @error="onError"
  49. @opensetting="onOpenSetting"
  50. @launchapp="onLaunchApp"
  51. @chooseavatar="onChooseAvatar"
  52. @chooseaddress="onChooseAddress"
  53. @chooseinvoicetitle="onChooseInvoiceTitle"
  54. @addgroupapp="onAddGroupApp"
  55. @subscribe="onSubscribe"
  56. @login="onLogin"
  57. @getrealtimephonenumber="onGetRealtimePhoneNumber"
  58. @agreeprivacyauthorization="onAgreePrivacyAuthorization"
  59. @touchstart="onTouchStart"
  60. @touchend="onTouchEnd"
  61. @touchcancel="onTouchCancel"
  62. ></button>
  63. <cl-loading
  64. :color="loadingIcon.color"
  65. :size="loadingIcon.size"
  66. :pt="{
  67. className: parseClass(['mr-[10rpx]', pt.loading?.className])
  68. }"
  69. v-if="loading && !disabled"
  70. ></cl-loading>
  71. <cl-icon
  72. :name="icon"
  73. :color="leftIcon.color"
  74. :size="leftIcon.size"
  75. :pt="{
  76. className: parseClass([
  77. {
  78. 'mr-[8rpx]': !isIcon
  79. },
  80. pt.icon?.className
  81. ])
  82. }"
  83. v-if="icon"
  84. ></cl-icon>
  85. <template v-if="!isIcon">
  86. <cl-text
  87. :color="textColor"
  88. :pt="{
  89. className: parseClass([
  90. 'cl-button__label',
  91. {
  92. '!text-sm': size == 'small'
  93. },
  94. pt.label?.className
  95. ])
  96. }"
  97. >
  98. <slot></slot>
  99. </cl-text>
  100. <slot name="content"></slot>
  101. </template>
  102. </view>
  103. </template>
  104. <script setup lang="ts">
  105. import { computed, ref, useSlots, type PropType } from "vue";
  106. import { get, isDark, parseClass, parsePt, useCache } from "@/cool";
  107. import type { ClIconProps } from "../cl-icon/props";
  108. import type { ClButtonType, PassThroughProps, Size } from "../../types";
  109. import type { ClLoadingProps } from "../cl-loading/props";
  110. defineOptions({
  111. name: "cl-button"
  112. });
  113. // 组件属性定义
  114. const props = defineProps({
  115. // 样式穿透
  116. pt: {
  117. type: Object,
  118. default: () => ({})
  119. },
  120. // 按钮类型
  121. type: {
  122. type: String as PropType<ClButtonType>,
  123. default: "primary"
  124. },
  125. // 字体、图标颜色
  126. color: {
  127. type: String,
  128. default: ""
  129. },
  130. // 图标
  131. icon: {
  132. type: String,
  133. default: ""
  134. },
  135. // 文本按钮
  136. text: {
  137. type: Boolean,
  138. default: false
  139. },
  140. // 圆角按钮
  141. rounded: {
  142. type: Boolean,
  143. default: false
  144. },
  145. // 边框按钮
  146. border: {
  147. type: Boolean,
  148. default: false
  149. },
  150. // 加载状态
  151. loading: {
  152. type: Boolean,
  153. default: false
  154. },
  155. // 禁用状态
  156. disabled: {
  157. type: Boolean,
  158. default: false
  159. },
  160. // 按钮尺寸
  161. size: {
  162. type: String as PropType<Size>,
  163. default: "normal"
  164. },
  165. // 按钮点击态样式类
  166. hoverClass: {
  167. type: String,
  168. default: ""
  169. },
  170. // 是否阻止点击态冒泡
  171. hoverStopPropagation: {
  172. type: Boolean,
  173. default: false
  174. },
  175. // 按住后多久出现点击态
  176. hoverStartTime: {
  177. type: Number,
  178. default: 20
  179. },
  180. // 手指松开后点击态保留时间
  181. hoverStayTime: {
  182. type: Number,
  183. default: 70
  184. },
  185. // 表单提交类型
  186. formType: {
  187. type: String as PropType<"submit" | "reset">,
  188. default: ""
  189. },
  190. // 开放能力类型
  191. openType: {
  192. type: String as PropType<
  193. | "agreePrivacyAuthorization"
  194. | "feedback"
  195. | "share"
  196. | "getUserInfo"
  197. | "contact"
  198. | "getPhoneNumber"
  199. | "launchApp"
  200. | "openSetting"
  201. | "chooseAvatar"
  202. | "getAuthorize"
  203. | "lifestyle"
  204. | "contactShare"
  205. | "openGroupProfile"
  206. | "openGuildProfile"
  207. | "openPublicProfile"
  208. | "shareMessageToFriend"
  209. | "addFriend"
  210. | "addColorSign"
  211. | "addGroupApp"
  212. | "addToFavorites"
  213. | "chooseAddress"
  214. | "chooseInvoiceTitle"
  215. | "login"
  216. | "subscribe"
  217. | "favorite"
  218. | "watchLater"
  219. | "openProfile"
  220. | "liveActivity"
  221. | "getRealtimePhoneNumber"
  222. >,
  223. default: ""
  224. },
  225. // 语言
  226. lang: {
  227. type: String as PropType<"en" | "zh_CN" | "zh_TW">,
  228. default: "zh_CN"
  229. },
  230. // 会话来源
  231. sessionFrom: {
  232. type: String,
  233. default: ""
  234. },
  235. // 会话标题
  236. sendMessageTitle: {
  237. type: String,
  238. default: ""
  239. },
  240. // 会话路径
  241. sendMessagePath: {
  242. type: String,
  243. default: ""
  244. },
  245. // 会话图片
  246. sendMessageImg: {
  247. type: String,
  248. default: ""
  249. },
  250. // 显示会话卡片
  251. showMessageCard: {
  252. type: Boolean,
  253. default: false
  254. },
  255. // 打开 APP 时,向 APP 传递的参数
  256. appParameter: {
  257. type: String,
  258. default: ""
  259. },
  260. // 群ID
  261. groupId: {
  262. type: String,
  263. default: ""
  264. },
  265. // 公会ID
  266. guildId: {
  267. type: String,
  268. default: ""
  269. },
  270. // 公众号ID
  271. publicId: {
  272. type: String,
  273. default: ""
  274. },
  275. // 手机号获取失败时是否弹出错误提示
  276. phoneNumberNoQuotaToast: {
  277. type: Boolean,
  278. default: false
  279. },
  280. // 是否创建直播活动
  281. createliveactivity: {
  282. type: Boolean,
  283. default: false
  284. }
  285. });
  286. // 事件定义
  287. const emit = defineEmits([
  288. "click",
  289. "tap",
  290. "getuserinfo",
  291. "contact",
  292. "getphonenumber",
  293. "error",
  294. "opensetting",
  295. "launchapp",
  296. "chooseavatar",
  297. "chooseaddress",
  298. "chooseinvoicetitle",
  299. "addgroupapp",
  300. "subscribe",
  301. "login",
  302. "getrealtimephonenumber",
  303. "agreeprivacyauthorization"
  304. ]);
  305. const slots = useSlots();
  306. const { cache } = useCache(() => [props.type, props.text, props.disabled, props.loading]);
  307. // 样式穿透类型
  308. type PassThrough = {
  309. className?: string;
  310. label?: PassThroughProps;
  311. icon?: ClIconProps;
  312. loading?: ClLoadingProps;
  313. };
  314. // 样式穿透计算
  315. const pt = computed(() => parsePt<PassThrough>(props.pt));
  316. // 是否是图标按钮
  317. const isIcon = computed(() => get(slots, "default") == null && get(slots, "content") == null);
  318. // 文本颜色
  319. const textColor = computed(() => {
  320. if (props.color != "") {
  321. return props.color;
  322. }
  323. let color = "light";
  324. if (props.text) {
  325. color = props.type;
  326. if (props.disabled) {
  327. color = "disabled";
  328. }
  329. }
  330. if (props.type == "light") {
  331. if (!isDark.value) {
  332. color = "dark";
  333. }
  334. }
  335. return color;
  336. });
  337. // 图标信息
  338. const leftIcon = computed<ClIconProps>(() => {
  339. let color = textColor.value;
  340. let size: number | string;
  341. switch (props.size) {
  342. case "small":
  343. size = 26;
  344. break;
  345. default:
  346. size = 32;
  347. break;
  348. }
  349. const ptIcon = pt.value.icon;
  350. if (ptIcon != null) {
  351. color = ptIcon.color ?? color;
  352. size = ptIcon.size ?? size;
  353. }
  354. return {
  355. size,
  356. color
  357. };
  358. });
  359. // 加载图标信息
  360. const loadingIcon = computed<ClLoadingProps>(() => {
  361. let color = textColor.value;
  362. let size: number | string;
  363. switch (props.size) {
  364. case "small":
  365. size = 22;
  366. break;
  367. default:
  368. size = 24;
  369. break;
  370. }
  371. const ptIcon = pt.value.loading;
  372. if (ptIcon != null) {
  373. color = ptIcon.color ?? color;
  374. size = ptIcon.size ?? size;
  375. }
  376. return {
  377. size,
  378. color
  379. };
  380. });
  381. // 按钮样式
  382. const buttonStyle = computed(() => {
  383. const style = {};
  384. if (props.color != "") {
  385. style["border-color"] = props.color;
  386. }
  387. return style;
  388. });
  389. // 是否禁用状态
  390. const isDisabled = computed(() => props.disabled || props.loading);
  391. // 点击事件处理
  392. function onTap(e: UniPointerEvent) {
  393. if (isDisabled.value) return;
  394. emit("click", e);
  395. emit("tap", e);
  396. }
  397. // 获取用户信息事件处理
  398. function onGetUserInfo(e: UniEvent) {
  399. emit("getuserinfo", e);
  400. }
  401. // 客服消息事件处理
  402. function onContact(e: UniEvent) {
  403. emit("contact", e);
  404. }
  405. // 获取手机号事件处理
  406. function onGetPhoneNumber(e: UniEvent) {
  407. emit("getphonenumber", e);
  408. }
  409. // 错误事件处理
  410. function onError(e: UniEvent) {
  411. emit("error", e);
  412. }
  413. // 打开设置事件处理
  414. function onOpenSetting(e: UniEvent) {
  415. emit("opensetting", e);
  416. }
  417. // 打开APP事件处理
  418. function onLaunchApp(e: UniEvent) {
  419. emit("launchapp", e);
  420. }
  421. // 选择头像事件处理
  422. function onChooseAvatar(e: UniEvent) {
  423. emit("chooseavatar", e);
  424. }
  425. // 选择收货地址事件处理
  426. function onChooseAddress(e: UniEvent) {
  427. emit("chooseaddress", e);
  428. }
  429. // 选择发票抬头事件处理
  430. function onChooseInvoiceTitle(e: UniEvent) {
  431. emit("chooseinvoicetitle", e);
  432. }
  433. // 添加群应用事件处理
  434. function onAddGroupApp(e: UniEvent) {
  435. emit("addgroupapp", e);
  436. }
  437. // 订阅消息事件处理
  438. function onSubscribe(e: UniEvent) {
  439. emit("subscribe", e);
  440. }
  441. // 登录事件处理
  442. function onLogin(e: UniEvent) {
  443. emit("login", e);
  444. }
  445. // 获取实时手机号事件处理
  446. function onGetRealtimePhoneNumber(e: UniEvent) {
  447. emit("getrealtimephonenumber", e);
  448. }
  449. // 同意隐私授权事件处理
  450. function onAgreePrivacyAuthorization(e: UniEvent) {
  451. emit("agreeprivacyauthorization", e);
  452. }
  453. // 点击态状态
  454. const isHover = ref(false);
  455. // 触摸开始事件处理
  456. function onTouchStart() {
  457. if (!isDisabled.value) {
  458. isHover.value = true;
  459. }
  460. }
  461. // 触摸结束事件处理
  462. function onTouchEnd() {
  463. isHover.value = false;
  464. }
  465. // 触摸取消事件处理
  466. function onTouchCancel() {
  467. isHover.value = false;
  468. }
  469. </script>
  470. <style lang="scss" scoped>
  471. @mixin button-type($color) {
  472. @apply bg-#{$color}-500;
  473. &.cl-button--hover {
  474. @apply bg-#{$color}-600;
  475. }
  476. &.cl-button--text {
  477. background-color: transparent;
  478. &.cl-button--hover {
  479. @apply bg-transparent opacity-50;
  480. }
  481. }
  482. &.cl-button--border {
  483. @apply border-#{$color}-500;
  484. }
  485. }
  486. .cl-button {
  487. @apply flex flex-row items-center justify-center relative;
  488. @apply border border-transparent border-solid;
  489. overflow: visible;
  490. transition-duration: 0.3s;
  491. transition-property: background-color, border-color, opacity;
  492. &__clicker {
  493. @apply absolute p-0 m-0;
  494. @apply w-full h-full;
  495. @apply opacity-0;
  496. @apply z-10;
  497. }
  498. &--small {
  499. padding: 6rpx 14rpx;
  500. border-radius: 12rpx;
  501. &.cl-button--icon {
  502. padding: 10rpx;
  503. }
  504. }
  505. &--normal {
  506. padding: 10rpx 28rpx;
  507. border-radius: 16rpx;
  508. &.cl-button--icon {
  509. padding: 14rpx;
  510. }
  511. }
  512. &--large {
  513. padding: 14rpx 32rpx;
  514. border-radius: 20rpx;
  515. &.cl-button--icon {
  516. padding: 18rpx;
  517. }
  518. }
  519. &--rounded {
  520. @apply rounded-full;
  521. }
  522. &--primary {
  523. @include button-type("primary");
  524. }
  525. &--warn {
  526. @include button-type("yellow");
  527. }
  528. &--error {
  529. @include button-type("red");
  530. }
  531. &--info {
  532. @include button-type("surface");
  533. }
  534. &--success {
  535. @include button-type("green");
  536. }
  537. &--light {
  538. @apply border-surface-700;
  539. &.cl-button--hover {
  540. @apply bg-surface-100;
  541. }
  542. &.is-dark {
  543. &.cl-button--hover {
  544. @apply bg-surface-700;
  545. }
  546. }
  547. }
  548. &--dark {
  549. @apply bg-surface-700;
  550. &.cl-button--hover {
  551. @apply bg-surface-800;
  552. }
  553. }
  554. &--disabled {
  555. @apply bg-surface-300;
  556. &.cl-button--border {
  557. @apply border-surface-300;
  558. }
  559. }
  560. &--loading {
  561. opacity: 0.6;
  562. }
  563. &.is-dark {
  564. &.cl-button--disabled {
  565. @apply bg-surface-400;
  566. &.cl-button--border {
  567. @apply border-surface-500;
  568. }
  569. }
  570. &.cl-button--text {
  571. @apply bg-transparent;
  572. }
  573. &.cl-button--light {
  574. @apply border-surface-500;
  575. }
  576. }
  577. }
  578. .cl-button {
  579. & + .cl-button {
  580. @apply ml-2;
  581. }
  582. }
  583. </style>