diff --git a/.changeset/neat-cups-fly.md b/.changeset/neat-cups-fly.md new file mode 100644 index 0000000000..1c32f6f272 --- /dev/null +++ b/.changeset/neat-cups-fly.md @@ -0,0 +1,7 @@ +--- +"@siemens/ix": patch +--- + +Fixed an issue in **ix-time-picker** where focusing numbers would scroll the page outside the component container. + +Fixes #2138 diff --git a/packages/core/src/components/time-picker/time-picker.tsx b/packages/core/src/components/time-picker/time-picker.tsx index 4086156010..622b6ff583 100644 --- a/packages/core/src/components/time-picker/time-picker.tsx +++ b/packages/core/src/components/time-picker/time-picker.tsx @@ -340,15 +340,11 @@ export class TimePicker { elementContainer.focus({ preventScroll: true }); if (!this.isElementVisible(elementContainer, elementList)) { - elementContainer.scrollIntoView({ - block: this.focusScrollAlignment, - }); - - if (this.focusScrollAlignment === 'end') { - elementList.scrollTop += 4; - } else { - elementList.scrollTop -= 4; - } + this.scrollElementIntoView( + elementContainer, + elementList, + this.focusScrollAlignment + ); } } } @@ -462,6 +458,24 @@ export class TimePicker { ); } + private scrollElementIntoView( + element: HTMLElement, + container: HTMLElement, + alignment: 'start' | 'end' + ) { + const SCROLL_BUFFER = 1; + const containerRect = container.getBoundingClientRect(); + const elementRect = element.getBoundingClientRect(); + + if (alignment === 'end') { + container.scrollTop += + elementRect.bottom - containerRect.bottom + SCROLL_BUFFER; + } else { + container.scrollTop += + elementRect.top - containerRect.top - SCROLL_BUFFER; + } + } + private updateFocusedValue(value: number) { const numberArray = this.getNumberArrayForUnit(this.focusedUnit); const maxValue = numberArray[numberArray.length - 1];