ナビゲーション

v2.1.0
フォーム

ステッパー

両端のボタンで数値を増減できる入力欄。席数、数量、文字サイズなどに。

このページの内容

ステッパーは、席数、数量、文字サイズなど、直接入力するより少しずつ増減することの多い数値に使います。ネイティブの<input type="number">を囲む入力グループで、入力欄の両端にボタンを配置します。グループにはstepper、入力フィールドにはstepper-fieldを追加します。

ステッパー

<div class="sheet flex items-center gap-lg p-md pl-lg">  <label class="text-sm fg-text-strong" for="seats">Seats</label>  <div class="input-group stepper">    <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Fewer seats">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M3 8L13 8" />      </svg>    </button>    <input class="input-group-field stepper-field" id="seats" type="number" min="1" max="12" value="4">    <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="More seats">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M3 8L13 8" /><path d="M8 3L8 13" />      </svg>    </button>  </div></div><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import MinusIcon from '@master/icons-react/native/16/soft/minus'import PlusIcon from '@master/icons-react/native/16/soft/plus'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root} className="sheet flex items-center gap-lg p-md pl-lg">      <label className="text-sm fg-text-strong" htmlFor="seats">Seats</label>      <div className="input-group stepper">        <button type="button" className="btn btn-square" value="-1" tabIndex={-1} aria-label="Fewer seats">          <MinusIcon className="icon" />        </button>        <input className="input-group-field stepper-field" id="seats" type="number" min="1" max="12" defaultValue="4" />        <button type="button" className="btn btn-square" value="1" tabIndex={-1} aria-label="More seats">          <PlusIcon className="icon" />        </button>      </div>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'  import MinusIcon from '@master/icons-svelte/native/16/soft/minus'  import PlusIcon from '@master/icons-svelte/native/16/soft/plus'</script><div class="sheet flex items-center gap-lg p-md pl-lg" {@attach attach}>  <label class="text-sm fg-text-strong" for="seats">Seats</label>  <div class="input-group stepper">    <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Fewer seats">      <MinusIcon class="icon" />    </button>    <input class="input-group-field stepper-field" id="seats" type="number" min="1" max="12" value="4">    <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="More seats">      <PlusIcon class="icon" />    </button>  </div></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import MinusIcon from '@master/icons-vue/native/16/soft/minus'import PlusIcon from '@master/icons-vue/native/16/soft/plus'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root" class="sheet flex items-center gap-lg p-md pl-lg">    <label class="text-sm fg-text-strong" for="seats">Seats</label>    <div class="input-group stepper">      <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Fewer seats">        <MinusIcon class="icon" />      </button>      <input class="input-group-field stepper-field" id="seats" type="number" min="1" max="12" value="4">      <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="More seats">        <PlusIcon class="icon" />      </button>    </div>  </div></template>
ボタンを押すか長押しするほか、数値を直接入力したり、入力欄で矢印キーを使ったりできます。値が1または12に達すると、それ以上増減できない側のボタンが平坦な表示になります。

#入力欄の標準機能

数値入力フィールドを使うため、ステッパーの基本機能の多くは、スクリプトを実行する前からプラットフォームが提供します。

  • ↑と↓で値をstepずつ増減でき、minとmaxで止まります。
  • 入力欄はスピンボタンとして、現在の値と上下限とともに読み上げられます。
  • スマートフォンでは数字キーボードが表示されます。
  • 入力した値が範囲外の場合は:user-invalidが適用され、ウェルにエラーを示す輪郭が付きます。

両端のボタンで増減を操作するため、スタイルシートでブラウザー標準のスピンボタンを非表示にします。

#ビヘイビア

stepper ビヘイビアを使うと、2つのボタンでその間の数値を増減できます。stepper クラスを持つルート要素と、書式付きの数値およびスクラブのオプションを受け取り、ビヘイビアを解除する関数を返します。

app.js
import { stepper } from '@master/ui/behaviors/stepper.js'const root = document.querySelector('.stepper')stepper(root)root.addEventListener('change', (event) => console.log(event.target.value))
  • 増減。 ボタンは入力欄の stepDown() または stepUp() を呼び出します。矢印キーも同じメソッドを使うため、min、max、step は入力欄に一度指定するだけで済みます。
  • 上限と下限。 値がボタンの増減方向の限界に達すると、そのボタンは disabled になります。無効になったボタンはウェル内でフラットに表示されます。入力欄が空の場合は、どちらの限界にも達していないものとして扱います。ビヘイビアを解除すると、各ボタンの disabled は適用前の状態に戻ります。
  • 長押し。 ボタンを押し続けると、短い待ち時間のあとに増減を繰り返します。押下を解除するか、ポインターがボタンの外に出るか、ポインター操作がキャンセルされるか、値が限界に達すると停止します。
  • 増減方向。 各ボタンの value で増減方向を指定します。負の値を指定すると減少します。指定がなければ、入力欄の前のボタンは減少、後ろのボタンは増加に割り当てられます。
  • イベント。 ボタンで数値が変わると、直接入力した場合と同じように、入力欄は input、続いて change を発火します。値は入力欄の value です。

