Skip to content

Commit b0dec8e

Browse files
committed
Use Element.scrollIntoView
Related to [@github/auto-complete-element#91][] The current `scrollTo` helper method implementation can be unreliable (or have no effect) at times. This commit replaces it with a call to [Element.scrollIntoView][]. To control that behavior, this commit also introduces a `scrollIntoViewOptions:` key to the package's constructor call. [@github/auto-complete-element#91]: github/auto-complete-element#91 [Element.scrollIntoView]: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView [ScrollIntoViewOptions]: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView#sect1
1 parent a3d2178 commit b0dec8e

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ These settings are available:
8282
- `tabInsertsSuggestions: boolean = true` - Control whether the highlighted suggestion is inserted when <kbd>Tab</kbd> is pressed (<kbd>Enter</kbd> will always insert a suggestion regardless of this setting). When `true`, tab-navigation will be hijacked when open (which can have negative impacts on accessibility) but the combobox will more closely imitate a native IDE experience.
8383
- `defaultFirstOption: boolean = false` - If no options are selected and the user presses <kbd>Enter</kbd>, should the first item be inserted? If enabled, the default option can be selected and styled with `[data-combobox-option-default]` . This should be styled differently from the `aria-selected` option.
8484
> **Warning** Screen readers will not announce that the first item is the default. This should be announced explicitly with the use of `aria-live` status text.
85+
- `scrollIntoViewOptions?: boolean | ScrollIntoViewOptions = undefined` - When
86+
controlling the element marked `[aria-selected="true"]` with keyboard navigation, the selected element will be scrolled into the viewport by a call to [Element.scrollIntoView][]. Configure this value to control the scrolling behavior (either with a `boolean` or a [ScrollIntoViewOptions][] object.
87+
88+
[Element.scrollIntoView]: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
89+
[ScrollIntoViewOptions]: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView#sect1
90+
8591

8692
## Development
8793

src/index.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export type ComboboxSettings = {
22
tabInsertsSuggestions?: boolean
33
defaultFirstOption?: boolean
4+
scrollIntoViewOptions?: boolean | ScrollIntoViewOptions
45
}
56

67
export default class Combobox {
@@ -13,16 +14,18 @@ export default class Combobox {
1314
ctrlBindings: boolean
1415
tabInsertsSuggestions: boolean
1516
defaultFirstOption: boolean
17+
scrollIntoViewOptions?: boolean | ScrollIntoViewOptions
1618

1719
constructor(
1820
input: HTMLTextAreaElement | HTMLInputElement,
1921
list: HTMLElement,
20-
{tabInsertsSuggestions, defaultFirstOption}: ComboboxSettings = {}
22+
{tabInsertsSuggestions, defaultFirstOption, scrollIntoViewOptions}: ComboboxSettings = {}
2123
) {
2224
this.input = input
2325
this.list = list
2426
this.tabInsertsSuggestions = tabInsertsSuggestions ?? true
2527
this.defaultFirstOption = defaultFirstOption ?? false
28+
this.scrollIntoViewOptions = scrollIntoViewOptions
2629

2730
this.isComposing = false
2831

@@ -107,7 +110,7 @@ export default class Combobox {
107110
if (target === el) {
108111
this.input.setAttribute('aria-activedescendant', target.id)
109112
target.setAttribute('aria-selected', 'true')
110-
scrollTo(this.list, target)
113+
target.scrollIntoView(this.scrollIntoViewOptions)
111114
} else {
112115
el.removeAttribute('aria-selected')
113116
}
@@ -204,17 +207,3 @@ function trackComposition(event: Event, combobox: Combobox): void {
204207

205208
combobox.clearSelection()
206209
}
207-
208-
function scrollTo(container: HTMLElement, target: HTMLElement) {
209-
if (!inViewport(container, target)) {
210-
container.scrollTop = target.offsetTop
211-
}
212-
}
213-
214-
function inViewport(container: HTMLElement, element: HTMLElement): boolean {
215-
const scrollTop = container.scrollTop
216-
const containerBottom = scrollTop + container.clientHeight
217-
const top = element.offsetTop
218-
const bottom = top + element.clientHeight
219-
return top >= scrollTop && bottom <= containerBottom
220-
}

0 commit comments

Comments
 (0)