Files
newsui/packages/components/src/media/Figure.tsx
T

52 lines
1.3 KiB
TypeScript
Raw Normal View History

2026-05-20 01:30:41 +08:00
'use client';
import React, { CSSProperties } from 'react';
import { clampSpan, cx } from '@newspaperui/utils';
import { useSection } from '../layout/Section';
import { Caption } from '../text/Caption';
2026-05-19 21:09:56 +08:00
export interface FigureProps {
src: string;
alt: string;
caption?: string;
2026-05-20 01:30:41 +08:00
credit?: string;
2026-05-19 21:09:56 +08:00
span?: number;
2026-05-20 01:30:41 +08:00
className?: string;
style?: CSSProperties;
2026-05-19 21:09:56 +08:00
}
/**
* Figure — 图文组合(img + Caption
*
* - 将图片与 Caption 组合为语义化 figure 元素
* - 支持 caption 文字说明和 credit 来源标注
* - 自动避免 print 分页断开(break-inside: avoid
*
* @example
* <Figure
* src="/photo.jpg"
* alt="City skyline"
* caption="Downtown at dusk"
* credit="Photo by John"
* span={10}
* />
*/
2026-05-19 21:09:56 +08:00
export const Figure: React.FC<FigureProps> = ({
2026-05-20 01:30:41 +08:00
src, alt, caption, credit, span, className, style,
2026-05-19 21:09:56 +08:00
}) => {
const section = useSection();
2026-05-20 01:30:41 +08:00
const cols = span ? clampSpan(span, section.columns) : undefined;
2026-05-19 21:09:56 +08:00
return (
<figure
2026-05-20 01:30:41 +08:00
className={cx('nui-figure nui-avoid-break', className)}
style={{
margin: 0,
gridColumn: cols ? `span ${cols}` : undefined,
...style,
}}
2026-05-19 21:09:56 +08:00
>
2026-05-20 01:30:41 +08:00
<img src={src} alt={alt} style={{ display: 'block', width: '100%', height: 'auto' }} />
{(caption || credit) && <Caption credit={credit}>{caption}</Caption>}
2026-05-19 21:09:56 +08:00
</figure>
);
};