Badge

introduce

Display the logo number or small red dot in the upper right corner.

Code Demo

Basic usage

After setting the content attribute, Badge will display the corresponding logo in the upper right corner of the child element, or you can use dot to display a small red dot.

<z-badge :content="5">
   <view class="child" />
</z-badge>
<z-badge :content="10">
   <view class="child" />
</z-badge>
<z-badge content="Hot">
   <view class="child" />
</z-badge>
<z-badge dot>
   <view class="child" />
</z-badge>

<style>
.demo-badge {
   .child {
     width: 80rpx;
     height: 80rpx;
     background: #fff;
     border-radius: 8rpx;
   }
}
</style>

Maximum value

After setting the max attribute, when the value of content exceeds the maximum value, it will automatically be displayed as {max}+.

<z-badge :content="20" max="9">
   <view class="child" />
</z-badge>
<z-badge :content="50" max="20">
   <view class="child" />
</z-badge>
<z-badge :content="200" max="99">
   <view class="child" />
</z-badge>

Custom color

Set the color of the logo via the color property.

<z-badge :content="5" color="#1989fa">
   <view class="child" />
</z-badge>
<z-badge :content="10" color="#1989fa">
   <view class="child" />
</z-badge>
<z-badge dot color="#1989fa">
   <view class="child" />
</z-badge>

Custom logo content

The content of the logo can be customized through the content slot, such as inserting an icon.

<z-badge>
   <view class="child" />
   <template #content>
     <z-icon name="download" />
   </template>
</z-badge>
<z-badge>
   <view class="child" />
   <template #content>
     <z-icon name="flag" />
   </template>
</z-badge>
<z-badge>
   <view class="child" />
   <template #content>
     <z-icon name="mail" />
   </template>
</z-badge>

Custom logo position

Set the position of the logo via the position property.

<z-badge :content="10" position="top-left">
   <view class="child" />
</z-badge>
<z-badge :content="10" position="bottom-left">
   <view class="child" />
</z-badge>
<z-badge :content="10" position="bottom-right">
   <view class="child" />
</z-badge>

Independent display

When Badge has no child elements, it will be displayed as an independent element.

<z-badge :content="20" />

<z-badge :content="200" max="99" />

API

Props

ParametersDescriptionTypeDefault value
contentLogo contentnumber | string-
colorLogo background colorstring#ee0a24
dotWhether to display as a small red dotbooleanfalse
maxThe maximum value. {max}+ will be displayed if the maximum value is exceeded. It is only valid when content is a numbernumber | string-
offsetSet the offset of the logo. The two items of the array correspond to the horizontal rightward and vertical downward offsets respectively. The default unit is pxnumber | string, number | string-
show-zeroWhen content is the number 0 or the string '0', whether to display the logobooleantrue
positionLogo position, optional values are top-left bottom-left bottom-rightstringtop-right

Slots

NameDescription
defaultChild element wrapped by logo
contentCustom logo content

Theme customization

Style variables

The component provides the following CSS variables, which can be used to customize styles. For usage, please refer to ConfigProvider component.

NameDefaultDescription
--z-badge-size32rpx-
--z-badge-colorvar(--z-white)-
--z-badge-padding0 6rpx-
--z-badge-font-sizevar(--z-font-size-sm)-
--z-badge-font-weightvar(--z-font-bold)-
--z-badge-border-widthvar(--z-border-width)-
--z-badge-backgroundvar(--z-danger-color)-
--z-badge-dot-colorvar(--z-danger-color)-
--z-badge-dot-size16rpx-