2020-07-08 14:47:07 +08:00
|
|
|
import classNames from 'classnames';
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { PresetColorType } from '../_util/colors';
|
2023-07-09 16:47:31 +08:00
|
|
|
import { isPresetColor } from '../_util/colors';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { LiteralUnion } from '../_util/type';
|
2023-07-09 16:47:31 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-03-18 17:26:17 +08:00
|
|
|
import useStyle from './style';
|
2020-07-08 14:47:07 +08:00
|
|
|
|
|
|
|
type RibbonPlacement = 'start' | 'end';
|
|
|
|
|
|
|
|
export interface RibbonProps {
|
|
|
|
className?: string;
|
|
|
|
prefixCls?: string;
|
|
|
|
style?: React.CSSProperties; // style of ribbon element, not the wrapper
|
|
|
|
text?: React.ReactNode;
|
2022-12-28 23:59:55 +08:00
|
|
|
color?: LiteralUnion<PresetColorType>;
|
2020-07-08 14:47:07 +08:00
|
|
|
children?: React.ReactNode;
|
|
|
|
placement?: RibbonPlacement;
|
|
|
|
}
|
|
|
|
|
2023-07-09 16:47:31 +08:00
|
|
|
const Ribbon: React.FC<RibbonProps> = (props) => {
|
|
|
|
const {
|
|
|
|
className,
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
style,
|
|
|
|
color,
|
|
|
|
children,
|
|
|
|
text,
|
|
|
|
placement = 'end',
|
|
|
|
} = props;
|
2022-04-08 00:18:02 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2020-07-08 14:47:07 +08:00
|
|
|
const prefixCls = getPrefixCls('ribbon', customizePrefixCls);
|
2023-01-06 11:06:46 +08:00
|
|
|
const colorInPreset = isPresetColor(color, false);
|
2020-09-06 13:07:39 +08:00
|
|
|
const ribbonCls = classNames(
|
|
|
|
prefixCls,
|
|
|
|
`${prefixCls}-placement-${placement}`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-color-${color}`]: colorInPreset,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2022-04-08 00:18:02 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2020-07-08 14:47:07 +08:00
|
|
|
const colorStyle: React.CSSProperties = {};
|
|
|
|
const cornerColorStyle: React.CSSProperties = {};
|
|
|
|
if (color && !colorInPreset) {
|
|
|
|
colorStyle.background = color;
|
|
|
|
cornerColorStyle.color = color;
|
|
|
|
}
|
2022-03-18 17:26:17 +08:00
|
|
|
return wrapSSR(
|
|
|
|
<div className={classNames(`${prefixCls}-wrapper`, hashId)}>
|
2020-07-08 14:47:07 +08:00
|
|
|
{children}
|
2022-03-18 17:26:17 +08:00
|
|
|
<div className={classNames(ribbonCls, hashId)} style={{ ...colorStyle, ...style }}>
|
2020-10-14 16:35:29 +08:00
|
|
|
<span className={`${prefixCls}-text`}>{text}</span>
|
2020-07-08 14:47:07 +08:00
|
|
|
<div className={`${prefixCls}-corner`} style={cornerColorStyle} />
|
|
|
|
</div>
|
2022-03-18 17:26:17 +08:00
|
|
|
</div>,
|
2020-07-08 14:47:07 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-01-08 21:30:41 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Ribbon.displayName = 'Ribbon';
|
|
|
|
}
|
|
|
|
|
2020-07-08 14:47:07 +08:00
|
|
|
export default Ribbon;
|