feat: FontAwesomeIcon counter

This commit is contained in:
h_mo 2022-10-02 20:29:07 +08:00
parent f8866ece2a
commit 6de5d36126
3 changed files with 59 additions and 6 deletions

View File

@ -18,7 +18,8 @@
const singleBeat = props.beat && !props.fade; const singleBeat = props.beat && !props.fade;
const singleFade = !props.beat && props.fade; const singleFade = !props.beat && props.fade;
const BeatFade = props.beat && props.fade; const beatFade = props.beat && props.fade;
const count = props.counter > props.counterMax ? `${props.counterMax}+` : props.counter;
const wrapClassObject = [ const wrapClassObject = [
props.float ? `fa-pull-${props.float}` : '', props.float ? `fa-pull-${props.float}` : '',
props.border ? 'fa-border' : '', props.border ? 'fa-border' : '',
@ -26,7 +27,7 @@
props.rotateFlip ? `fa-flip-${props.rotateFlip}` : '', props.rotateFlip ? `fa-flip-${props.rotateFlip}` : '',
singleBeat ? `fa-beat` : '', singleBeat ? `fa-beat` : '',
singleFade ? `fa-fade` : '', singleFade ? `fa-fade` : '',
BeatFade ? `fa-beat-fade` : '', beatFade ? `fa-beat-fade` : '',
props.bounce ? 'fa-bounce' : '', props.bounce ? 'fa-bounce' : '',
props.flip ? 'fa-flip' : '', props.flip ? 'fa-flip' : '',
props.shake ? 'fa-shake' : '', props.shake ? 'fa-shake' : '',
@ -36,6 +37,7 @@
props.stack ? 'fa-stack' : '', props.stack ? 'fa-stack' : '',
props.stackChild ? `fa-stack-${props.stackX}x` : '', props.stackChild ? `fa-stack-${props.stackX}x` : '',
props.stackInverse ? 'fa-inverse' : '', props.stackInverse ? 'fa-inverse' : '',
props.counter ? 'layers' : '',
]; ];
const wrapStyleObject = Object.assign( const wrapStyleObject = Object.assign(
@ -46,7 +48,7 @@
props.rotate ? { '--fa-rotate-angle': `${props.rotate}deg` } : {}, props.rotate ? { '--fa-rotate-angle': `${props.rotate}deg` } : {},
singleBeat ? { '--fa-beat-scale': `${props.scale}` } : {}, singleBeat ? { '--fa-beat-scale': `${props.scale}` } : {},
singleFade ? { '--fa-fade-opacity': `${props.opacity}` } : {}, singleFade ? { '--fa-fade-opacity': `${props.opacity}` } : {},
BeatFade beatFade
? { '--fa-beat-fade-scale': `${props.scale}`, '--fa-beat-fade-opacity': `${props.opacity}` } ? { '--fa-beat-fade-scale': `${props.scale}`, '--fa-beat-fade-opacity': `${props.opacity}` }
: {}, : {},
props.bounce props.bounce
@ -113,6 +115,10 @@
'font-size': props.size ? `${props.size}rpx` : false, 'font-size': props.size ? `${props.size}rpx` : false,
}); });
const counterStyleObject = {
'--fa-counter-background-color': props.counterMgColor,
};
const onClick = () => { const onClick = () => {
emit('click'); emit('click');
}; };
@ -121,20 +127,46 @@
<template v-if="props.stack"> <template v-if="props.stack">
<view @click="onClick" class="icon-wrap" :class="wrapClassObject" :style="wrapStyleObject"> <view @click="onClick" class="icon-wrap" :class="wrapClassObject" :style="wrapStyleObject">
<slot :stack="props.stack"></slot> <slot :stack="props.stack"></slot>
<text v-if="props.counter" class="counter" :style="counterStyleObject">{{ count }}</text>
</view> </view>
</template> </template>
<template v-else> <template v-else>
<view @click="onClick" class="icon-wrap" :class="wrapClassObject" :style="wrapStyleObject"> <view @click="onClick" class="icon-wrap" :class="wrapClassObject" :style="wrapStyleObject">
<text class="icon" :style="iconStyleObject" :class="iconClassObject" /> <text class="icon" :style="iconStyleObject" :class="iconClassObject" />
<text v-if="props.counter" class="counter" :style="counterStyleObject">{{ count }}</text>
</view> </view>
</template> </template>
</template> </template>
<style lang="scss" scoped> <style lang="scss" scoped>
.icon-wrap { .icon-wrap {
display: inline-block; display: inline-block;
&.layers {
position: relative;
text-align: center;
}
.icon { .icon {
display: inline-block; display: inline-block;
color: inherit; color: inherit;
} }
.counter {
display: inline-block;
background-color: var(--fa-counter-background-color, #ff253a);
border-radius: 1em;
box-sizing: border-box;
color: #fff;
line-height: 1;
max-width: 5em;
min-width: 1.5em;
overflow: hidden;
padding: 0.25em 0.5em;
position: absolute;
right: -0.35em;
text-overflow: ellipsis;
top: 0;
-webkit-transform: 0.35;
transform: scale(0.35);
-webkit-transform-origin: top right;
transform-origin: top right;
}
} }
</style> </style>

View File

@ -324,4 +324,25 @@ export const FontAwesomeIconProps = {
type: String, type: String,
default: '#fff', default: '#fff',
}, },
/** icon counter
* @description
*/
counter: {
type: [String, Number, Boolean],
default: false,
},
/** icon counterMax
* @description
*/
counterMax: {
type: Number,
default: 99,
},
/** icon counterMax
* @description
*/
counterMgColor: {
type: String,
default: '#ff253a',
},
}; };

View File

@ -5,13 +5,13 @@
<template> <template>
<AppProvider> <AppProvider>
<view>mode-不同风格</view> <view>mode-不同风格</view>
<view> <FontAwesomeIcon name="house" />solid </view> <view> <FontAwesomeIcon name="house" counter="999" />solid </view>
<view> <FontAwesomeIcon mode="regular" name="house" />regular</view> <view> <FontAwesomeIcon mode="regular" name="house" />regular</view>
<view> <FontAwesomeIcon mode="light" name="house" />light</view> <view> <FontAwesomeIcon mode="light" name="house" />light</view>
<view> <FontAwesomeIcon mode="thin" name="house" />thin</view> <view> <FontAwesomeIcon mode="thin" name="house" />thin</view>
<view> <FontAwesomeIcon mode="duotone" name="house" />duotone</view> <view> <FontAwesomeIcon mode="duotone" name="house" />duotone</view>
<view>sharp-直角图标</view> <view>sharp-直角图标</view>
<view> <FontAwesomeIcon name="user" sharp /> sharp </view> <view> <FontAwesomeIcon name="user" sharp counter="999" /> sharp </view>
<view>mode(brands)-品牌logo</view> <view>mode(brands)-品牌logo</view>
<view> <FontAwesomeIcon mode="brands" name="bilibili" /> bilibili </view> <view> <FontAwesomeIcon mode="brands" name="bilibili" /> bilibili </view>
@ -136,7 +136,7 @@
</template> </template>
</FontAwesomeIcon> </FontAwesomeIcon>
<FontAwesomeIcon stack> <FontAwesomeIcon stack counter="999">
<template #default="slotProps"> <template #default="slotProps">
<FontAwesomeIcon <FontAwesomeIcon
mode="brands" mode="brands"