文件
将单个文件呈现为带有名称的对象,可用作附件、上传项或下载项。
本页内容
文件组件将单份文档呈现为一个独立对象:左侧的小纸页标有扩展名,中间是文件名,下方低调地列出文件信息,末端有一个控件。组件采用平整的面板样式,因此在拖放区下方或对话框内纵向排列时,每个文件都清晰可辨。
文件
<ul class="flex flex-col gap-xs max-w(400) mx-auto"> <li class="file"> <span class="file-page" aria-hidden="true">pdf</span> <div class="file-body"> <p class="file-name">Q3 infrastructure report.pdf</p> <p class="file-meta">2.4 MB · PDF document</p> </div> <div class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove Q3 infrastructure report.pdf"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </div> </li> <li class="file"> <img class="file-thumb" src="/demo/moonstone.svg" alt> <div class="file-body"> <p class="file-name">moonstone-cover.png</p> <p class="file-meta">1,920 × 1,200 · 840 KB</p> </div> <div class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove moonstone-cover.png"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </div> </li> <li class="file"> <span class="file-page" aria-hidden="true">csv</span> <div class="file-body"> <p class="file-name">members-export-2026-11.csv</p> <p class="file-meta">312 rows · 18 KB</p> </div> <div class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove members-export-2026-11.csv"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </div> </li></ul>import XIcon from '@master/icons-react/native/16/soft/x'<ul className="flex flex-col gap-xs max-w(400) mx-auto"> <li className="file"> <span className="file-page" aria-hidden="true">pdf</span> <div className="file-body"> <p className="file-name">Q3 infrastructure report.pdf</p> <p className="file-meta">2.4 MB · PDF document</p> </div> <div className="file-end"> <button type="button" className="btn btn-quiet btn-square btn-sm" aria-label="Remove Q3 infrastructure report.pdf"> <XIcon className="icon" /> </button> </div> </li> <li className="file"> <img className="file-thumb" src="/demo/moonstone.svg" alt="" /> <div className="file-body"> <p className="file-name">moonstone-cover.png</p> <p className="file-meta">1,920 × 1,200 · 840 KB</p> </div> <div className="file-end"> <button type="button" className="btn btn-quiet btn-square btn-sm" aria-label="Remove moonstone-cover.png"> <XIcon className="icon" /> </button> </div> </li> <li className="file"> <span className="file-page" aria-hidden="true">csv</span> <div className="file-body"> <p className="file-name">members-export-2026-11.csv</p> <p className="file-meta">312 rows · 18 KB</p> </div> <div className="file-end"> <button type="button" className="btn btn-quiet btn-square btn-sm" aria-label="Remove members-export-2026-11.csv"> <XIcon className="icon" /> </button> </div> </li></ul><script> import XIcon from '@master/icons-svelte/native/16/soft/x'</script><ul class="flex flex-col gap-xs max-w(400) mx-auto"> <li class="file"> <span class="file-page" aria-hidden="true">pdf</span> <div class="file-body"> <p class="file-name">Q3 infrastructure report.pdf</p> <p class="file-meta">2.4 MB · PDF document</p> </div> <div class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove Q3 infrastructure report.pdf"> <XIcon class="icon" /> </button> </div> </li> <li class="file"> <img class="file-thumb" src="/demo/moonstone.svg" alt> <div class="file-body"> <p class="file-name">moonstone-cover.png</p> <p class="file-meta">1,920 × 1,200 · 840 KB</p> </div> <div class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove moonstone-cover.png"> <XIcon class="icon" /> </button> </div> </li> <li class="file"> <span class="file-page" aria-hidden="true">csv</span> <div class="file-body"> <p class="file-name">members-export-2026-11.csv</p> <p class="file-meta">312 rows · 18 KB</p> </div> <div class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove members-export-2026-11.csv"> <XIcon class="icon" /> </button> </div> </li></ul><script setup>import XIcon from '@master/icons-vue/native/16/soft/x'</script><template> <ul class="flex flex-col gap-xs max-w(400) mx-auto"> <li class="file"> <span class="file-page" aria-hidden="true">pdf</span> <div class="file-body"> <p class="file-name">Q3 infrastructure report.pdf</p> <p class="file-meta">2.4 MB · PDF document</p> </div> <div class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove Q3 infrastructure report.pdf"> <XIcon class="icon" /> </button> </div> </li> <li class="file"> <img class="file-thumb" src="/demo/moonstone.svg" alt> <div class="file-body"> <p class="file-name">moonstone-cover.png</p> <p class="file-meta">1,920 × 1,200 · 840 KB</p> </div> <div class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove moonstone-cover.png"> <XIcon class="icon" /> </button> </div> </li> <li class="file"> <span class="file-page" aria-hidden="true">csv</span> <div class="file-body"> <p class="file-name">members-export-2026-11.csv</p> <p class="file-meta">312 rows · 18 KB</p> </div> <div class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove members-export-2026-11.csv"> <XIcon class="icon" /> </button> </div> </li> </ul></template>#纸页图标
file-page 是一张 28 × 36 的小纸页,略微凸起,底部标有扩展名。输入时大小写不限,显示时统一转为大写。扩展名最多写四个字符。旁边的文件名已包含相同信息,因此用 aria-hidden 对辅助技术隐藏纸页图标。
折角由浏览器绘制。借助 corner-shape,末端的上角被切成斜角,折起的部分由一个小正方形经斜角裁切后留下的区域构成。浏览器不支持 corner-shape 时,纸页图标显示为四个圆角,无折角。
文件为图片时,在 <img> 上使用 file-thumb 代替纸页图标。
#上传中
在文件组件内放入带有 progress file-progress 的原生 <progress>。此时整个面板就是进度条。进度条铺在文字背后,从面板的一侧延伸到另一侧,按上传进度为面板着色,填充边缘沿用面板的边角形状。省略 value 时,进度未知,一条同色的色带会横向移动。
同时在 file-meta 中写明上传进度,并在上传期间用 aria-busy="true" 标记文件。
上传中
<ul class="flex flex-col gap-xs max-w(400) mx-auto"> <li class="file" aria-busy="true"> <span class="file-page" aria-hidden="true">zip</span> <div class="file-body"> <p class="file-name">atlas-build-2026-11-11.zip</p> <p class="file-meta">Uploading · 64% of 182 MB</p> </div> <div class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Cancel the upload of atlas-build-2026-11-11.zip"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </div> <progress class="progress file-progress" max="100" value="64" aria-label="Upload of atlas-build-2026-11-11.zip"> 64% </progress> </li> <li class="file" aria-busy="true"> <span class="file-page" aria-hidden="true">mp4</span> <div class="file-body"> <p class="file-name">onboarding-walkthrough.mp4</p> <p class="file-meta">Scanning for viruses…</p> </div> <progress class="progress file-progress" aria-label="Scan of onboarding-walkthrough.mp4"></progress> </li></ul>import XIcon from '@master/icons-react/native/16/soft/x'<ul className="flex flex-col gap-xs max-w(400) mx-auto"> <li className="file" aria-busy="true"> <span className="file-page" aria-hidden="true">zip</span> <div className="file-body"> <p className="file-name">atlas-build-2026-11-11.zip</p> <p className="file-meta">Uploading · 64% of 182 MB</p> </div> <div className="file-end"> <button type="button" className="btn btn-quiet btn-square btn-sm" aria-label="Cancel the upload of atlas-build-2026-11-11.zip"> <XIcon className="icon" /> </button> </div> <progress className="progress file-progress" max="100" value="64" aria-label="Upload of atlas-build-2026-11-11.zip"> 64% </progress> </li> <li className="file" aria-busy="true"> <span className="file-page" aria-hidden="true">mp4</span> <div className="file-body"> <p className="file-name">onboarding-walkthrough.mp4</p> <p className="file-meta">Scanning for viruses…</p> </div> <progress className="progress file-progress" aria-label="Scan of onboarding-walkthrough.mp4"></progress> </li></ul><script> import XIcon from '@master/icons-svelte/native/16/soft/x'</script><ul class="flex flex-col gap-xs max-w(400) mx-auto"> <li class="file" aria-busy="true"> <span class="file-page" aria-hidden="true">zip</span> <div class="file-body"> <p class="file-name">atlas-build-2026-11-11.zip</p> <p class="file-meta">Uploading · 64% of 182 MB</p> </div> <div class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Cancel the upload of atlas-build-2026-11-11.zip"> <XIcon class="icon" /> </button> </div> <progress class="progress file-progress" max="100" value="64" aria-label="Upload of atlas-build-2026-11-11.zip"> 64% </progress> </li> <li class="file" aria-busy="true"> <span class="file-page" aria-hidden="true">mp4</span> <div class="file-body"> <p class="file-name">onboarding-walkthrough.mp4</p> <p class="file-meta">Scanning for viruses…</p> </div> <progress class="progress file-progress" aria-label="Scan of onboarding-walkthrough.mp4"></progress> </li></ul><script setup>import XIcon from '@master/icons-vue/native/16/soft/x'</script><template> <ul class="flex flex-col gap-xs max-w(400) mx-auto"> <li class="file" aria-busy="true"> <span class="file-page" aria-hidden="true">zip</span> <div class="file-body"> <p class="file-name">atlas-build-2026-11-11.zip</p> <p class="file-meta">Uploading · 64% of 182 MB</p> </div> <div class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Cancel the upload of atlas-build-2026-11-11.zip"> <XIcon class="icon" /> </button> </div> <progress class="progress file-progress" max="100" value="64" aria-label="Upload of atlas-build-2026-11-11.zip"> 64% </progress> </li> <li class="file" aria-busy="true"> <span class="file-page" aria-hidden="true">mp4</span> <div class="file-body"> <p class="file-name">onboarding-walkthrough.mp4</p> <p class="file-meta">Scanning for viruses…</p> </div> <progress class="progress file-progress" aria-label="Scan of onboarding-walkthrough.mp4"></progress> </li> </ul></template>#上传失败
file-failed 为文件面板加上危险状态的轮廓,并将 file-meta 改为危险状态的墨色。在该处说明出错原因,并在末端提供解决操作。ARIA 没有表示列表项失败的属性,因此用类名设置失败状态,由文字传达失败信息。为这段文字加上 role="alert",即可在用户浏览页面期间发生失败时播报。
上传失败
<div class="file file-failed max-w(400) mx-auto"> <span class="file-page" aria-hidden="true">mov</span> <div class="file-body"> <p class="file-name">walkthrough-final-v2.mov</p> <p class="file-meta" role="alert">Larger than the 100 MB limit</p> </div> <div class="file-end"><button type="button" class="btn btn-sm">Replace</button></div></div><div className="file file-failed max-w(400) mx-auto"> <span className="file-page" aria-hidden="true">mov</span> <div className="file-body"> <p className="file-name">walkthrough-final-v2.mov</p> <p className="file-meta" role="alert">Larger than the 100 MB limit</p> </div> <div className="file-end"><button type="button" className="btn btn-sm">Replace</button></div></div><div class="file file-failed max-w(400) mx-auto"> <span class="file-page" aria-hidden="true">mov</span> <div class="file-body"> <p class="file-name">walkthrough-final-v2.mov</p> <p class="file-meta" role="alert">Larger than the 100 MB limit</p> </div> <div class="file-end"><button type="button" class="btn btn-sm">Replace</button></div></div><template> <div class="file file-failed max-w(400) mx-auto"> <span class="file-page" aria-hidden="true">mov</span> <div class="file-body"> <p class="file-name">walkthrough-final-v2.mov</p> <p class="file-meta" role="alert">Larger than the 100 MB limit</p> </div> <div class="file-end"><button type="button" class="btn btn-sm">Replace</button></div> </div></template>#紧凑样式
file-sm 采用单行布局,高 32px,宽度随文件名而定,可用作消息编辑器上方的附件,也可放在提及该文件的行内。它与小号按钮等高,可以同行排列。纸页图标太小,无法容纳扩展名,因此扩展名由旁边的文件名和文件信息呈现。无论使用哪种指针,末端控件的可见尺寸都是 24px,距面板边缘 4px。触摸屏上的触控区域以控件为中心扩展至 44px。
紧凑样式
<form class="composer max-w(480) mx-auto"> <div class="composer-attachments"> <span class="file file-sm"> <span class="file-page" aria-hidden="true"></span> <span class="file-body"> <span class="file-name">report.pdf</span><span class="file-meta">2.4 MB</span> </span> <span class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove report.pdf"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> </span> <span class="file file-sm"> <img class="file-thumb" src="/demo/moonstone.svg" alt> <span class="file-body"><span class="file-name">cover.png</span></span> <span class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove cover.png"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button> </span> </span> </div> <textarea class="composer-field" rows="1" placeholder="Add a note about these files" aria-label="Message"></textarea> <div class="composer-toolbar"> <div class="composer-end"> <button type="submit" class="btn btn-primary btn-sm">Send</button> </div> </div></form>import XIcon from '@master/icons-react/native/16/soft/x'<form className="composer max-w(480) mx-auto"> <div className="composer-attachments"> <span className="file file-sm"> <span className="file-page" aria-hidden="true"></span> <span className="file-body"> <span className="file-name">report.pdf</span><span className="file-meta">2.4 MB</span> </span> <span className="file-end"> <button type="button" className="btn btn-quiet btn-square btn-sm" aria-label="Remove report.pdf"> <XIcon className="icon" /> </button> </span> </span> <span className="file file-sm"> <img className="file-thumb" src="/demo/moonstone.svg" alt="" /> <span className="file-body"><span className="file-name">cover.png</span></span> <span className="file-end"> <button type="button" className="btn btn-quiet btn-square btn-sm" aria-label="Remove cover.png"> <XIcon className="icon" /> </button> </span> </span> </div> <textarea className="composer-field" rows={1} placeholder="Add a note about these files" aria-label="Message">{``}</textarea> <div className="composer-toolbar"> <div className="composer-end"> <button type="submit" className="btn btn-primary btn-sm">Send</button> </div> </div></form><script> import XIcon from '@master/icons-svelte/native/16/soft/x'</script><form class="composer max-w(480) mx-auto"> <div class="composer-attachments"> <span class="file file-sm"> <span class="file-page" aria-hidden="true"></span> <span class="file-body"> <span class="file-name">report.pdf</span><span class="file-meta">2.4 MB</span> </span> <span class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove report.pdf"> <XIcon class="icon" /> </button> </span> </span> <span class="file file-sm"> <img class="file-thumb" src="/demo/moonstone.svg" alt> <span class="file-body"><span class="file-name">cover.png</span></span> <span class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove cover.png"> <XIcon class="icon" /> </button> </span> </span> </div> <textarea class="composer-field" rows="1" placeholder="Add a note about these files" aria-label="Message"></textarea> <div class="composer-toolbar"> <div class="composer-end"> <button type="submit" class="btn btn-primary btn-sm">Send</button> </div> </div></form><script setup>import XIcon from '@master/icons-vue/native/16/soft/x'</script><template> <form class="composer max-w(480) mx-auto"> <div class="composer-attachments"> <span class="file file-sm"> <span class="file-page" aria-hidden="true"></span> <span class="file-body"> <span class="file-name">report.pdf</span><span class="file-meta">2.4 MB</span> </span> <span class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove report.pdf"> <XIcon class="icon" /> </button> </span> </span> <span class="file file-sm"> <img class="file-thumb" src="/demo/moonstone.svg" alt> <span class="file-body"><span class="file-name">cover.png</span></span> <span class="file-end"> <button type="button" class="btn btn-quiet btn-square btn-sm" aria-label="Remove cover.png"> <XIcon class="icon" /> </button> </span> </span> </div> <textarea class="composer-field" rows="1" placeholder="Add a note about these files" aria-label="Message"></textarea> <div class="composer-toolbar"> <div class="composer-end"> <button type="submit" class="btn btn-primary btn-sm">Send</button> </div> </div> </form></template>#可打开的文件
整个文件组件作为链接或按钮时,添加 file-interactive。它和按钮一样,默认凸起,指针悬停时抬升,按下时下沉。链接内部的各部分使用 <span>。
可打开的文件
<div class="grid grid-cols(2) gap-sm max-w(520) mx-auto"> <a class="file file-interactive" href="#" download> <span class="file-page" aria-hidden="true">docx</span> <span class="file-body"> <span class="file-name">Master services agreement.docx</span> <span class="file-meta">48 KB · Download</span> </span> </a> <a class="file file-interactive" href="#" download> <span class="file-page" aria-hidden="true">xlsx</span> <span class="file-body"> <span class="file-name">Pricing schedule.xlsx</span> <span class="file-meta">22 KB · Download</span> </span> </a></div><div className="grid grid-cols(2) gap-sm max-w(520) mx-auto"> <a className="file file-interactive" href="#" download> <span className="file-page" aria-hidden="true">docx</span> <span className="file-body"> <span className="file-name">Master services agreement.docx</span> <span className="file-meta">48 KB · Download</span> </span> </a> <a className="file file-interactive" href="#" download> <span className="file-page" aria-hidden="true">xlsx</span> <span className="file-body"> <span className="file-name">Pricing schedule.xlsx</span> <span className="file-meta">22 KB · Download</span> </span> </a></div><div class="grid grid-cols(2) gap-sm max-w(520) mx-auto"> <a class="file file-interactive" href="#" download> <span class="file-page" aria-hidden="true">docx</span> <span class="file-body"> <span class="file-name">Master services agreement.docx</span> <span class="file-meta">48 KB · Download</span> </span> </a> <a class="file file-interactive" href="#" download> <span class="file-page" aria-hidden="true">xlsx</span> <span class="file-body"> <span class="file-name">Pricing schedule.xlsx</span> <span class="file-meta">22 KB · Download</span> </span> </a></div><template> <div class="grid grid-cols(2) gap-sm max-w(520) mx-auto"> <a class="file file-interactive" href="#" download> <span class="file-page" aria-hidden="true">docx</span> <span class="file-body"> <span class="file-name">Master services agreement.docx</span> <span class="file-meta">48 KB · Download</span> </span> </a> <a class="file file-interactive" href="#" download> <span class="file-page" aria-hidden="true">xlsx</span> <span class="file-body"> <span class="file-name">Pricing schedule.xlsx</span> <span class="file-meta">22 KB · Download</span> </span> </a> </div></template>#无障碍
- 多个文件组成列表。使用
<ul>,并在每个<li>上添加file。 - 控件的无障碍名称应指明它作用于哪个文件,例如“移除 report.pdf”,而不是只写“移除”。
- 为
<progress>提供无障碍名称,并在文字中保留百分比。面板着色直观地呈现这个数值。 - 过长的文件名会从末尾截断。如果末尾信息很重要,例如文件扩展名,请在
title中保留完整文件名。
#类名
| 类名 | 说明 |
|---|---|
file | 容纳页面图标、文字和控件的平面面板 |
file-failed | 用于上传失败的文件。轮廓和 file-meta 使用危险墨色,在 file-meta 中说明失败原因。与 file 一同使用 |
file-sm | 单行显示,高 32px,与小按钮等高,宽度随文件名而定。页面图标较小,省略扩展名。内部按钮尺寸为 24px,向内缩进 4px。与 file 一同使用 |
file-interactive | 整个文件项是一个链接或按钮,默认凸起,悬停时抬高,按下时下沉。与 file 一同使用 |
file-page | 小型页面图标,尺寸为 28 × 36,底部用大写字母标注文件扩展名。 |
file-thumb | 文件为图片时,以图片替代页面图标 |
file-body | 名称及其下方的一行信息。 |
file-name | 文件名显示为单行,超出时在末尾显示省略号 |
file-meta | 文件大小、类型、上传进度或失败原因 |
file-end | 末端控件,用于移除、重试或下载。 |
file-progress | 在 progress 元素上使用 progress 类,将进度条置于文字底层,并按上传进度为面板着色 |
#自定义属性
| 属性 | 默认值 | 说明 |
|---|---|---|
--file-padding | .625rem | 面板的内边距,小尺寸文件项中为 .25rem |
--file-radius | var(--radius-control-lg) | 面板的圆角,小尺寸文件项使用 var(--radius-control-sm) |
--file-page-width | 1.75rem | 页面图标的宽度,小尺寸文件项中为 1rem |
--file-page-height | 2.25rem | 页面图标的高度,小尺寸文件项中为 1.25rem |
--file-fold | .5625rem | 折角大小,小尺寸文件项中为 .3125rem |
#相关内容
- 文件拖放区用于点击选择或拖入文件的大面积操作区,基于原生文件输入框,无需脚本即可工作。
- 消息编辑器用于撰写消息或提示词,输入区随内容增高,下方排列工具。
- 列表列表逐行展示条目,每行包含媒体、文字和行末内容。