Skip to content

feat: move selected tab to the main part of the menu-bar #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.flowingcode.vaadin.addons</groupId>
<artifactId>enhanced-tabs-addon</artifactId>
<version>1.1.1-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>
<name>Enhanced Tabs Add-on</name>
<description>Enhanced Tabs Add-on for Vaadin Flow</description>
<url>https://www.flowingcode.com/en/open-source/</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ public void add(Tab... tabs) {
ev ->
tab.getElement()
.executeJs(
"this.addEventListener('click', ()=>{let overlay = this.closest('vaadin-menu-bar-overlay, vaadin-context-menu-overlay'); overlay && overlay.close();})"));
"this.addEventListener('click', ()=>{let overlay = this.closest('vaadin-menu-bar-overlay, vaadin-context-menu-overlay'); overlay && overlay.close();})",
this));
}

if (tabs.length == 0) {
Expand Down Expand Up @@ -506,6 +507,7 @@ private void updateSelectedTab(boolean changedFromClient) {

if (selectedTab != null) {
selectedTab.setSelected(true);
getElement().executeJs("this.__detectOverflow()");
}

fireEvent(new SelectedChangeEvent(this, previousTab, changedFromClient));
Expand Down
30 changes: 29 additions & 1 deletion src/main/resources/META-INF/frontend/fcEnhancedTabs/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* #%L
* Enhanced Tabs Add-on
* %%
* Copyright (C) 2023 Flowing Code
* Copyright (C) 2023-2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,9 @@
(function () {
window.Vaadin.Flow.fcEnhancedTabsConnector = {
initLazy: tabs => {

if (tabs.fcEnhancedTabsConnector) return;
tabs.fcEnhancedTabsConnector = true;

let close = tabs._close;

Expand All @@ -44,6 +47,31 @@
}
});

const __detectOverflow = tabs.__detectOverflow.bind(tabs);

tabs.__detectOverflow = function() {
//restore the normal order of buttons
var buttons = tabs._buttons;
const selectedButton = buttons.find(e=>e._position!==undefined);
if (selectedButton) {
buttons[0].parentElement.insertBefore(selectedButton, buttons[selectedButton._position+1]);
__detectOverflow();
selectedButton._position=undefined;
}

__detectOverflow();

// move the selected tab out of the overflow button
buttons = tabs._buttons;
const selectedIndex = buttons.findIndex(e=>e.item.component && e.item.component.querySelector('vaadin-tab[selected]'));
const overflowIndex = buttons.findIndex(e=>e.style.visibility);
if (selectedIndex>=overflowIndex && overflowIndex>0) {
buttons[0].parentElement.insertBefore(buttons[selectedIndex], buttons[overflowIndex-1]);
__detectOverflow();
buttons[selectedIndex]._position = selectedIndex;
}
};

}
}
})();
Loading