导航

v2.1.0
表单

文件拖放区

用于点击选择或拖入文件的大面积操作区,基于原生文件输入框,无需脚本即可工作。

本页内容

文件拖放区提供一块宽阔的区域,用于选择或拖入文件。原生文件输入控件由 <label> 包裹,并以不可见的方式覆盖整个区域。点击区域会打开文件选择器,把文件拖到区域内任意位置都会交给输入控件,再由表单提交。整个过程无需脚本。虚线轮廓提示用户把文件拖到这里。

文件拖放区

<label class="dropzone">  <input type="file" class="dropzone-input" name="attachments" multiple accept="image/*,.pdf">  <svg class="icon" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">    <path d="M9.4989 2.6265A0.625 0.625 0 0 0 9.375 3L9.3758 12.0307A0.625 0.625 0 0 0 10.6182 12.0917L10.625 12L10.623 2.95A0.6252 0.6252 0 0 0 10.5011 2.6265Q10.2855 2.4578 10 2.4578Q9.7145 2.4578 9.4989 2.6265Z" fill="currentColor" stroke="none" fill-rule="evenodd" /><path d="M6.5 6.5L9.8586 3.1414A0.2 0.2 0 0 1 10.1414 3.1414L13.5 6.5" /><path d="M3 13.5L3 15.75A1.25 1.25 0 0 0 4.25 17L15.75 17A1.25 1.25 0 0 0 17 15.75L17 13.5" />  </svg>  <span class="dropzone-title">Drop files here or choose them</span>  <span class="dropzone-description">PNG, JPG or PDF, up to 10 MB each</span></label>
import UploadIcon from '@master/icons-react/native/20/soft/upload'<label className="dropzone">  <input type="file" className="dropzone-input" name="attachments" multiple accept="image/*,.pdf" />  <UploadIcon className="icon" />  <span className="dropzone-title">Drop files here or choose them</span>  <span className="dropzone-description">PNG, JPG or PDF, up to 10 MB each</span></label>
<script>  import UploadIcon from '@master/icons-svelte/native/20/soft/upload'</script><label class="dropzone">  <input type="file" class="dropzone-input" name="attachments" multiple accept="image/*,.pdf">  <UploadIcon class="icon" />  <span class="dropzone-title">Drop files here or choose them</span>  <span class="dropzone-description">PNG, JPG or PDF, up to 10 MB each</span></label>
<script setup>import UploadIcon from '@master/icons-vue/native/20/soft/upload'</script><template>  <label class="dropzone">    <input type="file" class="dropzone-input" name="attachments" multiple accept="image/*,.pdf">    <UploadIcon class="icon" />    <span class="dropzone-title">Drop files here or choose them</span>    <span class="dropzone-description">PNG, JPG or PDF, up to 10 MB each</span>  </label></template>

#禁用状态

禁用输入控件后,拖放区改用禁用状态的前景色,并停止响应指针操作。

禁用状态

<label class="dropzone">  <input type="file" class="dropzone-input" disabled>  <span class="dropzone-title">Uploads are paused</span>  <span class="dropzone-description">The project has reached its storage limit.</span></label>
<label className="dropzone">  <input type="file" className="dropzone-input" disabled />  <span className="dropzone-title">Uploads are paused</span>  <span className="dropzone-description">The project has reached its storage limit.</span></label>
<label class="dropzone">  <input type="file" class="dropzone-input" disabled>  <span class="dropzone-title">Uploads are paused</span>  <span class="dropzone-description">The project has reached its storage limit.</span></label>
<template>  <label class="dropzone">    <input type="file" class="dropzone-input" disabled>    <span class="dropzone-title">Uploads are paused</span>    <span class="dropzone-description">The project has reached its storage limit.</span>  </label></template>

#文件列表与拖入反馈

