-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
134 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/Umbraco.Web.UI.Client/src/common/directives/components/forms/umbfocuslock.directive.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
(function() { | ||
'use strict'; | ||
|
||
function FocusLock($timeout) { | ||
|
||
function getAutoFocusElement (elements) { | ||
var elmentWithAutoFocus = null; | ||
|
||
elements.forEach((element) => { | ||
if(element.getAttribute('umb-auto-focus') === 'true') { | ||
elmentWithAutoFocus = element; | ||
} | ||
}); | ||
|
||
return elmentWithAutoFocus; | ||
} | ||
|
||
function link(scope, element) { | ||
|
||
function onInit() { | ||
// List of elements that can be focusable within the focus lock | ||
var focusableElementsSelector = 'a[href]:not([disabled]), button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled])'; | ||
var bodyElement = document.querySelector('body'); | ||
|
||
$timeout(function() { | ||
var target = element[0]; | ||
|
||
var focusableElements = target.querySelectorAll(focusableElementsSelector); | ||
var defaultFocusedElement = getAutoFocusElement(focusableElements); | ||
var firstFocusableElement = focusableElements[0]; | ||
var lastFocusableElement = focusableElements[focusableElements.length -1]; | ||
|
||
// We need to add the tabbing-active class in order to highlight the focused button since the default style is | ||
// outline: none; set in the stylesheet specifically | ||
bodyElement.classList.add('tabbing-active'); | ||
|
||
// If there is no default focused element put focus on the first focusable element in the nodelist | ||
if(defaultFocusedElement === null ){ | ||
firstFocusableElement.focus(); | ||
} | ||
|
||
target.addEventListener('keydown', function(event){ | ||
var isTabPressed = (event.key === 'Tab' || event.keyCode === 9); | ||
|
||
if (!isTabPressed){ | ||
return; | ||
} | ||
|
||
// If shift + tab key | ||
if(event.shiftKey){ | ||
// Set focus on the last focusable element if shift+tab are pressed meaning we go backwards | ||
if(document.activeElement === firstFocusableElement){ | ||
lastFocusableElement.focus(); | ||
event.preventDefault(); | ||
} | ||
} | ||
// Else only the tab key is pressed | ||
else{ | ||
// Using only the tab key we set focus on the first focusable element mening we go forward | ||
if (document.activeElement === lastFocusableElement) { | ||
firstFocusableElement.focus(); | ||
event.preventDefault(); | ||
} | ||
} | ||
}); | ||
}, 250); | ||
} | ||
|
||
onInit(); | ||
} | ||
|
||
var directive = { | ||
restrict: 'A', | ||
link: link | ||
}; | ||
|
||
return directive; | ||
} | ||
|
||
angular.module('umbraco.directives').directive('umbFocusLock', FocusLock); | ||
|
||
})(); |
26 changes: 26 additions & 0 deletions
26
src/Umbraco.Web.UI.Client/src/common/services/focuslock.service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
(function () { | ||
"use strict"; | ||
|
||
function focusLockService() { | ||
var elementToInert = document.querySelector('#mainwrapper'); | ||
|
||
function addInertAttribute() { | ||
elementToInert.setAttribute('inert', true); | ||
} | ||
|
||
function removeInertAttribute() { | ||
elementToInert.removeAttribute('inert'); | ||
} | ||
|
||
var service = { | ||
addInertAttribute: addInertAttribute, | ||
removeInertAttribute: removeInertAttribute | ||
} | ||
|
||
return service; | ||
|
||
} | ||
|
||
angular.module("umbraco.services").factory("focusLockService", focusLockService); | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
src/Umbraco.Web.UI.Client/src/views/common/overlays/itempicker/itempicker.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 8 additions & 1 deletion
9
src/Umbraco.Web.UI.Client/src/views/components/overlays/umb-overlay.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters