網站導覽

v2.1.0
表單

數字鍵盤

仿手機按鍵排列,方便用拇指輸入代碼或金額。

本頁內容

數字鍵盤採用手機的按鍵排列,將十二個按鈕排成三欄,方便用拇指輸入代碼或金額。鍵面平時凸起,按下時立即下沉。每個數字鍵都以大字顯示數字,下方印著手機按鍵常見的小字母。數字鍵盤本身不會輸入任何內容。每個按鍵都是按鈕,由頁面把對應的數字填到指定位置。

數字鍵盤

<input class="input input-lg text-center w-full max-w(272)" id="pad-amount" type="text" inputmode="none" value placeholder="0" aria-label="Amount"><div class="keypad" id="pad" role="group" aria-label="Keypad">  <button class="keypad-key" type="button" value="1">1</button>  <button class="keypad-key" type="button" value="2">    2<span class="keypad-letters">abc</span>  </button>  <button class="keypad-key" type="button" value="3">    3<span class="keypad-letters">def</span>  </button>  <button class="keypad-key" type="button" value="4">    4<span class="keypad-letters">ghi</span>  </button>  <button class="keypad-key" type="button" value="5">    5<span class="keypad-letters">jkl</span>  </button>  <button class="keypad-key" type="button" value="6">    6<span class="keypad-letters">mno</span>  </button>  <button class="keypad-key" type="button" value="7">    7<span class="keypad-letters">pqrs</span>  </button>  <button class="keypad-key" type="button" value="8">    8<span class="keypad-letters">tuv</span>  </button>  <button class="keypad-key" type="button" value="9">    9<span class="keypad-letters">wxyz</span>  </button>  <button class="keypad-key keypad-key-quiet" type="button" value=".">.</button>  <button class="keypad-key" type="button" value="0">0</button>  <button class="keypad-key keypad-key-quiet" type="button" value="delete" aria-label="Delete">    <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="M7.6582 4.1667L16.25 4.1667A1.25 1.25 0 0 1 17.5 5.4167L17.5 14.5833A1.25 1.25 0 0 1 16.25 15.8333L7.6582 15.8333A1.25 1.25 0 0 1 6.7091 15.3968L2.7806 10.8135A1.25 1.25 0 0 1 2.7806 9.1865L6.7091 4.6032A1.25 1.25 0 0 1 7.6582 4.1667Z" /><path d="M10 8.3333L13.3333 11.6667" /><path d="M13.3333 8.3333L10 11.6667" />    </svg>  </button></div><script>var amount = document.getElementById('pad-amount')document.getElementById('pad').addEventListener('click', function (event) {  var key = event.target.closest('button')  if (!key) return  amount.value = key.value === 'delete' ? amount.value.slice(0, -1) : amount.value + key.value})</script>
按下按鍵,腳本就會將對應數字填入欄位。

#按鍵

keypad-key 是高度為 52px 的按鈕,採用與按鈕相同的凸起鍵面。用拇指連續輸入六位數時,按鍵節奏很快,因此按下時鍵面立即沉入凹槽,省去過場動畫。指標移到按鍵上時,輪廓線會加深,和其他控制項的懸停狀態一致。

keypad-letters 是數字下方的字母列,方便使用者用單字記住代碼。輸入金額時可省略這一列。

keypad-key-quiet 用於刪除、小數點或正負號等非數字按鍵。這些按鍵按下時才顯示鍵面,讓十個數字鍵成為視覺主體,其餘按鍵退居邊緣。如果鍵盤一角不需要按鍵,放入空的 <span> 即可維持網格排列。

#鍵盤版面

鍵盤分成三個等寬欄,欄距為 8px,寬度隨容器延展,最大為 17rem。用一般按鈕組成鍵盤時,容易漏掉以下兩項行為:

  • 設定 touch-action: manipulation,讓瀏覽器無須等待判斷第二次點按是否要觸發雙點縮放。快速連按同一個鍵會算作兩次按鍵。
  • 停用數字的文字選取,按下時即使手指稍微滑動,也不會反白選取文字。

--keypad-key 設定按鍵高度,--keypad-gap 設定按鍵之間的間距。

#頁面如何處理輸入

數字鍵盤的按鍵行為由頁面決定,例如在金額後加上一位數字、填入 OTP 欄位的一格,或撥出電話。和範例一樣,在鍵盤上掛一個事件監聽器即可。讀取按下的按鈕的 value,再將它寫入指定位置。

在頁面上保留真正的輸入欄位,讓使用者可以用實體鍵盤輸入,讓密碼管理員可以自動填入,也讓螢幕閱讀器可以讀出值的變化。在欄位上設定 inputmode="none",手機就不會彈出系統鍵盤蓋住數字鍵盤。

