網站導覽

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檔案輸入控制項以隱形方式覆蓋整個區域,可接收拖放到區域內任一處的檔案
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;  }}