* TODO improve the regex testing for this.
* @example reveal-for-large
*/
- revealClass: 'reveal-for-'
+ revealClass: 'reveal-for-',
+ /**
+ * Triggers optional focus trapping when opening an offcanvas. Sets tabindex of [data-off-canvas-content] to -1 for accessibility purposes.
+ * @option
+ * @example true
+ */
+ trapFocus: false
};
/**
* @private
*/
OffCanvas.prototype._events = function() {
- this.$element.on({
+ this.$element.off('.zf.trigger .zf.offcanvas').on({
'open.zf.trigger': this.open.bind(this),
'close.zf.trigger': this.close.bind(this),
'toggle.zf.trigger': this.toggle.bind(this),
// this.$element[0].style.transform = 'translate(0,' + scrollPos + 'px)';
// }
// if(this.options.isSticky){ this._stick(); }
+ this.$element.off('open.zf.trigger toggle.zf.trigger');
if($closer.length){ $closer.hide(); }
}else{
// if(this.options.isSticky || !this.options.forceTop){
// this.$element[0].style.transform = '';
// $(window).off('scroll.zf.offcanvas');
// }
+ this.$element.on({
+ 'open.zf.trigger': this.open.bind(this),
+ 'toggle.zf.trigger': this.toggle.bind(this)
+ });
if($closer.length){
$closer.show();
}
this.$element.attr('aria-hidden', 'false')
.trigger('opened.zf.offcanvas');
-
+
if(trigger){
this.$lastTrigger = trigger.attr('aria-expanded', 'true');
}
_this.$element.find('a, button').eq(0).focus();
});
}
+ if(this.options.trapFocus){
+ $('[data-off-canvas-content]').attr('tabindex', '-1');
+ this._trapFocus();
+ }
+};
+/**
+ * Traps focus within the offcanvas on open.
+ * @private
+ */
+OffCanvas.prototype._trapFocus = function(){
+ var focusable = Foundation.Keyboard.findFocusable(this.$element),
+ first = focusable.eq(0),
+ last = focusable.eq(-1);
+
+ focusable.off('.zf.offcanvas').on('keydown.zf.offcanvas', function(e){
+ if(e.which === 9 || e.keycode === 9){
+ if(e.target === last[0] && !e.shiftKey){
+ e.preventDefault();
+ first.focus();
+ }
+ if(e.target === first[0] && e.shiftKey){
+ e.preventDefault();
+ last.focus();
+ }
+ }
+ });
};
/**
* Allows the offcanvas to appear sticky utilizing translate properties.
// }
this.$lastTrigger.attr('aria-expanded', 'false');
+ if(this.options.trapFocus){
+ $('[data-off-canvas-content]').removeAttr('tabindex');
+ }
};