#無障礙

  • 為鍵盤設定 role="group" 和 aria-label。每個數字鍵都是按鈕,並以顯示的數字作為名稱。只顯示圖示的按鍵需要 aria-label。
  • 數字下方的字母是按鈕文字的一部分。如果使用者無須聽到「2 abc」,請用帶有 aria-hidden="true" 的元素包住字母。
  • 讓螢幕閱讀器追蹤接收數字的欄位並宣告輸入結果,避免逐次宣告按鍵。
  • 無論使用哪種指標裝置,按鍵高度都是 52px,高於觸控目標所需的 44px。

#類別

類別說明
keypad按鍵面板採三欄排列,按鍵間距為 8px,面板最寬為 17rem。數字無法選取,輕點兩下也不會縮放。
keypad-key每個按鍵使用一個按鈕,鍵面凸起、高 52px,並以大字顯示數字。按下時鍵面會立即下沉。
keypad-key-quiet刪除鍵、小數點鍵等非數字按鍵,按下時才顯示鍵面搭配 keypad-key 使用
keypad-letters數字下方的小字母,以較不醒目的樣式呈現

#自訂屬性

屬性預設值說明
--keypad-key3.25rem按鍵高度
--keypad-gap.5rem按鍵之間的間距

#原始碼

@master/ui/components/keypad.css
/* * Keypad: the keys of a phone, for a code or an amount typed with a thumb. * Twelve buttons in three columns. A key is a raised face that sinks at once under a press: there is no * wait and no lift, because a thumb moves faster than a transition. Its figure is large, and the letters a * phone prints under a figure are small and quiet. A key that is not a figure (delete, a decimal point) is * keypad-key-quiet: no face until it is pressed. * The pad never selects its figures and never zooms on a double tap, so two quick presses are two presses. * It types nothing by itself: each key is a button, and the page puts its figure where it belongs. * Geometry: keys are 52px tall, 8px apart, with a large control's corner; the pad is 17rem wide at most. */@layer components.ui {  .keypad {    --keypad-gap: .5rem;    --keypad-key: 3.25rem;    display: grid;    grid-template-columns: repeat(3, minmax(0, 1fr));    gap: var(--keypad-gap);    width: 100%;    max-width: 17rem;    min-width: 0;    margin: 0;    padding: 0;    touch-action: manipulation;    user-select: none;    -webkit-user-select: none;    /* A key held down is a key held down: no callout rises over it. */    -webkit-touch-callout: none;  }  .keypad-key {    --icon-size: 1.25rem;    display: grid;    align-content: center;    justify-items: center;    gap: .1875rem;    min-width: 0;    height: var(--keypad-key);    margin: 0;    padding: 0;    border: 0;    border-radius: calc(var(--radius-control-lg) * var(--corner-scale));    corner-shape: var(--corner-shape);    @apply --surface-sheet;    color: var(--color-text-strong);    box-shadow: var(--shadow-raised);    font: var(--font-weight-control) 1.375rem/1 var(--font-family-sans);    font-feature-settings: inherit;    font-variant-numeric: tabular-nums;    appearance: none;    cursor: pointer;    -webkit-tap-highlight-color: transparent;    @media (hover: hover) {      &:hover:not(:disabled) {        box-shadow: var(--shadow-raised-hover);      }    }    /* Pressed: into the well, with no transition to wait for. */    &:active:not(:disabled) {      background: var(--color-surface-inset);      box-shadow: var(--shadow-pressed);    }    &:focus-visible {      @apply --focus-ring;    }    &:disabled {      background: var(--color-surface-inset);      color: var(--color-text-disabled);      box-shadow: var(--shadow-flat);      cursor: not-allowed;    }    /* Not a figure: no face of its own until it is pressed. */    &.keypad-key-quiet {      background: transparent;      color: var(--color-text-body);      box-shadow: none;      @media (hover: hover) {        &:hover:not(:disabled) {          background: var(--color-surface-chip);          box-shadow: none;        }      }      &:active:not(:disabled) {        background: var(--color-surface-inset);        box-shadow: var(--shadow-pressed);      }      /* As heavy as the quiet ink above, so it says the disabled ink again. */      &:disabled {        background: transparent;        color: var(--color-text-disabled);        box-shadow: none;      }    }    @media (forced-colors: active) {      outline: 1px solid ButtonText;      outline-offset: -1px;      &:disabled {        outline-color: GrayText;      }    }  }  /* The letters under a figure. */  .keypad-letters {    color: var(--color-text-muted);    font-weight: 400;    font-size: .5625rem;    letter-spacing: .12em;    line-height: 1;    text-indent: .12em;    text-transform: uppercase;  }  /* A disabled key goes quiet as a whole: its letters take the disabled ink with its figure. */  .keypad-key:disabled .keypad-letters {    color: inherit;  }}