ボタンには tabindex="-1" を指定します。タブ移動でフォーカスが止まるのは入力欄だけで、キー操作でもボタンと同じように増減できます。ビヘイビアを適用しなくても入力欄は標準の機能を保ちますが、ボタンは動作しません。

#サイズ

ステッパーはグループのサイズに従います。input-group-sm は 32px、input-group-lg は 44px で、同じサイズのボタンと高さが揃います。内側のボタンはウェルより 8px 低く、角の丸みはウェルと同心になります。

サイズ

<div class="input-group input-group-sm stepper">  <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Decrease">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" />    </svg>  </button>  <input class="input-group-field stepper-field" type="number" value="2" aria-label="Small">  <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Increase">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" /><path d="M8 3L8 13" />    </svg>  </button></div><div class="input-group stepper">  <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Decrease">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" />    </svg>  </button>  <input class="input-group-field stepper-field" type="number" value="16" aria-label="Medium">  <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Increase">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" /><path d="M8 3L8 13" />    </svg>  </button></div><div class="input-group input-group-lg stepper">  <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Decrease">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" />    </svg>  </button>  <input class="input-group-field stepper-field" type="number" value="128" aria-label="Large">  <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Increase">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" /><path d="M8 3L8 13" />    </svg>  </button></div><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import MinusIcon from '@master/icons-react/native/16/soft/minus'import PlusIcon from '@master/icons-react/native/16/soft/plus'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root}>      <div className="input-group input-group-sm stepper">        <button type="button" className="btn btn-square" value="-1" tabIndex={-1} aria-label="Decrease">          <MinusIcon className="icon" />        </button>        <input className="input-group-field stepper-field" type="number" defaultValue="2" aria-label="Small" />        <button type="button" className="btn btn-square" value="1" tabIndex={-1} aria-label="Increase">          <PlusIcon className="icon" />        </button>      </div>      <div className="input-group stepper">        <button type="button" className="btn btn-square" value="-1" tabIndex={-1} aria-label="Decrease">          <MinusIcon className="icon" />        </button>        <input className="input-group-field stepper-field" type="number" defaultValue="16" aria-label="Medium" />        <button type="button" className="btn btn-square" value="1" tabIndex={-1} aria-label="Increase">          <PlusIcon className="icon" />        </button>      </div>      <div className="input-group input-group-lg stepper">        <button type="button" className="btn btn-square" value="-1" tabIndex={-1} aria-label="Decrease">          <MinusIcon className="icon" />        </button>        <input className="input-group-field stepper-field" type="number" defaultValue="128" aria-label="Large" />        <button type="button" className="btn btn-square" value="1" tabIndex={-1} aria-label="Increase">          <PlusIcon className="icon" />        </button>      </div>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'  import MinusIcon from '@master/icons-svelte/native/16/soft/minus'  import PlusIcon from '@master/icons-svelte/native/16/soft/plus'</script><div {@attach attach}>  <div class="input-group input-group-sm stepper">    <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Decrease">      <MinusIcon class="icon" />    </button>    <input class="input-group-field stepper-field" type="number" value="2" aria-label="Small">    <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Increase">      <PlusIcon class="icon" />    </button>  </div>  <div class="input-group stepper">    <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Decrease">      <MinusIcon class="icon" />    </button>    <input class="input-group-field stepper-field" type="number" value="16" aria-label="Medium">    <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Increase">      <PlusIcon class="icon" />    </button>  </div>  <div class="input-group input-group-lg stepper">    <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Decrease">      <MinusIcon class="icon" />    </button>    <input class="input-group-field stepper-field" type="number" value="128" aria-label="Large">    <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Increase">      <PlusIcon class="icon" />    </button>  </div></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import MinusIcon from '@master/icons-vue/native/16/soft/minus'import PlusIcon from '@master/icons-vue/native/16/soft/plus'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root">    <div class="input-group input-group-sm stepper">      <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Decrease">        <MinusIcon class="icon" />      </button>      <input class="input-group-field stepper-field" type="number" value="2" aria-label="Small">      <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Increase">        <PlusIcon class="icon" />      </button>    </div>    <div class="input-group stepper">      <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Decrease">        <MinusIcon class="icon" />      </button>      <input class="input-group-field stepper-field" type="number" value="16" aria-label="Medium">      <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Increase">        <PlusIcon class="icon" />      </button>    </div>    <div class="input-group input-group-lg stepper">      <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Decrease">        <MinusIcon class="icon" />      </button>      <input class="input-group-field stepper-field" type="number" value="128" aria-label="Large">      <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Increase">        <PlusIcon class="icon" />      </button>    </div>  </div></template>

#幅

ウェルの幅は数値の桁数に合わせて決まります。ブラウザーが field-sizing: content に対応している場合、入力欄は入力された桁数に応じて 3 桁分から 9 桁分まで広がります。それ以外では 3 桁分の幅になります。別の桁数に合わせるには --stepper-min と --stepper-max を設定します。ステッパー自体に幅を指定すると、ボタンは両端に寄り、数値はその間の中央に表示されます。

