useInvalidShake
Shakes a form control when a submit attempt finds it invalid, and never while someone is still typing.
pnpm dlx shadcn@latest add https://hextaui.com/r/use-invalid-shake.jsonAdds the hook and anything it depends on to your project.
Copy and paste the following code into your project.
hooks/use-invalid-shake.ts Update the import paths to match your project setup.
The hook only sets a data-shake attribute. The motion comes from the theme's animate-button-shake, which you attach with data-shake:motion-safe:animate-button-shake. HextaUI's Input, Textarea, Native select and Input group already include that class and call the hook. Use it directly for your own controls.
A shake answers one question: why didn't my submit work? So the hook only shakes right after someone tries to submit, and never while they're still filling in the form.
| Event | Description |
|---|---|
Submit attempt | Clicking a submit button, pressing Enter in an input of the form, or the form's submit event. |
Invalid within 600ms | The control fires invalid, or has aria-invalid="true", data-invalid or :user-invalid. That covers native validation, Base UI fields and server errors set right after submitting. |
Shake | data-shake is set for 400ms. It shakes once per attempt, however many of those signals arrive. |
- Inside an
InputGroup, the whole group shakes, not just the inner control. - Under reduced motion, nothing is set. Pair the shake with a visible error message either way, since the shake is a cue, not the message.
- The hook needs the control to belong to a
<form>. Without one, it does nothing.
- Turn it off per control with the
enabledargument, or withshake={false}on HextaUI's inputs. - Restarting the attribute restarts the animation, so a second failed attempt shakes again even if the first is still running.
- Native validation focuses the first invalid control on submit. The shake only adds motion on top of that and never moves focus.
| Prop | Type | Default |
|---|---|---|
refThe form control to watch. | RefObject<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null> | – |
enabledWhether to shake. | boolean | true |
| Attribute | Description |
|---|---|
data-shake | Present for 400ms after a failed submit attempt, on the control or its InputGroup. |
Input, Textarea, NativeSelect and InputGroup.