Repository navigation
feat(form): scroll to first invalid field on validation error #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Goosterhof
merged 7 commits into
script-development:main
from
Confmc:feat/form-scroll-to-first-error
Sep 14, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4a667f7
feat(form): scroll to first invalid field on validation error
Confmc d37f8fb
feat(form): reduced-motion + scrollTarget for scroll-to-error
Confmc 014f8ee
fix(form): guard matchMedia and skip scroll on an empty error bag
Confmc 55fb70e
fix(form): make scroll-to-error opt-in
Confmc 34e9a90
Merge branch 'main' into feat/form-scroll-to-first-error
Confmc 4b9bd42
docs(form): opt in to the scroll in every example
Confmc f841a5b
docs(form): stop offering useForm's scroll options to the primitive
Confmc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import type {Ref} from 'vue'; | ||
|
|
||
| import {watch} from 'vue'; | ||
|
|
||
| import type {ValidationErrors} from './types'; | ||
|
|
||
| /** The mark `@script-development/ui-inputs` renders from `:invalid`. */ | ||
| const DEFAULT_TARGET = '[aria-invalid="true"]'; | ||
|
|
||
| /** | ||
| * On every `errors` change, scroll the first invalid field into view (the mark the | ||
| * presentation layer sets); a no-op when the error bag is empty or nothing is marked. | ||
| * | ||
| * - `root` scopes the query to one form's subtree — omitted: document-wide; `null`: no | ||
| * scroll, never falling back to document. | ||
| * - `target` is the selector for the mark (default `[aria-invalid="true"]`); pass your | ||
| * own when your inputs mark errors with a class instead. | ||
| * - `behavior` is `'auto'` under `prefers-reduced-motion: reduce` — a JS `scrollIntoView` | ||
| * behavior is not subject to the CSS media query, so it is honoured here explicitly. | ||
| * | ||
| * `flush: 'post'` fires after the mark paints. Call inside `setup()` (as `useForm` | ||
| * does) so the watcher stops on unmount. | ||
| */ | ||
| export const useScrollToFirstError = ( | ||
| errors: Ref<ValidationErrors>, | ||
| root?: Ref<HTMLElement | null>, | ||
| target: string = DEFAULT_TARGET, | ||
| ): void => { | ||
| watch( | ||
|
jasperboerhof marked this conversation as resolved.
|
||
| errors, | ||
| () => { | ||
| if (!Object.keys(errors.value).length) return; | ||
|
|
||
| const scope = root === undefined ? document : root.value; | ||
|
jasperboerhof marked this conversation as resolved.
Goosterhof marked this conversation as resolved.
Goosterhof marked this conversation as resolved.
Goosterhof marked this conversation as resolved.
|
||
| const field = scope?.querySelector(target); | ||
| if (!field) return; | ||
|
|
||
| const reduced = typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches; | ||
| field.scrollIntoView({behavior: reduced ? 'auto' : 'smooth', block: 'center'}); | ||
| }, | ||
| {flush: 'post'}, | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.