Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/devextreme/js/__internal/ui/drop_down_box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<HTMLElement>(0);
firstElement?.focus({ preventScroll: true });
scrollIntoViewWithin(firstElement, this._popup?.$overlayContent()?.get<HTMLElement>(0));
}

_setCollectionWidgetOption(): void {}
Expand Down
8 changes: 6 additions & 2 deletions packages/devextreme/js/__internal/ui/overlay/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<HTMLElement>(0);

focusTarget?.focus({ preventScroll: Boolean(hideOnParentScroll) });
scrollIntoViewWithin(focusTarget, this._$content?.get<HTMLElement>(0));
}

completeShowAnimation.call(this, element, config);
Expand Down
73 changes: 72 additions & 1 deletion packages/devextreme/js/__internal/ui/overlay/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Comment on lines +110 to +111
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $('<div>').height(window.innerHeight * 3).appendTo('body');
const $input = $('<input>', { type: 'text' }).css('marginTop', window.innerHeight);
const $nextInput = $('<input>', { type: 'text' });

try {
const instance = new DropDownBox(this.$element, {
dropDownOptions: { maxHeight: window.innerHeight * 2 },
contentTemplate(component, content) {
$('<div>').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 = $('<input>', { type: 'text' });

const instance = new DropDownBox(this.$element, {
dropDownOptions: { height: 200 },
contentTemplate(component, content) {
$(content).append($('<div>').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 = $('<input>', { type: 'text' }).css('width', 50);
const $scroller = $('<div>').css({ overflowX: 'auto', whiteSpace: 'nowrap' });

const instance = new DropDownBox(this.$element, {
dropDownOptions: { width: 200 },
contentTemplate(component, content) {
$scroller.append($('<span>').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 = $('<input>', { type: 'text' }).css('display', 'block');
const $scroller = $('<div>').css({ height: 100, overflowY: 'auto', borderBottom: '20px solid' });

const instance = new DropDownBox(this.$element, {
contentTemplate(component, content) {
$scroller.append($('<div>').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 = $('<input>', { type: 'text' }).css({ display: 'inline-block', width: 50 });
const $scroller = $('<div>').css({ width: 100, height: 100, overflow: 'auto', whiteSpace: 'nowrap' });

const instance = new DropDownBox(this.$element, {
contentTemplate(component, content) {
$scroller.append($('<div>').height(300), $('<span>').css({ display: 'inline-block', width: 400 }), $input);
$('<div>').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 = $('<input>', { type: 'text' }).css({ display: 'inline-block', width: 50 });
const $scroller = $('<div>').css({ width: 100, height: 100, overflow: 'auto', whiteSpace: 'nowrap', scrollBehavior: 'smooth' });

const instance = new DropDownBox(this.$element, {
contentTemplate(component, content) {
$scroller.append($('<div>').height(300), $('<span>').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 = $('<input>', { type: 'text' }).css({ display: 'inline-block', width: 50 });
const $innerScroller = $('<div>').css({ display: 'inline-block', width: 100, height: 100, overflow: 'auto', whiteSpace: 'nowrap', scrollBehavior: 'smooth' });
const $outerScroller = $('<div>').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($('<div>').height(300), $('<span>').css({ display: 'inline-block', width: 400 }), $input);
$outerScroller.append(
$('<div>').height(300),
$('<span>').css({ display: 'inline-block', width: 300 }),
$innerScroller,
$('<div>').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');
});

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $('<div>').height(window.innerHeight * 3).appendTo('body');
const $target = $('<div>')
.css({ position: 'absolute', top: window.innerHeight - 10, left: 0, width: 10, height: 10 })
.appendTo('body');
const overlay = new Overlay($('<div>').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', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 $('<div>').append($('<div>').height(400), $('<input>', { 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();

Expand Down
Loading