Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Badge): use pseudo-element instead #3144

Merged
merged 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/badge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ isComponent: true

{{ size }}

## FAQ

### 如何处理由 ribbon 徽标溢出导致页面出现横向滚动?
角标溢出问题建议从父容器组件处理。如 <a href="https://github.com/Tencent/tdesign-miniprogram/issues/3063" title="如 #3063 " target="_blank" rel="noopener noreferrer"> #3063 </a>,可以给父容器 `cell` 组件添加 `overflow: hidden`,处理溢出造成页面出现横向滚动的问题。

## API

### Badge Props
Expand Down
1 change: 1 addition & 0 deletions src/badge/__test__/__snapshots__/demo.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ exports[`Badge Badge theme demo works fine 1`] = `
角标
</wx-view>
<t-cell
tClass="t-class-cell"
title="单行标题"
>
<t-badge
Expand Down
4 changes: 2 additions & 2 deletions src/badge/_example/theme/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
</view>

<view class="demo-desc" style="margin-bottom: 32rpx">角标</view>
<t-cell title="单行标题">
<t-badge count="New" offset="{{skylineRender ? [-8, -20]: [0, 0]}}" shape="ribbon" slot="note" />
<t-cell title="单行标题" t-class="t-class-cell">
<t-badge count="New" offset="{{skylineRender ? ['-16rpx', '-56rpx']: [0, 0]}}" shape="ribbon" slot="note" />
</t-cell>
6 changes: 5 additions & 1 deletion src/badge/_example/theme/index.wxss
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
margin-top: 28px;
margin-bottom: 24px;
align-items: center;
}
}

.t-class-cell {
overflow: hidden;
}
28 changes: 25 additions & 3 deletions src/badge/badge.less
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,39 @@
&__ribbon {
&-outer {
position: absolute;
overflow: hidden;
top: 0;
right: 0;
background-color: @badge-color;
}
}

&--ribbon {
position: relative;
display: inline-block;
transform-origin: top left;
transform-origin: center center;
transform: translate(calc(50% - @badge-basic-height + 1rpx), calc(-50% + @badge-basic-height - 1rpx)) rotate(45deg);
border-radius: 0;
// padding: 0;

&::before,
&::after {
content: '';
position: absolute;
width: 0;
height: 0;
bottom: 0;
border-bottom: @badge-basic-height solid @badge-color;
font-size: 0;
}

&::before {
left: calc(-1 * @badge-basic-height + 1rpx);
border-left: @badge-basic-height solid transparent;
}

&::after {
right: calc(-1 * @badge-basic-height + 1rpx);
border-right: @badge-basic-height solid transparent;
}
}

&--bubble {
Expand All @@ -87,6 +108,7 @@
}

&__content:not(:empty) + .t-has-count {
transform-origin: center center;
transform: translate(-50%, -50%);
position: absolute;
left: 100%;
Expand Down
21 changes: 1 addition & 20 deletions src/badge/badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SuperComponent, wxComponent } from '../common/src/index';
import config from '../common/config';
import props from './props';
import type { TdBadgeProps } from './type';
import { getRect, uniqueFactory } from '../common/utils';
import { uniqueFactory } from '../common/utils';

const { prefix } = config;
const name = `${prefix}-badge`;
Expand All @@ -26,30 +26,11 @@ export default class Badge extends SuperComponent {
value: '',
labelID: '',
descriptionID: '',
// ribbon
ribbonStyle: '',
ribbonOuterStyle: '',
};

lifetimes = {
ready() {
const uniqueID = getUniqueID();
if (this.properties.shape === 'ribbon') {
getRect(this, `.${prefix}-badge--ribbon`).then((rect) => {
const outerBoundingRect = rect.width / Math.SQRT2 + rect.height * Math.SQRT2; // 外接矩形的宽度:height * cos45deg + width / cos45deg
const translateX = rect.width - rect.width / Math.SQRT2 + outerBoundingRect - rect.width; // 旋转后的位移:原宽度 - 旋转后的宽度 + 外接矩形的宽度 - 原宽度
const ribbonHeightHypotenuse = rect.height * Math.SQRT2; // 斜边的长度:height * sin45deg
const ribbonWidthCatheti = rect.width / Math.SQRT2; // 直角边的长度:width / sin45deg
this.setData({
ribbonStyle: `transform: translateX(${translateX}px) rotate(45deg);`,
ribbonOuterStyle: `width: ${outerBoundingRect}px; height: ${outerBoundingRect}px;
clip-path: polygon(0 0, ${ribbonHeightHypotenuse}px 0,
${outerBoundingRect}px ${ribbonWidthCatheti}px,
${outerBoundingRect}px ${ribbonWidthCatheti + ribbonHeightHypotenuse}px);`,
});
});
}

this.setData({
labelID: `${uniqueID}_label`,
descriptionID: `${uniqueID}_description`,
Expand Down
4 changes: 2 additions & 2 deletions src/badge/badge.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
-->

<view
style="{{_._style([style, customStyle, ribbonOuterStyle])}}"
style="{{_._style([style, customStyle])}}"
class="{{this.getBadgeOuterClass({shape})}} class {{prefix}}-class"
aria-labelledby="{{labelID}}"
aria-describedby="{{descriptionID}}"
Expand All @@ -24,7 +24,7 @@
wx:if="{{this.isShowBadge({dot,count,showZero})}}"
id="{{descriptionID}}"
class="{{this.getBadgeInnerClass({dot, size, shape, count})}} {{prefix}}-has-count {{prefix}}-class-count"
style="{{_._style([this.getBadgeStyles({color, offset}), ribbonStyle])}}"
style="{{_._style([this.getBadgeStyles({color, offset})])}}"
aria-hidden="true"
aria-label="{{ ariaLabel || _.getBadgeAriaLabel({dot, count, maxCount}) }}"
>
Expand Down
Loading