単位は入力欄の後ろに input-group-addon で表示します。step を大きくすると、一度に増減する量が増えます。

単位と増減幅

<div class="input-group stepper" style="--stepper-min: 5ch">  <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Lower the budget">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" />    </svg>  </button>  <input class="input-group-field stepper-field" type="number" min="0" step="250" value="1250" aria-label="Monthly budget">  <span class="input-group-addon">USD</span>  <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Raise the budget">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" /><path d="M8 3L8 13" />    </svg>  </button></div><div class="input-group stepper w(280)">  <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Fewer replicas">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" />    </svg>  </button>  <input class="input-group-field stepper-field" type="number" min="1" max="64" value="8" aria-label="Replicas">  <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="More replicas">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" /><path d="M8 3L8 13" />    </svg>  </button></div><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import MinusIcon from '@master/icons-react/native/16/soft/minus'import PlusIcon from '@master/icons-react/native/16/soft/plus'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root}>      <div className="input-group stepper" style={{ '--stepper-min': '5ch' } as React.CSSProperties}>        <button type="button" className="btn btn-square" value="-1" tabIndex={-1} aria-label="Lower the budget">          <MinusIcon className="icon" />        </button>        <input className="input-group-field stepper-field" type="number" min="0" step="250" defaultValue="1250" aria-label="Monthly budget" />        <span className="input-group-addon">USD</span>        <button type="button" className="btn btn-square" value="1" tabIndex={-1} aria-label="Raise the budget">          <PlusIcon className="icon" />        </button>      </div>      <div className="input-group stepper w(280)">        <button type="button" className="btn btn-square" value="-1" tabIndex={-1} aria-label="Fewer replicas">          <MinusIcon className="icon" />        </button>        <input className="input-group-field stepper-field" type="number" min="1" max="64" defaultValue="8" aria-label="Replicas" />        <button type="button" className="btn btn-square" value="1" tabIndex={-1} aria-label="More replicas">          <PlusIcon className="icon" />        </button>      </div>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'  import MinusIcon from '@master/icons-svelte/native/16/soft/minus'  import PlusIcon from '@master/icons-svelte/native/16/soft/plus'</script><div {@attach attach}>  <div class="input-group stepper" style="--stepper-min: 5ch">    <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Lower the budget">      <MinusIcon class="icon" />    </button>    <input class="input-group-field stepper-field" type="number" min="0" step="250" value="1250" aria-label="Monthly budget">    <span class="input-group-addon">USD</span>    <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Raise the budget">      <PlusIcon class="icon" />    </button>  </div>  <div class="input-group stepper w(280)">    <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Fewer replicas">      <MinusIcon class="icon" />    </button>    <input class="input-group-field stepper-field" type="number" min="1" max="64" value="8" aria-label="Replicas">    <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="More replicas">      <PlusIcon class="icon" />    </button>  </div></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import MinusIcon from '@master/icons-vue/native/16/soft/minus'import PlusIcon from '@master/icons-vue/native/16/soft/plus'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root">    <div class="input-group stepper" style="--stepper-min: 5ch">      <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Lower the budget">        <MinusIcon class="icon" />      </button>      <input class="input-group-field stepper-field" type="number" min="0" step="250" value="1250" aria-label="Monthly budget">      <span class="input-group-addon">USD</span>      <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Raise the budget">        <PlusIcon class="icon" />      </button>    </div>    <div class="input-group stepper w(280)">      <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Fewer replicas">        <MinusIcon class="icon" />      </button>      <input class="input-group-field stepper-field" type="number" min="1" max="64" value="8" aria-label="Replicas">      <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="More replicas">        <PlusIcon class="icon" />      </button>    </div>  </div></template>

#書式付きの数値

価格、割合、大きな数は、$1,250、12%、1,250,000 のように書式を整えたほうが読みやすくなります。入力欄と stepper-display を stepper-value に入れ、ビヘイビアに format(Intl.NumberFormat のオプション)を渡します。ページとは異なるロケールで数値を書式設定する場合は、locale も渡します。

app.js
stepper(root, { format: { style: 'currency', currency: 'USD', maximumFractionDigits: 0 }, locale: 'en-US' })

書式付きの数値

