Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command Palette: Fix not being able to type if triggered whilst another modal is open #59728

Merged
merged 1 commit into from Dec 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 16 additions & 18 deletions public/app/features/commandPalette/CommandPalette.tsx
@@ -1,5 +1,7 @@
import { css } from '@emotion/css';
import { useDialog } from '@react-aria/dialog';
import { FocusScope } from '@react-aria/focus';
import { useOverlay } from '@react-aria/overlays';
import {
KBarAnimator,
KBarPortal,
Expand All @@ -12,12 +14,11 @@ import {
useRegisterActions,
useKBar,
} from 'kbar';
import React, { useEffect, useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';

import { GrafanaTheme2 } from '@grafana/data';
import { reportInteraction, locationService } from '@grafana/runtime';
import { useStyles2 } from '@grafana/ui';
import { useGrafana } from 'app/core/context/GrafanaContext';
import { useSelector } from 'app/types';

import { ResultItem } from './ResultItem';
Expand All @@ -31,7 +32,6 @@ import getGlobalActions from './actions/global.static.actions';

export const CommandPalette = () => {
const styles = useStyles2(getSearchStyles);
const { keybindings } = useGrafana();
const [actions, setActions] = useState<Action[]>([]);
const [staticActions, setStaticActions] = useState<Action[]>([]);
const { query, showing } = useKBar((state) => ({
Expand All @@ -45,6 +45,13 @@ export const CommandPalette = () => {
};
});

const ref = useRef<HTMLDivElement>(null);
const { overlayProps } = useOverlay(
{ isOpen: showing, onClose: () => query.setVisualState(VisualState.animatingOut) },
ref
);
const { dialogProps } = useDialog({}, ref);

useEffect(() => {
if (isNotLogin) {
const staticActionsResp = getGlobalActions(navBarTree);
Expand All @@ -61,29 +68,20 @@ export const CommandPalette = () => {
getDashboardNavActions('go/dashboard').then((dashAct) => {
setActions([...staticActions, ...dashAct]);
});

keybindings.bindGlobal('esc', () => {
query.setVisualState(VisualState.animatingOut);
});
}

return () => {
keybindings.bindGlobal('esc', () => {
keybindings.globalEsc();
});
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [showing]);
}, [showing, staticActions]);

useRegisterActions(actions, [actions]);

return actions.length > 0 ? (
<KBarPortal>
<KBarPositioner className={styles.positioner}>
<KBarAnimator className={styles.animator}>
<FocusScope contain>
<KBarSearch className={styles.search} />
<RenderResults />
<FocusScope contain autoFocus restoreFocus>
<div {...overlayProps} {...dialogProps}>
<KBarSearch className={styles.search} />
<RenderResults />
</div>
</FocusScope>
</KBarAnimator>
</KBarPositioner>
Expand Down