PatternsData Display
FileTypeIcon
@mox-hub/patterns 模式 · Data Display · canonical
file-type-icon@mox-hub/patternsData Displaycanonicalsrc/components/file-type-icon.tsx变体演示file-type-icon
app.tsx photo.png report.pdf archive.zip
Props API
| Prop | 类型 | 默认值 | 说明 |
|---|---|---|---|
| name* | string | — | — |
| mimeType* | string | — | — |
| size* | "sm" | "md" | 'sm' | — |
源码
import {
File,
FileArchive,
FileAudio,
FileCode,
FileImage,
FileSpreadsheet,
FileText,
FileVideo,
Presentation,
type LucideIcon,
} from 'lucide-react';
import { TONE_CLASS, type Tone } from '@mox-hub/ui/components/tone';
/**
* 文件类型图标(CAP-A-32):按扩展名(优先)→ MIME(兜底)映射图标与语义 tone。
* 产物列表/预览/上传对话框的 leading 图标唯一出口;禁页面自拼 File* 图标映射。
*/
export interface FileTypeIconProps {
/** 文件名(含扩展名),扩展名判定优先 */
name?: string | null;
/** MIME 兜底(扩展名无法判定时) */
mimeType?: string | null;
size?: 'sm' | 'md';
}
interface FileTypeVisual {
icon: LucideIcon;
tone: Tone;
}
const CODE_EXTENSIONS = new Set([
'ts', 'tsx', 'js', 'jsx', 'mjs', 'cjs', 'py', 'go', 'rs', 'java', 'c', 'h',
'cpp', 'hpp', 'cs', 'rb', 'php', 'swift', 'kt', 'sh', 'bat', 'ps1', 'sql',
'html', 'htm', 'css', 'scss', 'less', 'json', 'yaml', 'yml', 'toml', 'xml',
'vue', 'svelte', 'graphql', 'prisma',
]);
const IMAGE_EXTENSIONS = new Set(['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'ico', 'tiff']);
const ARCHIVE_EXTENSIONS = new Set(['zip', '7z', 'rar', 'tar', 'gz', 'tgz', 'bz2']);
const AUDIO_EXTENSIONS = new Set(['mp3', 'wav', 'flac', 'ogg', 'm4a']);
const VIDEO_EXTENSIONS = new Set(['mp4', 'mov', 'avi', 'mkv', 'webm']);
function resolveVisual(name?: string | null, mimeType?: string | null): FileTypeVisual {
const ext = name?.includes('.') ? (name.split('.').pop() ?? '').toLowerCase() : undefined;
if (ext) {
if (ext === 'md' || ext === 'mdx' || ext === 'txt' || ext === 'pdf') {
return { icon: FileText, tone: ext === 'pdf' ? 'danger' : 'info' };
}
if (ext === 'doc' || ext === 'docx' || ext === 'rtf' || ext === 'odt') {
return { icon: FileText, tone: 'info' };
}
if (ext === 'xls' || ext === 'xlsx' || ext === 'csv' || ext === 'ods') {
return { icon: FileSpreadsheet, tone: 'success' };
}
if (ext === 'ppt' || ext === 'pptx' || ext === 'odp') {
return { icon: Presentation, tone: 'warning' };
}
if (CODE_EXTENSIONS.has(ext)) return { icon: FileCode, tone: 'info' };
if (IMAGE_EXTENSIONS.has(ext)) return { icon: FileImage, tone: 'info' };
if (ARCHIVE_EXTENSIONS.has(ext)) return { icon: FileArchive, tone: 'warning' };
if (AUDIO_EXTENSIONS.has(ext)) return { icon: FileAudio, tone: 'default' };
if (VIDEO_EXTENSIONS.has(ext)) return { icon: FileVideo, tone: 'default' };
}
if (mimeType) {
if (mimeType.startsWith('image/')) {
return { icon: mimeType === 'image/svg+xml' ? FileCode : FileImage, tone: 'info' };
}
if (mimeType.startsWith('text/')) return { icon: FileText, tone: 'info' };
if (mimeType.startsWith('audio/')) return { icon: FileAudio, tone: 'default' };
if (mimeType.startsWith('video/')) return { icon: FileVideo, tone: 'default' };
if (mimeType === 'application/pdf') return { icon: FileText, tone: 'danger' };
if (mimeType.includes('zip') || mimeType.includes('compressed')) {
return { icon: FileArchive, tone: 'warning' };
}
}
return { icon: File, tone: 'default' };
}
export function FileTypeIcon({ name, mimeType, size = 'sm' }: FileTypeIconProps) {
const { icon: Icon, tone } = resolveVisual(name, mimeType);
return (
<Icon
aria-hidden
className={
size === 'md'
? `size-5 shrink-0 ${TONE_CLASS[tone].text}`
: `size-4 shrink-0 ${TONE_CLASS[tone].text}`
}
/>
);
}
可访问性与 RTL
- 键盘:交互件保留
:focus-visible焦点环;浮层支持 Escape 关闭与焦点归还,菜单 / 页签按 Base UI 约定支持方向键。 筛选菜单、拖拽排序、执行树与模型 listbox 等关键键盘流由 Playwright 断言覆盖,见规范示例 · 交互与可访问性。 - RTL:间距、对齐与方向性图标使用逻辑属性 (
ms-*/text-start/rtl:-scale-x-100);通过ThemeProvider或I18nProvider的direction="rtl"驱动,`lint:rtl` 会拦截新增物理类。 - 文案:优先用组件
labels/translate/translatorprops,其次用I18nProvider目录;内置 zh-CN 默认值,en-US 为参考翻译。见Theming · i18n 与 RTL。 - 对比度:高对比度 token、2px 控件边框与 3px 焦点环由主题层统一提供,见Foundations · Contrast。
用法说明
文件类型图标:扩展名优先、MIME 兜底映射图标与语义 tone;产物列表/预览/上传的 leading 图标唯一出口。