本文へ移動
日本語

仕組み

Plays a decaying horizontal shake with the Web Animations API: seven evenly spaced keyframes (0, -8, 7, -5, 3, -1, 0px, scaled by distance) with sine-like easing. It uses the standalone translate property, so the element's own transform is untouched, and cancels the previous run on every trigger so rapid errors restart cleanly. flash adds a red outline on top of existing shadows.

向いている場面

  • A wrong PIN, password or code, so people see “not right” at a glance.
  • A required field left empty on submit: shake that input.
  • Light “no” feedback, such as pressing an unavailable button or dropping an item where it is not allowed.

向かない場面

  • Do not express an error by shaking alone: always add text that screen readers can read.
  • Success feedback: a shake means “wrong”; use a check or a small bounce.
  • Large areas such as a whole card or page: the movement can cause dizziness; shake only the faulty control.

使い方

import { useState } from 'react'
import { Shake, useShake } from './ShakeFeedback.jsx'

// Option 1: the hook, shaking the input directly
export function PayPin() {
  const { ref, shake } = useShake({ distance: 8, duration: 400, flash: true })
  const submit = async (pin) => {
    if (!(await api.checkPin(pin))) shake()
  }
  return <input ref={ref} type="password" onKeyDown={(e) => e.key === 'Enter' && submit(e.currentTarget.value)} />
}

// Option 2: the wrapper, shaking whenever trigger changes
export function LoginForm() {
  const [errors, setErrors] = useState(0)
  return (
    <Shake trigger={errors} flash>
      <LoginFields onFail={() => setErrors((n) => n + 1)} />
    </Shake>
  )
}

プロパティ

プロパティ型既定値説明
useShake(options)(options?) => { ref, shake: (override?) => void }—Attach ref to the element to shake and call shake() to play it once. shake() can override the options for that call.
options.distancenumber8Largest displacement in px; later swings decay to 7/8, 5/8, 3/8 and 1/8 of it.
options.durationnumber400Total duration of the shake in ms.
options.flashbooleanfalseAlso flashes a red outline. With reduced motion it always flashes, in place of the movement.
Shake.triggerunknown—Shakes when it changes to a new truthy value; a counter that grows with each error works best. No shake on first render or when it returns to 0 / false.
Shake.distance / duration / flashsame as options—Same as the hook options.
Shake.inlinebooleanfalseRenders as inline-block instead of block.
Shake ...restHTMLAttributes<HTMLDivElement>—Other attributes go to the wrapper. The red outline radius comes from --ds-shake-radius (default --ds-radius-md).

アクセシビリティ

  • The shake is visual emphasis only and is not announced; put the error text in a role="alert" or aria-live region and link it with aria-describedby.
  • Set aria-invalid on the field when it is wrong (the demo shows how).
  • With reduced motion there is no movement, only a red flash; CSS also blocks movement on the <Shake> wrapper.
  • The red outline sits on top of existing shadows, so the focus ring is not lost.

モーション

400ms by default, seven keyframes 0 → -8 → 7 → -5 → 3 → -1 → 0px, each segment cubic-bezier(0.37, 0, 0.63, 1). The red flash lasts max(duration, 500ms): held for 35%, then fading out.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

ShakeFeedback.jsxshake-feedback.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フィードバック
「紙吹雪」は Pro に含まれます。解除するとプレビューが表示されます。