ナビゲーション

v2.1.0
フォーム

キーパッド

コードや金額を親指で入力できる、電話機のようなキーパッド。

このページの内容

キーパッドは、コードや金額を親指で入力するための、電話機と同じ3列12ボタンの配列です。各キーは立体的で、押すとすぐに沈み込みます。大きな数字の下には、電話機と同じ小さな英字が並びます。キー自体は入力処理を行いません。各キーはボタンで、数字の入力先はページ側で指定します。

キーパッド

<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のボタンです。ボタンと同じ立体感がありますが、押したときのトランジションはなく、すぐに沈んだ表示に切り替わります。親指で6桁を続けて入力するときの素早い操作に合わせるためです。ポインターを重ねると、コントロールのホバー時と同じく輪郭が強調されます。

keypad-lettersは数字の下に表示する英字の行です。コードを単語として覚えるときに役立ちます。金額の入力には不要です。

keypad-key-quietは、削除、小数点、符号など、数字以外のキーに使います。押すまでは背景が付かないため、10個の数字キーがひとまとまりに見え、ほかのキーは補助操作として区別できます。キーパッドの角にキーがない場合は、空の<span>を置いてグリッドを保ちます。

#キーパッドの配置

キーパッドは等幅の3列を8px間隔で並べ、コンテナーの幅いっぱいに広がります。最大幅は17remです。通常のボタンでキーパッドを組むと見落としやすい設定が2つあります。

  • touch-action: manipulationを設定すると、ブラウザーはダブルタップによるズームかどうかを判定するために、次のタップを待ちません。同じキーをすばやく2回押しても、別々の押下として扱われます。
  • 数字は選択できないため、キーを押すときに指が少し滑っても、テキストが選択状態になりません。

--keypad-keyでキーの高さを、--keypad-gapでキー間の間隔を設定します。

#ページ側のキー処理

キーパッド自体にビヘイビアはありません。金額に数字を追加する、OTPフィールドの1桁を埋める、発信するといったキーの処理は、ページ側で定義します。例のように、キーパッドにリスナーを1つ置くだけで十分です。押されたボタンのvalueを読み取り、対応する入力先に書き込みます。

ページには実際の入力フィールドを用意してください。ハードウェアキーボードから入力でき、パスワードマネージャーも値を入力でき、スクリーンリーダーは値の変化を読み上げられます。そのフィールドにinputmode="none"を設定すると、スマートフォンでシステムキーボードが開いてキーパッドを覆うのを防げます。

#アクセシビリティ

  • キーパッドにrole="group"とaria-labelを指定します。各キーは、表示する数字をアクセシブルな名前とするボタンです。アイコンだけを表示するキーにはaria-labelが必要です。
  • 数字の下の英字もボタンのテキストに含まれます。「2 abc」という読み上げが不要なら、英字をaria-hidden="true"を指定した要素で囲みます。
  • スクリーンリーダーには、キーを押すたびに通知するのではなく、数字が入力されるフィールドの値の変化を伝えます。
  • ポインターの種類を問わず、キーの高さは52pxで、タッチターゲットに必要な44pxを上回ります。

#クラス

クラス説明
keypadキーパッド。キーは3列で8px間隔、全体の幅は最大17remです。数字は選択できず、ダブルタップでもズームしません。
keypad-keyキー1つをボタンで実装します。高さ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;  }}