<div class="input-group stepper">  <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Lower the budget">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" />    </svg>  </button>  <span class="stepper-value">    <input class="input-group-field stepper-field" type="number" min="0" step="250" value="1250" aria-label="Monthly budget">    <span class="stepper-display" aria-hidden="true"></span>  </span>  <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="Raise the budget">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" /><path d="M8 3L8 13" />    </svg>  </button></div><div class="input-group stepper">  <button type="button" class="btn btn-square" value="-1" tabindex="-1" aria-label="Fewer requests">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" />    </svg>  </button>  <span class="stepper-value">    <input class="input-group-field stepper-field" type="number" min="0" step="50000" value="1250000" aria-label="Requests a month">    <span class="stepper-display" aria-hidden="true"></span>  </span>  <button type="button" class="btn btn-square" value="1" tabindex="-1" aria-label="More requests">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" /><path d="M8 3L8 13" />    </svg>  </button></div><script>addEventListener('DOMContentLoaded', function () {const [budget, requests] = document.querySelectorAll('.stepper')MasterUI.stepper(budget, { format: { style: 'currency', currency: 'USD', maximumFractionDigits: 0 }, locale: 'en-US' })MasterUI.stepper(requests, { locale: 'en-US' })})</script><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
予算を増減するか、クリックして入力します。入力欄にフォーカスがある間は編集用の書式なしの数値が表示され、フォーカスが外れると書式付きの数値が再び表示されます。2つ目の表示には書式がなく、桁区切りだけを行います。

attach() はオプションを渡さないため、このプレビューでは attach() の実行後に stepper を明示的に呼び出します。同じルートに対して 2 回呼び出すと、2 回目が 1 回目を置き換えます。MasterUI はプレビューが読み込むバンドルです。プロジェクトでは、ビヘイビアセクションの例に従って stepper をインポートします。

  • 数値は入力欄が保持します。 入力欄にフォーカスがない間は表示部が入力欄の位置に表示され、入力欄自体の数字は透明になります。入力欄にフォーカスすると表示部は退き、読者は $1,250 ではなく 1250 を編集します。矢印キー、min、max、step、入力検証、フォームが送信する値は、これまでどおり数値入力欄のものです。
  • 読み上げる言葉。 ボタン、キー操作、入力、外部からの値の設定など、値が変わるたびに、ビヘイビアは書式付きの数値を表示部と入力欄の aria-valuetext に書き込みます。そのため、スクリーンリーダーも「$1,250」と読み上げます。表示部には aria-hidden="true" を指定します。
  • 書式を指定しない場合。 stepper-display があり format がないステッパーは、ロケールに従って桁区切りだけを行います。Intl.NumberFormat では書けない表記には、format に数値を受け取る関数を指定することもできます。
  • 幅。 数値部分の幅は、入力欄と表示部のうち広いほうに合わせ、--stepper-min から --stepper-max の範囲に収まります。そのため、入力欄にフォーカスしてもボタンの位置は変わりません。
  • スクリプトがない場合、表示部は空のままで、入力欄が通常どおり数値を表示します。

#スクラブ

デザインツールでは、ラベルをドラッグして数値を変更することがよくあります。入力欄に対応する <label>、またはウェル内のハンドルに stepper-scrub クラスを付けます。ラベルはページ内のどこに置いてもかまいません。横にドラッグすると 4px ごとに 1 ステップずつ数値が変わり、行の終端方向で値が増えます。Shift を押している間は、10 ステップずつ変わります。

スクラブ

<div class="flex items-center gap-md">  <label class="stepper-scrub text-sm fg-text-strong" for="opacity">Opacity</label>  <div class="input-group input-group-sm stepper">    <input class="input-group-field stepper-field" id="opacity" type="number" min="0" max="100" step="1" value="64">    <span class="input-group-addon">%</span>  </div></div><div class="input-group input-group-sm stepper" style="--stepper-min: 4ch">  <span class="input-group-addon stepper-scrub" aria-hidden="true">W</span>  <input class="input-group-field stepper-field" type="number" min="1" max="4096" value="320" aria-label="Width">  <span class="input-group-addon">px</span></div><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root}>      <div className="flex items-center gap-md">        <label className="stepper-scrub text-sm fg-text-strong" htmlFor="opacity">Opacity</label>        <div className="input-group input-group-sm stepper">          <input className="input-group-field stepper-field" id="opacity" type="number" min="0" max="100" step="1" defaultValue="64" />          <span className="input-group-addon">%</span>        </div>      </div>      <div className="input-group input-group-sm stepper" style={{ '--stepper-min': '4ch' } as React.CSSProperties}>        <span className="input-group-addon stepper-scrub" aria-hidden="true">W</span>        <input className="input-group-field stepper-field" type="number" min="1" max="4096" defaultValue="320" aria-label="Width" />        <span className="input-group-addon">px</span>      </div>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'</script><div {@attach attach}>  <div class="flex items-center gap-md">    <label class="stepper-scrub text-sm fg-text-strong" for="opacity">Opacity</label>    <div class="input-group input-group-sm stepper">      <input class="input-group-field stepper-field" id="opacity" type="number" min="0" max="100" step="1" value="64">      <span class="input-group-addon">%</span>    </div>  </div>  <div class="input-group input-group-sm stepper" style="--stepper-min: 4ch">    <span class="input-group-addon stepper-scrub" aria-hidden="true">W</span>    <input class="input-group-field stepper-field" type="number" min="1" max="4096" value="320" aria-label="Width">    <span class="input-group-addon">px</span>  </div></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root">    <div class="flex items-center gap-md">      <label class="stepper-scrub text-sm fg-text-strong" for="opacity">Opacity</label>      <div class="input-group input-group-sm stepper">        <input class="input-group-field stepper-field" id="opacity" type="number" min="0" max="100" step="1" value="64">        <span class="input-group-addon">%</span>      </div>    </div>    <div class="input-group input-group-sm stepper" style="--stepper-min: 4ch">      <span class="input-group-addon stepper-scrub" aria-hidden="true">W</span>      <input class="input-group-field stepper-field" type="number" min="1" max="4096" value="320" aria-label="Width">      <span class="input-group-addon">px</span>    </div>  </div></template>
「Opacity」ラベル、または 2 つ目の入力欄の先頭にある「W」を横にドラッグします。ドラッグせずに押した場合は、入力欄にフォーカスが移ります。
  • 押すのではなくドラッグ。 ドラッグはポインターが 3px 動いた時点で始まるため、ラベルを押すだけなら入力欄にフォーカスが移ります。ドラッグを終えたときのクリックは無視されます。
  • ポインターロック。 マウスでは、ブラウザーが対応していればドラッグ中にポインターをロックするため、長くドラッグしても画面の端にぶつかりません。ポインターは元の位置に戻ります。ペンや指の場合は代わりにハンドルがポインターをキャプチャーし、タッチ画面での縦方向のスワイプでは引き続きページがスクロールします。
  • イベント。 入力欄はステップごとに input を発行し、ドラッグが終わったときに change を一度発行します。
  • 方向。 右から左に書くページでは、行の先頭方向、つまり左へのドラッグで値が増えます。
  • スクラブはポインター向けの便利な機能です。キー操作、ボタン、入力でスクラブと同じことがすべてできるため、専用のキー操作は不要で、ハンドルはタブ順序に含めません。

