ステッパー
両端のボタンで数値を増減できる入力欄。席数、数量、文字サイズなどに。
このページの内容
ステッパーは、席数、数量、文字サイズなど、直接入力するより少しずつ増減することの多い数値に使います。ネイティブの<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>#入力欄の標準機能
数値入力フィールドを使うため、ステッパーの基本機能の多くは、スクリプトを実行する前からプラットフォームが提供します。
- ↑と↓で値を
stepずつ増減でき、minとmaxで止まります。 - 入力欄はスピンボタンとして、現在の値と上下限とともに読み上げられます。
- スマートフォンでは数字キーボードが表示されます。
- 入力した値が範囲外の場合は
:user-invalidが適用され、ウェルにエラーを示す輪郭が付きます。
両端のボタンで増減を操作するため、スタイルシートでブラウザー標準のスピンボタンを非表示にします。
#ビヘイビア
stepper ビヘイビアを使うと、2つのボタンでその間の数値を増減できます。stepper クラスを持つルート要素と、書式付きの数値およびスクラブのオプションを受け取り、ビヘイビアを解除する関数を返します。
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 も渡します。
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>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>- 押すのではなくドラッグ。 ドラッグはポインターが 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を参照してください。
#クラス
| クラス | 説明 |
|---|---|
stepper | input-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-min | 3ch | 数値を収めるボックスの最小幅 |
--stepper-max | 9ch | field-sizingを使ったときの最大幅 |
#関連項目
- 入力フィールド背景面より一段くぼんだテキスト入力フィールド。ラベル、ヒント、エラー、グループに対応します。
- スライダー細くくぼんだトラックに、真珠調のつまみを備えたネイティブの範囲入力。
- ボタン面からわずかに浮いた操作要素です。ポインターを重ねるとさらに浮き上がり、押すと沈み込みます。