ant-design/components/divider/index.tsx
thinkasany 1f2d5838bc
feat: ConfigProvider support classNames and styles for divider (#53890)
* feat: ConfigProvider support classNames and styles for divider

* save

* save

* snap

* style update

* clean

* demo update

* fix

* rerun ci

* docs: update demo

---------

Co-authored-by: 二货机器人 <smith3816@gmail.com>
2025-05-27 15:48:52 +08:00

189 lines
5.1 KiB
TypeScript

import * as React from 'react';
import cls from 'classnames';
import useMergeSemantic from '../_util/hooks/useMergeSemantic';
import { devUseWarning } from '../_util/warning';
import { useComponentConfig } from '../config-provider/context';
import useSize from '../config-provider/hooks/useSize';
import { SizeType } from '../config-provider/SizeContext';
import useStyle from './style';
type SemanticName = 'root' | 'rail' | 'content';
export interface DividerProps {
prefixCls?: string;
type?: 'horizontal' | 'vertical';
/**
* @default center
*/
orientation?:
| 'left'
| 'right'
| 'center'
| 'start' // 👈 5.24.0+
| 'end'; // 👈 5.24.0+
orientationMargin?: string | number;
className?: string;
rootClassName?: string;
children?: React.ReactNode;
dashed?: boolean;
/**
* @since 5.20.0
* @default solid
*/
variant?: 'dashed' | 'dotted' | 'solid';
style?: React.CSSProperties;
size?: SizeType;
plain?: boolean;
classNames?: Partial<Record<SemanticName, string>>;
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
}
const sizeClassNameMap: Record<string, string> = { small: 'sm', middle: 'md' };
const Divider: React.FC<DividerProps> = (props) => {
const {
getPrefixCls,
direction,
className: dividerClassName,
style: dividerStyle,
classNames: contextClassNames,
styles: contextStyles,
} = useComponentConfig('divider');
const {
prefixCls: customizePrefixCls,
type = 'horizontal',
orientation = 'center',
orientationMargin,
className,
rootClassName,
children,
dashed,
variant = 'solid',
plain,
style,
size: customSize,
classNames,
styles,
...restProps
} = props;
const prefixCls = getPrefixCls('divider', customizePrefixCls);
const railCls = `${prefixCls}-rail`;
const [mergedClassNames, mergedStyles] = useMergeSemantic(
[contextClassNames, classNames],
[contextStyles, styles],
);
const [hashId, cssVarCls] = useStyle(prefixCls);
const sizeFullName = useSize(customSize);
const sizeCls = sizeClassNameMap[sizeFullName];
const hasChildren = !!children;
const mergedOrientation = React.useMemo<'start' | 'end' | 'center'>(() => {
if (orientation === 'left') {
return direction === 'rtl' ? 'end' : 'start';
}
if (orientation === 'right') {
return direction === 'rtl' ? 'start' : 'end';
}
return orientation;
}, [direction, orientation]);
const hasMarginStart = mergedOrientation === 'start' && orientationMargin != null;
const hasMarginEnd = mergedOrientation === 'end' && orientationMargin != null;
const classString = cls(
prefixCls,
dividerClassName,
hashId,
cssVarCls,
`${prefixCls}-${type}`,
{
[`${prefixCls}-with-text`]: hasChildren,
[`${prefixCls}-with-text-${mergedOrientation}`]: hasChildren,
[`${prefixCls}-dashed`]: !!dashed,
[`${prefixCls}-${variant}`]: variant !== 'solid',
[`${prefixCls}-plain`]: !!plain,
[`${prefixCls}-rtl`]: direction === 'rtl',
[`${prefixCls}-no-default-orientation-margin-start`]: hasMarginStart,
[`${prefixCls}-no-default-orientation-margin-end`]: hasMarginEnd,
[`${prefixCls}-${sizeCls}`]: !!sizeCls,
[railCls]: !children,
[mergedClassNames.rail as string]: mergedClassNames.rail && !children,
},
className,
rootClassName,
mergedClassNames.root,
);
const memoizedOrientationMargin = React.useMemo<string | number>(() => {
if (typeof orientationMargin === 'number') {
return orientationMargin;
}
if (/^\d+$/.test(orientationMargin!)) {
return Number(orientationMargin);
}
return orientationMargin!;
}, [orientationMargin]);
const innerStyle: React.CSSProperties = {
marginInlineStart: hasMarginStart ? memoizedOrientationMargin : undefined,
marginInlineEnd: hasMarginEnd ? memoizedOrientationMargin : undefined,
...mergedStyles.content,
};
// Warning children not work in vertical mode
if (process.env.NODE_ENV !== 'production') {
const warning = devUseWarning('Divider');
warning(
!children || type !== 'vertical',
'usage',
'`children` not working in `vertical` mode.',
);
}
return (
<div
className={classString}
style={{
...dividerStyle,
...mergedStyles.root,
...(children ? {} : mergedStyles.rail),
...style,
}}
{...restProps}
role="separator"
>
{children && type !== 'vertical' && (
<>
<div
className={cls(railCls, `${railCls}-start`, mergedClassNames.rail)}
style={mergedStyles.rail}
/>
<span
className={cls(`${prefixCls}-inner-text`, mergedClassNames.content)}
style={innerStyle}
>
{children}
</span>
<div
className={cls(railCls, `${railCls}-end`, mergedClassNames.rail)}
style={mergedStyles.rail}
/>
</>
)}
</div>
);
};
if (process.env.NODE_ENV !== 'production') {
Divider.displayName = 'Divider';
}
export default Divider;