ant-design/components/badge/Ribbon.tsx
lijianan 43e9785f37
feat: Badge support cssVar (#45906)
* feat: Badge support cssVar

* fix: fix

* fix: fix

* fix: fix

---------

Signed-off-by: lijianan <574980606@qq.com>
2023-11-16 14:49:13 +08:00

74 lines
2.1 KiB
TypeScript

import * as React from 'react';
import classNames from 'classnames';
import type { PresetColorType } from '../_util/colors';
import { isPresetColor } from '../_util/colors';
import type { LiteralUnion } from '../_util/type';
import { ConfigContext } from '../config-provider';
import useCSSVar from './style/cssVar';
import useStyle from './style/ribbon';
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;
rootClassName?: string;
}
const Ribbon: React.FC<RibbonProps> = (props) => {
const {
className,
prefixCls: customizePrefixCls,
style,
color,
children,
text,
placement = 'end',
rootClassName,
} = props;
const { getPrefixCls, direction } = React.useContext(ConfigContext);
const prefixCls = getPrefixCls('ribbon', customizePrefixCls);
const [, hashId] = useStyle(prefixCls);
const wrapCSSVar = useCSSVar(prefixCls);
const colorInPreset = isPresetColor(color, false);
const ribbonCls = classNames(
prefixCls,
`${prefixCls}-placement-${placement}`,
{
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-color-${color}`]: colorInPreset,
},
className,
);
const colorStyle: React.CSSProperties = {};
const cornerColorStyle: React.CSSProperties = {};
if (color && !colorInPreset) {
colorStyle.background = color;
cornerColorStyle.color = color;
}
return wrapCSSVar(
<div className={classNames(`${prefixCls}-wrapper`, rootClassName, hashId)}>
{children}
<div className={classNames(ribbonCls, hashId)} style={{ ...colorStyle, ...style }}>
<span className={`${prefixCls}-text`}>{text}</span>
<div className={`${prefixCls}-corner`} style={cornerColorStyle} />
</div>
</div>,
);
};
if (process.env.NODE_ENV !== 'production') {
Ribbon.displayName = 'Ribbon';
}
export default Ribbon;