diff --git a/packages/devextreme/js/__internal/ui/drop_down_box.ts b/packages/devextreme/js/__internal/ui/drop_down_box.ts index 3c5d9160e307..5a545d3d8d3f 100644 --- a/packages/devextreme/js/__internal/ui/drop_down_box.ts +++ b/packages/devextreme/js/__internal/ui/drop_down_box.ts @@ -19,7 +19,7 @@ import type { OptionChanged } from '@ts/core/widget/types'; import eventsEngine from '@ts/events/core/events_engine'; import DropDownEditor, { type DropDownEditorInternalProperties } from '@ts/ui/drop_down_editor/drop_down_editor'; import type { PositioningEvent } from '@ts/ui/overlay/overlay'; -import { getElementMaxHeightByWindow } from '@ts/ui/overlay/utils'; +import { getElementMaxHeightByWindow, scrollIntoViewWithin } from '@ts/ui/overlay/utils'; import type { PopupProperties as InternalPopupProperties } from '@ts/ui/popup/popup'; const { getActiveElement } = domAdapter; @@ -326,8 +326,9 @@ class DropDownBox< _popupShownHandler(): void { super._popupShownHandler(); - const $firstElement = this._getTabbableElements().first(); - eventsEngine.trigger($firstElement, 'focus'); + const firstElement = this._getTabbableElements().first().get(0); + firstElement?.focus({ preventScroll: true }); + scrollIntoViewWithin(firstElement, this._popup?.$overlayContent()?.get(0)); } _setCollectionWidgetOption(): void {} diff --git a/packages/devextreme/js/__internal/ui/overlay/overlay.ts b/packages/devextreme/js/__internal/ui/overlay/overlay.ts index 6802e814697a..14f1cbb845ad 100644 --- a/packages/devextreme/js/__internal/ui/overlay/overlay.ts +++ b/packages/devextreme/js/__internal/ui/overlay/overlay.ts @@ -57,6 +57,7 @@ import { OVERLAY_POSITION_ALIASES, OverlayPositionController, } from '@ts/ui/overlay/overlay_position_controller'; +import { scrollIntoViewWithin } from '@ts/ui/overlay/utils'; import * as zIndexPool from '@ts/ui/overlay/z_index'; import { themeModeChangedCallback } from '@ts/ui/themes_callback'; @@ -721,10 +722,13 @@ class Overlay< return; } - const { focusStateEnabled } = this.option(); + const { focusStateEnabled, hideOnParentScroll } = this.option(); if (focusStateEnabled) { - eventsEngine.trigger(this._focusTarget(), 'focus'); + const focusTarget = this._focusTarget()?.get(0); + + focusTarget?.focus({ preventScroll: Boolean(hideOnParentScroll) }); + scrollIntoViewWithin(focusTarget, this._$content?.get(0)); } completeShowAnimation.call(this, element, config); diff --git a/packages/devextreme/js/__internal/ui/overlay/utils.ts b/packages/devextreme/js/__internal/ui/overlay/utils.ts index 94d08db98c0c..52f88ad11588 100644 --- a/packages/devextreme/js/__internal/ui/overlay/utils.ts +++ b/packages/devextreme/js/__internal/ui/overlay/utils.ts @@ -2,7 +2,7 @@ import type { dxElementWrapper } from '@js/core/renderer'; import $ from '@js/core/renderer'; import { getInnerHeight, getOuterHeight } from '@js/core/utils/size'; import { isNumeric } from '@js/core/utils/type'; -import { getWindow } from '@ts/core/utils/window'; +import { getWindow, hasWindow } from '@ts/core/utils/window'; const WINDOW_HEIGHT_PERCENT = 0.9; @@ -41,3 +41,74 @@ export const getElementMaxHeightByWindow = ( return actualOffset * WINDOW_HEIGHT_PERCENT; }; + +const isScrollableOverflow = (overflow: string): boolean => overflow !== 'visible' && overflow !== 'clip'; + +const getScrollDelta = ( + start: number, + end: number, + viewStart: number, + viewEnd: number, +): number => { + if (start < viewStart) { + return start - viewStart; + } + + if (end > viewEnd) { + return Math.min(end - viewEnd, start - viewStart); + } + + return 0; +}; + +export const scrollIntoViewWithin = ( + element: HTMLElement | undefined, + container: HTMLElement | undefined, +): void => { + if (!element || !container || !hasWindow()) { + return; + } + + const elementRect = element.getBoundingClientRect(); + let shiftTop = 0; + let shiftLeft = 0; + + for ( + let parent = element.parentElement; + parent && container.contains(parent); + parent = parent.parentElement + ) { + const { overflowX, overflowY } = getWindow().getComputedStyle(parent); + const parentRect = parent.getBoundingClientRect(); + const scaleX = parent.offsetWidth ? parentRect.width / parent.offsetWidth : 1; + const scaleY = parent.offsetHeight ? parentRect.height / parent.offsetHeight : 1; + const viewTop = parentRect.top + parent.clientTop * scaleY; + const viewLeft = parentRect.left + parent.clientLeft * scaleX; + + const top = isScrollableOverflow(overflowY) + ? getScrollDelta( + elementRect.top - shiftTop, + elementRect.bottom - shiftTop, + viewTop, + viewTop + parent.clientHeight * scaleY, + ) + : 0; + const left = isScrollableOverflow(overflowX) + ? getScrollDelta( + elementRect.left - shiftLeft, + elementRect.right - shiftLeft, + viewLeft, + viewLeft + parent.clientWidth * scaleX, + ) + : 0; + + if (top || left) { + parent.scrollTo({ + top: parent.scrollTop + top / scaleY, + left: parent.scrollLeft + left / scaleX, + }); + shiftTop += top; + shiftLeft += left; + } + } +}; diff --git a/packages/devextreme/testing/tests/DevExpress.ui.widgets.editors/dropDownBox.tests.js b/packages/devextreme/testing/tests/DevExpress.ui.widgets.editors/dropDownBox.tests.js index cf21256a1b08..8f50bcceebfb 100644 --- a/packages/devextreme/testing/tests/DevExpress.ui.widgets.editors/dropDownBox.tests.js +++ b/packages/devextreme/testing/tests/DevExpress.ui.widgets.editors/dropDownBox.tests.js @@ -941,7 +941,174 @@ QUnit.module('keyboard navigation', moduleConfig, () => { instance.open(); - assert.ok(inputFocusedHandler.callCount, 1, 'input get focused'); + assert.strictEqual(inputFocusedHandler.callCount, 1, 'input get focused'); + }); + + QUnit.testInActiveWindow('first content element should be focused after popup opening without scrolling the page', function(assert) { + const $pageTail = $('
').height(window.innerHeight * 3).appendTo('body'); + const $input = $('', { type: 'text' }).css('marginTop', window.innerHeight); + const $nextInput = $('', { type: 'text' }); + + try { + const instance = new DropDownBox(this.$element, { + dropDownOptions: { maxHeight: window.innerHeight * 2 }, + contentTemplate(component, content) { + $('
').append($input, $nextInput).appendTo(content); + } + }); + const pageScrollTop = window.scrollY; + + instance.open(); + + const inputBottomInPage = $input.get(0).getBoundingClientRect().bottom + window.scrollY; + assert.ok(inputBottomInPage > pageScrollTop + window.innerHeight, 'input is below the visible part of the page'); + assert.strictEqual(document.activeElement, $input.get(0), 'input is focused'); + assert.strictEqual(window.scrollY, pageScrollTop, 'page is not scrolled'); + } finally { + $pageTail.remove(); + } + }); + + QUnit.testInActiveWindow('first content element should be scrolled into view inside the popup after popup opening', function(assert) { + const $input = $('', { type: 'text' }); + + const instance = new DropDownBox(this.$element, { + dropDownOptions: { height: 200 }, + contentTemplate(component, content) { + $(content).append($('
').height(400), $input); + } + }); + + instance.open(); + + const inputRect = $input.get(0).getBoundingClientRect(); + const overlayContentRect = $(instance.content()).parent(`.${OVERLAY_CONTENT_CLASS}`).get(0).getBoundingClientRect(); + + assert.strictEqual(document.activeElement, $input.get(0), 'input is focused'); + assert.ok(Math.round(inputRect.top) >= Math.round(overlayContentRect.top) && Math.round(inputRect.bottom) <= Math.round(overlayContentRect.bottom), 'input is in the visible part of the popup'); + }); + + QUnit.testInActiveWindow('first content element should be scrolled into view horizontally inside the popup after popup opening', function(assert) { + const $input = $('', { type: 'text' }).css('width', 50); + const $scroller = $('
').css({ overflowX: 'auto', whiteSpace: 'nowrap' }); + + const instance = new DropDownBox(this.$element, { + dropDownOptions: { width: 200 }, + contentTemplate(component, content) { + $scroller.append($('').css({ display: 'inline-block', width: 400 }), $input).appendTo(content); + } + }); + + instance.open(); + + const inputRect = $input.get(0).getBoundingClientRect(); + const scrollerRect = $scroller.get(0).getBoundingClientRect(); + + assert.strictEqual(document.activeElement, $input.get(0), 'input is focused'); + assert.ok(Math.round(inputRect.left) >= Math.round(scrollerRect.left) && Math.round(inputRect.right) <= Math.round(scrollerRect.right), 'input is in the visible part of the scroller'); + }); + + QUnit.testInActiveWindow('first content element should not stay under the border of a scroller inside the popup after popup opening', function(assert) { + const $input = $('', { type: 'text' }).css('display', 'block'); + const $scroller = $('
').css({ height: 100, overflowY: 'auto', borderBottom: '20px solid' }); + + const instance = new DropDownBox(this.$element, { + contentTemplate(component, content) { + $scroller.append($('
').height(300), $input).appendTo(content); + } + }); + + instance.open(); + + const inputRect = $input.get(0).getBoundingClientRect(); + const elementAtInputBottom = document.elementFromPoint(inputRect.left + inputRect.width / 2, inputRect.bottom - 1); + + assert.strictEqual(document.activeElement, $input.get(0), 'input is focused'); + assert.strictEqual(elementAtInputBottom, $input.get(0), 'input bottom edge is visible'); + }); + + QUnit.testInActiveWindow('first content element should be scrolled into view inside a scaled scroller in the popup after popup opening', function(assert) { + const $input = $('', { type: 'text' }).css({ display: 'inline-block', width: 50 }); + const $scroller = $('
').css({ width: 100, height: 100, overflow: 'auto', whiteSpace: 'nowrap' }); + + const instance = new DropDownBox(this.$element, { + contentTemplate(component, content) { + $scroller.append($('
').height(300), $('').css({ display: 'inline-block', width: 400 }), $input); + $('
').css({ transform: 'scale(0.5)', transformOrigin: '0 0' }).append($scroller).appendTo(content); + } + }); + + instance.open(); + + const inputRect = $input.get(0).getBoundingClientRect(); + const elementAtInputCorner = document.elementFromPoint(inputRect.right - 1, inputRect.bottom - 1); + + assert.strictEqual(document.activeElement, $input.get(0), 'input is focused'); + assert.strictEqual(elementAtInputCorner, $input.get(0), 'input bottom right corner is visible'); + }); + + QUnit.testInActiveWindow('first content element should be scrolled into view inside a smoothly scrolling scroller in the popup after popup opening', function(assert) { + const done = assert.async(); + const $input = $('', { type: 'text' }).css({ display: 'inline-block', width: 50 }); + const $scroller = $('
').css({ width: 100, height: 100, overflow: 'auto', whiteSpace: 'nowrap', scrollBehavior: 'smooth' }); + + const instance = new DropDownBox(this.$element, { + contentTemplate(component, content) { + $scroller.append($('
').height(300), $('').css({ display: 'inline-block', width: 400 }), $input).appendTo(content); + } + }); + + $scroller.get(0).addEventListener('scrollend', () => { + const inputRect = $input.get(0).getBoundingClientRect(); + const elementAtInputCorner = document.elementFromPoint(inputRect.right - 1, inputRect.bottom - 1); + + assert.strictEqual(elementAtInputCorner, $input.get(0), 'input bottom right corner is visible'); + done(); + }, { once: true }); + + instance.open(); + + assert.strictEqual(document.activeElement, $input.get(0), 'input is focused'); + }); + + QUnit.testInActiveWindow('first content element should be scrolled into view inside nested smoothly scrolling scrollers in the popup after popup opening', function(assert) { + const done = assert.async(); + const $input = $('', { type: 'text' }).css({ display: 'inline-block', width: 50 }); + const $innerScroller = $('
').css({ display: 'inline-block', width: 100, height: 100, overflow: 'auto', whiteSpace: 'nowrap', scrollBehavior: 'smooth' }); + const $outerScroller = $('
').css({ width: 150, height: 150, overflow: 'auto', whiteSpace: 'nowrap', scrollBehavior: 'smooth' }); + let pendingScrollEndCount = 2; + + const instance = new DropDownBox(this.$element, { + contentTemplate(component, content) { + $innerScroller.append($('
').height(300), $('').css({ display: 'inline-block', width: 400 }), $input); + $outerScroller.append( + $('
').height(300), + $('').css({ display: 'inline-block', width: 300 }), + $innerScroller, + $('
').css({ width: 900, height: 600 }) + ).appendTo(content); + } + }); + + [$innerScroller, $outerScroller].forEach(($scroller) => { + $scroller.get(0).addEventListener('scrollend', () => { + pendingScrollEndCount -= 1; + + if(pendingScrollEndCount) { + return; + } + + const inputRect = $input.get(0).getBoundingClientRect(); + const elementAtInputCorner = document.elementFromPoint(inputRect.right - 1, inputRect.bottom - 1); + + assert.strictEqual(elementAtInputCorner, $input.get(0), 'input bottom right corner is visible'); + done(); + }, { once: true }); + }); + + instance.open(); + + assert.strictEqual(document.activeElement, $input.get(0), 'input is focused'); }); }); diff --git a/packages/devextreme/testing/tests/DevExpress.ui.widgets/overlay.tests.js b/packages/devextreme/testing/tests/DevExpress.ui.widgets/overlay.tests.js index 7f19d406318a..00e6245d2c83 100644 --- a/packages/devextreme/testing/tests/DevExpress.ui.widgets/overlay.tests.js +++ b/packages/devextreme/testing/tests/DevExpress.ui.widgets/overlay.tests.js @@ -3381,6 +3381,42 @@ testModule('keyboard navigation', { assert.strictEqual(this.overlay.option('visible'), true, 'overlay doesn\'t handle keyboard propagated events'); }); + + [ + { hideOnParentScroll: true, isPageScrolled: false }, + { hideOnParentScroll: false, isPageScrolled: true }, + ].forEach(({ hideOnParentScroll, isPageScrolled }) => { + test(`overlay content should get focus on show and ${isPageScrolled ? 'scroll' : 'not scroll'} the page when hideOnParentScroll is ${hideOnParentScroll}`, function(assert) { + const $pageTail = $('
').height(window.innerHeight * 3).appendTo('body'); + const $target = $('
') + .css({ position: 'absolute', top: window.innerHeight - 10, left: 0, width: 10, height: 10 }) + .appendTo('body'); + const overlay = new Overlay($('
').appendTo('body'), { + focusStateEnabled: true, + hideOnParentScroll, + container: 'body', + width: 100, + height: 100, + position: { my: 'top left', at: 'bottom left', of: $target, collision: 'none' } + }); + const $overlayContent = $(overlay.$content()); + const pageScrollTop = window.scrollY; + + try { + overlay.show(); + + const contentBottomInPage = $overlayContent.get(0).getBoundingClientRect().bottom + window.scrollY; + assert.ok(contentBottomInPage > pageScrollTop + window.innerHeight, 'overlay content is below the visible part of the page'); + assert.strictEqual(document.activeElement, $overlayContent.get(0), 'overlay content is focused'); + assert.strictEqual(window.scrollY > pageScrollTop, isPageScrolled, 'page is scrolled'); + } finally { + overlay.dispose(); + $target.remove(); + $pageTail.remove(); + window.scrollTo(0, pageScrollTop); + } + }); + }); }); testModule('focus policy', { diff --git a/packages/devextreme/testing/tests/DevExpress.ui.widgets/popover.tests.js b/packages/devextreme/testing/tests/DevExpress.ui.widgets/popover.tests.js index 229fee29c9ed..e0bd37dbf8aa 100644 --- a/packages/devextreme/testing/tests/DevExpress.ui.widgets/popover.tests.js +++ b/packages/devextreme/testing/tests/DevExpress.ui.widgets/popover.tests.js @@ -3255,6 +3255,28 @@ QUnit.module('accessibility', { assert.strictEqual(document.activeElement, $input1.get(0), 'first tabbable element is focused'); }); + QUnit.test('Popover in dialog mode should scroll the first tabbable element into view inside content on show', function(assert) { + const instance = new Popover(this.$element, { + target: this.$target, + height: 200, + contentTemplate: function() { + return $('
').append($('
').height(400), $('', { id: 'input1' })); + }, + toolbarItems: [{ text: 'OK' }], + visible: false, + }); + + instance.show(); + this.clock.tick(500); + + const input = $('#input1').get(0); + const inputRect = input.getBoundingClientRect(); + const contentRect = instance.$content().get(0).getBoundingClientRect(); + + assert.strictEqual(document.activeElement, input, 'first tabbable element is focused'); + assert.ok(Math.round(inputRect.top) >= Math.round(contentRect.top) && Math.round(inputRect.bottom) <= Math.round(contentRect.bottom), 'first tabbable element is in the visible part of the content'); + }); + QUnit.test('Popover in dialog mode should restore focus to target on dispose when visible', function(assert) { this.$target.attr('tabindex', 0).focus();