Skip to content

Commit 6937d4d

Browse files
committed
fix(cellNav): add an empty element to focus on instead of focusing on the viewport
Focusing on the viewport caused the entire grid to scroll into view when navigating to a cell
1 parent 77467e4 commit 6937d4d

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/features/cellnav/js/cellnav.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -766,15 +766,21 @@
766766
};
767767
}]);
768768

769-
module.directive('uiGridViewport', ['$timeout', '$document', 'gridUtil', 'uiGridConstants', 'uiGridCellNavService', 'uiGridCellNavConstants','$log',
770-
function ($timeout, $document, gridUtil, uiGridConstants, uiGridCellNavService, uiGridCellNavConstants, $log) {
769+
module.directive('uiGridViewport', ['$timeout', '$document', 'gridUtil', 'uiGridConstants', 'uiGridCellNavService', 'uiGridCellNavConstants','$log','$compile',
770+
function ($timeout, $document, gridUtil, uiGridConstants, uiGridCellNavService, uiGridCellNavConstants, $log, $compile) {
771+
var focuser;
771772
return {
772773
replace: true,
773774
priority: -99999, //this needs to run very last
774775
require: ['^uiGrid', '^uiGridRenderContainer', '?^uiGridCellnav'],
775776
scope: false,
776777
compile: function () {
777778
return {
779+
pre: function ($scope, $elm, $attrs, uiGridCtrl) {
780+
//add an element with no dimensions that can be used to set focus and capture keystrokes
781+
focuser = $compile('<div class="ui-grid-focuser" tabindex="-1"></div>')($scope);
782+
$elm.append(focuser);
783+
},
778784
post: function ($scope, $elm, $attrs, controllers) {
779785
var uiGridCtrl = controllers[0],
780786
renderContainerCtrl = controllers[1];
@@ -787,11 +793,10 @@
787793
var grid = uiGridCtrl.grid;
788794

789795

790-
// Let the render container be focus-able
791-
$elm.attr("tabindex", -1);
796+
focuser[0].focus();
792797

793798
// Bind to keydown events in the render container
794-
$elm.on('keydown', function (evt) {
799+
focuser.on('keydown', function (evt) {
795800
evt.uiGridTargetRenderContainerId = containerId;
796801
var rowCol = uiGridCtrl.grid.api.cellNav.getFocusedCell();
797802
var result = uiGridCtrl.cellNav.handleKeyDown(evt);
@@ -839,8 +844,8 @@
839844
});
840845

841846
grid.api.cellNav.on.navigate($scope, function () {
842-
//focus the viewport because this can sometimes be lost
843-
$elm[0].focus();
847+
//focus again because it can be lost
848+
focuser[0].focus();
844849
});
845850

846851
}

0 commit comments

Comments
 (0)