2023-09-11 17:28:04 +08:00
|
|
|
import * as React from 'react';
|
2019-02-19 11:42:05 +08:00
|
|
|
import classNames from 'classnames';
|
2020-11-21 15:40:06 +08:00
|
|
|
import { composeRef } from 'rc-util/lib/ref';
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
2023-06-20 14:35:05 +08:00
|
|
|
import type { ConfigConsumerProps, DirectionType } from '../config-provider';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-03-16 19:10:36 +08:00
|
|
|
import useStyle from './style';
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2022-10-18 11:45:43 +08:00
|
|
|
export interface TypographyProps<C extends keyof JSX.IntrinsicElements>
|
|
|
|
extends React.HTMLAttributes<HTMLElement> {
|
2019-02-19 11:42:05 +08:00
|
|
|
id?: string;
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2019-02-19 11:42:05 +08:00
|
|
|
style?: React.CSSProperties;
|
|
|
|
children?: React.ReactNode;
|
2022-10-18 11:45:43 +08:00
|
|
|
/** @internal */
|
|
|
|
component?: C;
|
2024-06-22 21:59:12 +08:00
|
|
|
'aria-label'?: string;
|
2022-10-18 11:45:43 +08:00
|
|
|
direction?: DirectionType;
|
2019-02-19 11:42:05 +08:00
|
|
|
}
|
|
|
|
|
2022-10-18 11:45:43 +08:00
|
|
|
interface InternalTypographyProps<C extends keyof JSX.IntrinsicElements>
|
|
|
|
extends TypographyProps<C> {
|
2019-10-01 14:06:09 +08:00
|
|
|
/** @deprecated Use `ref` directly if using React 16 */
|
2019-03-05 13:48:38 +08:00
|
|
|
setContentRef?: (node: HTMLElement) => void;
|
2019-02-19 11:42:05 +08:00
|
|
|
}
|
|
|
|
|
2022-10-18 11:45:43 +08:00
|
|
|
const Typography = React.forwardRef<
|
|
|
|
HTMLElement,
|
|
|
|
InternalTypographyProps<keyof JSX.IntrinsicElements>
|
2023-06-20 14:35:05 +08:00
|
|
|
>((props, ref) => {
|
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
component: Component = 'article',
|
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
setContentRef,
|
|
|
|
children,
|
|
|
|
direction: typographyDirection,
|
|
|
|
style,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
2024-08-27 22:08:04 +08:00
|
|
|
|
2023-06-20 14:35:05 +08:00
|
|
|
const {
|
|
|
|
getPrefixCls,
|
|
|
|
direction: contextDirection,
|
|
|
|
typography,
|
|
|
|
} = React.useContext<ConfigConsumerProps>(ConfigContext);
|
2019-10-01 14:06:09 +08:00
|
|
|
|
2023-06-20 14:35:05 +08:00
|
|
|
const direction = typographyDirection ?? contextDirection;
|
2024-08-27 22:08:04 +08:00
|
|
|
const mergedRef = setContentRef ? composeRef(ref, setContentRef) : ref;
|
|
|
|
const prefixCls = getPrefixCls('typography', customizePrefixCls);
|
2022-03-16 19:10:36 +08:00
|
|
|
|
2023-09-11 17:28:04 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Typography');
|
|
|
|
warning.deprecated(!setContentRef, 'setContentRef', 'ref');
|
2023-09-11 17:28:04 +08:00
|
|
|
}
|
|
|
|
|
2023-06-20 14:35:05 +08:00
|
|
|
// Style
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2023-06-20 14:35:05 +08:00
|
|
|
const componentClassName = classNames(
|
|
|
|
prefixCls,
|
|
|
|
typography?.className,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
hashId,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2023-06-20 14:35:05 +08:00
|
|
|
);
|
2022-03-16 19:10:36 +08:00
|
|
|
|
2023-06-20 14:35:05 +08:00
|
|
|
const mergedStyle: React.CSSProperties = { ...typography?.style, ...style };
|
2022-10-18 11:45:43 +08:00
|
|
|
|
2023-11-13 15:05:27 +08:00
|
|
|
return wrapCSSVar(
|
2023-06-20 14:35:05 +08:00
|
|
|
// @ts-expect-error: Expression produces a union type that is too complex to represent.
|
|
|
|
<Component className={componentClassName} style={mergedStyle} ref={mergedRef} {...restProps}>
|
|
|
|
{children}
|
|
|
|
</Component>,
|
|
|
|
);
|
|
|
|
});
|
2019-10-01 14:06:09 +08:00
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2022-10-18 11:45:43 +08:00
|
|
|
Typography.displayName = 'Typography';
|
2022-06-21 10:24:52 +08:00
|
|
|
}
|
2019-02-19 11:42:05 +08:00
|
|
|
|
2022-10-18 11:45:43 +08:00
|
|
|
export default Typography;
|