Skip to content

Commit b4fe52d

Browse files
feat: allow for toolbox with multiple tab stops (#358)
- Switch to focusin/out for toolbox (maybe in future elsewhere) - Drop the tab index fiddling as it's not practical to get it right in the face of more than one tab stop in the toolbox, especially if that toolbox has a DOM structure not known to the plugin. Given that we're reinstating the ability to shift-tab flyout->toolbox it makes sense to allow tab to the flyout. Shift-tab to the flyout remains not possible in the auto-hide case but that's not unreasonable. This version can be made to work with MakeCode without further modifications.
1 parent 706b70f commit b4fe52d

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

src/index.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ export class KeyboardNavigation {
3434
private widgetDropDownDivFocusOutListener: (e: Event) => void;
3535

3636
/** Event handler run when the toolbox gains focus. */
37-
private toolboxFocusListener: () => void;
37+
private toolboxFocusInListener: (e: Event) => void;
3838

3939
/** Event handler run when the toolbox loses focus. */
40-
private toolboxBlurListener: (e: Event) => void;
40+
private toolboxFocusOutListener: (e: Event) => void;
4141

4242
/** Event handler run when the flyout gains focus. */
4343
private flyoutFocusListener: () => void;
@@ -112,10 +112,6 @@ export class KeyboardNavigation {
112112
flyoutElement,
113113
workspace.getParentSvg(),
114114
);
115-
// Allow tab to the flyout only when there's no toolbox.
116-
if (workspace.getToolbox() && flyoutElement) {
117-
flyoutElement.tabIndex = -1;
118-
}
119115

120116
// Temporary workaround for #136.
121117
this.oldMarkFocused = workspace.markFocused;
@@ -179,10 +175,26 @@ export class KeyboardNavigation {
179175
);
180176

181177
const toolboxElement = getToolboxElement(workspace);
182-
this.toolboxFocusListener = () => {
178+
this.toolboxFocusInListener = (e: Event) => {
179+
if (
180+
(e.currentTarget as Element).contains(
181+
(e as FocusEvent).relatedTarget as Node,
182+
)
183+
) {
184+
return;
185+
}
186+
183187
this.navigationController.handleFocusToolbox(workspace);
184188
};
185-
this.toolboxBlurListener = (e: Event) => {
189+
this.toolboxFocusOutListener = (e: Event) => {
190+
if (
191+
(e.currentTarget as Element).contains(
192+
(e as FocusEvent).relatedTarget as Node,
193+
)
194+
) {
195+
return;
196+
}
197+
186198
this.navigationController.handleBlurToolbox(
187199
workspace,
188200
this.shouldCloseFlyoutOnBlur(
@@ -191,8 +203,8 @@ export class KeyboardNavigation {
191203
),
192204
);
193205
};
194-
toolboxElement?.addEventListener('focus', this.toolboxFocusListener);
195-
toolboxElement?.addEventListener('blur', this.toolboxBlurListener);
206+
toolboxElement?.addEventListener('focusin', this.toolboxFocusInListener);
207+
toolboxElement?.addEventListener('focusout', this.toolboxFocusOutListener);
196208

197209
this.flyoutFocusListener = () => {
198210
this.navigationController.handleFocusFlyout(workspace);
@@ -238,8 +250,11 @@ export class KeyboardNavigation {
238250
);
239251

240252
const toolboxElement = getToolboxElement(this.workspace);
241-
toolboxElement?.removeEventListener('focus', this.toolboxFocusListener);
242-
toolboxElement?.removeEventListener('blur', this.toolboxBlurListener);
253+
toolboxElement?.removeEventListener('focusin', this.toolboxFocusInListener);
254+
toolboxElement?.removeEventListener(
255+
'focusout',
256+
this.toolboxFocusOutListener,
257+
);
243258

244259
const flyoutElement = getFlyoutElement(this.workspace);
245260
flyoutElement?.removeEventListener('focus', this.flyoutFocusListener);

src/navigation.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,6 @@ export class Navigation {
540540
if (!Blockly.Gesture.inProgress()) {
541541
this.defaultFlyoutCursorIfNeeded(workspace);
542542
}
543-
544-
// Prevent shift-tab to the toolbox while the flyout has focus.
545-
const toolboxElement = getToolboxElement(workspace);
546-
if (toolboxElement) {
547-
toolboxElement.tabIndex = -1;
548-
}
549543
}
550544

551545
/**
@@ -561,12 +555,6 @@ export class Navigation {
561555
workspace.hideChaff();
562556
}
563557
this.getFlyoutCursor(workspace)?.hide();
564-
565-
// Reinstate tab to toolbox.
566-
const toolboxElement = getToolboxElement(workspace);
567-
if (toolboxElement) {
568-
toolboxElement.tabIndex = 0;
569-
}
570558
}
571559

572560
/**

0 commit comments

Comments
 (0)