Last active
April 25, 2016 03:22
-
-
Save huoxito/7f0a05e0df322cc190141f4a282f2903 to your computer and use it in GitHub Desktop.
trap focus
This file contains hidden or 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
# based of https://accessibility.oit.ncsu.edu/blog/2015/02/13/the-incredible-accessible-modal-window-version-3/ | |
class FocusTrap | |
@init = (evt, $domNode) -> | |
focusableElements = 'a[href], button, input:not([disabled]), select:not([disabled]), textarea, *[contenteditable]' | |
if evt.which == 9 | |
o = $domNode.find('*') | |
focusableItems = o.filter(focusableElements).filter(':visible') | |
return unless focusableItems.length | |
focusedItem = jQuery(':focus') | |
numberOfFocusableItems = focusableItems.length | |
focusedItemIndex = focusableItems.index(focusedItem) | |
if evt.shiftKey | |
if focusedItemIndex == 0 | |
focusableItems.get(numberOfFocusableItems - 1).focus() | |
evt.preventDefault() | |
else if (focusedItemIndex == numberOfFocusableItems - 1) || focusedItemIndex == -1 | |
focusableItems.get(0).focus() | |
evt.preventDefault() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment