Skip to content

Commit fe65049

Browse files
authored
feat: add default tooltip generator for menu-bar (#4617)
1 parent 2ec1f1f commit fe65049

6 files changed

Lines changed: 68 additions & 9 deletions

File tree

‎dev/menu-bar.html‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
},
3434
{ text: 'Duplicate', tooltip: 'Create a duplicate' },
3535
];
36-
37-
const tooltip = document.querySelector('[slot="tooltip"]');
38-
tooltip.generator = ({ item }) => item.tooltip;
3936
</script>
4037
</head>
4138

‎integration/tests/menu-bar-tooltip.test.js‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export function mouseover(target) {
2020
fire(target, 'mouseover');
2121
}
2222

23+
function getTooltipText() {
24+
const overlay = document.querySelector('vaadin-tooltip-overlay:not([hidden])');
25+
return overlay && overlay.textContent;
26+
}
27+
2328
describe('menu-bar with tooltip', () => {
2429
let menuBar, tooltip, buttons;
2530

@@ -30,13 +35,14 @@ describe('menu-bar with tooltip', () => {
3035
</vaadin-menu-bar>
3136
`);
3237
menuBar.items = [
33-
{ text: 'Edit' },
38+
{ text: 'Edit', tooltip: 'Edit tooltip' },
3439
{
3540
text: 'Share',
3641
children: [{ text: 'By email' }],
3742
},
3843
{
3944
text: 'Move',
45+
tooltip: 'Move tooltip',
4046
children: [{ text: 'To folder' }],
4147
},
4248
];
@@ -45,7 +51,6 @@ describe('menu-bar with tooltip', () => {
4551
buttons = menuBar._buttons;
4652

4753
tooltip = menuBar.querySelector('vaadin-tooltip');
48-
tooltip.generator = ({ item }) => item && `${item.text} tooltip`;
4954
});
5055

5156
it('should set manual on the tooltip to true', () => {
@@ -57,6 +62,16 @@ describe('menu-bar with tooltip', () => {
5762
expect(tooltip.opened).to.be.true;
5863
});
5964

65+
it('should use the tooltip property of an item as tooltip', () => {
66+
mouseover(buttons[0]);
67+
expect(getTooltipText()).to.equal('Edit tooltip');
68+
});
69+
70+
it('should not show tooltip for an item which has no tooltip', () => {
71+
mouseover(buttons[1]);
72+
expect(getTooltipText()).not.to.be.ok;
73+
});
74+
6075
it('should not show tooltip on another parent menu button mouseover when open', async () => {
6176
mouseover(buttons[1]);
6277
buttons[1].click();
@@ -179,6 +194,12 @@ describe('menu-bar with tooltip', () => {
179194
expect(spyOpened.called).to.be.false;
180195
});
181196

197+
it('should not override a custom generator', () => {
198+
tooltip.generator = () => 'Custom tooltip';
199+
mouseover(buttons[0]);
200+
expect(getTooltipText()).to.equal('Custom tooltip');
201+
});
202+
182203
describe('overflow button', () => {
183204
beforeEach(async () => {
184205
// Reduce the width by the width of the last visible button

‎packages/menu-bar/src/vaadin-menu-bar-interactions-mixin.js‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,19 @@ export const InteractionsMixin = (superClass) =>
107107
*/
108108
_showTooltip(button) {
109109
// Check if there is a slotted vaadin-tooltip element.
110-
if (this._tooltipController.node && this._tooltipController.node.isConnected && !this._subMenu.opened) {
111-
this._tooltipController.setTarget(button);
112-
this._tooltipController.setContext({ item: button.item });
113-
this._tooltipController.setOpened(true);
110+
const tooltip = this._tooltipController.node;
111+
if (tooltip && tooltip.isConnected) {
112+
// If the tooltip element doesn't have a generator assigned, use a default one
113+
// that reads the `tooltip` property of an item.
114+
if (tooltip.generator === undefined) {
115+
tooltip.generator = ({ item }) => item && item.tooltip;
116+
}
117+
118+
if (!this._subMenu.opened) {
119+
this._tooltipController.setTarget(button);
120+
this._tooltipController.setContext({ item: button.item });
121+
this._tooltipController.setOpened(true);
122+
}
114123
}
115124
}
116125

‎packages/menu-bar/src/vaadin-menu-bar.d.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,31 @@ import { ButtonsMixin } from './vaadin-menu-bar-buttons-mixin.js';
1111
import { InteractionsMixin } from './vaadin-menu-bar-interactions-mixin.js';
1212

1313
export interface MenuBarItem {
14+
/**
15+
* Text to be set as the menu button component's textContent.
16+
*/
1417
text?: string;
18+
/**
19+
* Text to be set as the menu button's tooltip.
20+
* Requires a `<vaadin-tooltip slot="tooltip">` element to be added inside the `<vaadin-menu-bar>`.
21+
*/
22+
tooltip?: string;
23+
/**
24+
* The component to represent the button content.
25+
* Either a tagName or an element instance. Defaults to "vaadin-context-menu-item".
26+
*/
1527
component?: HTMLElement | string;
28+
/**
29+
* If true, the button is disabled and cannot be activated.
30+
*/
1631
disabled?: boolean;
32+
/**
33+
* Theme(s) to be set as the theme attribute of the button, overriding any theme set on the menu bar.
34+
*/
1735
theme?: string[] | string;
36+
/**
37+
* Array of submenu items.
38+
*/
1839
children?: SubMenuItem[];
1940
}
2041

‎packages/menu-bar/src/vaadin-menu-bar.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ class MenuBar extends ButtonsMixin(
129129
* @typedef MenuBarItem
130130
* @type {object}
131131
* @property {string} text - Text to be set as the menu button component's textContent.
132+
* @property {string} tooltip - Text to be set as the menu button's tooltip.
133+
* Requires a `<vaadin-tooltip slot="tooltip">` element to be added inside the `<vaadin-menu-bar>`.
132134
* @property {union: string | object} component - The component to represent the button content.
133135
* Either a tagName or an element instance. Defaults to "vaadin-context-menu-item".
134136
* @property {boolean} disabled - If true, the button is disabled and cannot be activated.

‎packages/menu-bar/test/typings/menu-bar.types.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,12 @@ menu.addEventListener('item-selected', (event) => {
1616
assertType<MenuBarItemSelectedEvent>(event);
1717
assertType<MenuBarItem>(event.detail.value);
1818
});
19+
20+
const menuItem = menu.items[0];
21+
22+
assertType<string | undefined>(menuItem.tooltip);
23+
assertType<string | undefined>(menuItem.text);
24+
assertType<boolean | undefined>(menuItem.disabled);
25+
assertType<string[] | string | undefined>(menuItem.theme);
26+
assertType<MenuBarItem[] | undefined>(menuItem.children);
27+
assertType<HTMLElement | string | undefined>(menuItem.component);

0 commit comments

Comments
 (0)