fix: use preventScroll when focusing hidden inputs#647
Open
tggreene wants to merge 1 commit intokobaltedev:mainfrom
Open
fix: use preventScroll when focusing hidden inputs#647tggreene wants to merge 1 commit intokobaltedev:mainfrom
tggreene wants to merge 1 commit intokobaltedev:mainfrom
Conversation
When control elements (checkbox, switch, radio, etc.) are clicked,
they call `inputRef()?.focus()` to move focus to the visually-hidden
input element. Because the input is positioned with `position: absolute`
and `clip: rect(0, 0, 0, 0)`, the browser's default focus-scroll
behavior calculates a bogus scroll target, causing the scroll container
to jump to an unexpected position.
This is especially noticeable when:
- The HTML/body elements are not the scroll container (a nested div is)
- The control is inside a scrollable panel or modal
- Multiple controls are stacked vertically
The fix is straightforward: pass `{ preventScroll: true }` to all
`focus()` calls on hidden input refs. This is a standard DOM API
supported in all modern browsers that focuses the element without
triggering scroll-into-view behavior.
Affected components:
- Checkbox.Control
- Switch.Control
- RadioGroup.ItemControl
- NumberField.VaryTrigger
- Combobox.Trigger
- Combobox.HiddenSelect
Fixes kobaltedev#452
👷 Deploy request for kobalte pending review.Visit the deploys page to approve it
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When control elements (Checkbox, Switch, Radio, etc.) are clicked, they call
inputRef()?.focus()to move focus to the visually-hidden input element. Because the input usesposition: absolutewithclip: rect(0, 0, 0, 0), the browser's default focus-scroll behavior calculates a bogus scroll target, causing the scroll container to jump to an unexpected position.This is especially noticeable when:
Fix
Pass
{ preventScroll: true }to allfocus()calls on hidden input refs. This is a standard DOM API supported in all modern browsers that focuses the element without triggering scroll-into-view behavior.Affected components
Checkbox.ControlSwitch.ControlRadioGroup.ItemControlNumberField.VaryTriggerCombobox.TriggerCombobox.HiddenSelectReproduction
See the StackBlitz repro from #452 - clicking a checkbox in a scrollable container causes the page to jump.
Fixes #452