输入控件不可见,因此浏览器不会在拖放区内显示已选文件。CSS 也没有表示文件正被拖到元素上方的状态。如果表单需要这些反馈,可以用一小段脚本列出已选文件,并在文件拖到区域上方时高亮该区域。如果只需在一行字段中选择一个文件,普通的文件输入控件就能直接显示文件名,无需额外处理。

#无障碍

  • 焦点落在文件输入控件上,拖放区则通过 :has(.dropzone-input:focus-visible) 在自身周围绘制焦点环。
  • 标题和描述共同充当输入控件的标签。在描述中说明用户必须满足的限制,例如文件类型和大小。

#类名

类名说明
dropzone在 label 上用带虚线轮廓的凹槽呈现区域,并为关联的 input 绘制焦点环。
dropzone-input文件 input 隐形覆盖整个区域,以接收拖放到区域内任意位置的文件
dropzone-title以高强调度的文字颜色显示操作指引
dropzone-description文件需满足的限制,如文件类型和大小

#源码

@master/ui/components/dropzone.css
/* * Dropzone: a wide target for choosing or dropping files. * It is a <label> around a native file input that covers it invisibly, so a click opens the picker, a dropped * file lands in the input, and the form submits it, all without script. A dashed contour says "put it here". */@layer components.ui {  .dropzone {    --icon-size: 1.25rem;    --icon-color: var(--color-text-muted);    position: relative;    display: flex;    flex-direction: column;    align-items: center;    gap: .25rem;    min-width: 0;    padding: 2rem 1.5rem;    border: 1px dashed var(--color-line-strong);    border-radius: calc(var(--radius-control-lg) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--color-surface-inset);    color: var(--color-text-muted);    font-size: .8125rem;    line-height: 1.5;    text-align: center;    cursor: pointer;    -webkit-tap-highlight-color: transparent;    transition: background-color var(--duration-quick) var(--easing-glide), border-color var(--duration-quick) var(--easing-glide);    @media (hover: hover) {      &:hover:not(:has(.dropzone-input:disabled)) {        border-color: var(--color-text-muted);        background: var(--color-surface-inset-hover);      }    }    /* Pressed: the edge takes the strong ink, so a tap is answered where there is no pointer. */    &:active:not(:has(.dropzone-input:disabled)) {      border-color: var(--color-text-strong);      background: var(--color-surface-inset-hover);    }    &:has(.dropzone-input:focus-visible) {      @apply --focus-ring;    }    /*     * A file input that is wrong (aria-invalid, or a required one left empty once the reader has tried): the dashed     * edge takes the danger color and keeps it under the pointer and the press.     */    &:has(.dropzone-input:is([aria-invalid="true"], :user-invalid)):not(:has(.dropzone-input:disabled)) {      border-color: var(--color-danger);    }    &:has(.dropzone-input:disabled) {      --icon-color: var(--color-text-disabled);      border-color: var(--color-line-strong);      color: var(--color-text-disabled);      cursor: not-allowed;    }    @media (forced-colors: active) {      border-color: CanvasText;      &:has(.dropzone-input:is([aria-invalid="true"], :user-invalid)):not(:has(.dropzone-input:disabled)) {        border-color: Mark;      }      &:has(.dropzone-input:disabled) {        border-color: GrayText;        color: GrayText;      }    }  }  /* The file input: it fills the zone, so files dropped anywhere on it are taken. */  .dropzone-input {    position: absolute;    inset: 0;    width: 100%;    height: 100%;    margin: 0;    padding: 0;    border: 0;    border-radius: inherit;    corner-shape: inherit;    opacity: 0;    cursor: inherit;  }  .dropzone-title {    margin: .375rem 0 0;    color: var(--color-text-strong);    font-size: .875rem;    font-weight: var(--font-weight-control);    text-wrap: balance;    overflow-wrap: anywhere;  }  .dropzone:has(.dropzone-input:disabled) .dropzone-title {    color: inherit;  }  .dropzone-description {    margin: 0;    text-wrap: pretty;    overflow-wrap: anywhere;  }}