Skip to content

Commit 4817ba4

Browse files
Brock Whittakertimabbott
Brock Whittaker
authored andcommitted
tab-switcher: Switch tabs with left/right arrows.
This now allows all tab switcher components to be used with left and right arrows, given that a tab is already in focus, which is the default behavior unless overridden (like on the streams overlay).
1 parent 5b86b65 commit 4817ba4

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

static/js/components.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ exports.toggle = (function () {
2424
opts.values.forEach(function (value, i) {
2525
// create a tab with a tab-id so they don't have to be referenced
2626
// by text value which can be inconsistent.
27-
var tab = $("<div class='ind-tab' data-tab-key='" + value.key + "' data-tab-id='" + i + "'>" + value.label + "</div>");
27+
var tab = $("<div class='ind-tab' data-tab-key='" + value.key + "' data-tab-id='" + i + "' tabindex='0'>" + value.label + "</div>");
2828

2929
// add proper classes for styling in CSS.
3030
if (i === 0) {
@@ -58,6 +58,22 @@ exports.toggle = (function () {
5858
opts.callback(meta.last_value, opts.values[id].key, {});
5959
}
6060
}
61+
$(this).focus();
62+
});
63+
64+
meta.$ind_tab.keydown(function (e) {
65+
var key = e.which || e.keyCode;
66+
var idx = $(this).data("tab-id");
67+
68+
if (key === 37) {
69+
if (idx > 0) {
70+
$(this).prev().click();
71+
}
72+
} else if (key === 39) {
73+
if (idx < opts.values.length - 1) {
74+
$(this).next().click();
75+
}
76+
}
6177
});
6278
if (typeof opts.selected === "number") {
6379
$(component).find(".ind-tab[data-tab-id='" + opts.selected + "']").click();
@@ -90,15 +106,18 @@ exports.toggle = (function () {
90106
});
91107

92108
var idx = opts.values.indexOf(value);
109+
var $this_tab = meta.$ind_tab.filter("[data-tab-id='" + idx + "']");
93110

94111
if (idx !== -1 && idx !== meta.last_value) {
95112
meta.$ind_tab.removeClass("selected");
96-
meta.$ind_tab.filter("[data-tab-id='" + idx + "']").addClass("selected");
113+
$this_tab.addClass("selected");
97114

98115
opts.callback(value.label, value.key, payload || {});
99116

100117
meta.last_value = idx;
101118
}
119+
120+
$this_tab.focus();
102121
},
103122
};
104123

static/styles/app_components.css

+4
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@
199199
align-items: center;
200200
}
201201

202+
.new-style .tab-switcher .ind-tab:focus {
203+
outline: none;
204+
}
205+
202206
.informational-overlays .tab-switcher {
203207
display: flex;
204208
}

0 commit comments

Comments
 (0)