导航

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 设置按键间距。

#页面负责的行为

数字键盘本身不处理按键行为。每个键的作用由页面决定,可以将数字追加到金额末尾、填入一次性验证码输入框中的一格,或发起呼叫。像示例那样,在键盘上加一个事件监听器即可。读取被按下按钮的 value,再将值写入对应位置。

在页面上保留一个实际的输入框。用户可以通过实体键盘输入,密码管理器可以自动填充,屏幕阅读器也能读出值的变化。在该输入框上设置 inputmode="none",可避免手机弹出系统键盘并遮挡数字键盘。

#无障碍

  • 为数字键盘设置 role="group" 和 aria-label。每个键都是按钮,数字键以显示的数字作为无障碍名称。只显示图标的键需要设置 aria-label。
  • 数字下方的字母也是按钮文本的一部分。如果用户听到“2 abc”只会受到干扰,就用带有 aria-hidden="true" 的元素包住这些字母。
  • 让屏幕阅读器跟踪接收数字的输入框,读出输入结果即可,无需逐次播报按键。
  • 无论使用哪种指针设备,按键高度均为 52px,高于触控目标所需的 44px。

#类名

类名说明
keypad按键分三列,间距为 8px,键盘最大宽度为 17rem。按键上的数字不可选中,双点按键盘也不会触发缩放。
keypad-key用于 button,表示一个按键:凸起的表面高 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;  }}