#無効

入力欄と両方のボタンを無効にします。ウェルの配置はそのままで、数値は無効状態の文字色になります。入力欄が無効または読み取り専用の場合、ビヘイビアは値を増減しません。このプレビューはマークアップのみで、スクリプトは適用していません。

無効

<div class="input-group stepper">  <button type="button" class="btn btn-square" tabindex="-1" aria-label="Decrease" disabled>    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" />    </svg>  </button>  <input class="input-group-field stepper-field" type="number" value="3" aria-label="Seats included in your plan" disabled>  <button type="button" class="btn btn-square" tabindex="-1" aria-label="Increase" disabled>    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M3 8L13 8" /><path d="M8 3L8 13" />    </svg>  </button></div>
import MinusIcon from '@master/icons-react/native/16/soft/minus'import PlusIcon from '@master/icons-react/native/16/soft/plus'<div className="input-group stepper">  <button type="button" className="btn btn-square" tabIndex={-1} aria-label="Decrease" disabled>    <MinusIcon className="icon" />  </button>  <input className="input-group-field stepper-field" type="number" defaultValue="3" aria-label="Seats included in your plan" disabled />  <button type="button" className="btn btn-square" tabIndex={-1} aria-label="Increase" disabled>    <PlusIcon className="icon" />  </button></div>
<script>  import MinusIcon from '@master/icons-svelte/native/16/soft/minus'  import PlusIcon from '@master/icons-svelte/native/16/soft/plus'</script><div class="input-group stepper">  <button type="button" class="btn btn-square" tabindex="-1" aria-label="Decrease" disabled>    <MinusIcon class="icon" />  </button>  <input class="input-group-field stepper-field" type="number" value="3" aria-label="Seats included in your plan" disabled>  <button type="button" class="btn btn-square" tabindex="-1" aria-label="Increase" disabled>    <PlusIcon class="icon" />  </button></div>
<script setup>import MinusIcon from '@master/icons-vue/native/16/soft/minus'import PlusIcon from '@master/icons-vue/native/16/soft/plus'</script><template>  <div class="input-group stepper">    <button type="button" class="btn btn-square" tabindex="-1" aria-label="Decrease" disabled>      <MinusIcon class="icon" />    </button>    <input class="input-group-field stepper-field" type="number" value="3" aria-label="Seats included in your plan" disabled>    <button type="button" class="btn btn-square" tabindex="-1" aria-label="Increase" disabled>      <PlusIcon class="icon" />    </button>  </div></template>

#ほかのコンポーネントを使う場面

  • ポート番号や価格のように、一度入力したあとで調整することが少ない数値には、type="number" を指定した通常の入力欄を使います。
  • 広い範囲の中で、正確な数値よりも感覚的な調整を重視する場合は、スライダーを使います。
  • ラベル付きの少数の数量から選ぶ場合は、セグメントコントロールを使います。

#キーボード操作

数値入力欄の標準のキー操作を使います。ビヘイビアによるキー操作の追加はありません。Shift はスクラブでのみ意味を持ちます。

キー操作
ArrowUp, ArrowDown数値入力と同様に、値を step 刻みで増減し、min と max で止めます。
Shift (while scrubbing)ドラッグ量 1 ステップごとに 10 ステップ増減します

