blob: 82c3a6f08b37fb5af7c4c7be82109b7775f09277 [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<template>
2 <button
3 id="u-wave-btn"
4 class="u-btn u-line-1 u-fix-ios-appearance"
5 :class="[
6 'u-size-' + size,
7 plain ? 'u-btn--' + type + '--plain' : '',
8 loading ? 'u-loading' : '',
9 shape == 'circle' ? 'u-round-circle' : '',
10 hairLine ? showHairLineBorder : 'u-btn--bold-border',
11 'u-btn--' + type,
12 disabled ? `u-btn--${type}--disabled` : '',
13 ]"
14 :hover-start-time="Number(hoverStartTime)"
15 :hover-stay-time="Number(hoverStayTime)"
16 :disabled="disabled"
17 :form-type="formType"
18 :open-type="openType"
19 :app-parameter="appParameter"
20 :hover-stop-propagation="hoverStopPropagation"
21 :send-message-title="sendMessageTitle"
22 send-message-path="sendMessagePath"
23 :lang="lang"
24 :data-name="dataName"
25 :session-from="sessionFrom"
26 :send-message-img="sendMessageImg"
27 :show-message-card="showMessageCard"
28 @getphonenumber="getphonenumber"
29 @getuserinfo="getuserinfo"
30 @error="error"
31 @opensetting="opensetting"
32 @launchapp="launchapp"
33 :style="[customStyle, {
34 overflow: ripple ? 'hidden' : 'visible'
35 }]"
36 @tap.stop="click($event)"
37 :hover-class="getHoverClass"
38 :loading="loading"
39 >
40 <slot></slot>
41 <view
42 v-if="ripple"
43 class="u-wave-ripple"
44 :class="[waveActive ? 'u-wave-active' : '']"
45 :style="{
46 top: rippleTop + 'px',
47 left: rippleLeft + 'px',
48 width: fields.targetWidth + 'px',
49 height: fields.targetWidth + 'px',
50 'background-color': rippleBgColor || 'rgba(0, 0, 0, 0.15)'
51 }"
52 ></view>
53 </button>
54</template>
55
56<script>
57/**
58 * button 按钮
59 * @description Button 按钮
60 * @tutorial https://www.uviewui.com/components/button.html
61 * @property {String} size 按钮的大小
62 * @property {Boolean} ripple 是否开启点击水波纹效果
63 * @property {String} ripple-bg-color 水波纹的背景色,ripple为true时有效
64 * @property {String} type 按钮的样式类型
65 * @property {Boolean} plain 按钮是否镂空,背景色透明
66 * @property {Boolean} disabled 是否禁用
67 * @property {Boolean} hair-line 是否显示按钮的细边框(默认true)
68 * @property {Boolean} shape 按钮外观形状,见文档说明
69 * @property {Boolean} loading 按钮名称前是否带 loading 图标(App-nvue 平台,在 ios 上为雪花,Android上为圆圈)
70 * @property {String} form-type 用于 <form> 组件,点击分别会触发 <form> 组件的 submit/reset 事件
71 * @property {String} open-type 开放能力
72 * @property {String} data-name 额外传参参数,用于小程序的data-xxx属性,通过target.dataset.name获取
73 * @property {String} hover-class 指定按钮按下去的样式类。当 hover-class="none" 时,没有点击态效果(App-nvue 平台暂不支持)
74 * @property {Number} hover-start-time 按住后多久出现点击态,单位毫秒
75 * @property {Number} hover-stay-time 手指松开后点击态保留时间,单位毫秒
76 * @property {Object} custom-style 对按钮的自定义样式,对象形式,见文档说明
77 * @event {Function} click 按钮点击
78 * @event {Function} getphonenumber open-type="getPhoneNumber"时有效
79 * @event {Function} getuserinfo 用户点击该按钮时,会返回获取到的用户信息,从返回参数的detail中获取到的值同uni.getUserInfo
80 * @event {Function} error 当使用开放能力时,发生错误的回调
81 * @event {Function} opensetting 在打开授权设置页并关闭后回调
82 * @event {Function} launchapp 打开 APP 成功的回调
83 * @example <u-button>月落</u-button>
84 */
85export default {
86 name: 'u-button',
87 props: {
88 // 是否细边框
89 hairLine: {
90 type: Boolean,
91 default: true
92 },
93 // 按钮的预置样式,default,primary,error,warning,success
94 type: {
95 type: String,
96 default: 'default'
97 },
98 // 按钮尺寸,default,medium,mini
99 size: {
100 type: String,
101 default: 'default'
102 },
103 // 按钮形状,circle(两边为半圆),square(带圆角)
104 shape: {
105 type: String,
106 default: 'square'
107 },
108 // 按钮是否镂空
109 plain: {
110 type: Boolean,
111 default: false
112 },
113 // 是否禁止状态
114 disabled: {
115 type: Boolean,
116 default: false
117 },
118 // 是否加载中
119 loading: {
120 type: Boolean,
121 default: false
122 },
123 // 开放能力,具体请看uniapp稳定关于button组件部分说明
124 // https://uniapp.dcloud.io/component/button
125 openType: {
126 type: String,
127 default: ''
128 },
129 // 用于 <form> 组件,点击分别会触发 <form> 组件的 submit/reset 事件
130 // 取值为submit(提交表单),reset(重置表单)
131 formType: {
132 type: String,
133 default: ''
134 },
135 // 打开 APP 时,向 APP 传递的参数,open-type=launchApp时有效
136 // 只微信小程序、QQ小程序有效
137 appParameter: {
138 type: String,
139 default: ''
140 },
141 // 指定是否阻止本节点的祖先节点出现点击态,微信小程序有效
142 hoverStopPropagation: {
143 type: Boolean,
144 default: false
145 },
146 // 指定返回用户信息的语言,zh_CN 简体中文,zh_TW 繁体中文,en 英文。只微信小程序有效
147 lang: {
148 type: String,
149 default: 'en'
150 },
151 // 会话来源,open-type="contact"时有效。只微信小程序有效
152 sessionFrom: {
153 type: String,
154 default: ''
155 },
156 // 会话内消息卡片标题,open-type="contact"时有效
157 // 默认当前标题,只微信小程序有效
158 sendMessageTitle: {
159 type: String,
160 default: ''
161 },
162 // 会话内消息卡片点击跳转小程序路径,open-type="contact"时有效
163 // 默认当前分享路径,只微信小程序有效
164 sendMessagePath: {
165 type: String,
166 default: ''
167 },
168 // 会话内消息卡片图片,open-type="contact"时有效
169 // 默认当前页面截图,只微信小程序有效
170 sendMessageImg: {
171 type: String,
172 default: ''
173 },
174 // 是否显示会话内消息卡片,设置此参数为 true,用户进入客服会话会在右下角显示"可能要发送的小程序"提示,
175 // 用户点击后可以快速发送小程序消息,open-type="contact"时有效
176 showMessageCard: {
177 type: Boolean,
178 default: false
179 },
180 // 手指按(触摸)按钮时按钮时的背景颜色
181 hoverBgColor: {
182 type: String,
183 default: ''
184 },
185 // 水波纹的背景颜色
186 rippleBgColor: {
187 type: String,
188 default: ''
189 },
190 // 是否开启水波纹效果
191 ripple: {
192 type: Boolean,
193 default: false
194 },
195 // 按下的类名
196 hoverClass: {
197 type: String,
198 default: ''
199 },
200 // 自定义样式,对象形式
201 customStyle: {
202 type: Object,
203 default() {
204 return {};
205 }
206 },
207 // 额外传参参数,用于小程序的data-xxx属性,通过target.dataset.name获取
208 dataName: {
209 type: String,
210 default: ''
211 },
212 // 节流,一定时间内只能触发一次
213 throttleTime: {
214 type: [String, Number],
215 default: 1000
216 },
217 // 按住后多久出现点击态,单位毫秒
218 hoverStartTime: {
219 type: [String, Number],
220 default: 20
221 },
222 // 手指松开后点击态保留时间,单位毫秒
223 hoverStayTime: {
224 type: [String, Number],
225 default: 150
226 },
227 },
228 computed: {
229 // 当没有传bgColor变量时,按钮按下去的颜色类名
230 getHoverClass() {
231 // 如果开启水波纹效果,则不启用hover-class效果
232 if (this.loading || this.disabled || this.ripple || this.hoverClass) return '';
233 let hoverClass = '';
234 hoverClass = this.plain ? 'u-' + this.type + '-plain-hover' : 'u-' + this.type + '-hover';
235 return hoverClass;
236 },
237 // 在'primary', 'success', 'error', 'warning'类型下,不显示边框,否则会造成四角有毛刺现象
238 showHairLineBorder() {
239 if (['primary', 'success', 'error', 'warning'].indexOf(this.type) >= 0 && !this.plain) {
240 return '';
241 } else {
242 return 'u-hairline-border';
243 }
244 }
245 },
246 data() {
247 return {
248 rippleTop: 0, // 水波纹的起点Y坐标到按钮上边界的距离
249 rippleLeft: 0, // 水波纹起点X坐标到按钮左边界的距离
250 fields: {}, // 波纹按钮节点信息
251 waveActive: false // 激活水波纹
252 };
253 },
254 methods: {
255 // 按钮点击
256 click(e) {
257 // 进行节流控制,每this.throttle毫秒内,只在开始处执行
258 this.$u.throttle(() => {
259 // 如果按钮时disabled和loading状态,不触发水波纹效果
260 if (this.loading === true || this.disabled === true) return;
261 // 是否开启水波纹效果
262 if (this.ripple) {
263 // 每次点击时,移除上一次的类,再次添加,才能触发动画效果
264 this.waveActive = false;
265 this.$nextTick(function() {
266 this.getWaveQuery(e);
267 });
268 }
269 this.$emit('click', e);
270 }, this.throttleTime);
271 },
272 // 查询按钮的节点信息
273 getWaveQuery(e) {
274 this.getElQuery().then(res => {
275 // 查询返回的是一个数组节点
276 let data = res[0];
277 // 查询不到节点信息,不操作
278 if (!data.width || !data.width) return;
279 // 水波纹的最终形态是一个正方形(通过border-radius让其变为一个圆形),这里要保证正方形的边长等于按钮的最长边
280 // 最终的方形(变换后的圆形)才能覆盖整个按钮
281 data.targetWidth = data.height > data.width ? data.height : data.width;
282 if (!data.targetWidth) return;
283 this.fields = data;
284 let touchesX = '',
285 touchesY = '';
286 // #ifdef MP-BAIDU
287 touchesX = e.changedTouches[0].clientX;
288 touchesY = e.changedTouches[0].clientY;
289 // #endif
290 // #ifdef MP-ALIPAY
291 touchesX = e.detail.clientX;
292 touchesY = e.detail.clientY;
293 // #endif
294 // #ifndef MP-BAIDU || MP-ALIPAY
295 touchesX = e.touches[0].clientX;
296 touchesY = e.touches[0].clientY;
297 // #endif
298 // 获取触摸点相对于按钮上边和左边的x和y坐标,原理是通过屏幕的触摸点(touchesY),减去按钮的上边界data.top
299 // 但是由于`transform-origin`默认是center,所以这里再减去半径才是水波纹view应该的位置
300 // 总的来说,就是把水波纹的矩形(变换后的圆形)的中心点,移动到我们的触摸点位置
301 this.rippleTop = touchesY - data.top - data.targetWidth / 2;
302 this.rippleLeft = touchesX - data.left - data.targetWidth / 2;
303 this.$nextTick(() => {
304 this.waveActive = true;
305 });
306 });
307 },
308 // 获取节点信息
309 getElQuery() {
310 return new Promise(resolve => {
311 let queryInfo = '';
312 // 获取元素节点信息,请查看uniapp相关文档
313 // https://uniapp.dcloud.io/api/ui/nodes-info?id=nodesrefboundingclientrect
314 queryInfo = uni.createSelectorQuery().in(this);
315 //#ifdef MP-ALIPAY
316 queryInfo = uni.createSelectorQuery();
317 //#endif
318 queryInfo.select('.u-btn').boundingClientRect();
319 queryInfo.exec(data => {
320 resolve(data);
321 });
322 });
323 },
324 // 下面为对接uniapp官方按钮开放能力事件回调的对接
325 getphonenumber(res) {
326 this.$emit('getphonenumber', res);
327 },
328 getuserinfo(res) {
329 this.$emit('getuserinfo', res);
330 },
331 error(res) {
332 this.$emit('error', res);
333 },
334 opensetting(res) {
335 this.$emit('opensetting', res);
336 },
337 launchapp(res) {
338 this.$emit('launchapp', res);
339 }
340 }
341};
342</script>
343
344<style scoped lang="scss">
345@import '../../libs/css/style.components.scss';
346.u-btn::after {
347 border: none;
348}
349
350.u-btn {
351 position: relative;
352 border: 0;
353 //border-radius: 10rpx;
354 /* #ifndef APP-NVUE */
355 display: inline-flex;
356 /* #endif */
357 // 避免边框某些场景可能被“裁剪”,不能设置为hidden
358 overflow: visible;
359 line-height: 1;
360 @include vue-flex;
361 align-items: center;
362 justify-content: center;
363 cursor: pointer;
364 padding: 0 40rpx;
365 z-index: 1;
366 box-sizing: border-box;
367 transition: all 0.15s;
368
369 &--bold-border {
370 border: 1px solid #ffffff;
371 }
372
373 &--default {
374 color: $u-content-color;
375 border-color: #c0c4cc;
376 background-color: #ffffff;
377 }
378
379 &--primary {
380 color: #ffffff;
381 border-color: $u-type-primary;
382 background-color: $u-type-primary;
383 }
384
385 &--success {
386 color: #ffffff;
387 border-color: $u-type-success;
388 background-color: $u-type-success;
389 }
390
391 &--error {
392 color: #ffffff;
393 border-color: $u-type-error;
394 background-color: $u-type-error;
395 }
396
397 &--warning {
398 color: #ffffff;
399 border-color: $u-type-warning;
400 background-color: $u-type-warning;
401 }
402
403 &--default--disabled {
404 color: #ffffff;
405 border-color: #e4e7ed;
406 background-color: #ffffff;
407 }
408
409 &--primary--disabled {
410 color: #ffffff!important;
411 border-color: $u-type-primary-disabled!important;
412 background-color: $u-type-primary-disabled!important;
413 }
414
415 &--success--disabled {
416 color: #ffffff!important;
417 border-color: $u-type-success-disabled!important;
418 background-color: $u-type-success-disabled!important;
419 }
420
421 &--error--disabled {
422 color: #ffffff!important;
423 border-color: $u-type-error-disabled!important;
424 background-color: $u-type-error-disabled!important;
425 }
426
427 &--warning--disabled {
428 color: #ffffff!important;
429 border-color: $u-type-warning-disabled!important;
430 background-color: $u-type-warning-disabled!important;
431 }
432
433 &--primary--plain {
434 color: $u-type-primary!important;
435 border-color: $u-type-primary-disabled!important;
436 background-color: $u-type-primary-light!important;
437 }
438
439 &--success--plain {
440 color: $u-type-success!important;
441 border-color: $u-type-success-disabled!important;
442 background-color: $u-type-success-light!important;
443 }
444
445 &--error--plain {
446 color: $u-type-error!important;
447 border-color: $u-type-error-disabled!important;
448 background-color: $u-type-error-light!important;
449 }
450
451 &--warning--plain {
452 color: $u-type-warning!important;
453 border-color: $u-type-warning-disabled!important;
454 background-color: $u-type-warning-light!important;
455 }
456}
457
458.u-hairline-border:after {
459 content: ' ';
460 position: absolute;
461 pointer-events: none;
462 // 设置为border-box,意味着下面的scale缩小为0.5,实际上缩小的是伪元素的内容(border-box意味着内容不含border
463 box-sizing: border-box;
464 // 中心点作为变形(scale())的原点
465 -webkit-transform-origin: 0 0;
466 transform-origin: 0 0;
467 left: 0;
468 top: 0;
469 width: 199.8%;
470 height: 199.7%;
471 -webkit-transform: scale(0.5, 0.5);
472 transform: scale(0.5, 0.5);
473 border: 1px solid currentColor;
474 z-index: 1;
475}
476
477.u-wave-ripple {
478 z-index: 0;
479 position: absolute;
480 border-radius: 100%;
481 background-clip: padding-box;
482 pointer-events: none;
483 user-select: none;
484 transform: scale(0);
485 opacity: 1;
486 transform-origin: center;
487}
488
489.u-wave-ripple.u-wave-active {
490 opacity: 0;
491 transform: scale(2);
492 transition: opacity 1s linear, transform 0.4s linear;
493}
494
495.u-round-circle {
496 border-radius: 100rpx;
497}
498
499.u-round-circle::after {
500 border-radius: 100rpx;
501}
502
503.u-loading::after {
504 background-color: hsla(0, 0%, 100%, 0.35);
505}
506
507.u-size-default {
508 font-size: 30rpx;
509 height: 80rpx;
510 line-height: 80rpx;
511}
512
513.u-size-medium {
514 /* #ifndef APP-NVUE */
515 display: inline-flex;
516 /* #endif */
517 width: auto;
518 font-size: 26rpx;
519 height: 70rpx;
520 line-height: 70rpx;
521 padding: 0 80rpx;
522}
523
524.u-size-mini {
525 /* #ifndef APP-NVUE */
526 display: inline-flex;
527 /* #endif */
528 width: auto;
529 font-size: 22rpx;
530 padding-top: 1px;
531 height: 50rpx;
532 line-height: 50rpx;
533 padding: 0 20rpx;
534}
535
536.u-primary-plain-hover {
537 color: #ffffff !important;
538 background: $u-type-primary-dark !important;
539}
540
541.u-default-plain-hover {
542 color: $u-type-primary-dark !important;
543 background: $u-type-primary-light !important;
544}
545
546.u-success-plain-hover {
547 color: #ffffff !important;
548 background: $u-type-success-dark !important;
549}
550
551.u-warning-plain-hover {
552 color: #ffffff !important;
553 background: $u-type-warning-dark !important;
554}
555
556.u-error-plain-hover {
557 color: #ffffff !important;
558 background: $u-type-error-dark !important;
559}
560
561.u-info-plain-hover {
562 color: #ffffff !important;
563 background: $u-type-info-dark !important;
564}
565
566.u-default-hover {
567 color: $u-type-primary-dark !important;
568 border-color: $u-type-primary-dark !important;
569 background-color: $u-type-primary-light !important;
570}
571
572.u-primary-hover {
573 background: $u-type-primary-dark !important;
574 color: #fff;
575}
576
577.u-success-hover {
578 background: $u-type-success-dark !important;
579 color: #fff;
580}
581
582.u-info-hover {
583 background: $u-type-info-dark !important;
584 color: #fff;
585}
586
587.u-warning-hover {
588 background: $u-type-warning-dark !important;
589 color: #fff;
590}
591
592.u-error-hover {
593 background: $u-type-error-dark !important;
594 color: #fff;
595}
596</style>