Files
newsui/packages/components/src/text/BodyText.tsx
T

74 lines
2.4 KiB
TypeScript
Raw Normal View History

2026-05-20 01:30:41 +08:00
'use client';
import React, { ReactNode, CSSProperties } from 'react';
2026-05-19 21:09:56 +08:00
import { visualWeights, resolveFontSize } from '@newspaperui/theme';
2026-05-20 01:30:41 +08:00
import { clampSpan, cx } from '@newspaperui/utils';
import { useSection } from '../layout/Section';
2026-05-19 21:09:56 +08:00
export interface BodyTextProps {
weight?: 'High' | 'Medium' | 'Low';
span?: number;
2026-05-20 01:30:41 +08:00
columns?: 1 | 2 | 3 | 4; // 多栏文字流
columnWidth?: string; // 可选:显式指定每栏目标宽度(与 columnCount 配合)
columnFill?: 'auto' | 'balance'; // 默认 balance(平均分布到各栏)
2026-05-20 01:30:41 +08:00
dropCap?: boolean;
className?: string;
style?: CSSProperties;
children: ReactNode;
2026-05-19 21:09:56 +08:00
}
/**
* BodyText — 正文(核心:columns 多栏文字流 + dropCap 首字下沉)
*
* - CSS Multi-column 实现真实报纸多栏排版(1-4 栏)
* - dropCap 启用 ::first-letter 首字下沉效果
* - 自动应用 old-style figures、hanging punctuation、column-rule hairline
* - columnFill 默认 'balance',浏览器自动均分内容到各栏(无需指定 height)
*
* @example
* <BodyText columns={3} dropCap>
* <p>The story begins with a dramatic opening paragraph...</p>
* </BodyText>
*/
2026-05-19 21:09:56 +08:00
export const BodyText: React.FC<BodyTextProps> = ({
weight = 'Medium', span, columns = 1, columnWidth,
columnFill = 'balance', dropCap = false,
2026-05-20 01:30:41 +08:00
className, style, children,
2026-05-19 21:09:56 +08:00
}) => {
const section = useSection();
2026-05-20 01:30:41 +08:00
const config = visualWeights.BodyText[weight]!;
const cols = span ? clampSpan(span, section.columns) : undefined;
const useColumns = columns >= 2;
2026-05-19 21:09:56 +08:00
return (
2026-05-20 01:30:41 +08:00
<div
className={cx(
'nui-bodytext',
'nui-paragraph-flow',
'nui-osf',
'nui-hanging-punctuation',
useColumns && 'nui-column-rule',
dropCap && 'nui-drop-cap',
className,
)}
2026-05-19 21:09:56 +08:00
style={{
2026-05-20 01:30:41 +08:00
fontFamily: `var(${config.fontFamily})`,
2026-05-19 21:09:56 +08:00
fontSize: resolveFontSize(config.fontSize),
fontWeight: config.fontWeight,
lineHeight: config.lineHeight,
2026-05-20 01:30:41 +08:00
color: `var(${config.color})`,
2026-05-19 21:09:56 +08:00
margin: config.margin,
2026-05-20 01:30:41 +08:00
gridColumn: cols ? `span ${cols}` : undefined,
columnCount: useColumns ? columns : undefined,
columnWidth: useColumns ? columnWidth : undefined,
columnGap: useColumns ? 'var(--nui-gutter)' : undefined,
columnFill: useColumns ? columnFill : undefined,
...style,
2026-05-19 21:09:56 +08:00
}}
data-weight={weight}
2026-05-20 01:30:41 +08:00
data-columns={columns}
2026-05-19 21:09:56 +08:00
>
{children}
2026-05-20 01:30:41 +08:00
</div>
2026-05-19 21:09:56 +08:00
);
};