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

feat: add ribbon #3681

Merged
merged 3 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 16 additions & 2 deletions components/badge/Badge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { cloneElement } from '../_util/vnode';
import { getTransitionProps, Transition } from '../_util/transition';
import isNumeric from '../_util/isNumeric';
import { defaultConfigProvider } from '../config-provider';
import { inject, defineComponent, CSSProperties, VNode } from 'vue';
import { inject, defineComponent, CSSProperties, VNode, App, Plugin } from 'vue';
import { tuple } from '../_util/type';
import Ribbon from './Ribbon';

const BadgeProps = {
/** Number to show in badge */
Expand All @@ -30,8 +31,10 @@ const BadgeProps = {
function isPresetColor(color?: string): boolean {
return (PresetColorTypes as string[]).indexOf(color) !== -1;
}
export default defineComponent({

const Badge = defineComponent({
name: 'ABadge',
Ribbon: Ribbon,
props: initDefaultProps(BadgeProps, {
showZero: false,
dot: false,
Expand Down Expand Up @@ -225,3 +228,14 @@ export default defineComponent({
);
},
});

Badge.install = function(app: App) {
app.component(Badge.name, Badge);
app.component(Badge.Ribbon.displayName, Badge.Ribbon);
return app;
};

export default Badge as typeof Badge &
Plugin & {
readonly Ribbon: typeof Ribbon;
};
52 changes: 52 additions & 0 deletions components/badge/Ribbon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { LiteralUnion } from '../_util/type';
import { PresetColorType } from '../_util/colors';
import { isPresetColor } from './utils';
import { defaultConfigProvider } from '../config-provider';
import { HTMLAttributes, FunctionalComponent, VNodeTypes, inject, CSSProperties } from 'vue';

type RibbonPlacement = 'start' | 'end';

export interface RibbonProps extends HTMLAttributes {
prefixCls?: string;
text?: VNodeTypes;
color?: LiteralUnion<PresetColorType, string>;
placement?: RibbonPlacement;
}

const Ribbon: FunctionalComponent<RibbonProps> = (props, { attrs, slots }) => {
const { prefixCls: customizePrefixCls, color, text, placement = 'end' } = props;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1、属性没声明,从props里面拿不到啊
2、text 支持slot
3、没有声明 inheritAttrs: false class和style 会被挂载到根节点上

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const { class: className, style } = attrs;
const children = slots.default?.();
const { getPrefixCls, direction } = inject('configProvider', defaultConfigProvider);

const prefixCls = getPrefixCls('ribbon', customizePrefixCls);
const colorInPreset = isPresetColor(color);
const ribbonCls = [
prefixCls,
`${prefixCls}-placement-${placement}`,
{
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-color-${color}`]: colorInPreset,
},
className,
];
const colorStyle: CSSProperties = {};
const cornerColorStyle: CSSProperties = {};
if (color && !colorInPreset) {
colorStyle.background = color;
cornerColorStyle.color = color;
}
return (
<div class={`${prefixCls}-wrapper`}>
{children}
<div class={ribbonCls} style={{ ...colorStyle, ...(style as CSSProperties) }}>
<span class={`${prefixCls}-text`}>{text}</span>
<div class={`${prefixCls}-corner`} style={cornerColorStyle} />
</div>
</div>
);
};

Ribbon.displayName = 'ABadgeRibbon';

export default Ribbon;
3 changes: 1 addition & 2 deletions components/badge/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Badge from './Badge';
import { withInstall } from '../_util/type';

export default withInstall(Badge);
export default Badge;
2 changes: 2 additions & 0 deletions components/badge/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,5 @@
opacity: 0;
}
}

@import './ribbon';
81 changes: 81 additions & 0 deletions components/badge/style/ribbon.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
@import '../../style/themes/index';
@import '../../style/mixins/index';

@ribbon-prefix-cls: ~'@{ant-prefix}-ribbon';
@ribbon-wrapper-prefix-cls: ~'@{ant-prefix}-ribbon-wrapper';

.@{ribbon-wrapper-prefix-cls} {
position: relative;
}

.@{ribbon-prefix-cls} {
.reset-component();

position: absolute;
top: 8px;
height: 22px;
padding: 0 8px;
color: @badge-text-color;
line-height: 22px;
white-space: nowrap;
background-color: @primary-color;
border-radius: @border-radius-sm;

&-text {
color: @white;
}

&-corner {
position: absolute;
top: 100%;
width: 8px;
height: 8px;
color: currentColor;
border: 4px solid;
transform: scaleY(0.75);
transform-origin: top;
// If not support IE 11, use filter: brightness(75%) instead
&::after {
position: absolute;
top: -4px;
left: -4px;
width: inherit;
height: inherit;
color: rgba(0, 0, 0, 0.25);
border: inherit;
content: '';
}
}

// colors
// mixin to iterate over colors and create CSS class for each one
.make-color-classes(@i: length(@preset-colors)) when (@i > 0) {
.make-color-classes(@i - 1);
@color: extract(@preset-colors, @i);
@darkColor: '@{color}-6';
&-color-@{color} {
color: @@darkColor;
background: @@darkColor;
}
}
.make-color-classes();

// placement
&.@{ribbon-prefix-cls}-placement-end {
right: -8px;
border-bottom-right-radius: 0;
.@{ribbon-prefix-cls}-corner {
right: 0;
border-color: currentColor transparent transparent currentColor;
}
}

&.@{ribbon-prefix-cls}-placement-start {
left: -8px;
border-bottom-left-radius: 0;
.@{ribbon-prefix-cls}-corner {
left: 0;
border-color: currentColor currentColor transparent transparent;
}
}
}
5 changes: 5 additions & 0 deletions components/badge/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { PresetColorTypes } from '../_util/colors';

export function isPresetColor(color?: string): boolean {
return (PresetColorTypes as any[]).indexOf(color) !== -1;
}