Skip to content

Commit

Permalink
Handle focus more parsimoniously
Browse files Browse the repository at this point in the history
Signed-off-by: Colin Grant <colin.grant@ericsson.com>
  • Loading branch information
colin-grant-work committed Aug 23, 2021
1 parent be67333 commit dff955c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,15 @@ export class BreadcrumbPopupContainer implements Disposable {
result.style.left = `${position.x}px`;
result.style.top = `${position.y}px`;
result.tabIndex = 0;
result.onblur = event => this.onBlur(event, this.breadcrumbId);
result.addEventListener('focusout', this.onFocusOut);
this.parent.appendChild(result);
return result;
}

protected onBlur = (event: FocusEvent, breadcrumbId: string) => {
if (event.relatedTarget && event.relatedTarget instanceof HTMLElement) {
// event.relatedTarget is the element that has the focus after this popup looses the focus.
// If a breadcrumb was clicked the following holds the breadcrumb ID of the clicked breadcrumb.
const clickedBreadcrumbId = event.relatedTarget.getAttribute('data-breadcrumb-id');
if (clickedBreadcrumbId && clickedBreadcrumbId === breadcrumbId) {
// This is a click on the breadcrumb that has openend this popup.
// We do not close this popup here but let the click event of the breadcrumb handle this instead
// because it needs to know that this popup is open to decide if it just closes this popup or
// also opens a new popup.
return;
}
if (this._container.contains(event.relatedTarget)) {
// A child element gets focus. Set the focus to the container again.
// Otherwise the popup would not be closed when elements outside the popup get the focus.
// A popup content should not rely on getting a focus.
this._container.focus();
return;
}
protected onFocusOut = (event: FocusEvent) => {
if (!(event.relatedTarget instanceof Element) || !this._container.contains(event.relatedTarget)) {
this.dispose();
}
this.dispose();
};

protected escFunction = (event: KeyboardEvent) => {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/browser/breadcrumbs/breadcrumb-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ export interface BreadcrumbRenderer {
/**
* Renders the given breadcrumb. If `onClick` is given, it is called on breadcrumb click.
*/
render(breadcrumb: Breadcrumb, onClick?: (breadcrumb: Breadcrumb, event: React.MouseEvent) => void): React.ReactNode;
render(breadcrumb: Breadcrumb, onMouseDown?: (breadcrumb: Breadcrumb, event: React.MouseEvent) => void): React.ReactNode;
}

@injectable()
export class DefaultBreadcrumbRenderer implements BreadcrumbRenderer {
render(breadcrumb: Breadcrumb, onClick?: (breadcrumb: Breadcrumb, event: React.MouseEvent) => void): React.ReactNode {
render(breadcrumb: Breadcrumb, onMouseDown?: (breadcrumb: Breadcrumb, event: React.MouseEvent) => void): React.ReactNode {
return <li key={breadcrumb.id} title={breadcrumb.longLabel}
className={Breadcrumbs.Styles.BREADCRUMB_ITEM + (!onClick ? '' : ' ' + Breadcrumbs.Styles.BREADCRUMB_ITEM_HAS_POPUP)}
onClick={event => onClick && onClick(breadcrumb, event)}
className={Breadcrumbs.Styles.BREADCRUMB_ITEM + (!onMouseDown ? '' : ' ' + Breadcrumbs.Styles.BREADCRUMB_ITEM_HAS_POPUP)}
onMouseDown={event => onMouseDown && onMouseDown(breadcrumb, event)}
tabIndex={0}
data-breadcrumb-id={breadcrumb.id}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class FilepathBreadcrumbsContribution implements BreadcrumbsContribution
if (targetNode && SelectableTreeNode.is(targetNode)) {
model.selectNode(targetNode);
}
this.breadcrumbsFileTreeWidget.activate();
}
});
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export class OutlineBreadcrumbsContribution implements BreadcrumbsContribution {
this.outlineView.model.selectNode(node);
this.outlineView.model.collapseAll();
Widget.attach(this.outlineView, parent);
this.outlineView.activate();
toDisposeOnHide.pushAll([
this.outlineView.model.onExpansionChanged(expandedNode => SelectableTreeNode.is(expandedNode) && this.outlineView.model.selectNode(expandedNode)),
Disposable.create(() => {
Expand Down

0 comments on commit dff955c

Please sign in to comment.