Skip to content

Commit

Permalink
Implement scrolling to active tab, fix styles, add release guard
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Mar 28, 2022
1 parent 0b65894 commit 4d8a850
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
33 changes: 32 additions & 1 deletion packages/widgets/src/tabbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ export class TabBar<T> extends Widget {
if (value) {
this.node.classList.add('lm-mod-scrollable');
} else {
this.node.classList.add('lm-mod-scrollable');
this.node.classList.remove('lm-mod-scrollable');
}
this.maybeSwitchScrollButtons();
}
Expand Down Expand Up @@ -708,14 +708,40 @@ export class TabBar<T> extends Widget {
content[i] = renderer.renderTab({ title, current, zIndex });
}
VirtualDOM.render(content, this.contentNode);

this.maybeSwitchScrollButtons();

// Scroll the current tab into view.
this.scrollCurrentIntoView();
}

protected onResize(msg: Widget.ResizeMessage): void {
super.onResize(msg);
this.maybeSwitchScrollButtons();
}

/**
* Scroll the current tab into view.
*/
protected scrollCurrentIntoView() {
if (this.scrollingEnabled) {
const currentNode = this.contentNode.children.item(this.currentIndex);
if (currentNode) {
currentNode.scrollIntoView();
if (this.orientation == 'horizontal') {
currentNode.scrollTop = 0;
} else {
currentNode.scrollLeft = 0;
}
} else {
console.error('Current tab node not found');
}
}
}

/**
* Show/hide scroll buttons if needed.
*/
protected maybeSwitchScrollButtons() {
const scrollBefore = this.scrollBeforeButtonNode;
const scrollAfter = this.scrollAfterButtonNode;
Expand Down Expand Up @@ -1174,6 +1200,11 @@ export class TabBar<T> extends Widget {
return;
}

// Do nothing if neither press nor release was on a tab.
if (index === -1 && data.index === -1) {
return;
}

// Ignore the release if the title is not closable.
let title = this._titles[index];
if (!title.closable) {
Expand Down
2 changes: 1 addition & 1 deletion packages/widgets/style/tabbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
display: none !important;
}

.lm-TabBar-addButton.lm-mod-hidden {
.lm-TabBar-button.lm-mod-hidden {
display: none !important;
}

Expand Down

0 comments on commit 4d8a850

Please sign in to comment.