#アクセシビリティ

  • <label>、フィールド、または aria-label で入力欄にラベルを付けます。各ボタンには、何を変更するかがわかる名前を付けます。たとえば「プラス」ではなく「座席数を増やす」とします。
  • min、max、step は入力欄に指定します。キー操作、ボタン、支援技術はいずれもこの設定を参照します。
  • ボタンはタブ順序から外します。キーボードでは入力欄の矢印キーで値を増減できます。ポインター操作では 30px の操作領域を、タッチ操作では同じ 30px のボタンの周囲に 44px の操作領域を確保します。
  • 書式付きの数値は入力欄の aria-valuetext で伝え、それを表示する表示部は支援技術から隠します。ウェル内のスクラブ用ハンドルも、キー操作と同じことを繰り返すだけなので隠します。

#React、Svelte、Vue

stepper コンポーネントはビヘイビアを適用し、数値の受け渡しを行います。書式付きの数値には format と、必要に応じて locale を渡します。コンポーネントは stepper-value と表示部を描画し、最初の数値を書き込んだ状態にするため、サーバーが送る HTML にも数値が表示されます。stepper-scrub クラスを付けた入力欄のラベルは、フレームワークを使わない場合と同じようにスクラブで値を変更します。npx master-ui add stepper でプロジェクトにコピーできます。コンポーネントの仕組みと使い方は、React、Svelte、Vueを参照してください。

