Skip to content

Instantly share code, notes, and snippets.

@srph
Last active July 22, 2026 18:38
Show Gist options
  • Select an option

  • Save srph/1dc05cee97fa29c5b3513e0379d63cc0 to your computer and use it in GitHub Desktop.

Select an option

Save srph/1dc05cee97fa29c5b3513e0379d63cc0 to your computer and use it in GitHub Desktop.
React: useWiggle - Wiggle relevant elements when the user does the wrong thing twice

what

Wiggle relevant elements when the user does the wrong thing twice (e.g. submitting a form with validation errors twice)

usage

const { scope: wiggleRef, wiggle } = useWiggle();

const handleSubmit = () => {
  if (errors) {
    wiggle();
    return;
  }
}

<span ref={wiggleRef} className="inline-flex">
  <Button type="submit" variant="primary" disabled={pending}>
    Save address
  </Button>
</span>
import { useAnimate } from "motion/react";
const WIGGLE_X = [0, -8, 8, -6, 6, -3, 3, 0];
export function useWiggle() {
const [scope, animate] = useAnimate<HTMLElement>();
async function wiggle() {
if (!scope.current) return;
await animate(scope.current, { x: WIGGLE_X }, { duration: 0.35, ease: "easeInOut" });
}
return { scope, wiggle };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment