Skip to content

Commit 4a3963e

Browse files
authored
feat: add tooltip API to menubar (#3747)
1 parent d3abb00 commit 4a3963e

4 files changed

Lines changed: 333 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2022 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.menubar.tests;
17+
18+
import com.vaadin.flow.component.html.Div;
19+
import com.vaadin.flow.component.html.NativeButton;
20+
import com.vaadin.flow.component.html.Span;
21+
import com.vaadin.flow.component.menubar.MenuBar;
22+
import com.vaadin.flow.router.Route;
23+
24+
@Route("vaadin-menu-bar/tooltip")
25+
public class MenuBarTooltipPage extends Div {
26+
27+
public MenuBarTooltipPage() {
28+
MenuBar menuBar = new MenuBar();
29+
// Use each add API with tooltip parameter to add menu items
30+
menuBar.addItem("Edit", "Edit tooltip");
31+
menuBar.addItem(new Span("Share"), "Share tooltip");
32+
menuBar.addItem("Move", "Move tooltip", (e) -> {
33+
});
34+
menuBar.addItem(new Span("Duplicate"), "Duplicate tooltip");
35+
36+
// Add a button for toggling the menu-bar attached state.
37+
var toggleAttachedButton = new NativeButton("Toggle attached",
38+
event -> {
39+
if (menuBar.getParent().isPresent()) {
40+
remove(menuBar);
41+
} else {
42+
add(menuBar);
43+
}
44+
});
45+
toggleAttachedButton.setId("toggle-attached-button");
46+
47+
add(toggleAttachedButton, menuBar);
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2022 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.menubar.tests;
17+
18+
import org.junit.Assert;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.openqa.selenium.By;
22+
23+
import com.vaadin.flow.component.menubar.testbench.MenuBarElement;
24+
import com.vaadin.tests.AbstractComponentIT;
25+
import com.vaadin.flow.testutil.TestPath;
26+
import com.vaadin.testbench.TestBenchElement;
27+
28+
@TestPath("vaadin-menu-bar/tooltip")
29+
public class MenuBarTooltipIT extends AbstractComponentIT {
30+
31+
@Before
32+
public void init() {
33+
open();
34+
}
35+
36+
@Test
37+
public void hoverOverMenuBarButton_showTooltip() {
38+
var menuBar = $(MenuBarElement.class).first();
39+
40+
showTooltip(menuBar.getButtons().get(0));
41+
Assert.assertEquals("Edit tooltip", getActiveTooltipText());
42+
43+
showTooltip(menuBar.getButtons().get(1));
44+
Assert.assertEquals("Share tooltip", getActiveTooltipText());
45+
46+
showTooltip(menuBar.getButtons().get(2));
47+
Assert.assertEquals("Move tooltip", getActiveTooltipText());
48+
49+
showTooltip(menuBar.getButtons().get(3));
50+
Assert.assertEquals("Duplicate tooltip", getActiveTooltipText());
51+
}
52+
53+
@Test
54+
public void toggleAttached_hoverOverTooltipColumnCell_showTooltip() {
55+
// Remove the menubar
56+
clickElementWithJs("toggle-attached-button");
57+
// Add the menubar
58+
clickElementWithJs("toggle-attached-button");
59+
60+
var menuBar = $(MenuBarElement.class).first();
61+
showTooltip(menuBar.getButtons().get(0));
62+
Assert.assertEquals("Edit tooltip", getActiveTooltipText());
63+
}
64+
65+
private void showTooltip(TestBenchElement button) {
66+
executeScript(
67+
"arguments[0].dispatchEvent(new Event('mouseover', {bubbles:true}))",
68+
button);
69+
}
70+
71+
private String getActiveTooltipText() {
72+
return findElement(By.tagName("vaadin-tooltip-overlay")).getText();
73+
}
74+
}

‎vaadin-menu-bar-flow-parent/vaadin-menu-bar-flow/src/main/java/com/vaadin/flow/component/menubar/MenuBar.java‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.vaadin.flow.component.contextmenu.SubMenu;
3939
import com.vaadin.flow.component.dependency.JsModule;
4040
import com.vaadin.flow.component.dependency.NpmPackage;
41+
import com.vaadin.flow.dom.Element;
4142
import com.vaadin.flow.function.SerializableConsumer;
4243
import com.vaadin.flow.internal.JsonSerializer;
4344

@@ -55,8 +56,10 @@
5556
@JsModule("@vaadin/polymer-legacy-adapter/style-modules.js")
5657
@JsModule("./menubarConnector.js")
5758
@JsModule("@vaadin/menu-bar/src/vaadin-menu-bar.js")
59+
@JsModule("@vaadin/tooltip/src/vaadin-tooltip.js")
5860
@NpmPackage(value = "@vaadin/menu-bar", version = "23.3.0-alpha1")
5961
@NpmPackage(value = "@vaadin/vaadin-menu-bar", version = "23.3.0-alpha1")
62+
@NpmPackage(value = "@vaadin/tooltip", version = "23.3.0-alpha1")
6063
public class MenuBar extends Component
6164
implements HasMenuItems, HasSize, HasStyle, HasTheme, HasEnabled {
6265

@@ -176,6 +179,113 @@ public MenuItem addItem(Component component,
176179
return menuManager.addItem(component, clickListener);
177180
}
178181

182+
/**
183+
* Creates a new {@link MenuItem} component with the provided text content
184+
* and the tooltip text and adds it to the root level of this menu bar.
185+
* <p>
186+
* The added {@link MenuItem} component is placed inside a button in the
187+
* menu bar. If this button overflows the menu bar horizontally, the
188+
* {@link MenuItem} is moved out of the button, into a context menu openable
189+
* via an overflow button at the end of the button row.
190+
* <p>
191+
* To add content to the sub menu opened by clicking the root level item,
192+
* use {@link MenuItem#getSubMenu()}.
193+
*
194+
* @param text
195+
* the text content for the new item
196+
* @param tooltipText
197+
* the tooltip text for the new item
198+
* @return the added {@link MenuItem} component
199+
*/
200+
public MenuItem addItem(String text, String tooltipText) {
201+
var item = addItem(text);
202+
setTooltip(item, tooltipText);
203+
return item;
204+
}
205+
206+
/**
207+
* Creates a new {@link MenuItem} component with the provided tooltip text
208+
* and adds it to the root level of this menu bar. The provided component is
209+
* added into the created {@link MenuItem}.
210+
* <p>
211+
* The added {@link MenuItem} component is placed inside a button in the
212+
* menu bar. If this button overflows the menu bar horizontally, the
213+
* {@link MenuItem} is moved out of the button, into a context menu openable
214+
* via an overflow button at the end of the button row.
215+
* <p>
216+
* To add content to the sub menu opened by clicking the root level item,
217+
* use {@link MenuItem#getSubMenu()}.
218+
*
219+
* @param component
220+
* the component to add inside new item
221+
* @param tooltipText
222+
* the tooltip text for the new item
223+
* @return the added {@link MenuItem} component
224+
*/
225+
public MenuItem addItem(Component component, String tooltipText) {
226+
var item = addItem(component);
227+
setTooltip(item, tooltipText);
228+
return item;
229+
}
230+
231+
/**
232+
* Creates a new {@link MenuItem} component with the provided text content
233+
* and the tooltip text and click listener and adds it to the root level of
234+
* this menu bar.
235+
* <p>
236+
* The added {@link MenuItem} component is placed inside a button in the
237+
* menu bar. If this button overflows the menu bar horizontally, the
238+
* {@link MenuItem} is moved out of the button, into a context menu openable
239+
* via an overflow button at the end of the button row.
240+
* <p>
241+
* To add content to the sub menu opened by clicking the root level item,
242+
* use {@link MenuItem#getSubMenu()}.
243+
*
244+
* @param text
245+
* the text content for the new item
246+
* @param tooltipText
247+
* the tooltip text for the new item
248+
* @param clickListener
249+
* the handler for clicking the new item, can be {@code null} to
250+
* not add listener
251+
* @return the added {@link MenuItem} component
252+
*/
253+
public MenuItem addItem(String text, String tooltipText,
254+
ComponentEventListener<ClickEvent<MenuItem>> clickListener) {
255+
var item = addItem(text, clickListener);
256+
setTooltip(item, tooltipText);
257+
return item;
258+
}
259+
260+
/**
261+
* Creates a new {@link MenuItem} component with the provided click listener
262+
* and the tooltip text and adds it to the root level of this menu bar. The
263+
* provided component is added into the created {@link MenuItem}.
264+
* <p>
265+
* The added {@link MenuItem} component is placed inside a button in the
266+
* menu bar. If this button overflows the menu bar horizontally, the
267+
* {@link MenuItem} is moved out of the button, into a context menu openable
268+
* via an overflow button at the end of the button row.
269+
* <p>
270+
* To add content to the sub menu opened by clicking the root level item,
271+
* use {@link MenuItem#getSubMenu()}.
272+
*
273+
* @param component
274+
* the component to add inside the added menu item
275+
* @param tooltipText
276+
* the tooltip text for the new item
277+
* @param clickListener
278+
* the handler for clicking the new item, can be {@code null} to
279+
* not add listener
280+
* @return the added {@link MenuItem} component
281+
*/
282+
public MenuItem addItem(Component component, String tooltipText,
283+
ComponentEventListener<ClickEvent<MenuItem>> clickListener) {
284+
var item = addItem(component, clickListener);
285+
setTooltip(item, tooltipText);
286+
return item;
287+
}
288+
179289
/**
180290
* Gets the {@link MenuItem} components added to the root level of the menu
181291
* bar.
@@ -393,4 +503,28 @@ public MenuBarI18n setMoreOptions(String moreOptions) {
393503
return this;
394504
}
395505
}
506+
507+
/**
508+
* Sets the tooltip property for the given menu item. A vaadin-tooltip
509+
* element with a custom generator is created and added inside the menu bar
510+
* in case it doesn't exist.
511+
*/
512+
private void setTooltip(MenuItem item, String tooltipText) {
513+
if (!getElement().getChildren().anyMatch(
514+
child -> "tooltip".equals(child.getAttribute("slot")))) {
515+
// No <vaadin-tooltip> yet added, add one
516+
var tooltipElement = new Element("vaadin-tooltip");
517+
tooltipElement.setAttribute("slot", "tooltip");
518+
519+
tooltipElement.addAttachListener(e -> {
520+
// Assigns a generator that reads the tooltip property of the
521+
// item component
522+
tooltipElement.executeJs(
523+
"this.textGenerator = ({item}) => { return (item && item.component) ? item.component.tooltip : ''; }");
524+
});
525+
getElement().appendChild(tooltipElement);
526+
}
527+
528+
item.getElement().setProperty("tooltip", tooltipText);
529+
}
396530
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2022 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.menubar.tests;
17+
18+
import java.util.Optional;
19+
import java.util.stream.Stream;
20+
21+
import org.junit.Assert;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
import com.vaadin.flow.component.menubar.MenuBar;
26+
import com.vaadin.flow.dom.Element;
27+
28+
/**
29+
* Unit tests for MenuBar tooltip.
30+
*
31+
* @author Vaadin Ltd.
32+
*/
33+
public class MenuBarTooltipTest {
34+
35+
private MenuBar menuBar;
36+
37+
@Before
38+
public void setup() {
39+
menuBar = new MenuBar();
40+
}
41+
42+
@Test
43+
public void default_doesNotHaveTooltipElement() {
44+
Assert.assertFalse(getTooltipElement(menuBar).isPresent());
45+
}
46+
47+
@Test
48+
public void addItemWithTooltip_hasTooltipElement() {
49+
menuBar.addItem("Item", "Item tooltip");
50+
Assert.assertTrue(getTooltipElement(menuBar).isPresent());
51+
}
52+
53+
@Test
54+
public void addItemWithTooltip_tooltipHasSlot() {
55+
menuBar.addItem("Item", "Item tooltip");
56+
Assert.assertEquals("tooltip",
57+
getTooltipElement(menuBar).get().getAttribute("slot"));
58+
}
59+
60+
@Test
61+
public void addAnotherItemWithTooltip_hasOneTooltipElement() {
62+
menuBar.addItem("Item 0", "Item 0 tooltip");
63+
menuBar.addItem("Item 1", "Item 1 tooltip");
64+
Assert.assertEquals(1, getTooltipElements(menuBar).count());
65+
}
66+
67+
private Optional<Element> getTooltipElement(MenuBar menuBar) {
68+
return getTooltipElements(menuBar).findFirst();
69+
}
70+
71+
private Stream<Element> getTooltipElements(MenuBar menuBar) {
72+
return menuBar.getElement().getChildren()
73+
.filter(child -> child.getTag().equals("vaadin-tooltip"));
74+
}
75+
76+
}

0 commit comments

Comments
 (0)