<template>
<button
id="u-wave-btn"
class="u-btn u-line-1 u-fix-ios-appearance"
:class="[
'u-size-' + size,
plain ? 'u-btn--' + type + '--plain' : '',
loading ? 'u-loading' : '',
shape == 'circle' ? 'u-round-circle' : '',
hairLine ? showHairLineBorder : 'u-btn--bold-border',
'u-btn--' + type,
disabled ? `u-btn--${type}--disabled` : '',
]"
:disabled="disabled"
:form-type="formType"
:open-type="openType"
:app-parameter="appParameter"
:hover-stop-propagation="hoverStopPropagation"
:send-message-title="sendMessageTitle"
send-message-path="sendMessagePath"
:lang="lang"
:data-name="dataName"
:session-from="sessionFrom"
:send-message-img="sendMessageImg"
:show-message-card="showMessageCard"
@getphonenumber="getphonenumber"
@getuserinfo="getuserinfo"
@error="error"
@opensetting="opensetting"
@launchapp="launchapp"
:style="[customStyle]"
@tap.stop="click($event)"
:hover-class="getHoverClass"
:loading="loading"
>
<slot></slot>
<view
v-if="ripple"
class="u-wave-ripple"
:class="[waveActive ? 'u-wave-active' : '']"
:style="{
top: rippleTop + 'px',
left: rippleLeft + 'px',
width: fields.targetWidth + 'px',
height: fields.targetWidth + 'px',
'background-color': rippleBgColor || 'rgba(0, 0, 0, 0.15)'
}"
></view>
</button>
</template>
<script>
export default {
name: 'u-button',
props: {
hairLine: {
type: Boolean,
default: false,
},
type: {
type: String,
default: 'default',
},
size: {
type: String,
default: 'default',
},
shape: {
type: String,
default: 'square',
},
plain: {
type: Boolean,
default: false,
},
disabled: {
type: Boolean,
default: false,
},
loading: {
type: Boolean,
default: false,
},
openType: {
type: String,
default: '',
},
formType: {
type: String,
default: '',
},
appParameter: {
type: String,
default: '',
},
hoverStopPropagation: {
type: Boolean,
default: false,
},
lang: {
type: String,
default: 'en',
},
sessionFrom: {
type: String,
default: '',
},
sendMessageTitle: {
type: String,
default: '',
},
sendMessagePath: {
type: String,
default: '',
},
sendMessageImg: {
type: String,
default: '',
},
showMessageCard: {
type: Boolean,
default: false,
},
hoverBgColor: {
type: String,
default: '',
},
rippleBgColor: {
type: String,
default: '',
},
ripple: {
type: Boolean,
default: false,
},
hoverClass: {
type: String,
default: '',
},
customStyle: {
type: Object,
default() {
return {}
},
},
dataName: {
type: String,
default: '',
},
},
computed: {
getHoverClass() {
if (this.loading || this.disabled || this.ripple || this.hoverClass)
return ''
let hoverClass = ''
hoverClass = this.plain
? 'u-' + this.type + '-plain-hover'
: 'u-' + this.type + '-hover'
return hoverClass
},
showHairLineBorder() {
if (
['primary', 'success', 'error', 'warning'].indexOf(this.type) >= 0 &&
!this.plain
) {
return ''
} else {
return 'u-hairline-border'
}
},
},
data() {
return {
rippleTop: 0,
rippleLeft: 0,
fields: {},
waveActive: false,
}
},
methods: {
click(e) {
if (this.loading === true || this.disabled === true) return
if (this.ripple) {
this.waveActive = false
this.$nextTick(function () {
this.getWaveQuery(e)
})
}
this.$emit('click')
},
getWaveQuery(e) {
this.getElQuery().then((res) => {
let data = res[0]
if (!data.width || !data.width) return
data.targetWidth = data.height > data.width ? data.height : data.width
if (!data.targetWidth) return
this.fields = data
let touchesX = '',
touchesY = ''
touchesX = e.changedTouches[0].clientX
touchesY = e.changedTouches[0].clientY
touchesX = e.detail.clientX
touchesY = e.detail.clientY
touchesX = e.touches[0].clientX
touchesY = e.touches[0].clientY
this.rippleTop = touchesY - data.top - data.targetWidth / 2
this.rippleLeft = touchesX - data.left - data.targetWidth / 2
this.$nextTick(() => {
this.waveActive = true
})
})
},
getElQuery() {
return new Promise((resolve) => {
let queryInfo = ''
queryInfo = uni.createSelectorQuery().in(this)
queryInfo = uni.createSelectorQuery()
queryInfo.select('.u-btn').boundingClientRect()
queryInfo.exec((data) => {
resolve(data)
})
})
},
getphonenumber(res) {
this.$emit('getphonenumber', res)
},
getuserinfo(res) {
this.$emit('getuserinfo', res)
},
error(res) {
this.$emit('error', res)
},
opensetting(res) {
this.$emit('opensetting', res)
},
launchapp(res) {
this.$emit('launchapp', res)
},
},
}
</script>
<style scoped lang="scss">
@import '../../libs/css/style.components.scss';
.u-btn::after {
border: none;
}
.u-btn {
position: relative;
border: 0;
//border-radius: 10rpx;
display: inline-block;
overflow: hidden;
line-height: 1;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
padding: 0 20rpx;
z-index: 1;
box-sizing: border-box;
transition: all 0.15s;
&--bold-border {
border: 1px solid #ffffff;
}
&--default {
background: linear-gradient(
180deg,
rgba(247, 247, 247, 1) 0%,
rgba(184, 184, 184, 1) 100%
);
box-shadow: 0px 1px 0px 0px rgba(255, 255, 255, 1);
border-radius: 3.88rpx;
border: 0.5px solid rgba(139, 139, 139, 1);
text-shadow:0px 0.5px 0px rgba(255,255,255,1);
font-weight: bold;
font-size: 12.36rpx;
}
&--primary {
background: linear-gradient(
180deg,
rgba(123, 186, 255, 1) 0%,
rgba(0, 114, 238, 1) 73%,
rgba(0, 122, 255, 1) 100%
);
box-shadow: 0px 1px 0px 0px rgba(255, 255, 255, 1);
border-radius: 3.88rpx;
border: 0.5px solid rgba(27, 99, 179, 1);
color: #fff;
font-weight: bold;
text-shadow:0px 0.5px 0px rgba(0,0,0,0.5);
font-size: 12.36rpx;
margin-right: 16.66rpx;
}
&--success {
color: #ffffff;
border-color: #19be6b;
background-color: #19be6b;
}
&--error {
color: #ffffff;
border-color: #fa3534;
background-color: #fa3534;
}
&--warning {
color: #ffffff;
border-color: #ff9900;
background-color: #ff9900;
}
&--default--disabled {
color: #ffffff;
border-color: #e4e7ed;
background-color: #ffffff;
}
&--primary--disabled {
color: #ffffff !important;
border-color: #a0cfff !important;
background-color: #a0cfff !important;
}
&--success--disabled {
color: #ffffff !important;
border-color: #71d5a1 !important;
background-color: #71d5a1 !important;
}
&--error--disabled {
color: #ffffff !important;
border-color: #fab6b6 !important;
background-color: #fab6b6 !important;
}
&--warning--disabled {
color: #ffffff !important;
border-color: #fcbd71 !important;
background-color: #fcbd71 !important;
}
&--primary--plain {
color: #2979ff !important;
border-color: #a0cfff !important;
background-color: #ecf5ff !important;
}
&--success--plain {
color: #19be6b !important;
border-color: #71d5a1 !important;
background-color: #dbf1e1 !important;
}
&--error--plain {
color: #fa3534 !important;
border-color: #fab6b6 !important;
background-color: #fef0f0 !important;
}
&--warning--plain {
color:#ff9900 !important;
border-color: #fcbd71 !important;
background-color: #fdf6ec !important;
}
}
.u-hairline-border:after {
content: ' ';
position: absolute;
pointer-events: none;
// 设置为border-box,意味着下面的scale缩小为0.5,实际上缩小的是伪元素的内容(border-box意味着内容不含border)
box-sizing: border-box;
// 中心点作为变形(scale())的原点
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
left: 0;
top: 0;
width: 199.8%;
height: 199.7%;
-webkit-transform: scale(0.5, 0.5);
transform: scale(0.5, 0.5);
border: 1px solid currentColor;
z-index: 1;
}
.u-wave-ripple {
z-index: 0;
position: absolute;
border-radius: 100%;
background-clip: padding-box;
pointer-events: none;
user-select: none;
transform: scale(0);
opacity: 1;
transform-origin: center;
}
.u-wave-ripple.u-wave-active {
opacity: 0;
transform: scale(2);
transition: opacity 1s linear, transform 0.4s linear;
}
.u-round-circle {
border-radius: 50rpx;
}
.u-round-circle::after {
border-radius: 50rpx;
}
.u-loading::after {
background-color: hsla(0, 0%, 100%, 0.35);
}
.u-size-default {
font-size: 15rpx;
height: 40rpx;
line-height: 40rpx;
}
.u-size-medium {
display: inline-flex;
width: auto;
font-size: 13rpx;
height: 35rpx;
line-height: 35rpx;
padding: 0 40rpx;
}
.u-size-mini {
display: inline-flex;
width: auto;
font-size: 11rpx;
padding-top: 1px;
height: 25rpx;
line-height: 25rpx;
padding: 0 10rpx;
}
.u-primary-plain-hover {
color: #ffffff !important;
background: #2b85e4 !important;
}
.u-default-plain-hover {
color: #2b85e4 !important;
background: #ecf5ff !important;
}
.u-success-plain-hover {
color: #ffffff !important;
background: #18b566 !important;
}
.u-warning-plain-hover {
color: #ffffff !important;
background: #f29100 !important;
}
.u-error-plain-hover {
color: #ffffff !important;
background: #dd6161 !important;
}
.u-info-plain-hover {
color: #ffffff !important;
background: #82848a !important;
}
.u-default-hover {
color: #2b85e4 !important;
border-color: #2b85e4 !important;
background-color: #ecf5ff !important;
}
.u-primary-hover {
background: #2b85e4 !important;
color: #fff;
}
.u-success-hover {
background: #18b566 !important;
color: #fff;
}
.u-info-hover {
background: #82848a !important;
color: #fff;
}
.u-warning-hover {
background: #f29100 !important;
color: #fff;
}
.u-error-hover {
background: #dd6161 !important;
color: #fff;
}
</style>