Skip to content
English

How it works

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.

Use it for

  • 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.

Look elsewhere when

  • 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.

Usage

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>
  )
}

Props

PropTypeDefaultWhat it does
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).

Accessibility

  • 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.

Motion

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.

Add it to your project

Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.

ShakeFeedback.jsxshake-feedback.csstokens.cssREADME.md

Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.

Feedback
Confetti burst comes with Pro. Its preview shows once you unlock it.