ant-design/components/badge/Ribbon.tsx
Wuxh 7f189d56e5
refactor: merge preset colors (#39742)
* style: remove redundant code

* feat(style): add preset colour style generation method

(cherry picked from commit 1266e42ba27accb48a6544e3eddaa4a94daef00c)

* feat: uniform preset colour generation css selector method

(cherry picked from commit 5af87e8d5ffcfd23e755946167f200c0565f8222)

* chore: merge preset colors

(cherry picked from commit 05040dfb703f60a3ea1715326748c508acbf41a6)

* chore: update

(cherry picked from commit 241b40a1361469487a6a3e8f1ad07a25d250463d)

* chore: remove Badge preset inverse colors

* chore: remove fix

删除的这部分其实一个 Bug,但是为了给 PR 减负(尽可能单一, 后面新开一个 PR 来修复这个问题

* suggestions accepted

Update components/style/presetColor.tsx

Co-authored-by: MadCcc <1075746765@qq.com>

Co-authored-by: MadCcc <1075746765@qq.com>
2023-01-06 11:06:46 +08:00

61 lines
1.8 KiB
TypeScript

import classNames from 'classnames';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import type { PresetColorType } from '../_util/colors';
import type { LiteralUnion } from '../_util/type';
import useStyle from './style';
import { isPresetColor } from '../_util/colors';
type RibbonPlacement = 'start' | 'end';
export interface RibbonProps {
className?: string;
prefixCls?: string;
style?: React.CSSProperties; // style of ribbon element, not the wrapper
text?: React.ReactNode;
color?: LiteralUnion<PresetColorType>;
children?: React.ReactNode;
placement?: RibbonPlacement;
}
const Ribbon: React.FC<RibbonProps> = function Ribbon({
className,
prefixCls: customizePrefixCls,
style,
color,
children,
text,
placement = 'end',
}) {
const { getPrefixCls, direction } = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('ribbon', customizePrefixCls);
const colorInPreset = isPresetColor(color, false);
const ribbonCls = classNames(
prefixCls,
`${prefixCls}-placement-${placement}`,
{
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-color-${color}`]: colorInPreset,
},
className,
);
const [wrapSSR, hashId] = useStyle(prefixCls);
const colorStyle: React.CSSProperties = {};
const cornerColorStyle: React.CSSProperties = {};
if (color && !colorInPreset) {
colorStyle.background = color;
cornerColorStyle.color = color;
}
return wrapSSR(
<div className={classNames(`${prefixCls}-wrapper`, hashId)}>
{children}
<div className={classNames(ribbonCls, hashId)} style={{ ...colorStyle, ...style }}>
<span className={`${prefixCls}-text`}>{text}</span>
<div className={`${prefixCls}-corner`} style={cornerColorStyle} />
</div>
</div>,
);
};
export default Ribbon;