ui/stepper.tsx
'use client'import { useState, type ComponentProps } from 'react'import { stepper } from '@master/ui/behaviors/stepper.js'import { useBehavior } from './use-behavior'export interface StepperProps extends Omit<ComponentProps<'input'>, 'value' | 'defaultValue' | 'type'> {  /** The number; null while the field is empty. */  value?: number | null  defaultValue?: number | null  onValueChange?: (value: number | null) => void  /** The names of the two buttons, which show only an icon. */  decrementLabel: string  incrementLabel: string  /**   * Write the figure with `Intl.NumberFormat` while the field is not focused: a unit, a currency, grouping.   * The field keeps the plain number. Read when the stepper attaches, as `locale` is.   */  format?: Intl.NumberFormatOptions  /** The locale the figure is written in. The page's by default. */  locale?: string}/** * A number between two buttons. Name the field with `aria-label`, or give it an `id` a label points at; * `min`, `max`, `step` and every other prop of an input go to the field; `disabled` disables the buttons too. A * label with the `stepper-scrub` class steps the number as it is dragged sideways. */export function Stepper({ value, defaultValue, onValueChange, decrementLabel, incrementLabel, format, locale, disabled, className, ...field }: StepperProps) {  const ref = useBehavior(stepper, { format, locale }, { value, defaultValue, onValueChange })  const [first] = useState(value ?? defaultValue)  // The first figure is drawn into the first render, so the HTML a server sends shows it.  const [written] = useState(() => (format && typeof first === 'number' ? new Intl.NumberFormat(locale, format).format(first) : undefined))  const input = <input className="input-group-field stepper-field" type="number" defaultValue={first ?? undefined} aria-valuetext={written} disabled={disabled} {...field} />  return (    <div className={className ? `input-group stepper ${className}` : 'input-group stepper'} ref={ref}>      <button type="button" className="btn btn-square" value="-1" tabIndex={-1} aria-label={decrementLabel} disabled={disabled}>        <svg className="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M3 8L13 8" /></svg>      </button>      {format        ? (            <span className="stepper-value">              {input}              <span className="stepper-display" aria-hidden="true">{written}</span>            </span>          )        : input}      <button type="button" className="btn btn-square" value="1" tabIndex={-1} aria-label={incrementLabel} disabled={disabled}>        <svg className="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M3 8L13 8" /><path d="M8 3L8 13" /></svg>      </button>    </div>  )}
ui/Stepper.svelte
<script lang="ts" module>  import type { HTMLInputAttributes } from 'svelte/elements'  export interface StepperProps extends Omit<HTMLInputAttributes, 'value' | 'type' | 'children'> {    /** The number; null while the field is empty. Bind it, or leave it out. */    value?: number | null    onValueChange?: (value: number | null) => void    /** The names of the two buttons, which show only an icon. */    decrementLabel: string    incrementLabel: string    /**     * Write the figure with `Intl.NumberFormat` while the field is not focused: a unit, a currency, grouping.     * The field keeps the plain number. Read when the stepper attaches, as `locale` is.     */    format?: Intl.NumberFormatOptions    /** The locale the figure is written in. The page's by default. */    locale?: string  }</script><script lang="ts">  import { untrack } from 'svelte'  import { stepper } from '@master/ui/behaviors/stepper.js'  import { behavior } from './behavior.svelte.js'  let { value = $bindable(), onValueChange, decrementLabel, incrementLabel, format, locale, disabled, class: className, ...field }: StepperProps = $props()  const first = untrack(() => value)  // The first figure is drawn into the first render, so the HTML a server sends shows it.  const written = untrack(() => (format && typeof first === 'number' ? new Intl.NumberFormat(locale, format).format(first) : undefined))  const attach = behavior(stepper, () => ({ format, locale }), {    value: () => value,    change: (next) => { value = next; onValueChange?.(next) },  })</script><!--  @component  A number between two buttons. Name the field with `aria-label`, or give it an `id` a label points at;  `min`, `max`, `step` and every other prop of an input go to the field; `disabled` disables the buttons too. A  label with the `stepper-scrub` class steps the number as it is dragged sideways.--><div class={['input-group', 'stepper', className]} {@attach attach}>  <button type="button" class="btn btn-square" value="-1" tabindex={-1} aria-label={decrementLabel} {disabled}>    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 8L13 8" /></svg>  </button>  {#if format}    <span class="stepper-value">      <input class="input-group-field stepper-field" type="number" value={first ?? undefined} aria-valuetext={written} {disabled} {...field} />      <span class="stepper-display" aria-hidden="true">{written}</span>    </span>  {:else}    <input class="input-group-field stepper-field" type="number" value={first ?? undefined} {disabled} {...field} />  {/if}  <button type="button" class="btn btn-square" value="1" tabindex={-1} aria-label={incrementLabel} {disabled}>    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 8L13 8" /><path d="M8 3L8 13" /></svg>  </button></div>
ui/Stepper.vue
<script setup lang="ts">import { useAttrs, useTemplateRef } from 'vue'import { stepper } from '@master/ui/behaviors/stepper.js'import { useBehavior } from './use-behavior'defineOptions({ inheritAttrs: false })const props = defineProps<{  /** The names of the two buttons, which show only an icon. */  decrementLabel: string  incrementLabel: string  /**   * Write the figure with `Intl.NumberFormat` while the field is not focused: a unit, a currency, grouping.   * The field keeps the plain number. Read when the stepper attaches, as `locale` is.   */  format?: Intl.NumberFormatOptions  /** The locale the figure is written in. The page's by default. */  locale?: string  /** Disables the field and both buttons. */  disabled?: boolean}>()/** The number; null while the field is empty: `v-model`. */const model = defineModel<number | null>()/* * A number between two buttons. Name the field with `aria-label`, or give it an `id` a label points at; * `min`, `max`, `step` and every other attribute but the class go to the field; `disabled` disables the buttons * too. A label with the `stepper-scrub` class steps the number as it is dragged sideways. */const attrs = useAttrs()const field = () => {  const { class: _, ...rest } = attrs  return rest}// The first number is the field's default value, an attribute: what is typed after is not Vue's to write.const first = model.value// The first figure is drawn into the first render, so the HTML a server sends shows it.const written = props.format && typeof first === 'number' ? new Intl.NumberFormat(props.locale, props.format).format(first) : undefinedconst root = useTemplateRef<HTMLDivElement>('root')useBehavior(root, stepper, () => ({ format: props.format, locale: props.locale }), model)</script><template>  <div class="input-group stepper" :class="$attrs.class" ref="root">    <button type="button" class="btn btn-square" value="-1" tabindex="-1" :aria-label="decrementLabel" :disabled="disabled">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 8L13 8" /></svg>    </button>    <span v-if="format" class="stepper-value">      <input class="input-group-field stepper-field" type="number" :value.attr="first ?? undefined" :aria-valuetext="written" :disabled="disabled" v-bind="field()">      <span class="stepper-display" aria-hidden="true">{{ written }}</span>    </span>    <input v-else class="input-group-field stepper-field" type="number" :value.attr="first ?? undefined" :disabled="disabled" v-bind="field()">    <button type="button" class="btn btn-square" value="1" tabindex="-1" :aria-label="incrementLabel" :disabled="disabled">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 8L13 8" /><path d="M8 3L8 13" /></svg>    </button>  </div></template>

#クラス

クラス説明
stepperinput-group に適用します。数値の幅に合わせたくぼみの両端にボタンを配置します。上限または下限に達して無効になったボタンは、くぼみの中で平らに表示されます。
stepper-fieldグループ内の input-group-field であるネイティブの数値入力に適用します。等幅数字を中央に揃え、スピンボタンを非表示にします。
stepper-valueフィールドとその stepper-display を囲み、2 つのうち幅の広い方に合わせた 1 つのセルにします。フィールドにフォーカスがなく、表示に数値がある間は、フィールド自体の数字は透明になります。
stepper-displayフィールドの後ろ、stepper-value 内に置き、aria-hidden="true" を付けます。Intl.NumberFormat で書式化した数値を、フィールドにフォーカスがない間フィールドの位置に表示します。ビヘイビアが書き込みます。
stepper-scrubくぼみ内のハンドル、またはページ内の任意の位置にあるフィールドのラベルで、横にドラッグすると数値を増減します。ew-resize カーソルを表示し、テキストは選択されません。

#カスタムプロパティ

プロパティ既定値説明
--stepper-min3ch数値を収めるボックスの最小幅
--stepper-max9chfield-sizingを使ったときの最大幅

#ソース

@master/ui/components/stepper.css
/* * Stepper: a number between the two buttons that step it. * It is an input group around a native number input: the well holds a small raised button at each end, 4px in, * and the figure stands centered between them in tabular numerals. The field keeps what the platform gives a * number input: the arrow keys, min, max, step and the spinbutton role. The buttons call stepDown() and * stepUp() from a few lines of script; without it the field still types and steps from the keys. * The browser's own spin buttons are removed, since the two buttons replace them. * The well is as wide as its figure. Where field-sizing exists it follows the digits typed, between * --stepper-min and --stepper-max; elsewhere it keeps --stepper-min. * A figure written with a unit, a currency or grouping stands in a stepper-display over the field, shown while * the field is not focused; a stepper-scrub handle or label steps the number as it is dragged sideways. */@layer components.ui {  .stepper {    --stepper-min: 3ch;    --stepper-max: 9ch;    display: inline-flex;    max-width: 100%;    vertical-align: middle;    /* The buttons stand 4px from the figure's box, as they stand 4px from the well. */    gap: 4px;    cursor: default;    /* A step that has reached its limit lies flat in the well: no face, no edge, only its quiet glyph. */    .btn:disabled {      background: transparent;      box-shadow: none;    }  }  .stepper-field {    flex: 1 1 auto;    width: calc(var(--stepper-min) + 1rem);    padding-inline: .5rem;    font-weight: var(--font-weight-control);    font-variant-numeric: tabular-nums;    text-align: center;    appearance: textfield;    cursor: text;    /* Its own text cursor would otherwise stand over the group's not-allowed one. */    &:disabled {      cursor: not-allowed;    }    &::-webkit-inner-spin-button, &::-webkit-outer-spin-button {      display: none;    }    /*     * A unit after the field belongs to the figure: the digits stand at the field's end and the unit follows them     * at the gap, as one phrase.     */    &:has(+ .input-group-addon) {      padding-inline-end: 0;      text-align: end;    }    /*     * Sized by its digits, the field stops at --stepper-max even in a wider stepper. The room it leaves goes to     * either side of the figure, and of its unit when one follows, so the figure stays centered between the     * buttons and the buttons keep to the ends.     */    @supports (field-sizing: content) {      width: auto;      min-width: calc(var(--stepper-min) + 1rem);      max-width: calc(var(--stepper-max) + 1rem);      margin-inline: auto;      field-sizing: content;      &:has(+ .input-group-addon) {        margin-inline-end: 0;      }      + .input-group-addon {        margin-inline-end: auto;      }    }  }  /*   * A formatted figure: the field and its display stacked in one cell, as wide as the wider of the two. The   * field always fills the cell, so a press anywhere on the figure lands in it. While the field is not focused   * its own digits are clear and the display shows the formatted figure in the same place; while it is   * focused the display steps aside and the plain number is edited. Without the script the display is empty   * and the field shows its number as usual.   */  .stepper-value {    display: inline-grid;    flex: 1 1 auto;    align-self: stretch;    min-width: 0;    .stepper-field {      grid-area: 1 / 1;      min-width: 100%;    }    &:not(:focus-within):has(.stepper-display:not(:empty)) .stepper-field {      color: transparent;    }    &:focus-within .stepper-display {      visibility: hidden;    }    &:has(.stepper-field:disabled) .stepper-display {      color: var(--color-text-disabled);    }    /*     * Forced colors would repaint the clear digits in the field's text color, over the display's. The field     * keeps its own colors there while the display stands in for it.     */    @media (forced-colors: active) {      &:not(:focus-within):has(.stepper-display:not(:empty)) .stepper-field {        forced-color-adjust: none;      }    }  }  /* A unit stands as far from the button after it as the figure stands from the button before. */  .stepper .input-group-addon {    padding-inline-end: .5rem;  }  .stepper-display {    grid-area: 1 / 1;    align-self: center;    min-width: calc(var(--stepper-min) + 1rem);    max-width: calc(var(--stepper-max) + 1rem);    padding-inline: .5rem;    overflow: hidden;    color: var(--color-text-strong);    font-weight: var(--font-weight-control);    font-variant-numeric: tabular-nums;    text-align: center;    text-overflow: ellipsis;    white-space: nowrap;    pointer-events: none;    user-select: none;    -webkit-user-select: none;  }  /*   * A scrub handle: a handle in the well, or a label of the field anywhere in the page. Dragged sideways it   * steps the number. It keeps a vertical swipe for the page and takes the sideways one.   */  .stepper-scrub {    cursor: ew-resize;    touch-action: pan-y;    user-select: none;    -webkit-user-select: none;    -webkit-tap-highlight-color: transparent;  }  /* A field that is disabled or read-only steps nothing, so a handle in its well no longer offers to drag. */  .stepper:has(.stepper-field:is(:disabled, [readonly])) .stepper-scrub {    cursor: inherit;  }}