Discover tabs bar menu (#216075)

## Summary

Closes #216070 

This PR implements `TabsBarMenu` feature. After clicking on three dots
icon on the right side of menu bar we should see a popup with 2 lists:
- Opened tabs
- Recently closed

Those should be rendered separately and have a separate overflow scroll.
`Recently closed` can be constrained to a chosen constant in a followup
PR, when we have real data.

Clicking on a single element behaves differently in opened tabs and
recently closed.

**Opened tabs**: 
- it should indicate which tab is currently visible (with a checkmark)
- clicking on a single element should navigate to clicked tab
(implemented  )

**Recently closed**:
- it shouldn't have an indicator
- clicking on a single element should restore a tab (TO BE DONE in a
separated PR)

<img width="298" alt="Screenshot 2025-03-26 at 16 45 26"
src="https://github.com/user-attachments/assets/40f8250f-237c-405d-b31c-8ddfe5ca05c5"
/>

### Checklist

Check the PR satisfies following conditions. 

Reviewers should verify this PR satisfies this list as well.

- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

Does this PR introduce any risks? For example, consider risks like hard
to test bugs, performance regression, potential of data loss.

Describe the risk, its severity, and mitigation for each identified
risk. Invite stakeholders and evaluate how to proceed before merging.

- [ ] [See some risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
- [ ] ...
This commit is contained in:
Ania Kowalska 2025-03-31 15:28:59 +02:00 committed by GitHub
parent 3247960336
commit f270bd5956
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 334 additions and 7 deletions

View file

@ -70,6 +70,8 @@ export const TabMenu: React.FC<TabMenuProps> = ({
id={contextMenuPopoverId}
isOpen={isPopoverOpen}
panelPaddingSize="none"
hasArrow={false}
anchorPosition="downLeft"
closePopover={closePopover}
button={
<EuiButtonIcon

View file

@ -23,6 +23,22 @@ import {
} from '../../utils/manage_tabs';
import type { TabItem, TabsServices, TabPreviewData } from '../../types';
// TODO replace with real data when ready
const RECENTLY_CLOSED_TABS_MOCK = [
{
label: 'Session 4',
id: '4',
},
{
label: 'Session 5',
id: '5',
},
{
label: 'Session 6',
id: '6',
},
];
export interface TabbedContentProps extends Pick<TabsBarProps, 'maxItemsCount'> {
initialItems: TabItem[];
initialSelectedItemId?: string;
@ -128,6 +144,7 @@ export const TabbedContent: React.FC<TabbedContentProps> = ({
<TabsBar
items={items}
selectedItem={selectedItem}
recentlyClosedItems={RECENTLY_CLOSED_TABS_MOCK}
maxItemsCount={maxItemsCount}
tabContentId={tabContentId}
getTabMenuItems={getTabMenuItems}

View file

@ -18,6 +18,11 @@ const items = Array.from({ length: 5 }).map((_, i) => ({
label: `Tab ${i}`,
}));
const recentlyClosedItems = Array.from({ length: 3 }).map((_, i) => ({
id: `closed-tab-${i}`,
label: `Closed Tab ${i}`,
}));
const tabContentId = 'test-content-id';
describe('TabsBar', () => {
@ -41,6 +46,7 @@ describe('TabsBar', () => {
<TabsBar
tabContentId={tabContentId}
items={items}
recentlyClosedItems={recentlyClosedItems}
selectedItem={selectedItem}
services={servicesMock}
onAdd={onAdd}

View file

@ -16,6 +16,7 @@ import type { TabItem, TabsServices } from '../../types';
import { getTabIdAttribute } from '../../utils/get_tab_attributes';
import { useResponsiveTabs } from '../../hooks/use_responsive_tabs';
import { TabsBarWithBackground } from '../tabs_visual_glue_to_header/tabs_bar_with_background';
import { TabsBarMenu } from '../tabs_bar_menu';
const growingFlexItemCss = css`
min-width: 0;
@ -27,6 +28,7 @@ export type TabsBarProps = Pick<
> & {
items: TabItem[];
selectedItem: TabItem | null;
recentlyClosedItems: TabItem[];
maxItemsCount?: number;
services: TabsServices;
onAdd: () => Promise<void>;
@ -35,6 +37,7 @@ export type TabsBarProps = Pick<
export const TabsBar: React.FC<TabsBarProps> = ({
items,
selectedItem,
recentlyClosedItems,
maxItemsCount,
tabContentId,
getTabMenuItems,
@ -82,7 +85,7 @@ export const TabsBar: React.FC<TabsBarProps> = ({
alignItems="center"
gutterSize="s"
css={css`
padding-right: ${euiTheme.size.xs};
padding-right: ${euiTheme.size.base};
`}
>
<EuiFlexItem ref={setTabsContainerWithPlusElement} grow css={growingFlexItemCss}>
@ -130,12 +133,11 @@ export const TabsBar: React.FC<TabsBarProps> = ({
</EuiFlexGroup>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButtonIcon
iconType="boxesVertical"
color="text"
aria-label="Tabs menu placeholder"
title="Tabs menu placeholder"
onClick={() => alert('TODO: Implement tabs menu')}
<TabsBarMenu
openedItems={items}
selectedItem={selectedItem}
onSelectOpenedTab={onSelect}
recentlyClosedItems={recentlyClosedItems}
/>
</EuiFlexItem>
</EuiFlexGroup>

View file

@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
export { TabsBarMenu } from './tabs_bar_menu';

View file

@ -0,0 +1,125 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { TabsBarMenu } from './tabs_bar_menu';
const mockTabs = [
{ id: 'tab1', label: 'Tab 1' },
{ id: 'tab2', label: 'Tab 2' },
{ id: 'tab3', label: 'Tab 3' },
];
const mockRecentlyClosedTabs = [
{ id: 'closed1', label: 'Closed Tab 1' },
{ id: 'closed2', label: 'Closed Tab 2' },
];
const tabsBarMenuButtonTestId = 'unifiedTabs_tabsBarMenuButton';
describe('TabsBarMenu', () => {
const mockOnSelectOpenedTab = jest.fn();
const defaultProps = {
openedItems: mockTabs,
selectedItem: mockTabs[0],
onSelectOpenedTab: mockOnSelectOpenedTab,
recentlyClosedItems: mockRecentlyClosedTabs,
};
beforeEach(() => {
jest.clearAllMocks();
});
it('renders the menu button', () => {
render(<TabsBarMenu {...defaultProps} />);
const menuButton = screen.getByTestId(tabsBarMenuButtonTestId);
expect(menuButton).toBeInTheDocument();
});
it('opens popover when menu button is clicked', () => {
render(<TabsBarMenu {...defaultProps} />);
const menuButton = screen.getByTestId(tabsBarMenuButtonTestId);
fireEvent.click(menuButton);
const tabsBarMenu = screen.getByTestId('unifiedTabs_tabsBarMenu');
expect(tabsBarMenu).toBeInTheDocument();
expect(screen.getByText('Opened tabs')).toBeInTheDocument();
});
it('displays opened tabs correctly', () => {
render(<TabsBarMenu {...defaultProps} />);
const menuButton = screen.getByTestId(tabsBarMenuButtonTestId);
fireEvent.click(menuButton);
mockTabs.forEach((tab) => {
expect(screen.getByText(tab.label)).toBeInTheDocument();
});
});
it('selects a tab when clicked', () => {
render(<TabsBarMenu {...defaultProps} />);
const menuButton = screen.getByTestId(tabsBarMenuButtonTestId);
fireEvent.click(menuButton);
const secondTabOption = screen.getByText(mockTabs[1].label);
fireEvent.click(secondTabOption);
expect(mockOnSelectOpenedTab).toHaveBeenCalledWith(mockTabs[1]);
});
it('shows recently closed tabs when present', () => {
render(<TabsBarMenu {...defaultProps} />);
const menuButton = screen.getByTestId(tabsBarMenuButtonTestId);
fireEvent.click(menuButton);
expect(screen.getByText('Recently closed')).toBeInTheDocument();
mockRecentlyClosedTabs.forEach((tab) => {
expect(screen.getByText(tab.label)).toBeInTheDocument();
});
});
it('does not show recently closed section when array is empty', () => {
const propsWithNoClosedTabs = {
...defaultProps,
recentlyClosedItems: [],
};
render(<TabsBarMenu {...propsWithNoClosedTabs} />);
const menuButton = screen.getByTestId(tabsBarMenuButtonTestId);
fireEvent.click(menuButton);
expect(screen.queryByText('Recently closed')).not.toBeInTheDocument();
});
it('marks the selected tab as checked', () => {
render(<TabsBarMenu {...defaultProps} />);
const menuButton = screen.getByTestId(tabsBarMenuButtonTestId);
fireEvent.click(menuButton);
const selectedTabOption = screen.getByText(mockTabs[0].label);
expect(selectedTabOption.closest('[aria-selected="true"]')).toBeInTheDocument();
});
});

View file

@ -0,0 +1,165 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import React, { useCallback, useMemo, useState } from 'react';
import { i18n } from '@kbn/i18n';
import { css } from '@emotion/react';
import {
EuiButtonIcon,
EuiPopover,
useGeneratedHtmlId,
EuiSelectableOption,
EuiSelectable,
EuiPopoverTitle,
EuiHorizontalRule,
EuiSelectableOptionsListProps,
} from '@elastic/eui';
import type { TabItem } from '../../types';
import type { TabsBarProps } from '../tabs_bar';
const getOpenedTabsList = (
tabItems: TabItem[],
selectedTab: TabItem | null
): EuiSelectableOption[] => {
return tabItems.map((tab) => ({
label: tab.label,
checked: selectedTab && tab.id === selectedTab.id ? 'on' : undefined,
key: tab.id,
}));
};
const getRecentlyClosedTabsList = (tabItems: TabItem[]): EuiSelectableOption[] => {
return tabItems.map((tab) => ({
label: tab.label,
key: tab.id,
}));
};
interface TabsBarMenuProps {
onSelectOpenedTab: TabsBarProps['onSelect'];
selectedItem: TabsBarProps['selectedItem'];
openedItems: TabsBarProps['items'];
recentlyClosedItems: TabsBarProps['recentlyClosedItems'];
}
export const TabsBarMenu: React.FC<TabsBarMenuProps> = React.memo(
({ openedItems, selectedItem, onSelectOpenedTab, recentlyClosedItems }) => {
const openedTabsList = useMemo(
() => getOpenedTabsList(openedItems, selectedItem),
[openedItems, selectedItem]
);
const recentlyClosedTabsList = useMemo(
() => getRecentlyClosedTabsList(recentlyClosedItems),
[recentlyClosedItems]
);
const [isPopoverOpen, setPopover] = useState(false);
const contextMenuPopoverId = useGeneratedHtmlId();
const menuButtonLabel = i18n.translate('unifiedTabs.tabsBarMenu.tabsBarMenuButton', {
defaultMessage: 'Tabs bar menu',
});
const closePopover = useCallback(() => {
setPopover(false);
}, [setPopover]);
const selectableListProps = {
onFocusBadge: false,
truncationProps: {
truncation: 'middle',
},
} as Partial<EuiSelectableOptionsListProps>;
return (
<EuiPopover
data-test-subj="unifiedTabs_tabsBarMenu"
id={contextMenuPopoverId}
isOpen={isPopoverOpen}
closePopover={closePopover}
panelPaddingSize="none"
anchorPosition="downRight"
hasArrow={false}
panelProps={{
css: popoverCss,
}}
button={
<EuiButtonIcon
aria-label={menuButtonLabel}
title={menuButtonLabel}
color="text"
data-test-subj="unifiedTabs_tabsBarMenuButton"
iconType="boxesVertical"
onClick={() => setPopover((prev) => !prev)}
/>
}
>
<EuiSelectable
aria-label={i18n.translate('unifiedTabs.tabsBarMenu.openedTabsList', {
defaultMessage: 'Opened tabs list',
})}
options={openedTabsList}
onChange={(newOptions) => {
const clickedTabId = newOptions.find((option) => option.checked)?.key;
const tabToNavigate = openedItems.find((tab) => tab.id === clickedTabId);
if (tabToNavigate) {
onSelectOpenedTab(tabToNavigate);
closePopover();
}
}}
singleSelection="always"
listProps={selectableListProps}
>
{(tabs) => (
<>
<EuiPopoverTitle paddingSize="s">
{i18n.translate('unifiedTabs.tabsBarMenu.openedItems', {
defaultMessage: 'Opened tabs',
})}
</EuiPopoverTitle>
{tabs}
</>
)}
</EuiSelectable>
{recentlyClosedItems.length > 0 && (
<>
<EuiHorizontalRule margin="none" />
<EuiSelectable
aria-label={i18n.translate('unifiedTabs.tabsBarMenu.recentlyClosedTabsList', {
defaultMessage: 'Recently closed tabs list',
})}
options={recentlyClosedTabsList}
onChange={() => {
alert('restore tab'); // TODO restore closed tab
closePopover();
}}
singleSelection={true}
listProps={selectableListProps}
>
{(tabs) => (
<>
<EuiPopoverTitle paddingSize="s">
{i18n.translate('unifiedTabs.tabsBarMenu.recentlyClosed', {
defaultMessage: 'Recently closed',
})}
</EuiPopoverTitle>
{tabs}
</>
)}
</EuiSelectable>
</>
)}
</EuiPopover>
);
}
);
const popoverCss = css`
width: 240px;
`;