[Discover] Allow command/ctrl click for "New" action in top nav (#210982)

## Summary

Closes #198887 

[A-la-carte](https://akowalska622-pr-210982-allow-cmd-click-new-action.kbndev.co/)
to click around

Allows to convert button into anchor tag if `href` is passed to the
element.
Allows to use modifier key to open view in a new tab, without loosing
the current view.


![Cmd+click](https://github.com/user-attachments/assets/90499445-cdc4-4385-8273-2e83267e52e9)


### Checklist

Reviewers should verify this PR satisfies this list as well.

- [ ] 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
- [ ] [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-02-17 17:10:39 +01:00 committed by GitHub
parent a91e4fcd08
commit d6b250b806
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 26 additions and 4 deletions

View file

@ -26,6 +26,7 @@ describe('convertAppMenuItemToTopNavItem', () => {
testId: 'action-1',
iconType: 'share',
onClick: jest.fn(),
href: '/test-href',
},
};
@ -42,6 +43,7 @@ describe('convertAppMenuItemToTopNavItem', () => {
run: expect.any(Function),
iconType: 'share',
iconOnly: true,
href: '/test-href',
});
});

View file

@ -50,5 +50,6 @@ export function convertAppMenuItemToTopNavItem({
...(appMenuItem.type === AppMenuActionType.primary
? { iconType: appMenuItem.controlProps.iconType, iconOnly: true }
: {}),
...(appMenuItem.controlProps.href ? { href: appMenuItem.controlProps.href } : {}),
};
}

View file

@ -12,8 +12,10 @@ import { i18n } from '@kbn/i18n';
export const getNewSearchAppMenuItem = ({
onNewSearch,
newSearchUrl,
}: {
onNewSearch: () => void;
newSearchUrl?: string;
}): AppMenuActionPrimary => {
return {
id: AppMenuActionId.new,
@ -24,6 +26,7 @@ export const getNewSearchAppMenuItem = ({
}),
iconType: 'plus',
testId: 'discoverNewButton',
href: newSearchUrl,
onClick: () => {
onNewSearch();
},

View file

@ -7,7 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { useMemo } from 'react';
import { useEffect, useMemo, useState } from 'react';
import { i18n } from '@kbn/i18n';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { TopNavMenuData } from '@kbn/navigation-plugin/public';
@ -52,6 +52,15 @@ export const useTopNavLinks = ({
topNavCustomization: TopNavCustomization | undefined;
shouldShowESQLToDataViewTransitionModal: boolean;
}): TopNavMenuData[] => {
const [newSearchUrl, setNewSearchUrl] = useState<string | undefined>(undefined);
useEffect(() => {
const fetchData = async () => {
const url = await services.locator.getUrl({});
setNewSearchUrl(url);
};
fetchData();
}, [services]);
const discoverParams: AppMenuDiscoverParams = useMemo(
() => ({
isEsqlMode,
@ -90,6 +99,7 @@ export const useTopNavLinks = ({
if (!defaultMenu?.newItem?.disabled) {
const newSearchMenuItem = getNewSearchAppMenuItem({
newSearchUrl,
onNewSearch: () => {
services.locator.navigate({});
},
@ -114,7 +124,7 @@ export const useTopNavLinks = ({
}
return items;
}, [discoverParams, state, services, defaultMenu, onOpenInspector]);
}, [discoverParams, state, services, defaultMenu, onOpenInspector, newSearchUrl]);
const getAppMenuAccessor = useProfileAccessor('getAppMenu');
const appMenuRegistry = useMemo(() => {

View file

@ -68,9 +68,14 @@ export function TopNavMenuItem(props: TopNavMenuItemProps) {
}
}
function handleClick(e: MouseEvent<HTMLButtonElement>) {
const isModifiedEvent = (event: MouseEvent) =>
!!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
function handleClick(event: MouseEvent<HTMLButtonElement | HTMLAnchorElement>) {
if (isDisabled()) return;
props.run(e.currentTarget);
if (props.href && isModifiedEvent(event)) return;
props.run(event.currentTarget);
if (props.isMobileMenu) {
props.closePopover();
}
@ -80,6 +85,7 @@ export function TopNavMenuItem(props: TopNavMenuItemProps) {
isDisabled: isDisabled(),
onClick: handleClick,
isLoading: props.isLoading,
href: props.href,
iconType: props.iconType,
iconSide: props.iconSide,
'data-test-subj': props.testId,